From d779403b623d1ba9c41b3f3a9aa396141fe00dd4 Mon Sep 17 00:00:00 2001 From: Thomas Chazot Date: Sat, 26 Nov 2022 14:13:35 +0100 Subject: [PATCH] ADD: Chat en fonction des conversations de l'utilisateur --- bob_party/App.tsx | 34 +++-------------- bob_party/src/components/BotBar.tsx | 18 +++++++-- .../src/components/ConversationComponent.tsx | 16 +++++--- bob_party/src/components/Skin.tsx | 4 +- bob_party/src/context/conversationContext.tsx | 20 ++++++++++ bob_party/src/core/User/userCoinsModifier.ts | 2 +- bob_party/src/core/User/userCreator.ts | 2 +- .../src/core/User/userModificationManager.ts | 2 +- bob_party/src/core/conversation.ts | 2 +- bob_party/src/screens/Chat.tsx | 12 +++--- bob_party/src/screens/SignIn.tsx | 6 +-- .../fakeSaverConversation.ts | 14 +++++++ .../loaderConversationApi.ts | 38 +++++++++++++++++++ .../managerConversation.ts | 34 +++++++++++++++++ 14 files changed, 151 insertions(+), 53 deletions(-) create mode 100644 bob_party/src/context/conversationContext.tsx create mode 100644 bob_party/src/services/conversationService/fakeSaverConversation.ts create mode 100644 bob_party/src/services/conversationService/loaderConversationApi.ts create mode 100644 bob_party/src/services/conversationService/managerConversation.ts diff --git a/bob_party/App.tsx b/bob_party/App.tsx index a15a665..8442ede 100644 --- a/bob_party/App.tsx +++ b/bob_party/App.tsx @@ -2,43 +2,19 @@ import MainTabNavigator from './src/navigation/AppNavigator' import store from './src/redux/store' import { Provider } from 'react-redux' import LoaderUserApi from './src/services/userServices/loaderUserApi' -import ManagerUser from './src/services/userServices/ManagerUser' +import ManagerUser from './src/services/userServices/managerUser' import FakeSaverUser from './src/services/userServices/fakeSaverUser' import React, { useCallback } from 'react'; import { useUserStore } from './src/context/userContext'; +import ManagerConversation from './src/services/conversationService/managerConversation' +import { LoaderConversationApi } from './src/services/conversationService/loaderConversationApi' +import { FakeSaverConversation } from './src/services/conversationService/fakeSaverConversation' export const MANAGER_USER = new ManagerUser(new LoaderUserApi, new FakeSaverUser); - +export const MANAGER_CONVERSATION = new ManagerConversation(new LoaderConversationApi, new FakeSaverConversation); export default function App() { - const setUser = useUserStore((state) => state.setUser); - const resetUser = useUserStore((state) => state.resetUser); - - const handleUserConnect = useCallback(async () => { - - await MANAGER_USER.getLoaderUser().loadByID("14").then((res) => { - MANAGER_USER.setCurrentUser(res); - console.log(res); - }); - setUser(MANAGER_USER.getCurrentUser()); - - - }, []); - - const handleUserLogout = useCallback(async () => { - // TODO: Call logout API - MANAGER_USER.setCurrentUser(null); - resetUser(); - }, []); - - - - const test = useCallback(async () => { - const tab = await MANAGER_USER.getLoaderUser().loadAllUser(); - MANAGER_USER.setCurrentUser(tab[0]); - setUser(MANAGER_USER.getCurrentUser()); - }, []); return ( diff --git a/bob_party/src/components/BotBar.tsx b/bob_party/src/components/BotBar.tsx index 3c2cfe1..c7c51ef 100644 --- a/bob_party/src/components/BotBar.tsx +++ b/bob_party/src/components/BotBar.tsx @@ -7,8 +7,9 @@ import React from "react" */ import styles from './style/BotBar.style'; import { useStoreStore } from "../context/storeContext"; -import { MANAGER_USER } from "../../App"; +import { MANAGER_CONVERSATION, MANAGER_USER } from "../../App"; import tabSkinApp from "../constSkin"; +import { useConversationStore } from "../context/conversationContext"; /* Images that are required to create a bottom bar @@ -40,7 +41,7 @@ FC<{nav: any, state?: String}> = { const setTabSkin = useStoreStore((state) => state.setTabSkin); - + const setTabConv = useConversationStore((state) => state.setTabConv); const handleStoreChange = useCallback(async () => { @@ -57,6 +58,17 @@ FC<{nav: any, state?: String}> = setTabSkin(tabSkinStore); } }, []); + + const handleConversationChange = useCallback(async () => { + let tmp=MANAGER_USER.getCurrentUser(); + if (tmp!=undefined){ + await MANAGER_CONVERSATION.getLoaderConversation().loadByUser(tmp).then((res) => { + MANAGER_CONVERSATION.setCurrentTabConv(res); + setTabConv(res); + }); + } + + }, []); /* By default, all the images are the white ones @@ -89,7 +101,7 @@ FC<{nav: any, state?: String}> = */ return ( - nav.navigate('ChatTab')}> + {handleConversationChange() ;nav.navigate('ChatTab')}}> = -({conv, state}) => -{ +FC<{conv: Conversation, state: String, navigation: any}> = +({conv, state, navigation}) => +{ /* The display of this component depends of the screen from where it has been called: * From the TopBar (icon) : Small image in a circle * From the shop (shop) : Image + Name + Price, Pressable => Buy the skin * From the profile (profile) : Name + Image, Pressable => Change the skin */ + + switch (state) { case 'Preview': return( - + - {conv.getTabUser()[1].getUsername()} - {conv.getLastMessage()} + {conv.getLastMessage().getMessageSender().getUsername()} + {conv.getLastMessage().getMessageContent()} ) diff --git a/bob_party/src/components/Skin.tsx b/bob_party/src/components/Skin.tsx index e6f587b..b4120ea 100644 --- a/bob_party/src/components/Skin.tsx +++ b/bob_party/src/components/Skin.tsx @@ -13,7 +13,7 @@ import { RootState } from "../redux/store" import { MANAGER_USER } from "../../App" import { useUserStore } from "../context/userContext" import { ManagerCoinsUser } from "../core/User/userCoinsModifier" -import ManagerUser from "../services/userServices/ManagerUser" +import ManagerUser from "../services/userServices/managerUser" import UserSkinModifier from "../core/User/userSkinModifier" import { useStoreStore } from "../context/storeContext" import tabSkinApp from "../constSkin" @@ -68,7 +68,7 @@ FC<{nav : any, skin: Skin, state: String}> = } - }, []); + }, []); async function buySkin(skin:Skin) { const mSkin=new UserSkinModifier(); diff --git a/bob_party/src/context/conversationContext.tsx b/bob_party/src/context/conversationContext.tsx new file mode 100644 index 0000000..215674e --- /dev/null +++ b/bob_party/src/context/conversationContext.tsx @@ -0,0 +1,20 @@ +import React from "react"; +import create from "zustand"; +import { MANAGER_USER } from "../../App"; +import { Conversation } from "../core/conversation"; + + +// Define store types +interface ConversationState { + tabConv: Conversation[] | undefined; + setTabConv: (tabConv: Conversation[]) => void; + resetTabConv: () => void; + } + +// Define store data and methods +export const useConversationStore = create()((set, get) => ({ + tabConv: undefined, + setTabConv: (tabConv) => set((state) => ({ tabConv: tabConv })), + resetTabConv: () => set((state) => ({tabConv: undefined})), +})); + diff --git a/bob_party/src/core/User/userCoinsModifier.ts b/bob_party/src/core/User/userCoinsModifier.ts index 44974f1..1bb0e1b 100644 --- a/bob_party/src/core/User/userCoinsModifier.ts +++ b/bob_party/src/core/User/userCoinsModifier.ts @@ -1,5 +1,5 @@ import { MANAGER_USER } from "../../../App"; -import ManagerUser from "../../services/userServices/ManagerUser"; +import ManagerUser from "../../services/userServices/managerUser"; import { User } from "./user"; export class ManagerCoinsUser{ diff --git a/bob_party/src/core/User/userCreator.ts b/bob_party/src/core/User/userCreator.ts index 142a72b..18be053 100644 --- a/bob_party/src/core/User/userCreator.ts +++ b/bob_party/src/core/User/userCreator.ts @@ -1,7 +1,7 @@ import { User } from "./user"; import tabSkinApp from "../../constSkin"; import { Conversation } from "../conversation"; -import ManagerUser from "../../services/userServices/ManagerUser"; +import ManagerUser from "../../services/userServices/managerUser"; import { MANAGER_USER } from "../../../App"; export class UserCreator{ diff --git a/bob_party/src/core/User/userModificationManager.ts b/bob_party/src/core/User/userModificationManager.ts index 808e0ac..26af738 100644 --- a/bob_party/src/core/User/userModificationManager.ts +++ b/bob_party/src/core/User/userModificationManager.ts @@ -9,7 +9,7 @@ export default class UserModificationManager{ } async changeUsername(user:User, username:string){ - user.setPassword(username); + user.setUsername(username); await MANAGER_USER.getsaverUser().updateUser(user); } diff --git a/bob_party/src/core/conversation.ts b/bob_party/src/core/conversation.ts index 5d4eed3..ac94d9a 100644 --- a/bob_party/src/core/conversation.ts +++ b/bob_party/src/core/conversation.ts @@ -56,7 +56,7 @@ export class Conversation{ /* Brief : function returning the last message of a conversation */ getLastMessage(){ this.sortMessageDesc(); - return this.tabMessage[0].getMessageContent(); + return this.tabMessage[0]; } /* Brief : function sorting the messages of a conversation to be in the discussion order */ diff --git a/bob_party/src/screens/Chat.tsx b/bob_party/src/screens/Chat.tsx index 2d243d1..b2cc9d1 100644 --- a/bob_party/src/screens/Chat.tsx +++ b/bob_party/src/screens/Chat.tsx @@ -1,15 +1,17 @@ import { StatusBar } from 'expo-status-bar' import {View} from 'react-native' -import React from 'react'; +import React, { useCallback } from 'react'; import stylesScreen from './style/screens.style'; import { TopBar } from '../components/TopBar'; import { BotBar } from '../components/BotBar'; import { FlatList } from 'react-native-gesture-handler'; import { ConversationComponent } from '../components/ConversationComponent'; +import { Conversation } from '../core/conversation'; +import { MANAGER_CONVERSATION, MANAGER_USER } from '../../App'; +import { useConversationStore } from '../context/conversationContext'; function Chat(props: { navigation: any; }) { const { navigation } = props - return ( @@ -18,12 +20,10 @@ function Chat(props: { navigation: any; }) { /> - {/* } + data={useConversationStore().tabConv} + renderItem={({item}) => } /> - */} { - const us =await MANAGER_USER.getLoaderUser().loadByUsernamePassword(pseudo, password).then((res) => { + await MANAGER_USER.getLoaderUser().loadByUsernamePassword(pseudo, password).then((res) => { if (res!=null){ MANAGER_USER.setCurrentUser(res); setUser(MANAGER_USER.getCurrentUser()); @@ -41,9 +41,9 @@ function SignIn(props: { navigation: any; }) { else{ console.log("wesh c'est null"); } - }); + }); - }, []); + }, []); return ( diff --git a/bob_party/src/services/conversationService/fakeSaverConversation.ts b/bob_party/src/services/conversationService/fakeSaverConversation.ts new file mode 100644 index 0000000..907cf07 --- /dev/null +++ b/bob_party/src/services/conversationService/fakeSaverConversation.ts @@ -0,0 +1,14 @@ +import { Conversation } from "../../core/conversation"; +import ISaverConversation from "./ISaverConversation"; + +export class FakeSaverConversation implements ISaverConversation{ + async saveConversation(c: Conversation): Promise { + return; + } + async deleteConversation(c: Conversation): Promise { + return; + } + async updateConversation(c: Conversation): Promise { + return; + } +} \ No newline at end of file diff --git a/bob_party/src/services/conversationService/loaderConversationApi.ts b/bob_party/src/services/conversationService/loaderConversationApi.ts new file mode 100644 index 0000000..ae78e87 --- /dev/null +++ b/bob_party/src/services/conversationService/loaderConversationApi.ts @@ -0,0 +1,38 @@ +import { Conversation } from "../../core/conversation"; +import { Message } from "../../core/message"; +import { Skin } from "../../core/skin"; +import { User } from "../../core/User/user"; +import ILoaderConversation from "./ILoaderConversation"; + +export class LoaderConversationApi implements ILoaderConversation{ + + private axios = require('axios').default; + + loadAllConversation(): Promise { + throw new Error("Method not implemented."); + } + loadByID(id: string): Promise { + throw new Error("Method not implemented."); + } + async loadByUser(u: User): Promise { + + let tabConv:Conversation[]=[]; + await this.axios({ + method: 'get', + url: 'https://jsonplaceholder.typicode.com/todos/1', + params: { + name: "getConversationByUser", + //Les params genre nom de la fonction en php + } + }) + .then(function (response: any) { + tabConv=[new Conversation("C0001", + [new User("U0001", "Alban", "oui", "ouioui", "homme", new Date(2022,12,12), 555, 667, 12, new Skin("S0001", "Bob",require('bob_party/assets/BobsSkins/BobClassic.png'), 0), [new Skin("S0001", "Bob",require('bob_party/assets/BobsSkins/BobClassic.png'), 0)]), + new User("U0002", "Fefe63", "jesuishm", "ouioui", "homme", new Date(2022,12,12), 12222, 123324, 12, new Skin("S0001", "Bob",require('bob_party/assets/BobsSkins/BobClassic.png'), 0), [new Skin("S0001", "Bob",require('bob_party/assets/BobsSkins/BobClassic.png'), 0)]),], + [new Message("M0001", "bonjour", new User("U0001", "Alban", "oui", "ouioui", "homme", new Date(2022,12,12), 555, 667, 12, new Skin("S0001", "Bob",require('bob_party/assets/BobsSkins/BobClassic.png'), 0), [new Skin("S0001", "Bob",require('bob_party/assets/BobsSkins/BobClassic.png'), 0)]), new Date(2022,12,12)), + new Message("M0002", "test", new User("U0002", "Fefe63", "oui", "ouioui", "homme", new Date(2022,12,12), 555, 667, 12, new Skin("S0002", "Bob Blue",require('bob_party/assets/BobsSkins/BobBlue.png'), 100), [new Skin("S0002", "Bob Blue",require('bob_party/assets/BobsSkins/BobBlue.png'), 100)]), new Date(2022,12,13))], "leNom")]; + }); + return tabConv; + } + +} \ No newline at end of file diff --git a/bob_party/src/services/conversationService/managerConversation.ts b/bob_party/src/services/conversationService/managerConversation.ts new file mode 100644 index 0000000..0e4758b --- /dev/null +++ b/bob_party/src/services/conversationService/managerConversation.ts @@ -0,0 +1,34 @@ +import { Conversation } from "../../core/conversation"; +import { User } from "../../core/User/user"; +import ILoaderConversation from "./ILoaderConversation"; +import ISaverConversation from "./ISaverConversation"; + +export default class ManagerConversation{ + + private currentTabConv:Conversation[]=[]; + + private loaderConversation: ILoaderConversation; + + private saverConversation: ISaverConversation; + + constructor(loaderConversation:ILoaderConversation, saverConversation:ISaverConversation){ + this.loaderConversation=loaderConversation; + this.saverConversation=saverConversation; + } + + getCurrentTabConv(){ + return this.currentTabConv; + } + + setCurrentTabConv(c:Conversation[]){ + this.currentTabConv=c; + } + + getLoaderConversation(){ + return this.loaderConversation; + } + + getsaverConversation(){ + return this.saverConversation; + } +} \ No newline at end of file