diff --git a/src/appStart/StartHelper.tsx b/src/appStart/StartHelper.tsx index 4483171..ce3ce5a 100644 --- a/src/appStart/StartHelper.tsx +++ b/src/appStart/StartHelper.tsx @@ -20,7 +20,8 @@ import {initKey} from '@caj/helper/storage/bdm/encryption'; import UserManager from '@caj/user/UserManager'; import MyUserManager from '@caj/user/MyUserManager'; import DBSchemas from '@caj/helper/storage/bdm/schemas'; -import {chatTags} from '@caj/components/chat/types'; +import {chatTags, convertDatabaseChat} from '@caj/components/chat/types'; +import {addChatEntity} from '@caj/components/chat/initChatDatabase'; const AnimationView = animated(View); @@ -30,6 +31,28 @@ function onAppStart() { BigDataManager.initDatabase() .then(async () => { + const keys = BigDataManager.databases.chatRoomInfos.keys; + const entries = + (await BigDataManager.databases.chatRoomInfos.getAllEntries()) || []; + + entries.sort((a, b) => + a[keys.timestamp] > b[keys.timestamp] + ? 1 + : b[keys.timestamp] > a[keys.timestamp] + ? -1 + : 0, + ); + + for (let i = 0; i < entries.length; i++) { + console.log(entries[i]); + const chat = convertDatabaseChat(entries[i]); + if (chat === undefined) continue; + + console.log(chat); + + addChatEntity(chat); + } + console.log('finish'); const usrDBKeys = BigDataManager.databases.users.keys; await BigDataManager.databases.users.setEntry({ diff --git a/src/caj/Navigation.tsx b/src/caj/Navigation.tsx index ffbb710..a7a7b29 100644 --- a/src/caj/Navigation.tsx +++ b/src/caj/Navigation.tsx @@ -179,12 +179,12 @@ export function WebBackButtonOptions( return { headerLeft: () => ( { nav.pop(); }} - size={32} + size={24} color={defaultHeaderStyle(_theme, place).headerTintColor} /> ), diff --git a/src/caj/components/chat/initChatDatabase.ts b/src/caj/components/chat/initChatDatabase.ts index 775f899..f89a370 100644 --- a/src/caj/components/chat/initChatDatabase.ts +++ b/src/caj/components/chat/initChatDatabase.ts @@ -11,7 +11,12 @@ async function initChatDatabase(chat: chatEntity) { [keys.syncId]: chat.syncId, [keys.unreadMessages]: chat.unreadMessages, [keys.users]: chat.users, + [keys.timestamp]: chat.timestamp, }); + addChatEntity(chat); +} + +export function addChatEntity(chat: chatEntity) { store.dispatch(appNonSaveVarActions.setChatEntity(chat)); } diff --git a/src/caj/components/chat/listChats.tsx b/src/caj/components/chat/listChats.tsx index 8cd5cca..75ec587 100644 --- a/src/caj/components/chat/listChats.tsx +++ b/src/caj/components/chat/listChats.tsx @@ -5,6 +5,7 @@ import { getBackgroundColor, themeSelector, } from '@caj/configs/colors'; +import BigDataManager from '@caj/helper/storage/BigDataManager'; import LangFormat from '@caj/lang/default'; import {HomeScreenNavigationProp} from '@caj/Navigation'; import {RootState, store} from '@caj/redux/store'; @@ -16,7 +17,7 @@ import {useSelector} from 'react-redux'; import {start} from 'repl'; import {ProfilePicture} from '../ProfilePicture'; import {TextSkeleton} from '../simple/Skeleton'; -import {chatEntity, getTagUI} from './types'; +import {chatEntity, convertChatDatabase, getTagUI} from './types'; function ChatItem(props: { chat: chatEntity; @@ -41,6 +42,16 @@ function ChatItem(props: { return ( { + let chat = {...store.getState().nonSaveVariables.chats[roomId]}; + + //chat.timestamp = Math.round(new Date().getTime() / 1000); + + store.dispatch(appNonSaveVarActions.changeChatEntity(chat)); + + BigDataManager.databases.chatRoomInfos.setEntry( + convertChatDatabase(chat), + ); + store.dispatch(appNonSaveVarActions.setSelectedChat(roomId)); navigation.navigate('ChatList', {screen: 'Chat'}); //navigation.navigate('Register', {screen: 'RegStepOne'}); @@ -166,11 +177,6 @@ export function ListChats() { const chatActivity = useSelector( (state: RootState) => state.nonSaveVariables.chatActivity, ); - const selectedChat = useSelector( - (state: RootState) => state.nonSaveVariables.selectedChat, - ); - - console.log('selectedChat', selectedChat); const space = 4; const ppSize = 64; diff --git a/src/caj/components/chat/types.ts b/src/caj/components/chat/types.ts index ced17f8..d009846 100644 --- a/src/caj/components/chat/types.ts +++ b/src/caj/components/chat/types.ts @@ -1,5 +1,7 @@ -import {UserId} from '@caj/configs/types'; +import {timestamp, UserId} from '@caj/configs/types'; +import DBSchemas from '@caj/helper/storage/bdm/schemas'; import LangFormat from '@caj/lang/default'; +import {timeStamp} from 'console'; export enum chatTags { GROUP = 0, @@ -51,10 +53,9 @@ type syncId = number; export interface chatEntity { roomId: roomId; - syncId: syncId; initSyncId: syncId; - + timestamp: timestamp; users: [UserId, ...UserId[]]; title?: string; @@ -62,3 +63,45 @@ export interface chatEntity { tags: chatTags[]; } + +export function convertDatabaseChat( + chat: typeof DBSchemas.chatRoomInfos.defaultProps, +): chatEntity | undefined { + const keys = DBSchemas.chatRoomInfos.keys; + + if (chat[keys.users].length === 0) return undefined; + + const newChat: chatEntity = { + initSyncId: chat[keys.initSyncId], + roomId: chat[keys.RoomId], + syncId: chat[keys.syncId], + tags: [], + unreadMessages: chat[keys.unreadMessages], + timestamp: chat[keys.timestamp], + //@ts-ignore + users: [...chat[keys.users]], + //title: chat[keys.title] + }; + + return newChat; +} + +export function convertChatDatabase( + chat: chatEntity, +): typeof DBSchemas.chatRoomInfos.defaultProps { + const keys = DBSchemas.chatRoomInfos.keys; + + const newChat: typeof DBSchemas.chatRoomInfos.defaultProps = { + [keys.initSyncId]: chat.initSyncId, + [keys.RoomId]: chat.roomId, + [keys.syncId]: chat.syncId, + + [keys.unreadMessages]: chat.unreadMessages, + [keys.timestamp]: chat.timestamp, + //@ts-ignore + [keys.users]: chat.users, + //title: chat[keys.title] + }; + + return newChat; +} diff --git a/src/caj/configs/appNonSaveVarReducer.ts b/src/caj/configs/appNonSaveVarReducer.ts index 3958662..4e3a55c 100644 --- a/src/caj/configs/appNonSaveVarReducer.ts +++ b/src/caj/configs/appNonSaveVarReducer.ts @@ -37,8 +37,9 @@ export const appNonSaveVariablesSlice = createSlice({ if (state.chatActivity.includes(roomId) === false) state.chatActivity.unshift(roomId); + state.chatActivity.sort(function (x, y) { - return x == roomId ? -1 : y == roomId ? 1 : 0; + return x === roomId ? -1 : y === roomId ? 1 : 0; }); }, removeChatEntity: (state, action: PayloadAction) => { diff --git a/src/caj/helper/storage/bdm/get.ts b/src/caj/helper/storage/bdm/get.ts index 9489da6..b592a76 100644 --- a/src/caj/helper/storage/bdm/get.ts +++ b/src/caj/helper/storage/bdm/get.ts @@ -1,6 +1,10 @@ import {getDatabase} from './getDB'; import {databaseConf, possibleDBKeys} from './types'; +export interface filterParam { + type: 'name'; +} + export const getEntry = async , T>( schema: T2, key: possibleDBKeys, @@ -15,3 +19,15 @@ export const getEntry = async , T>( return val as T; }; + +export const getAllEntries = async , T>( + schema: T2, + filter?: filterParam, +): Promise => { + const dbName = schema.details.name; + const realm = await getDatabase(dbName); + + const val = realm.objects(dbName); + + return [...val] as T[]; +}; diff --git a/src/caj/helper/storage/bdm/get.web.ts b/src/caj/helper/storage/bdm/get.web.ts index 785c5f4..4fff298 100644 --- a/src/caj/helper/storage/bdm/get.web.ts +++ b/src/caj/helper/storage/bdm/get.web.ts @@ -1,3 +1,4 @@ +import {filterParam} from './get'; import {getDatabase} from './getDB.web'; import {databaseConf, possibleDBKeys} from './types'; @@ -15,3 +16,18 @@ export const getEntry = async , T>( //await store.put(value); //await tx.done; }; + +export const getAllEntries = async , T>( + schema: T2, + filter?: filterParam, +): Promise => { + const dbName = schema.details.name; + const db = await getDatabase(dbName); + + const tx = db.transaction(dbName, 'readonly'); + const store = tx.objectStore(dbName); + + return await store.getAll(); + //await store.put(value); + //await tx.done; +}; diff --git a/src/caj/helper/storage/bdm/schemas/chat.ts b/src/caj/helper/storage/bdm/schemas/chat.ts index df72411..cc855ca 100644 --- a/src/caj/helper/storage/bdm/schemas/chat.ts +++ b/src/caj/helper/storage/bdm/schemas/chat.ts @@ -1,4 +1,4 @@ -import {getEntry} from '../get'; +import {filterParam, getAllEntries, getEntry} from '../get'; import {DBMigration} from '../migration'; import {setEntry} from '../set'; @@ -41,6 +41,12 @@ const thisSchema: databaseConf = { key, ); }, + getAllEntries: (filter?: filterParam) => { + return getAllEntries( + thisSchema, + filter, + ); + }, defaultProps: propsDefault, details: { name, diff --git a/src/caj/helper/storage/bdm/schemas/chatRoomInfos.ts b/src/caj/helper/storage/bdm/schemas/chatRoomInfos.ts index 3772ea0..f45f912 100644 --- a/src/caj/helper/storage/bdm/schemas/chatRoomInfos.ts +++ b/src/caj/helper/storage/bdm/schemas/chatRoomInfos.ts @@ -1,4 +1,4 @@ -import {getEntry} from '../get'; +import {filterParam, getAllEntries, getEntry} from '../get'; import {DBMigration} from '../migration'; import {setEntry} from '../set'; @@ -10,6 +10,7 @@ enum keys { initSyncId = 'c', unreadMessages = 'd', users = 'e', + timestamp = 'f', } const name = 'chatRoomInfos'; @@ -21,6 +22,7 @@ const propsType: {[key in keyof typeof propsDefault]: string} = { [keys.initSyncId]: 'int', [keys.unreadMessages]: 'int', [keys.users]: 'string[]', + [keys.timestamp]: 'int', }; const propsDefault = { @@ -29,6 +31,7 @@ const propsDefault = { [keys.initSyncId]: 0, [keys.unreadMessages]: 0, [keys.users]: ['none'], + [keys.timestamp]: 0, }; const thisSchema: databaseConf = { @@ -50,6 +53,12 @@ const thisSchema: databaseConf = { key, ); }, + getAllEntries: (filter?: filterParam) => { + return getAllEntries( + thisSchema, + filter, + ); + }, defaultProps: propsDefault, details: { name, diff --git a/src/caj/helper/storage/bdm/schemas/users.ts b/src/caj/helper/storage/bdm/schemas/users.ts index 5ed081d..a81f708 100644 --- a/src/caj/helper/storage/bdm/schemas/users.ts +++ b/src/caj/helper/storage/bdm/schemas/users.ts @@ -1,4 +1,4 @@ -import {getEntry} from '../get'; +import {filterParam, getAllEntries, getEntry} from '../get'; import {DBMigration} from '../migration'; import {setEntry} from '../set'; @@ -73,6 +73,12 @@ const thisSchema: databaseConf = { key, ); }, + getAllEntries: (filter?: filterParam) => { + return getAllEntries( + thisSchema, + filter, + ); + }, defaultProps: propsDefault, details: { name, diff --git a/src/caj/helper/storage/bdm/types.ts b/src/caj/helper/storage/bdm/types.ts index 75857f7..7a0eb49 100644 --- a/src/caj/helper/storage/bdm/types.ts +++ b/src/caj/helper/storage/bdm/types.ts @@ -1,3 +1,5 @@ +import {filterParam} from './get'; + export type databaseNames = 'users' | 'chat' | 'chatRoomInfos'; export type possibleDBKeys = string; @@ -7,6 +9,7 @@ export interface databaseConf { migration?: any; keys: enums; getEntry: (key: possibleDBKeys) => Promise; + getAllEntries: (filter?: filterParam) => Promise; setEntry: (val: props) => Promise; defaultProps: props; details: { diff --git a/src/caj/tabs/main/AccountTab.tsx b/src/caj/tabs/main/AccountTab.tsx index d96405f..879e177 100644 --- a/src/caj/tabs/main/AccountTab.tsx +++ b/src/caj/tabs/main/AccountTab.tsx @@ -84,6 +84,7 @@ function AccountScreen() { tags: [chatTags.GOOD_FRIEND], unreadMessages: 1234, + timestamp: new Date().getTime() / 1000, }); initChatDatabase({ @@ -93,6 +94,17 @@ function AccountScreen() { syncId: 0, tags: [chatTags.GROUP, chatTags.GOOD_FRIEND, chatTags.FRIEND], unreadMessages: 0, + timestamp: new Date().getTime() / 1000, + }); + + initChatDatabase({ + roomId: '88139d16-acb1-410e-91a8-827ee8533c09', + users: ['88139d16-acb1-410e-91a8-827ee8533c09'], + initSyncId: 0, + syncId: 0, + tags: [chatTags.FRIEND], + unreadMessages: 0, + timestamp: new Date().getTime() / 1000, }); }}> Init Chats