Files
doneit-web/src/app/services/storage.service.ts
T

82 lines
2.0 KiB
TypeScript
Raw Normal View History

2020-08-05 15:39:16 +01:00
import { Injectable } from '@angular/core';
2021-02-25 12:41:29 +01:00
import { Storage } from '@ionic/storage';
2021-08-31 09:00:33 +01:00
import { AnimationController, ModalController,Platform } from '@ionic/angular';
import { SHA1 } from 'crypto-js'
import { localstoreService } from '../store/localstore.service';
2021-02-23 16:10:14 +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-08-31 09:00:33 +01:00
private keyName: string;
constructor(private storage:Storage,
private platform: Platform
) {}
key(key) {
2021-08-31 13:33:52 +01:00
this.keyName = (SHA1('service'+this.constructor.name+key)).toString()
2021-08-31 09:00:33 +01:00
}
2020-08-06 14:31:07 +01:00
2021-02-25 12:41:29 +01:00
// Store the value
2021-08-31 09:00:33 +01:00
async store(key: string, value: any) {
if (this.platform.is('desktop') || this.platform.is('mobileweb')) {
await localstoreService.set(this.key(key), value)
} else {
const encryptedValue = btoa(escape(JSON.stringify(value)));
await this.storage.set(key, encryptedValue);
}
2020-08-06 14:31:07 +01:00
}
2021-02-25 12:41:29 +01:00
// Get the value
async get(key: string) {
2021-08-31 09:00:33 +01:00
if (this.platform.is('desktop') || this.platform.is('mobileweb')) {
return new Promise((resolve, reject)=>{
const data = localstoreService.get(this.key(key), false)
if(data) resolve(data)
else reject(data)
})
} else {
const ret = await this.storage.get(key).then((val) => { return val; });
try {
return JSON.parse(unescape(atob(ret)));
} catch (error) {
return unescape(atob(ret))
}
2021-08-20 12:31:14 +01:00
}
2021-08-31 09:00:33 +01:00
2021-02-26 08:38:33 +01:00
}
2021-08-18 17:36:40 +01:00
async remove(key: string){
2021-08-31 09:00:33 +01:00
if (this.platform.is('desktop') || this.platform.is('mobileweb')) {
await localstoreService.delete(this.key(key))
} else {
await this.storage.remove(key);
}
2021-08-18 17:36:40 +01:00
}
2021-08-20 12:31:14 +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();
2021-02-23 16:10:14 +01:00
} */
2020-08-05 15:39:16 +01:00
}