Created
September 25, 2025 07:19
-
-
Save parollon/40769ba7329eda42e1e9f625c8114ee3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { Injectable } from '@angular/core'; | |
| import { | |
| Preferences, | |
| ConfigureOptions, | |
| GetResult, | |
| MigrateResult, | |
| KeysResult | |
| } from '@capacitor/preferences'; | |
| @Injectable({ | |
| providedIn: 'root', | |
| }) | |
| export class CapacitorStorageService { | |
| constructor() {} | |
| public async set(_key: string, _value: any): Promise<void> { | |
| return Preferences.set({ key: _key, value: JSON.stringify(_value) }); | |
| } | |
| public async get<T>(_key: string): Promise<T | undefined> { | |
| try { | |
| const res = await Preferences.get({ key: _key }); | |
| return res.value ? (JSON.parse(res.value) as T) : undefined; | |
| } catch { | |
| return undefined; | |
| } | |
| } | |
| public async remove(_key: string): Promise<void> { | |
| return Preferences.remove({ key: _key }); | |
| } | |
| public async clear(): Promise<void> { | |
| return Preferences.clear(); | |
| } | |
| public async keys(): Promise<string[]> { | |
| try{ | |
| const keySet:KeysResult = await Preferences.keys() | |
| return keySet.keys ? keySet.keys : [] | |
| }catch{ | |
| return [] | |
| } | |
| } | |
| public async configure(_options: ConfigureOptions): Promise<void> { | |
| return Preferences.configure(_options); | |
| } | |
| public async migrate(): Promise<MigrateResult> { | |
| return Preferences.migrate(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment