diff --git a/src/appStart/StartHelper.tsx b/src/appStart/StartHelper.tsx index f16e678..cb35b03 100644 --- a/src/appStart/StartHelper.tsx +++ b/src/appStart/StartHelper.tsx @@ -30,26 +30,36 @@ function onAppStart() { BigDataManager.initDatabase() .then(async () => { console.log('finish'); - await BigDataManager.setEntry('users', { + await BigDataManager.databases.users.setEntry({ UserId: 'test', AccountName: 'test222222222', Username: 'us', + Description: 'daw', + FollowersCount: 2, + FollowingCount: 24, + lastUpdateTimestamp: 412341234, + ProfilePicture: '', + ProfilePictureBinaryHQ: new ArrayBuffer(0), + ProfilePictureBinaryLQ: new ArrayBuffer(0), + XpLevel: 0, + XpPoints: 0, }); setTimeout(async () => { console.log('get'); const obj = await BigDataManager.databases.users.getEntry('test'); - console.log('obj', obj); - console.log('objAccName', obj?.AccountName); - console.log( - 'user', - await UserManager.getUser(MyUserManager.getSelectedUserId()), - ); - console.log( - 'user2', - await UserManager.getUser('ce97da21-c5f1-46f8-947b-ce9288b74ed1'), - ); - }, 1100); + + //await UserManager.getUser(MyUserManager.getSelectedUserId()); + + //await UserManager.getUser('ce97da21-c5f1-46f8-947b-ce9288b74ed1'); + }, 4100); + setTimeout(async () => { + console.log('get'); + const obj = await BigDataManager.databases.users.getEntry('test'); + + //await UserManager.getUser(MyUserManager.getSelectedUserId()); + //await UserManager.getUser('7396bd88-fe36-4e2e-9810-a83b33440ab6'); + }, 2100); store.dispatch( appNonSaveVarActions.setAppStatus(appStatus.APP_RUNNING), diff --git a/src/caj/components/AccountInfoBanner.tsx b/src/caj/components/AccountInfoBanner.tsx index 0f3532d..522a6df 100644 --- a/src/caj/components/AccountInfoBanner.tsx +++ b/src/caj/components/AccountInfoBanner.tsx @@ -1,5 +1,56 @@ -import {Center} from 'native-base'; +import {UserId} from '@caj/configs/types'; +import {RootState} from '@caj/redux/store'; +import MyUserManager from '@caj/user/MyUserManager'; +import UserManager from '@caj/user/UserManager'; +import {Center, Skeleton} from 'native-base'; +import {useSelector} from 'react-redux'; + +// 7396bd88-fe36-4e2e-9810-a83b33440ab6 + +function Test() { + /*UserManager.getUser(uuid).then(user => { + //console.log(user); + });*/ + + const myUser = UserManager.getUserSelector(MyUserManager.getSelectedUserId()); + + console.log('reload', myUser); + + if (myUser === undefined) { + return ( +
+ +
+ ); + } + + return
{myUser.Username.data}
; +} + +function Test2() { + const uuid = 'ce97da21-c5f1-46f8-947b-ce9288b74ed1'; + + /*UserManager.getUser(uuid).then(user => { + //console.log(user); + });*/ + + const myUser = UserManager.getUserSelector(uuid); + + console.log('reload', myUser); + + if (myUser === undefined) { + return
undefined
; + } + + return
{myUser.AccountName.data}
; +} export default function AccountInfoBanner() { - return
Account
; + return ( +
+ Account + + +
+ ); } diff --git a/src/caj/configs/appNonSaveVar.ts b/src/caj/configs/appNonSaveVar.ts index a9737dd..75d0653 100644 --- a/src/caj/configs/appNonSaveVar.ts +++ b/src/caj/configs/appNonSaveVar.ts @@ -1,9 +1,12 @@ //these variables should not changed by the user and will not be saved in storage import {getVersionByNum, VersionType} from '@caj/helper/version'; +import {User} from '@caj/user/types'; +import {UserId} from './types'; export const APP_VERSION = getVersionByNum(2); export const AppVarMaxBackups: number = 10; +export const maxCachedUsers = 30; export enum appStatus { IS_LOADING, @@ -21,10 +24,12 @@ export interface NON_SAVE_VARS { currentAppVersion: VersionType; appStatus: appStatus; connectionStatus: connectionStatus; + cachedUsers: {[key: UserId]: User}; } export const non_save_vars: NON_SAVE_VARS = { currentAppVersion: APP_VERSION, appStatus: appStatus.IS_LOADING, connectionStatus: connectionStatus.UNKNOWN, + cachedUsers: {}, }; diff --git a/src/caj/configs/appNonSaveVarReducer.ts b/src/caj/configs/appNonSaveVarReducer.ts index 4f5cd0e..f19371c 100644 --- a/src/caj/configs/appNonSaveVarReducer.ts +++ b/src/caj/configs/appNonSaveVarReducer.ts @@ -2,6 +2,8 @@ import {createSlice} from '@reduxjs/toolkit'; import type {PayloadAction} from '@reduxjs/toolkit'; import {appStatus, non_save_vars} from './appNonSaveVar'; +import {User} from '@caj/user/types'; +import {UserId} from './types'; export const appNonSaveVariablesSlice = createSlice({ name: 'non_save_vars', @@ -10,6 +12,12 @@ export const appNonSaveVariablesSlice = createSlice({ setAppStatus: (state, action: PayloadAction) => { state.appStatus = action.payload; }, + setCachedUser: (state, action: PayloadAction) => { + state.cachedUsers[action.payload.UserId] = action.payload; + }, + removeCachedUser: (state, action: PayloadAction) => { + delete state.cachedUsers[action.payload]; + }, }, }); diff --git a/src/caj/helper/storage/bdm/get.ts b/src/caj/helper/storage/bdm/get.ts index a645906..bc2271a 100644 --- a/src/caj/helper/storage/bdm/get.ts +++ b/src/caj/helper/storage/bdm/get.ts @@ -1,15 +1,14 @@ -import DBSchemas from './schemas'; import {getDatabase} from './getDB'; -import {databaseConf, databaseNames, possibleDBKeys} from './types'; +import {databaseConf, possibleDBKeys} from './types'; export const getEntry = async , T>( - db: T2, + schema: T2, key: possibleDBKeys, ): Promise => { - const dbName = db.details.name; + const dbName = schema.details.name; const realm = await getDatabase(dbName); - const val = realm.objectForPrimaryKey( + const val = realm.objectForPrimaryKey( dbName, key, ); diff --git a/src/caj/helper/storage/bdm/get.web.ts b/src/caj/helper/storage/bdm/get.web.ts index 26cf4c7..1c52c1d 100644 --- a/src/caj/helper/storage/bdm/get.web.ts +++ b/src/caj/helper/storage/bdm/get.web.ts @@ -1,12 +1,13 @@ -import MyUserManager from '@caj/user/MyUserManager'; import {getDatabase} from './getDB.web'; -import {databaseNames, possibleDBKeys} from './types'; +import {databaseConf, possibleDBKeys} from './types'; -export const getEntry = async (dbName: databaseNames, key: possibleDBKeys) => { +export const getEntry = async , T>( + schema: T2, + key: possibleDBKeys, +): Promise => { + const dbName = schema.details.name; const db = await getDatabase(dbName); - const UserId = MyUserManager.getSelectedUserId(); - const tx = db.transaction(dbName, 'readonly'); const store = tx.objectStore(dbName); diff --git a/src/caj/helper/storage/bdm/getDB.ts b/src/caj/helper/storage/bdm/getDB.ts index ae8cc9f..a3b7a52 100644 --- a/src/caj/helper/storage/bdm/getDB.ts +++ b/src/caj/helper/storage/bdm/getDB.ts @@ -67,7 +67,7 @@ export async function openMyDatabase( } return await Realm.open({ - schema: [schema.details], + schema: [schema.details as any], schemaVersion: schema.version, path, encryptionKey: getKey(), diff --git a/src/caj/helper/storage/bdm/schemas/chat.ts b/src/caj/helper/storage/bdm/schemas/chat.ts index 2c8dbfe..babee26 100644 --- a/src/caj/helper/storage/bdm/schemas/chat.ts +++ b/src/caj/helper/storage/bdm/schemas/chat.ts @@ -1,5 +1,6 @@ import {getEntry} from '../get'; import {DBMigration} from '../migration'; +import {setEntry} from '../set'; import {databaseConf, possibleDBKeys} from '../types'; @@ -22,9 +23,17 @@ const thisSchema: databaseConf = { migration: () => { return DBMigration[name](thisSchema); }, + setEntry: (val: typeof thisSchema.defaultProps) => { + return setEntry( + thisSchema, + val, + ); + }, getEntry: (key: possibleDBKeys) => { - const thisObj = thisSchema; - return getEntry(thisObj, key); + return getEntry( + thisSchema, + key, + ); }, defaultProps: propsDefault, details: { diff --git a/src/caj/helper/storage/bdm/schemas/users.ts b/src/caj/helper/storage/bdm/schemas/users.ts index 46f5dbf..6ed463f 100644 --- a/src/caj/helper/storage/bdm/schemas/users.ts +++ b/src/caj/helper/storage/bdm/schemas/users.ts @@ -1,5 +1,6 @@ import {getEntry} from '../get'; import {DBMigration} from '../migration'; +import {setEntry} from '../set'; import {databaseConf, possibleDBKeys} from '../types'; @@ -15,7 +16,9 @@ const propsType: {[key in keyof typeof propsDefault]: string} = { FollowingCount: 'int', lastUpdateTimestamp: 'int', ProfilePicture: 'string', //URL - ProfilePictureBinary: 'data', + ProfilePictureBinaryLQ: 'data', + ProfilePictureBinaryHQ: 'data', + XpLevel: 'int', XpPoints: 'int', }; @@ -29,7 +32,8 @@ const propsDefault = { FollowingCount: 0, lastUpdateTimestamp: 0, ProfilePicture: '', //URL - ProfilePictureBinary: [], + ProfilePictureBinaryLQ: new ArrayBuffer(0), + ProfilePictureBinaryHQ: new ArrayBuffer(0), XpLevel: 0, XpPoints: 0, }; @@ -40,9 +44,17 @@ const thisSchema: databaseConf = { migration: () => { return DBMigration[name](thisSchema); }, + setEntry: (val: typeof thisSchema.defaultProps) => { + return setEntry( + thisSchema, + val, + ); + }, getEntry: (key: possibleDBKeys) => { - const thisObj = thisSchema; - return getEntry(thisObj, key); + return getEntry( + thisSchema, + key, + ); }, defaultProps: propsDefault, details: { diff --git a/src/caj/helper/storage/bdm/set.ts b/src/caj/helper/storage/bdm/set.ts index bfbb893..2d7f0ec 100644 --- a/src/caj/helper/storage/bdm/set.ts +++ b/src/caj/helper/storage/bdm/set.ts @@ -1,10 +1,14 @@ import {getDatabase} from './getDB'; -import {databaseNames} from './types'; +import {databaseConf} from './types'; -export const setEntry = async (dbName: databaseNames, value: any) => { +export const setEntry = async , T>( + schema: T2, + value: T, +) => { + const dbName = schema.details.name; const realm = await getDatabase(dbName); realm.write(() => { - realm.create(dbName, value, Realm.UpdateMode.Modified); + realm.create(dbName, value as any, Realm.UpdateMode.Modified); }); }; diff --git a/src/caj/helper/storage/bdm/set.web.ts b/src/caj/helper/storage/bdm/set.web.ts index 0f3f4da..602e64e 100644 --- a/src/caj/helper/storage/bdm/set.web.ts +++ b/src/caj/helper/storage/bdm/set.web.ts @@ -1,15 +1,16 @@ -import MyUserManager from '@caj/user/MyUserManager'; import {getDatabase} from './getDB.web'; -import {databaseNames} from './types'; +import {databaseConf} from './types'; -export const setEntry = async (dbName: databaseNames, value: any) => { +export const setEntry = async , T>( + schema: T2, + value: T, +) => { + const dbName = schema.details.name; const db = await getDatabase(dbName); - const UserId = MyUserManager.getSelectedUserId(); - const tx = db.transaction(dbName, 'readwrite'); const store = tx.objectStore(dbName); - //const val = (await store.get('counter')) || 0; + await store.put(value); await tx.done; }; diff --git a/src/caj/helper/storage/bdm/types.ts b/src/caj/helper/storage/bdm/types.ts index 783b64b..0af4655 100644 --- a/src/caj/helper/storage/bdm/types.ts +++ b/src/caj/helper/storage/bdm/types.ts @@ -6,6 +6,7 @@ export interface databaseConf { version: number; migration?: any; getEntry: (key: possibleDBKeys) => Promise; + setEntry: (val: props) => Promise; defaultProps: props; details: { name: databaseNames; diff --git a/src/caj/user/MyUserManager.ts b/src/caj/user/MyUserManager.ts index ccc85b6..afeaefe 100644 --- a/src/caj/user/MyUserManager.ts +++ b/src/caj/user/MyUserManager.ts @@ -36,7 +36,6 @@ function createNewMyUser( WebSocketSessionId, lastUpdateTimestamp: Math.floor(new Date().getTime() / 1000), ProfilePicture: { - hq: createUserProp(SourceProp.online), lq: createUserProp(SourceProp.online), }, userSettings: { diff --git a/src/caj/user/UserManager.ts b/src/caj/user/UserManager.ts index 36207c6..5d57202 100644 --- a/src/caj/user/UserManager.ts +++ b/src/caj/user/UserManager.ts @@ -1,16 +1,26 @@ +import {maxCachedUsers} from '@caj/configs/appNonSaveVar'; +import {appNonSaveVarActions} from '@caj/configs/appNonSaveVarReducer'; import {UserId, XAuthorization} from '@caj/configs/types'; import {makeRequest, apiBackendRequest} from '@caj/helper/request'; import BigDataManager from '@caj/helper/storage/BigDataManager'; -import {store} from '@caj/redux/store'; +import {RootState, store} from '@caj/redux/store'; +import {useSelector} from 'react-redux'; import {createUserProp, SourceProp, User} from './types'; +let cachedUserList: UserId[] = []; + async function getUser( UserId: UserId, save?: boolean, ): Promise { + if (UserId === 'none') { + return undefined; + } + let user: User | undefined; let state = store.getState(); + let userIsInCache = false; if (UserId === state.appVariables.preferences.selectedAccount) { const usr = state.appVariables.preferences.accounts[UserId]; @@ -31,18 +41,42 @@ async function getUser( } } + if (user === undefined) { + const usr = state.nonSaveVariables.cachedUsers[UserId]; + if (usr !== undefined) { + user = usr; + userIsInCache = true; + } + } + if (user === undefined) { const usr = await BigDataManager.databases.users.getEntry(UserId); - if (usr !== null) { + if (usr !== undefined && usr !== null) { + let ProfilePicture = { + lq: + usr.ProfilePictureBinaryLQ.byteLength !== 0 + ? createUserProp( + SourceProp.offline, + new Blob([usr.ProfilePictureBinaryLQ]), + ) + : createUserProp(SourceProp.online, usr.ProfilePicture), + hq: + usr.ProfilePictureBinaryHQ.byteLength !== 0 + ? createUserProp( + SourceProp.offline, + new Blob([usr.ProfilePictureBinaryHQ]), + ) + : createUserProp(SourceProp.online, usr.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, - ProfilePicture: createUserProp(SourceProp.offline, usr.ProfilePicture), + ProfilePicture, UserId, Username: createUserProp(SourceProp.offline, usr.Username), XpLevel: createUserProp(SourceProp.offline, usr.XpLevel), @@ -87,7 +121,6 @@ async function getUser( ), lastUpdateTimestamp: Math.floor(new Date().getTime() / 1000), ProfilePicture: { - hq: createUserProp(SourceProp.online, resp.response.AvatarUrl), lq: createUserProp(SourceProp.online, resp.response.AvatarUrl), }, UserId, @@ -96,14 +129,37 @@ async function getUser( XpPoints: createUserProp(SourceProp.cached, resp.response.XpPoints), }; - BigDataManager.setEntry('users', user); + //BigDataManager.setEntry('users', user); } catch (error: any) { console.error(error.status); } } + if (userIsInCache === false && user !== undefined) { + store.dispatch(appNonSaveVarActions.setCachedUser(user)); + cachedUserList.push(user.UserId); + + if (cachedUserList.length > maxCachedUsers) { + let usrId = cachedUserList[0]; + cachedUserList.shift(); + console.log('usrId', usrId); + + store.dispatch(appNonSaveVarActions.removeCachedUser(usrId)); + } + } + return user; } -const UserManager = {getUser}; +function getUserSelector(UserId: UserId) { + getUser(UserId); + + const myUser = useSelector( + (state: RootState) => state.nonSaveVariables.cachedUsers[UserId], + ); + + return myUser; +} + +const UserManager = {getUser, getUserSelector}; export default UserManager; diff --git a/src/caj/user/types.ts b/src/caj/user/types.ts index 0524f33..d76552a 100644 --- a/src/caj/user/types.ts +++ b/src/caj/user/types.ts @@ -23,8 +23,8 @@ interface BasicUserProp { } export interface ProfilePicture { - lq: BasicUserProp; - hq: BasicUserProp; + lq: BasicUserProp; + hq?: BasicUserProp; } export interface User {