database
parent
468c5bbff6
commit
31bf860d84
|
@ -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),
|
||||
|
|
|
@ -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 (
|
||||
<Center width={300}>
|
||||
<Skeleton.Text lines={1} px="12" />
|
||||
</Center>
|
||||
);
|
||||
}
|
||||
|
||||
return <Center>{myUser.Username.data}</Center>;
|
||||
}
|
||||
|
||||
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 <Center>undefined</Center>;
|
||||
}
|
||||
|
||||
return <Center>{myUser.AccountName.data}</Center>;
|
||||
}
|
||||
|
||||
export default function AccountInfoBanner() {
|
||||
return <Center>Account</Center>;
|
||||
return (
|
||||
<Center>
|
||||
Account
|
||||
<Test />
|
||||
<Test2 />
|
||||
</Center>
|
||||
);
|
||||
}
|
||||
|
|
|
@ -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: {},
|
||||
};
|
||||
|
|
|
@ -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<appStatus>) => {
|
||||
state.appStatus = action.payload;
|
||||
},
|
||||
setCachedUser: (state, action: PayloadAction<User>) => {
|
||||
state.cachedUsers[action.payload.UserId] = action.payload;
|
||||
},
|
||||
removeCachedUser: (state, action: PayloadAction<UserId>) => {
|
||||
delete state.cachedUsers[action.payload];
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
|
|
|
@ -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 <T2 extends databaseConf<T>, T>(
|
||||
db: T2,
|
||||
schema: T2,
|
||||
key: possibleDBKeys,
|
||||
): Promise<null | T> => {
|
||||
const dbName = db.details.name;
|
||||
const dbName = schema.details.name;
|
||||
const realm = await getDatabase(dbName);
|
||||
|
||||
const val = realm.objectForPrimaryKey<typeof db.details.properties>(
|
||||
const val = realm.objectForPrimaryKey<typeof schema.details.properties>(
|
||||
dbName,
|
||||
key,
|
||||
);
|
||||
|
|
|
@ -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 <T2 extends databaseConf<T>, T>(
|
||||
schema: T2,
|
||||
key: possibleDBKeys,
|
||||
): Promise<null | T> => {
|
||||
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);
|
||||
|
||||
|
|
|
@ -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(),
|
||||
|
|
|
@ -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<typeof propsDefault> = {
|
|||
migration: () => {
|
||||
return DBMigration[name](thisSchema);
|
||||
},
|
||||
setEntry: (val: typeof thisSchema.defaultProps) => {
|
||||
return setEntry<typeof thisSchema, typeof thisSchema.defaultProps>(
|
||||
thisSchema,
|
||||
val,
|
||||
);
|
||||
},
|
||||
getEntry: (key: possibleDBKeys) => {
|
||||
const thisObj = thisSchema;
|
||||
return getEntry<typeof thisObj, typeof thisObj.defaultProps>(thisObj, key);
|
||||
return getEntry<typeof thisSchema, typeof thisSchema.defaultProps>(
|
||||
thisSchema,
|
||||
key,
|
||||
);
|
||||
},
|
||||
defaultProps: propsDefault,
|
||||
details: {
|
||||
|
|
|
@ -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<typeof propsDefault> = {
|
|||
migration: () => {
|
||||
return DBMigration[name](thisSchema);
|
||||
},
|
||||
setEntry: (val: typeof thisSchema.defaultProps) => {
|
||||
return setEntry<typeof thisSchema, typeof thisSchema.defaultProps>(
|
||||
thisSchema,
|
||||
val,
|
||||
);
|
||||
},
|
||||
getEntry: (key: possibleDBKeys) => {
|
||||
const thisObj = thisSchema;
|
||||
return getEntry<typeof thisObj, typeof thisObj.defaultProps>(thisObj, key);
|
||||
return getEntry<typeof thisSchema, typeof thisSchema.defaultProps>(
|
||||
thisSchema,
|
||||
key,
|
||||
);
|
||||
},
|
||||
defaultProps: propsDefault,
|
||||
details: {
|
||||
|
|
|
@ -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 <T2 extends databaseConf<T>, 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);
|
||||
});
|
||||
};
|
||||
|
|
|
@ -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 <T2 extends databaseConf<T>, 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;
|
||||
};
|
||||
|
|
|
@ -6,6 +6,7 @@ export interface databaseConf<props> {
|
|||
version: number;
|
||||
migration?: any;
|
||||
getEntry: (key: possibleDBKeys) => Promise<props | null>;
|
||||
setEntry: (val: props) => Promise<void>;
|
||||
defaultProps: props;
|
||||
details: {
|
||||
name: databaseNames;
|
||||
|
|
|
@ -36,7 +36,6 @@ function createNewMyUser(
|
|||
WebSocketSessionId,
|
||||
lastUpdateTimestamp: Math.floor(new Date().getTime() / 1000),
|
||||
ProfilePicture: {
|
||||
hq: createUserProp(SourceProp.online),
|
||||
lq: createUserProp(SourceProp.online),
|
||||
},
|
||||
userSettings: {
|
||||
|
|
|
@ -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<User | undefined> {
|
||||
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;
|
||||
|
|
|
@ -23,8 +23,8 @@ interface BasicUserProp<T1> {
|
|||
}
|
||||
|
||||
export interface ProfilePicture {
|
||||
lq: BasicUserProp<string>;
|
||||
hq: BasicUserProp<string>;
|
||||
lq: BasicUserProp<string | Blob>;
|
||||
hq?: BasicUserProp<string | Blob>;
|
||||
}
|
||||
|
||||
export interface User {
|
||||
|
|
Loading…
Reference in New Issue