Skip to content

Commit

Permalink
feat(core): migrate preferences storage to basic storage (#484)
Browse files Browse the repository at this point in the history
* feat: add func migrate preferences storage to basic storage

* fix: unittest

* chore: fix error unittest

* chore: add unittest for biametry cache and indentifier view type cache

* chore: change location function createOrUpdate and some unittest

* chore: change misc record id name

* chore: change misc record name

* chore: change use biometryCacheCache

* chore: rm state and fix unittest
  • Loading branch information
bao-sotatek committed May 23, 2024
1 parent f45e214 commit 516e779
Show file tree
Hide file tree
Showing 45 changed files with 607 additions and 449 deletions.
9 changes: 0 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
"@capacitor/core": "^5.0.0",
"@capacitor/ios": "^5.0.0",
"@capacitor/keyboard": "^5.0.0",
"@capacitor/preferences": "^5.0.0",
"@capacitor/screen-orientation": "^5.0.7",
"@capacitor/share": "^5.0.0",
"@capacitor/splash-screen": "^5.0.0",
Expand Down
11 changes: 10 additions & 1 deletion src/core/agent/agent.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,16 @@ interface ConnectionHistoryItem {
}

enum MiscRecordId {
OP_PASS_HINT = "app-op-password-hint",
OP_PASS_HINT = "op-password-hint",
APP_ALREADY_INIT = "app-already-init",
APP_STATE_FLAGS = "app-state-flags",
APP_LANGUAGE = "app-language",
IDENTIFIERS_FAVOURITES = "identifiers-favourites",
CREDS_FAVOURITES = "creds-favourites",
USER_NAME = "user-name",
APP_BIOMETRY = "app-biometry",
KERIA_NOTIFICATION_MARKER = "keria-notification-marker",
APP_IDENTIFIER_VIEW_TYPE = "app-identifier-view-type",
}

interface ConnectionShortDetails {
Expand Down
18 changes: 18 additions & 0 deletions src/core/agent/records/basicStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import {
import { BasicRecord } from "./basicRecord";

class BasicStorage implements StorageApi {
static readonly RECORD_DOES_NOT_EXIST_ERROR_MSG =
"Record does not exist with id";

private storageService: StorageService<BasicRecord>;

constructor(storageService: StorageService<BasicRecord>) {
Expand Down Expand Up @@ -39,6 +42,21 @@ class BasicStorage implements StorageApi {
getAll(): Promise<BasicRecord[]> {
return this.storageService.getAll(BasicRecord);
}
async createOrUpdateBasicRecord(record: BasicRecord): Promise<void> {
try {
await this.update(record);
} catch (error) {
if (
error instanceof Error &&
error.message ===
`${BasicStorage.RECORD_DOES_NOT_EXIST_ERROR_MSG} ${record.id}`
) {
await this.save(record);
} else {
throw error;
}
}
}
}

export { BasicStorage };
52 changes: 23 additions & 29 deletions src/core/agent/services/signifyNotificationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import {
AgentServicesProps,
KeriaNotification,
KeriaNotificationMarker,
MiscRecordId,
NotificationRoute,
} from "../agent.types";
import { Notification } from "./credentialService.types";
import { PreferencesKeys, PreferencesStorage } from "../../storage";
import { NotificationStorage } from "../records";
import { BasicRecord, NotificationStorage } from "../records";
import { Agent } from "../agent";

class SignifyNotificationService extends AgentService {
Expand All @@ -31,27 +31,17 @@ class SignifyNotificationService extends AgentService {
nextIndex: 0,
lastNotificationId: "",
};
try {
notificationQuery = (await PreferencesStorage.get(
PreferencesKeys.APP_KERIA_NOTIFICATION_MARKER
)) as unknown as KeriaNotificationMarker;
} catch (error) {
if (
(error as Error).message ==
`${PreferencesStorage.KEY_NOT_FOUND} ${PreferencesKeys.APP_KERIA_NOTIFICATION_MARKER}`
) {
// Set the preference key
await PreferencesStorage.set(
PreferencesKeys.APP_KERIA_NOTIFICATION_MARKER,
{
nextIndex: 0,
lastNotificationId: "",
}
);
} else {
throw error;
}
}
const notificationQueryRecord = await Agent.agent.basicStorage.findById(
MiscRecordId.KERIA_NOTIFICATION_MARKER
);
if (!notificationQueryRecord) {
await Agent.agent.basicStorage.save({
id: MiscRecordId.KERIA_NOTIFICATION_MARKER,
content: notificationQuery,
});
} else
notificationQuery =
notificationQueryRecord.content as unknown as KeriaNotificationMarker;
// eslint-disable-next-line no-constant-condition
while (true) {
if (!Agent.agent.getKeriaOnlineStatus()) {
Expand Down Expand Up @@ -91,9 +81,11 @@ class SignifyNotificationService extends AgentService {
nextIndex: 0,
lastNotificationId: "",
};
await PreferencesStorage.set(
PreferencesKeys.APP_KERIA_NOTIFICATION_MARKER,
notificationQuery
await Agent.agent.basicStorage.createOrUpdateBasicRecord(
new BasicRecord({
id: MiscRecordId.KERIA_NOTIFICATION_MARKER,
content: notificationQuery,
})
);
continue;
}
Expand All @@ -112,9 +104,11 @@ class SignifyNotificationService extends AgentService {
lastNotificationId:
notifications.notes[notifications.notes.length - 1].i,
};
await PreferencesStorage.set(
PreferencesKeys.APP_KERIA_NOTIFICATION_MARKER,
notificationQuery
await Agent.agent.basicStorage.createOrUpdateBasicRecord(
new BasicRecord({
id: MiscRecordId.KERIA_NOTIFICATION_MARKER,
content: notificationQuery,
})
);
} else {
await new Promise((rs) =>
Expand Down
1 change: 0 additions & 1 deletion src/core/storage/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from "./secureStorage";
export * from "./preferences";
1 change: 0 additions & 1 deletion src/core/storage/preferences/index.ts

This file was deleted.

60 changes: 0 additions & 60 deletions src/core/storage/preferences/preferencesStorage.test.ts

This file was deleted.

46 changes: 0 additions & 46 deletions src/core/storage/preferences/preferencesStorage.ts

This file was deleted.

5 changes: 0 additions & 5 deletions src/core/storage/preferences/preferencesStorage.type.ts

This file was deleted.

12 changes: 12 additions & 0 deletions src/routes/backRoute/backRoute.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ describe("getBackRoute", () => {
connectedWallet: null,
pendingConnection: null,
},
identifierViewTypeCacheCache: {
viewType: null,
},
biometryCache: {
enabled: false,
},
};
});

Expand Down Expand Up @@ -176,6 +182,12 @@ describe("getPreviousRoute", () => {
connectedWallet: null,
pendingConnection: null,
},
identifierViewTypeCacheCache: {
viewType: null,
},
biometryCache: {
enabled: false,
},
};
});

Expand Down
12 changes: 12 additions & 0 deletions src/routes/nextRoute/nextRoute.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ describe("NextRoute", () => {
connectedWallet: null,
pendingConnection: null,
},
identifierViewTypeCacheCache: {
viewType: null,
},
biometryCache: {
enabled: false,
},
};
data = {
store: storeMock,
Expand Down Expand Up @@ -176,6 +182,12 @@ describe("getNextRoute", () => {
connectedWallet: null,
pendingConnection: null,
},
identifierViewTypeCacheCache: {
viewType: null,
},
biometryCache: {
enabled: false,
},
};
const state = {};
const payload = {};
Expand Down
7 changes: 7 additions & 0 deletions src/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import { identifiersCacheSlice } from "./reducers/identifiersCache";
import { credsCacheSlice } from "./reducers/credsCache";
import { connectionsCacheSlice } from "./reducers/connectionsCache";
import { walletConnectionsCacheSlice } from "./reducers/walletConnectionsCache";
import {
getIdentifierViewTypeCacheCache,
identifierViewTypeCacheSlice,
} from "./reducers/identifierViewTypeCache";
import { biometryCacheSlice } from "./reducers/biometryCache";

const store = configureStore({
reducer: {
Expand All @@ -14,6 +19,8 @@ const store = configureStore({
credsCache: credsCacheSlice.reducer,
connectionsCache: connectionsCacheSlice.reducer,
walletConnectionsCache: walletConnectionsCacheSlice.reducer,
identifierViewTypeCacheCache: identifierViewTypeCacheSlice.reducer,
biometryCache: biometryCacheSlice.reducer,
},
});

Expand Down
21 changes: 21 additions & 0 deletions src/store/reducers/biometryCache/biometryCache.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { PayloadAction } from "@reduxjs/toolkit";
import { biometryCacheSlice, setEnableBiometryCache } from "./biometryCache";

describe("biometryCache", () => {
const initialState = {
enabled: false,
};
it("should return the initial state", () => {
expect(biometryCacheSlice.reducer(undefined, {} as PayloadAction)).toEqual(
initialState
);
});

it("should handle setEnableBiometryCache", () => {
const newState = biometryCacheSlice.reducer(
initialState,
setEnableBiometryCache(true)
);
expect(newState.enabled).toEqual(true);
});
});
Loading

0 comments on commit 516e779

Please sign in to comment.