chat activity database sort

alpha
Jan Umbach 2023-03-11 00:16:34 +01:00
parent 28a03d3844
commit 62c39fdc30
13 changed files with 162 additions and 16 deletions

View File

@ -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({

View File

@ -179,12 +179,12 @@ export function WebBackButtonOptions<L extends ParamListBase>(
return {
headerLeft: () => (
<MaterialIcon
style={{marginLeft: 16}}
style={{padding: 8, marginLeft: 8, marginRight: -8}}
name="arrow-back"
onPress={() => {
nav.pop();
}}
size={32}
size={24}
color={defaultHeaderStyle(_theme, place).headerTintColor}
/>
),

View File

@ -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));
}

View File

@ -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 (
<Pressable
onPress={() => {
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;

View File

@ -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;
}

View File

@ -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<roomId>) => {

View File

@ -1,6 +1,10 @@
import {getDatabase} from './getDB';
import {databaseConf, possibleDBKeys} from './types';
export interface filterParam {
type: 'name';
}
export const getEntry = async <T2 extends databaseConf<T, any>, T>(
schema: T2,
key: possibleDBKeys,
@ -15,3 +19,15 @@ export const getEntry = async <T2 extends databaseConf<T, any>, T>(
return val as T;
};
export const getAllEntries = async <T2 extends databaseConf<T, any>, T>(
schema: T2,
filter?: filterParam,
): Promise<null | T[]> => {
const dbName = schema.details.name;
const realm = await getDatabase(dbName);
const val = realm.objects<typeof schema.details.properties>(dbName);
return [...val] as T[];
};

View File

@ -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 <T2 extends databaseConf<T, any>, T>(
//await store.put(value);
//await tx.done;
};
export const getAllEntries = async <T2 extends databaseConf<T, any>, T>(
schema: T2,
filter?: filterParam,
): Promise<null | T[]> => {
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;
};

View File

@ -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<typeof propsDefault, typeof keys> = {
key,
);
},
getAllEntries: (filter?: filterParam) => {
return getAllEntries<typeof thisSchema, typeof thisSchema.defaultProps>(
thisSchema,
filter,
);
},
defaultProps: propsDefault,
details: {
name,

View File

@ -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<typeof propsDefault, typeof keys> = {
@ -50,6 +53,12 @@ const thisSchema: databaseConf<typeof propsDefault, typeof keys> = {
key,
);
},
getAllEntries: (filter?: filterParam) => {
return getAllEntries<typeof thisSchema, typeof thisSchema.defaultProps>(
thisSchema,
filter,
);
},
defaultProps: propsDefault,
details: {
name,

View File

@ -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<typeof propsDefault, typeof keys> = {
key,
);
},
getAllEntries: (filter?: filterParam) => {
return getAllEntries<typeof thisSchema, typeof thisSchema.defaultProps>(
thisSchema,
filter,
);
},
defaultProps: propsDefault,
details: {
name,

View File

@ -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<props, enums> {
migration?: any;
keys: enums;
getEntry: (key: possibleDBKeys) => Promise<props | null>;
getAllEntries: (filter?: filterParam) => Promise<props[] | null>;
setEntry: (val: props) => Promise<void>;
defaultProps: props;
details: {

View File

@ -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