changed the database keys to enums
parent
74a5ba5106
commit
28a03d3844
|
@ -20,7 +20,7 @@ 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, chatType} from '@caj/components/chat/types';
|
||||
import {chatTags} from '@caj/components/chat/types';
|
||||
|
||||
const AnimationView = animated(View);
|
||||
|
||||
|
@ -31,41 +31,26 @@ function onAppStart() {
|
|||
BigDataManager.initDatabase()
|
||||
.then(async () => {
|
||||
console.log('finish');
|
||||
const usrDBKeys = BigDataManager.databases.users.keys;
|
||||
await BigDataManager.databases.users.setEntry({
|
||||
UserId: 'test',
|
||||
AccountName: '#845613',
|
||||
Username: 'TestGroupVirtual',
|
||||
Description: 'This is a test account that is not real. ^^',
|
||||
FollowersCount: 2,
|
||||
FollowingCount: 24,
|
||||
lastUpdateTimestamp: 412341234,
|
||||
ProfilePicture: '',
|
||||
ProfilePictureBinaryHQ: new ArrayBuffer(0),
|
||||
ProfilePictureBinaryLQ: new ArrayBuffer(0),
|
||||
XpLevel: 0,
|
||||
XpPoints: 0,
|
||||
[usrDBKeys.UserId]: 'test',
|
||||
[usrDBKeys.AccountName]: '#845613',
|
||||
[usrDBKeys.Username]: 'TestGroupVirtual',
|
||||
[usrDBKeys.Description]:
|
||||
'This is a test account that is not real. ^^',
|
||||
[usrDBKeys.FollowersCount]: 2,
|
||||
[usrDBKeys.FollowingCount]: 24,
|
||||
[usrDBKeys.lastUpdateTimestamp]: 412341234,
|
||||
[usrDBKeys.ProfilePicture]: '',
|
||||
[usrDBKeys.ProfilePictureBinaryHQ]: new ArrayBuffer(0),
|
||||
[usrDBKeys.ProfilePictureBinaryLQ]: new ArrayBuffer(0),
|
||||
[usrDBKeys.XpLevel]: 0,
|
||||
[usrDBKeys.XpPoints]: 0,
|
||||
});
|
||||
|
||||
store.dispatch(
|
||||
appNonSaveVarActions.setAppStatus(appStatus.APP_RUNNING),
|
||||
);
|
||||
|
||||
store.dispatch(
|
||||
appNonSaveVarActions.setChatEntity({
|
||||
id: MyUserManager.getSelectedUserId(),
|
||||
type: chatType.User,
|
||||
tags: [chatTags.GOOD_FRIEND],
|
||||
unreadMessages: 1234,
|
||||
}),
|
||||
);
|
||||
store.dispatch(
|
||||
appNonSaveVarActions.setChatEntity({
|
||||
id: 'test',
|
||||
type: chatType.User,
|
||||
tags: [chatTags.GROUP, chatTags.GOOD_FRIEND, chatTags.FRIEND],
|
||||
unreadMessages: 0,
|
||||
}),
|
||||
);
|
||||
})
|
||||
.catch(err => {
|
||||
console.error("Database Error! Can't start App :(", err);
|
||||
|
|
|
@ -980,7 +980,7 @@ function StepFinal() {
|
|||
icon={
|
||||
<MaterialIcons
|
||||
size={20}
|
||||
color={colors.white[200]}
|
||||
color={colors.white[600]}
|
||||
name={showPassword ? 'visibility' : 'visibility-off'}
|
||||
/>
|
||||
}
|
||||
|
@ -1413,7 +1413,7 @@ function Login() {
|
|||
icon={
|
||||
<MaterialIcons
|
||||
size={20}
|
||||
color={colors.white[200]}
|
||||
color={colors.white[600]}
|
||||
name={showPassword ? 'visibility' : 'visibility-off'}
|
||||
/>
|
||||
}
|
||||
|
|
|
@ -0,0 +1,18 @@
|
|||
import {appNonSaveVarActions} from '@caj/configs/appNonSaveVarReducer';
|
||||
import BigDataManager from '@caj/helper/storage/BigDataManager';
|
||||
import {store} from '@caj/redux/store';
|
||||
import {chatEntity} from './types';
|
||||
|
||||
async function initChatDatabase(chat: chatEntity) {
|
||||
const keys = BigDataManager.databases.chatRoomInfos.keys;
|
||||
await BigDataManager.databases.chatRoomInfos.setEntry({
|
||||
[keys.RoomId]: chat.roomId,
|
||||
[keys.initSyncId]: chat.initSyncId,
|
||||
[keys.syncId]: chat.syncId,
|
||||
[keys.unreadMessages]: chat.unreadMessages,
|
||||
[keys.users]: chat.users,
|
||||
});
|
||||
store.dispatch(appNonSaveVarActions.setChatEntity(chat));
|
||||
}
|
||||
|
||||
export default initChatDatabase;
|
|
@ -1,9 +1,11 @@
|
|||
import {appNonSaveVarActions} from '@caj/configs/appNonSaveVarReducer';
|
||||
import {ThemeMode} from '@caj/configs/appVar';
|
||||
import {
|
||||
darkThemeStyle,
|
||||
getBackgroundColor,
|
||||
themeSelector,
|
||||
} from '@caj/configs/colors';
|
||||
import LangFormat from '@caj/lang/default';
|
||||
import {HomeScreenNavigationProp} from '@caj/Navigation';
|
||||
import {RootState, store} from '@caj/redux/store';
|
||||
import {ChatScreenNavigationProp} from '@caj/tabs/main/ChatTab';
|
||||
|
@ -14,7 +16,149 @@ import {useSelector} from 'react-redux';
|
|||
import {start} from 'repl';
|
||||
import {ProfilePicture} from '../ProfilePicture';
|
||||
import {TextSkeleton} from '../simple/Skeleton';
|
||||
import {chatType, getTagUI} from './types';
|
||||
import {chatEntity, getTagUI} from './types';
|
||||
|
||||
function ChatItem(props: {
|
||||
chat: chatEntity;
|
||||
navigation: HomeScreenNavigationProp;
|
||||
theme: ThemeMode;
|
||||
ppSize: number;
|
||||
tagSize: number;
|
||||
space: number;
|
||||
lang: LangFormat;
|
||||
}) {
|
||||
const chat = props.chat;
|
||||
const navigation = props.navigation;
|
||||
const theme = props.theme;
|
||||
const ppSize = props.ppSize;
|
||||
const tagSize = props.tagSize;
|
||||
const space = props.space;
|
||||
const lang = props.lang;
|
||||
|
||||
const roomId = chat.roomId;
|
||||
const user = UserManager.getUserSelector(chat.users[0]);
|
||||
|
||||
return (
|
||||
<Pressable
|
||||
onPress={() => {
|
||||
store.dispatch(appNonSaveVarActions.setSelectedChat(roomId));
|
||||
navigation.navigate('ChatList', {screen: 'Chat'});
|
||||
//navigation.navigate('Register', {screen: 'RegStepOne'});
|
||||
}}>
|
||||
{({isHovered, isFocused, isPressed}) => {
|
||||
return (
|
||||
<Box
|
||||
bg={isHovered ? 'black.200' : 'blackBG.400'}
|
||||
{...darkThemeStyle(theme, 'black.400')}
|
||||
borderRadius={ppSize / 2 + 'px'}
|
||||
borderTopRightRadius={
|
||||
chat.unreadMessages !== 0 ? tagSize / 2 + 'px' : ppSize / 2 + 'px'
|
||||
}
|
||||
borderBottomLeftRadius={
|
||||
chat.tags.length > 0 ? tagSize / 2 + 'px' : ppSize / 2 + 'px'
|
||||
}
|
||||
marginX={space + 'px'}
|
||||
padding={space + 'px'}>
|
||||
<HStack
|
||||
position={'absolute'}
|
||||
space={space * 2 + 'px'}
|
||||
right={space + 'px'}
|
||||
top={-tagSize * 0.3 + 'px'}>
|
||||
<Box
|
||||
height={tagSize + 'px'}
|
||||
paddingX={tagSize / 3 + 'px'}
|
||||
borderRadius={tagSize / 2}
|
||||
bg={'#666'}>
|
||||
<TextSkeleton
|
||||
color={'#eee'}
|
||||
SkeletonProps={{
|
||||
lines: 1,
|
||||
width: '75px',
|
||||
}}
|
||||
lineHeight={tagSize + 'px'}
|
||||
fontSize={tagSize / 1.75 + 'px'}>
|
||||
{user.AccountName.data}
|
||||
</TextSkeleton>
|
||||
</Box>
|
||||
<Box
|
||||
alignItems={'center'}
|
||||
height={tagSize + 'px'}
|
||||
paddingX={tagSize / 2.75 + 'px'}
|
||||
mt={tagSize * 0.3 + space + 'px'}
|
||||
borderRadius={tagSize / 2}
|
||||
bg={'primary.600'}
|
||||
opacity={chat.unreadMessages === 0 ? 0 : 1}>
|
||||
<TextSkeleton
|
||||
color={'#fff'}
|
||||
SkeletonProps={{
|
||||
lines: 1,
|
||||
width: '75px',
|
||||
}}
|
||||
lineHeight={tagSize + 'px'}
|
||||
fontSize={tagSize / 1.75 + 'px'}>
|
||||
{chat.unreadMessages}
|
||||
</TextSkeleton>
|
||||
</Box>
|
||||
</HStack>
|
||||
<VStack>
|
||||
<HStack space={space + 'px'}>
|
||||
<ProfilePicture UserId={roomId} size={ppSize}></ProfilePicture>
|
||||
<VStack mt={ppSize / 6 + 'px'}>
|
||||
<TextSkeleton
|
||||
lineHeight={ppSize / 3 + 'px'}
|
||||
numberOfLines={1}
|
||||
SkeletonProps={{
|
||||
lines: 1,
|
||||
width: '160px',
|
||||
}}
|
||||
color="primary.400">
|
||||
{user.Username.data}
|
||||
</TextSkeleton>
|
||||
<TextSkeleton
|
||||
lineHeight={ppSize / 3 + 'px'}
|
||||
numberOfLines={1}
|
||||
SkeletonProps={{
|
||||
lines: 1,
|
||||
width: '160px',
|
||||
}}
|
||||
color="white.700">
|
||||
{'I love Chicken McNuggets von Burgerking'}
|
||||
</TextSkeleton>
|
||||
</VStack>
|
||||
</HStack>
|
||||
{chat.tags.length > 0 ? (
|
||||
<HStack flexWrap={'wrap'}>
|
||||
{chat.tags.map((tag, i) => {
|
||||
const tagObj = getTagUI(tag, lang);
|
||||
|
||||
let tagName = tagObj.name;
|
||||
return (
|
||||
<Box
|
||||
key={'tag' + i + tag + chat.roomId}
|
||||
height={tagSize + 'px'}
|
||||
mr={space + 'px'}
|
||||
mt={space + 'px'}
|
||||
paddingX={tagSize / 3 + 'px'}
|
||||
borderRadius={tagSize / 2}
|
||||
bg={tagObj.background}>
|
||||
<Text
|
||||
color={tagObj.textColor}
|
||||
lineHeight={tagSize + 'px'}
|
||||
fontSize={tagSize / 1.75 + 'px'}>
|
||||
{tagName}
|
||||
</Text>
|
||||
</Box>
|
||||
);
|
||||
})}
|
||||
</HStack>
|
||||
) : null}
|
||||
</VStack>
|
||||
</Box>
|
||||
);
|
||||
}}
|
||||
</Pressable>
|
||||
);
|
||||
}
|
||||
|
||||
export function ListChats() {
|
||||
const theme = themeSelector();
|
||||
|
@ -42,139 +186,21 @@ export function ListChats() {
|
|||
space={tagSize * 0.3 + space + 'px'}
|
||||
width="100%"
|
||||
marginY={space + 'px'}>
|
||||
{chatActivity.map((chatId, i) => {
|
||||
const chat = chats[chatId];
|
||||
if (chat.type === chatType.User) {
|
||||
const user = UserManager.getUserSelector(chatId);
|
||||
{chatActivity.map((roomId, i) => {
|
||||
const chat = chats[roomId];
|
||||
|
||||
return (
|
||||
<Pressable
|
||||
key={'chatList_i' + i + chatId}
|
||||
onPress={() => {
|
||||
store.dispatch(appNonSaveVarActions.setSelectedChat(chatId));
|
||||
navigation.navigate('ChatList', {screen: 'Chat'});
|
||||
//navigation.navigate('Register', {screen: 'RegStepOne'});
|
||||
}}>
|
||||
{({isHovered, isFocused, isPressed}) => {
|
||||
return (
|
||||
<Box
|
||||
bg={isHovered ? 'black.200' : 'blackBG.400'}
|
||||
{...darkThemeStyle(theme, 'black.400')}
|
||||
borderRadius={ppSize / 2 + 'px'}
|
||||
borderTopRightRadius={
|
||||
chat.unreadMessages !== 0
|
||||
? tagSize / 2 + 'px'
|
||||
: ppSize / 2 + 'px'
|
||||
}
|
||||
borderBottomLeftRadius={
|
||||
chat.tags.length > 0
|
||||
? tagSize / 2 + 'px'
|
||||
: ppSize / 2 + 'px'
|
||||
}
|
||||
marginX={space + 'px'}
|
||||
padding={space + 'px'}>
|
||||
<HStack
|
||||
position={'absolute'}
|
||||
space={space * 2 + 'px'}
|
||||
right={space + 'px'}
|
||||
top={-tagSize * 0.3 + 'px'}>
|
||||
<Box
|
||||
height={tagSize + 'px'}
|
||||
paddingX={tagSize / 3 + 'px'}
|
||||
borderRadius={tagSize / 2}
|
||||
bg={'#666'}>
|
||||
<TextSkeleton
|
||||
color={'#eee'}
|
||||
SkeletonProps={{
|
||||
lines: 1,
|
||||
width: '75px',
|
||||
}}
|
||||
lineHeight={tagSize + 'px'}
|
||||
fontSize={tagSize / 1.75 + 'px'}>
|
||||
{user.AccountName.data}
|
||||
</TextSkeleton>
|
||||
</Box>
|
||||
<Box
|
||||
alignItems={'center'}
|
||||
height={tagSize + 'px'}
|
||||
paddingX={tagSize / 2.75 + 'px'}
|
||||
mt={tagSize * 0.3 + space + 'px'}
|
||||
borderRadius={tagSize / 2}
|
||||
bg={'primary.600'}
|
||||
opacity={chat.unreadMessages === 0 ? 0 : 1}>
|
||||
<TextSkeleton
|
||||
color={'#fff'}
|
||||
SkeletonProps={{
|
||||
lines: 1,
|
||||
width: '75px',
|
||||
}}
|
||||
lineHeight={tagSize + 'px'}
|
||||
fontSize={tagSize / 1.75 + 'px'}>
|
||||
{chat.unreadMessages}
|
||||
</TextSkeleton>
|
||||
</Box>
|
||||
</HStack>
|
||||
<VStack>
|
||||
<HStack space={space + 'px'}>
|
||||
<ProfilePicture
|
||||
UserId={chatId}
|
||||
size={ppSize}></ProfilePicture>
|
||||
<VStack mt={ppSize / 6 + 'px'}>
|
||||
<TextSkeleton
|
||||
lineHeight={ppSize / 3 + 'px'}
|
||||
numberOfLines={1}
|
||||
SkeletonProps={{
|
||||
lines: 1,
|
||||
width: '160px',
|
||||
}}
|
||||
color="primary.400">
|
||||
{user.Username.data}
|
||||
</TextSkeleton>
|
||||
<TextSkeleton
|
||||
lineHeight={ppSize / 3 + 'px'}
|
||||
numberOfLines={1}
|
||||
SkeletonProps={{
|
||||
lines: 1,
|
||||
width: '160px',
|
||||
}}
|
||||
color="white.700">
|
||||
{'I love Chicken McNuggets von Burgerking'}
|
||||
</TextSkeleton>
|
||||
</VStack>
|
||||
</HStack>
|
||||
{chat.tags.length > 0 ? (
|
||||
<HStack flexWrap={'wrap'}>
|
||||
{chat.tags.map((tag, i) => {
|
||||
const tagObj = getTagUI(tag, lang);
|
||||
|
||||
let tagName = tagObj.name;
|
||||
return (
|
||||
<Box
|
||||
key={'tag' + i + tag + chat.id}
|
||||
height={tagSize + 'px'}
|
||||
mr={space + 'px'}
|
||||
mt={space + 'px'}
|
||||
paddingX={tagSize / 3 + 'px'}
|
||||
borderRadius={tagSize / 2}
|
||||
bg={tagObj.background}>
|
||||
<Text
|
||||
color={tagObj.textColor}
|
||||
lineHeight={tagSize + 'px'}
|
||||
fontSize={tagSize / 1.75 + 'px'}>
|
||||
{tagName}
|
||||
</Text>
|
||||
</Box>
|
||||
);
|
||||
})}
|
||||
</HStack>
|
||||
) : null}
|
||||
</VStack>
|
||||
</Box>
|
||||
);
|
||||
}}
|
||||
</Pressable>
|
||||
);
|
||||
}
|
||||
return (
|
||||
<ChatItem
|
||||
key={'chatList_i' + i + roomId}
|
||||
navigation={navigation}
|
||||
chat={chat}
|
||||
theme={theme}
|
||||
space={space}
|
||||
ppSize={ppSize}
|
||||
tagSize={tagSize}
|
||||
lang={lang}
|
||||
/>
|
||||
);
|
||||
})}
|
||||
</VStack>
|
||||
);
|
||||
|
|
|
@ -1,13 +1,6 @@
|
|||
import {UserId} from '@caj/configs/types';
|
||||
import LangFormat from '@caj/lang/default';
|
||||
|
||||
type GroupId = string;
|
||||
|
||||
export enum chatType {
|
||||
User = 0,
|
||||
Group = 1,
|
||||
}
|
||||
|
||||
export enum chatTags {
|
||||
GROUP = 0,
|
||||
FRIEND = 1,
|
||||
|
@ -52,12 +45,20 @@ export function getTagUI(tagKey: chatTags, lang?: LangFormat): tagUITypeFinal {
|
|||
return tag as tagUITypeFinal;
|
||||
}
|
||||
|
||||
export type chatId = UserId | GroupId;
|
||||
export type roomId = string;
|
||||
type syncId = number;
|
||||
|
||||
export interface chatEntity {
|
||||
type: chatType;
|
||||
id: chatId;
|
||||
roomId: roomId;
|
||||
|
||||
|
||||
syncId: syncId;
|
||||
initSyncId: syncId;
|
||||
|
||||
|
||||
users: [UserId, ...UserId[]];
|
||||
title?: string;
|
||||
unreadMessages: number;
|
||||
|
||||
tags: chatTags[];
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
//these variables should not changed by the user and will not be saved in storage
|
||||
|
||||
import {chatEntity, chatId} from '@caj/components/chat/types';
|
||||
import {chatEntity, roomId} from '@caj/components/chat/types';
|
||||
import {getVersionByNum, VersionType} from '@caj/helper/version';
|
||||
import {User} from '@caj/user/types';
|
||||
import {UserId} from './types';
|
||||
|
@ -26,9 +26,9 @@ export interface NON_SAVE_VARS {
|
|||
appStatus: appStatus;
|
||||
connectionStatus: connectionStatus;
|
||||
cachedUsers: {[key: UserId]: User};
|
||||
chats: {[key: chatId]: chatEntity};
|
||||
chatActivity: chatId[];
|
||||
selectedChat: chatId | 'none';
|
||||
chats: {[key: roomId]: chatEntity};
|
||||
chatActivity: roomId[];
|
||||
selectedChat: roomId | 'none';
|
||||
}
|
||||
|
||||
export const non_save_vars: NON_SAVE_VARS = {
|
||||
|
|
|
@ -4,7 +4,7 @@ import type {PayloadAction} from '@reduxjs/toolkit';
|
|||
import {appStatus, non_save_vars} from './appNonSaveVar';
|
||||
import {User} from '@caj/user/types';
|
||||
import {UserId} from './types';
|
||||
import {chatEntity, chatId} from '@caj/components/chat/types';
|
||||
import {chatEntity, roomId} from '@caj/components/chat/types';
|
||||
|
||||
export const appNonSaveVariablesSlice = createSlice({
|
||||
name: 'non_save_vars',
|
||||
|
@ -19,29 +19,29 @@ export const appNonSaveVariablesSlice = createSlice({
|
|||
removeCachedUser: (state, action: PayloadAction<UserId>) => {
|
||||
delete state.cachedUsers[action.payload];
|
||||
},
|
||||
setSelectedChat: (state, action: PayloadAction<chatId>) => {
|
||||
setSelectedChat: (state, action: PayloadAction<roomId>) => {
|
||||
state.selectedChat = action.payload;
|
||||
},
|
||||
setChatEntity: (state, action: PayloadAction<chatEntity>) => {
|
||||
const chatId = action.payload.id;
|
||||
const roomId = action.payload.roomId;
|
||||
|
||||
state.chats[chatId] = action.payload;
|
||||
state.chats[roomId] = action.payload;
|
||||
|
||||
if (state.chatActivity.includes(chatId) === false)
|
||||
state.chatActivity.unshift(chatId);
|
||||
if (state.chatActivity.includes(roomId) === false)
|
||||
state.chatActivity.unshift(roomId);
|
||||
},
|
||||
changeChatEntity: (state, action: PayloadAction<chatEntity>) => {
|
||||
const chatId = action.payload.id;
|
||||
const roomId = action.payload.roomId;
|
||||
|
||||
state.chats[chatId] = action.payload;
|
||||
state.chats[roomId] = action.payload;
|
||||
|
||||
if (state.chatActivity.includes(chatId) === false)
|
||||
state.chatActivity.unshift(chatId);
|
||||
if (state.chatActivity.includes(roomId) === false)
|
||||
state.chatActivity.unshift(roomId);
|
||||
state.chatActivity.sort(function (x, y) {
|
||||
return x == chatId ? -1 : y == chatId ? 1 : 0;
|
||||
return x == roomId ? -1 : y == roomId ? 1 : 0;
|
||||
});
|
||||
},
|
||||
removeChatEntity: (state, action: PayloadAction<chatId>) => {
|
||||
removeChatEntity: (state, action: PayloadAction<roomId>) => {
|
||||
delete state.chats[action.payload];
|
||||
|
||||
state.chatActivity = state.chatActivity.filter(function (ele) {
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import {getDatabase} from './getDB';
|
||||
import {databaseConf, possibleDBKeys} from './types';
|
||||
|
||||
export const getEntry = async <T2 extends databaseConf<T>, T>(
|
||||
export const getEntry = async <T2 extends databaseConf<T, any>, T>(
|
||||
schema: T2,
|
||||
key: possibleDBKeys,
|
||||
): Promise<null | T> => {
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import {getDatabase} from './getDB.web';
|
||||
import {databaseConf, possibleDBKeys} from './types';
|
||||
|
||||
export const getEntry = async <T2 extends databaseConf<T>, T>(
|
||||
export const getEntry = async <T2 extends databaseConf<T, any>, T>(
|
||||
schema: T2,
|
||||
key: possibleDBKeys,
|
||||
): Promise<null | T> => {
|
||||
|
|
|
@ -5,6 +5,7 @@ import {getKey} from './encryption';
|
|||
import {databaseConf, databaseNames} from './types';
|
||||
|
||||
import RNFS from 'react-native-fs';
|
||||
import {databaseConfType} from './schemas';
|
||||
|
||||
const closeTimeout = 5; // in seconds // when DB read/writes is too long in idle it will be closed
|
||||
|
||||
|
@ -12,7 +13,7 @@ type DBType = Realm;
|
|||
|
||||
export interface DBObject {
|
||||
name: databaseNames;
|
||||
schema: databaseConf<{[key: string]: string}>;
|
||||
schema: databaseConfType;
|
||||
db: DBType | undefined;
|
||||
lastUsedTimestamp?: timestamp; // when timeout is undefined then db is closed
|
||||
}
|
||||
|
@ -53,7 +54,7 @@ export function closeAllDatabases() {
|
|||
}
|
||||
|
||||
export async function openMyDatabase(
|
||||
schema: databaseConf<{[key: string]: string}>,
|
||||
schema: databaseConfType,
|
||||
): Promise<DBType> {
|
||||
const folderPath = MyUserManager.getSelectedUserId();
|
||||
const path = folderPath + '/' + schema.filePath;
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import {timestamp} from '@caj/configs/types';
|
||||
import MyUserManager from '@caj/user/MyUserManager';
|
||||
import {IDBPDatabase, openDB} from 'idb';
|
||||
import {databaseConfType} from './schemas';
|
||||
import {databaseConf, databaseNames} from './types';
|
||||
|
||||
const closeTimeout = 5; // in seconds // when DB read/writes is too long in idle it will be closed
|
||||
|
@ -9,7 +10,7 @@ type DBType = IDBPDatabase<unknown>;
|
|||
|
||||
export interface DBObject {
|
||||
name: databaseNames;
|
||||
schema: databaseConf<{[key: string]: string}>;
|
||||
schema: databaseConfType;
|
||||
db: DBType | undefined;
|
||||
lastUsedTimestamp?: timestamp; // when timeout is undefined then db is closed
|
||||
}
|
||||
|
@ -50,7 +51,7 @@ export function closeAllDatabases() {
|
|||
}
|
||||
|
||||
export async function openMyDatabase(
|
||||
schema: databaseConf<{[key: string]: string}>,
|
||||
schema: databaseConfType,
|
||||
init?: boolean,
|
||||
): Promise<DBType> {
|
||||
const db = await openDB(
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import {openDB, deleteDB, wrap, unwrap} from 'idb';
|
||||
import {closeAllDatabases, databases, DBObject, getDatabase} from './getDB';
|
||||
import DBSchemas from './schemas';
|
||||
import {databaseConf} from './types';
|
||||
|
||||
export const initDatabase = (): Promise<void> => {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
|
|
|
@ -3,7 +3,7 @@ import DBSchemas from './schemas';
|
|||
import {databaseConf, databaseNames} from './types';
|
||||
|
||||
export const DBMigration: {[key in databaseNames]: any} = {
|
||||
users: (Schema: databaseConf<typeof DBSchemas.users.defaultProps>) => {
|
||||
users: (Schema: typeof DBSchemas.users) => {
|
||||
const callback: MigrationCallback = (oldRealm, newRealm) => {
|
||||
/*// only apply this change if upgrading to schemaVersion 2
|
||||
if (oldRealm.schemaVersion < 2) {
|
||||
|
@ -20,7 +20,7 @@ export const DBMigration: {[key in databaseNames]: any} = {
|
|||
|
||||
return callback;
|
||||
},
|
||||
chat: (Schema: databaseConf<typeof DBSchemas.chat.defaultProps>) => {
|
||||
chat: (Schema: typeof DBSchemas.chat) => {
|
||||
const callback: MigrationCallback = (oldRealm, newRealm) => {
|
||||
/*// only apply this change if upgrading to schemaVersion 2
|
||||
if (oldRealm.schemaVersion < 2) {
|
||||
|
@ -35,6 +35,11 @@ export const DBMigration: {[key in databaseNames]: any} = {
|
|||
}*/
|
||||
};
|
||||
|
||||
return callback;
|
||||
},
|
||||
chatRoomInfos: (Schema: typeof DBSchemas.chatRoomInfos) => {
|
||||
const callback: MigrationCallback = (oldRealm, newRealm) => {};
|
||||
|
||||
return callback;
|
||||
},
|
||||
};
|
||||
|
|
|
@ -21,7 +21,7 @@ type upgradeFunc = (
|
|||
) => void;
|
||||
|
||||
export const DBMigration: {[key in databaseNames]: any} = {
|
||||
users: (Schema: databaseConf<typeof DBSchemas.users.defaultProps>) => {
|
||||
users: (Schema: typeof DBSchemas.users) => {
|
||||
const callback: upgradeFunc = (
|
||||
db,
|
||||
oldVersion,
|
||||
|
@ -47,7 +47,33 @@ export const DBMigration: {[key in databaseNames]: any} = {
|
|||
|
||||
return callback;
|
||||
},
|
||||
chat: (Schema: databaseConf<typeof DBSchemas.chat.defaultProps>) => {
|
||||
chat: (Schema: typeof DBSchemas.chat) => {
|
||||
const callback: upgradeFunc = (
|
||||
db,
|
||||
oldVersion,
|
||||
newVersion,
|
||||
transaction,
|
||||
event,
|
||||
) => {
|
||||
if (oldVersion == 0) {
|
||||
// perform the initialization
|
||||
db.createObjectStore(Schema.details.name, {
|
||||
keyPath: Schema.details.primaryKey,
|
||||
});
|
||||
} else if (newVersion !== null) {
|
||||
let ver = oldVersion;
|
||||
|
||||
while (ver < newVersion) {
|
||||
console.log('upgrade from v', ver, ' to v', ver + 1);
|
||||
|
||||
ver++;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
return callback;
|
||||
},
|
||||
chatRoomInfos: (Schema: typeof DBSchemas.chatRoomInfos) => {
|
||||
const callback: upgradeFunc = (
|
||||
db,
|
||||
oldVersion,
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
import chat from './schemas/chat';
|
||||
import users from './schemas/users';
|
||||
import chatRoomInfos from './schemas/chatRoomInfos';
|
||||
|
||||
const DBSchemas = {users, chat};
|
||||
const DBSchemas = {users, chat, chatRoomInfos};
|
||||
export default DBSchemas;
|
||||
export type databaseConfType = typeof DBSchemas[keyof typeof DBSchemas];
|
||||
|
|
|
@ -4,22 +4,28 @@ import {setEntry} from '../set';
|
|||
|
||||
import {databaseConf, possibleDBKeys} from '../types';
|
||||
|
||||
enum keys {
|
||||
UserId = 'a',
|
||||
msg = 'b',
|
||||
}
|
||||
|
||||
const name = 'chat';
|
||||
const primaryKey: keyof typeof propsDefault = 'UserId';
|
||||
const primaryKey: keyof typeof propsDefault = keys.UserId;
|
||||
|
||||
const propsType: {[key in keyof typeof propsDefault]: string} = {
|
||||
UserId: 'string',
|
||||
msg: 'string',
|
||||
[keys.UserId]: 'string',
|
||||
[keys.msg]: 'string',
|
||||
};
|
||||
|
||||
const propsDefault = {
|
||||
UserId: 'test',
|
||||
msg: 'test',
|
||||
[keys.UserId]: 'test',
|
||||
[keys.msg]: 'test',
|
||||
};
|
||||
|
||||
const thisSchema: databaseConf<typeof propsDefault> = {
|
||||
const thisSchema: databaseConf<typeof propsDefault, typeof keys> = {
|
||||
filePath: name,
|
||||
version: 1,
|
||||
keys,
|
||||
migration: () => {
|
||||
return DBMigration[name](thisSchema);
|
||||
},
|
||||
|
|
|
@ -0,0 +1,61 @@
|
|||
import {getEntry} from '../get';
|
||||
import {DBMigration} from '../migration';
|
||||
import {setEntry} from '../set';
|
||||
|
||||
import {databaseConf, possibleDBKeys} from '../types';
|
||||
|
||||
enum keys {
|
||||
RoomId = 'a',
|
||||
syncId = 'b',
|
||||
initSyncId = 'c',
|
||||
unreadMessages = 'd',
|
||||
users = 'e',
|
||||
}
|
||||
|
||||
const name = 'chatRoomInfos';
|
||||
const primaryKey: keyof typeof propsDefault = keys.RoomId;
|
||||
|
||||
const propsType: {[key in keyof typeof propsDefault]: string} = {
|
||||
[keys.RoomId]: 'string',
|
||||
[keys.syncId]: 'int',
|
||||
[keys.initSyncId]: 'int',
|
||||
[keys.unreadMessages]: 'int',
|
||||
[keys.users]: 'string[]',
|
||||
};
|
||||
|
||||
const propsDefault = {
|
||||
[keys.RoomId]: '',
|
||||
[keys.syncId]: 0,
|
||||
[keys.initSyncId]: 0,
|
||||
[keys.unreadMessages]: 0,
|
||||
[keys.users]: ['none'],
|
||||
};
|
||||
|
||||
const thisSchema: databaseConf<typeof propsDefault, typeof keys> = {
|
||||
filePath: name,
|
||||
version: 1,
|
||||
keys,
|
||||
migration: () => {
|
||||
return DBMigration[name](thisSchema);
|
||||
},
|
||||
setEntry: (val: typeof thisSchema.defaultProps) => {
|
||||
return setEntry<typeof thisSchema, typeof thisSchema.defaultProps>(
|
||||
thisSchema,
|
||||
val,
|
||||
);
|
||||
},
|
||||
getEntry: (key: possibleDBKeys) => {
|
||||
return getEntry<typeof thisSchema, typeof thisSchema.defaultProps>(
|
||||
thisSchema,
|
||||
key,
|
||||
);
|
||||
},
|
||||
defaultProps: propsDefault,
|
||||
details: {
|
||||
name,
|
||||
properties: propsType,
|
||||
primaryKey,
|
||||
},
|
||||
};
|
||||
|
||||
export default thisSchema;
|
|
@ -4,43 +4,60 @@ import {setEntry} from '../set';
|
|||
|
||||
import {databaseConf, possibleDBKeys} from '../types';
|
||||
|
||||
enum keys {
|
||||
UserId = 'a',
|
||||
AccountName = 'b',
|
||||
Username = 'c',
|
||||
Description = 'd',
|
||||
FollowersCount = 'e',
|
||||
FollowingCount = 'f',
|
||||
lastUpdateTimestamp = 'g',
|
||||
ProfilePicture = 'h',
|
||||
ProfilePictureBinaryLQ = 'i',
|
||||
ProfilePictureBinaryHQ = 'j',
|
||||
|
||||
XpLevel = 'k',
|
||||
XpPoints = 'l',
|
||||
}
|
||||
|
||||
const name = 'users';
|
||||
const primaryKey: keyof typeof propsDefault = 'UserId';
|
||||
const primaryKey: keyof typeof propsDefault = keys.UserId;
|
||||
|
||||
const propsType: {[key in keyof typeof propsDefault]: string} = {
|
||||
UserId: 'string',
|
||||
AccountName: 'string',
|
||||
Username: 'string',
|
||||
Description: 'string',
|
||||
FollowersCount: 'int',
|
||||
FollowingCount: 'int',
|
||||
lastUpdateTimestamp: 'int',
|
||||
ProfilePicture: 'string', //URL
|
||||
ProfilePictureBinaryLQ: 'data',
|
||||
ProfilePictureBinaryHQ: 'data',
|
||||
[keys.UserId]: 'string',
|
||||
[keys.AccountName]: 'string',
|
||||
[keys.Username]: 'string',
|
||||
[keys.Description]: 'string',
|
||||
[keys.FollowersCount]: 'int',
|
||||
[keys.FollowingCount]: 'int',
|
||||
[keys.lastUpdateTimestamp]: 'int',
|
||||
[keys.ProfilePicture]: 'string', //URL
|
||||
[keys.ProfilePictureBinaryLQ]: 'data',
|
||||
[keys.ProfilePictureBinaryHQ]: 'data',
|
||||
|
||||
XpLevel: 'int',
|
||||
XpPoints: 'int',
|
||||
[keys.XpLevel]: 'int',
|
||||
[keys.XpPoints]: 'int',
|
||||
};
|
||||
|
||||
const propsDefault = {
|
||||
UserId: '',
|
||||
AccountName: '',
|
||||
Username: '',
|
||||
Description: '',
|
||||
FollowersCount: 0,
|
||||
FollowingCount: 0,
|
||||
lastUpdateTimestamp: 0,
|
||||
ProfilePicture: '', //URL
|
||||
ProfilePictureBinaryLQ: new ArrayBuffer(0),
|
||||
ProfilePictureBinaryHQ: new ArrayBuffer(0),
|
||||
XpLevel: 0,
|
||||
XpPoints: 0,
|
||||
[keys.UserId]: '',
|
||||
[keys.AccountName]: '',
|
||||
[keys.Username]: '',
|
||||
[keys.Description]: '',
|
||||
[keys.FollowersCount]: 0,
|
||||
[keys.FollowingCount]: 0,
|
||||
[keys.lastUpdateTimestamp]: 0,
|
||||
[keys.ProfilePicture]: '', //URL
|
||||
[keys.ProfilePictureBinaryLQ]: new ArrayBuffer(0),
|
||||
[keys.ProfilePictureBinaryHQ]: new ArrayBuffer(0),
|
||||
[keys.XpLevel]: 0,
|
||||
[keys.XpPoints]: 0,
|
||||
};
|
||||
|
||||
const thisSchema: databaseConf<typeof propsDefault> = {
|
||||
const thisSchema: databaseConf<typeof propsDefault, typeof keys> = {
|
||||
filePath: name,
|
||||
version: 1,
|
||||
keys,
|
||||
migration: () => {
|
||||
return DBMigration[name](thisSchema);
|
||||
},
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import {getDatabase} from './getDB';
|
||||
import {databaseConf} from './types';
|
||||
|
||||
export const setEntry = async <T2 extends databaseConf<T>, T>(
|
||||
export const setEntry = async <T2 extends databaseConf<T, any>, T>(
|
||||
schema: T2,
|
||||
value: T,
|
||||
) => {
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import {getDatabase} from './getDB.web';
|
||||
import {databaseConf} from './types';
|
||||
|
||||
export const setEntry = async <T2 extends databaseConf<T>, T>(
|
||||
export const setEntry = async <T2 extends databaseConf<T, any>, T>(
|
||||
schema: T2,
|
||||
value: T,
|
||||
) => {
|
||||
|
|
|
@ -1,10 +1,11 @@
|
|||
export type databaseNames = 'users' | 'chat';
|
||||
export type databaseNames = 'users' | 'chat' | 'chatRoomInfos';
|
||||
export type possibleDBKeys = string;
|
||||
|
||||
export interface databaseConf<props> {
|
||||
export interface databaseConf<props, enums> {
|
||||
filePath: string;
|
||||
version: number;
|
||||
migration?: any;
|
||||
keys: enums;
|
||||
getEntry: (key: possibleDBKeys) => Promise<props | null>;
|
||||
setEntry: (val: props) => Promise<void>;
|
||||
defaultProps: props;
|
||||
|
|
|
@ -10,8 +10,11 @@ import {
|
|||
createNativeStackNavigator,
|
||||
NativeStackNavigationProp,
|
||||
} from '@react-navigation/native-stack';
|
||||
import {Box, Center, Container, Text} from 'native-base';
|
||||
import {Box, Button, Center, Container, Text} from 'native-base';
|
||||
import {useSelector} from 'react-redux';
|
||||
import initChatDatabase from '@caj/components/chat/initChatDatabase';
|
||||
import MyUserManager from '@caj/user/MyUserManager';
|
||||
import {chatTags} from '@caj/components/chat/types';
|
||||
|
||||
export const AccountTabName = 'Account';
|
||||
|
||||
|
@ -66,6 +69,35 @@ function AccountScreen() {
|
|||
<MyUserAccountInfoBanner />
|
||||
</Box>
|
||||
<NotLoggedIn />
|
||||
<Center mt={100}>
|
||||
<Button
|
||||
w="72"
|
||||
colorScheme="primary"
|
||||
rounded="xl"
|
||||
_text={{fontSize: 'xl'}}
|
||||
onPress={() => {
|
||||
initChatDatabase({
|
||||
roomId: MyUserManager.getSelectedUserId(),
|
||||
users: [MyUserManager.getSelectedUserId()],
|
||||
initSyncId: 0,
|
||||
syncId: 10,
|
||||
|
||||
tags: [chatTags.GOOD_FRIEND],
|
||||
unreadMessages: 1234,
|
||||
});
|
||||
|
||||
initChatDatabase({
|
||||
roomId: 'test',
|
||||
users: ['test'],
|
||||
initSyncId: 0,
|
||||
syncId: 0,
|
||||
tags: [chatTags.GROUP, chatTags.GOOD_FRIEND, chatTags.FRIEND],
|
||||
unreadMessages: 0,
|
||||
});
|
||||
}}>
|
||||
Init Chats
|
||||
</Button>
|
||||
</Center>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
|
|
@ -57,37 +57,58 @@ async function getUser(
|
|||
}
|
||||
|
||||
if (user === undefined) {
|
||||
const usrDBKeys = BigDataManager.databases.users.keys;
|
||||
const usr = await BigDataManager.databases.users.getEntry(UserId);
|
||||
|
||||
if (usr !== undefined && usr !== null) {
|
||||
let ProfilePicture = {
|
||||
lq:
|
||||
usr.ProfilePictureBinaryLQ.byteLength !== 0
|
||||
usr[usrDBKeys.ProfilePictureBinaryLQ].byteLength !== 0
|
||||
? createUserProp(
|
||||
SourceProp.offline,
|
||||
new Blob([usr.ProfilePictureBinaryLQ]),
|
||||
new Blob([usr[usrDBKeys.ProfilePictureBinaryLQ]]),
|
||||
)
|
||||
: createUserProp(SourceProp.online, undefined, usr.ProfilePicture),
|
||||
: createUserProp(
|
||||
SourceProp.online,
|
||||
undefined,
|
||||
usr[usrDBKeys.ProfilePicture],
|
||||
),
|
||||
hq:
|
||||
usr.ProfilePictureBinaryHQ.byteLength !== 0
|
||||
usr[usrDBKeys.ProfilePictureBinaryHQ].byteLength !== 0
|
||||
? createUserProp(
|
||||
SourceProp.offline,
|
||||
new Blob([usr.ProfilePictureBinaryHQ]),
|
||||
new Blob([usr[usrDBKeys.ProfilePictureBinaryHQ]]),
|
||||
)
|
||||
: createUserProp(SourceProp.online, undefined, usr.ProfilePicture),
|
||||
: createUserProp(
|
||||
SourceProp.online,
|
||||
undefined,
|
||||
usr[usrDBKeys.ProfilePicture],
|
||||
),
|
||||
};
|
||||
|
||||
user = {
|
||||
AccountName: createUserProp(SourceProp.offline, usr.AccountName),
|
||||
Description: createUserProp(SourceProp.offline, usr.Description),
|
||||
FollowersCount: createUserProp(SourceProp.offline, usr.FollowersCount),
|
||||
FollowingCount: createUserProp(SourceProp.offline, usr.FollowingCount),
|
||||
lastUpdateTimestamp: usr.lastUpdateTimestamp,
|
||||
AccountName: createUserProp(
|
||||
SourceProp.offline,
|
||||
usr[usrDBKeys.AccountName],
|
||||
),
|
||||
Description: createUserProp(
|
||||
SourceProp.offline,
|
||||
usr[usrDBKeys.Description],
|
||||
),
|
||||
FollowersCount: createUserProp(
|
||||
SourceProp.offline,
|
||||
usr[usrDBKeys.FollowersCount],
|
||||
),
|
||||
FollowingCount: createUserProp(
|
||||
SourceProp.offline,
|
||||
usr[usrDBKeys.FollowingCount],
|
||||
),
|
||||
lastUpdateTimestamp: usr[usrDBKeys.lastUpdateTimestamp],
|
||||
ProfilePicture,
|
||||
UserId,
|
||||
Username: createUserProp(SourceProp.offline, usr.Username),
|
||||
XpLevel: createUserProp(SourceProp.offline, usr.XpLevel),
|
||||
XpPoints: createUserProp(SourceProp.offline, usr.XpPoints),
|
||||
Username: createUserProp(SourceProp.offline, usr[usrDBKeys.Username]),
|
||||
XpLevel: createUserProp(SourceProp.offline, usr[usrDBKeys.XpLevel]),
|
||||
XpPoints: createUserProp(SourceProp.offline, usr[usrDBKeys.XpPoints]),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
{
|
||||
"compilerOptions": {
|
||||
"outDir": "./dist",
|
||||
"baseUrl": ".",
|
||||
"paths" : {
|
||||
"@caj/*": ["src/caj/*"]
|
||||
|
|
Loading…
Reference in New Issue