web_lib

Common web application libraries
git clone https://radroots.dev/git/web_lib.git
Log | Files | Refs | LICENSE

commit 6cc4fbbf49d1b3ece31e37942637b311455a222b
parent 040708a7567bf08145e1f2a024daa091eba4343b
Author: triesap <137732411+triesap@users.noreply.github.com>
Date:   Fri, 23 Aug 2024 10:39:32 +0000

client: add capacitor preferences api

Diffstat:
Mclient/package.json | 1+
Mclient/src/capacitor/index.ts | 8+++++++-
Aclient/src/capacitor/preferences.ts | 20++++++++++++++++++++
Mclient/src/types.ts | 6++++++
4 files changed, 34 insertions(+), 1 deletion(-)

diff --git a/client/package.json b/client/package.json @@ -12,6 +12,7 @@ "@capacitor/device": "^6.0.0", "@capacitor/haptics": "^6.0.0", "@capacitor/network": "^6.0.0", + "@capacitor/preferences": "^6.0.0", "@radroots/capacitor-secure-storage": "workspace:*" }, "devDependencies": { diff --git a/client/src/capacitor/index.ts b/client/src/capacitor/index.ts @@ -1,11 +1,12 @@ import { Capacitor } from "@capacitor/core"; -import type { IClient, IClientDevice, IClientHaptics, IClientKeystore, IClientNetwork, IClientPlatform } from "../types"; +import type { IClient, IClientDevice, IClientHaptics, IClientKeystore, IClientNetwork, IClientPlatform, IClientPreferences } from "../types"; import { parse_platform } from "../utils"; import { CapacitorClientDevice } from "./device"; import { CapacitorClientHaptics } from "./haptics"; import { CapacitorClientKeystore } from "./keystore"; import { CapacitorClientNetwork } from "./network"; +import { CapacitorClientPreferences } from "./preferences"; export class ClientCapacitor implements IClient { private _platform: IClientPlatform = parse_platform(Capacitor.getPlatform()); @@ -13,6 +14,7 @@ export class ClientCapacitor implements IClient { private _device: IClientDevice = new CapacitorClientDevice(); private _haptics: IClientHaptics = new CapacitorClientHaptics(); private _network: IClientNetwork = new CapacitorClientNetwork(); + private _preferences: IClientPreferences = new CapacitorClientPreferences(); public get platform() { return this._platform; @@ -33,4 +35,8 @@ export class ClientCapacitor implements IClient { public get network() { return this._network; } + + public get preferences() { + return this._preferences; + } }; \ No newline at end of file diff --git a/client/src/capacitor/preferences.ts b/client/src/capacitor/preferences.ts @@ -0,0 +1,20 @@ +import { Preferences } from '@capacitor/preferences'; +import type { IClientPreferences } from '../types'; + +export class CapacitorClientPreferences implements IClientPreferences { + public async set(key: string, value: string): Promise<boolean> { + try { + await Preferences.set({ key, value }); + return true; + } catch (e) { + return false; + }; + } + + public async get(key: string): Promise<string | undefined> { + try { + const res = await Preferences.get({ key }); + if (typeof res.value === 'string') return res.value; + } catch (e) { }; + } +} diff --git a/client/src/types.ts b/client/src/types.ts @@ -6,6 +6,7 @@ export type IClient = { device: IClientDevice; haptics: IClientHaptics; network: IClientNetwork; + preferences: IClientPreferences; }; export type IClientPlatform = `androiď` | `ios` | `web`; @@ -44,4 +45,9 @@ export type IClientNetworkConnection = { export type IClientNetwork = { status(): Promise<IClientNetworkConnection | undefined>; close(): Promise<boolean>; +}; + +export type IClientPreferences = { + set(key: string, value: string): Promise<boolean>; + get(key: string): Promise<string | undefined>; }; \ No newline at end of file