2020-08-05 15:39:16 +01:00
|
|
|
import { Injectable } from '@angular/core';
|
2021-01-13 10:02:30 +01:00
|
|
|
import { Plugins } from '@capacitor/core';
|
|
|
|
|
const { Storage } = Plugins;
|
2020-08-05 15:39:16 +01:00
|
|
|
@Injectable({
|
|
|
|
|
providedIn: 'root'
|
2020-08-06 14:31:07 +01:00
|
|
|
})
|
|
|
|
|
export class StorageService {
|
2021-01-13 10:02:30 +01:00
|
|
|
constructor() {}
|
2020-08-06 14:31:07 +01:00
|
|
|
|
2021-01-13 10:02:30 +01:00
|
|
|
// Store the value
|
|
|
|
|
async store(storageKey: string, value: any) {
|
|
|
|
|
const encryptedValue = btoa(escape(JSON.stringify(value)));
|
|
|
|
|
await Storage.set({
|
|
|
|
|
key: storageKey,
|
|
|
|
|
value: encryptedValue
|
|
|
|
|
});
|
2020-08-06 14:31:07 +01:00
|
|
|
}
|
2020-12-21 16:37:44 +01:00
|
|
|
|
2021-01-13 10:02:30 +01:00
|
|
|
// Get the value
|
|
|
|
|
async get(storageKey: string) {
|
|
|
|
|
const ret = await Storage.get({ key: storageKey });
|
|
|
|
|
return JSON.parse(unescape(atob(ret.value)));
|
2020-08-06 14:31:07 +01:00
|
|
|
}
|
|
|
|
|
|
2021-01-13 10:02:30 +01:00
|
|
|
async removeStorageItem(storageKey: string) {
|
|
|
|
|
await Storage.remove({ key: storageKey });
|
|
|
|
|
}
|
2020-08-05 15:39:16 +01:00
|
|
|
|
2020-08-06 14:31:07 +01:00
|
|
|
// Clear storage
|
|
|
|
|
async clear() {
|
2021-01-13 10:02:30 +01:00
|
|
|
await Storage.clear();
|
2020-12-16 16:06:14 +01:00
|
|
|
}
|
2020-08-05 15:39:16 +01:00
|
|
|
}
|