commit 9ec167714affa617b8c4cf0c0f0883d543bc964f
parent cf2c20ebdb095f72d7e7bcfab50e185e8747b311
Author: triesap <137732411+triesap@users.noreply.github.com>
Date: Fri, 23 Aug 2024 12:20:28 +0000
client: add capacitor dialog api
Diffstat:
4 files changed, 68 insertions(+), 4 deletions(-)
diff --git a/client/package.json b/client/package.json
@@ -10,6 +10,7 @@
"dependencies": {
"@capacitor/core": "^6.1.2",
"@capacitor/device": "^6.0.0",
+ "@capacitor/dialog": "^6.0.0",
"@capacitor/haptics": "^6.0.0",
"@capacitor/network": "^6.0.0",
"@capacitor/preferences": "^6.0.0",
diff --git a/client/src/capacitor/dialog.ts b/client/src/capacitor/dialog.ts
@@ -0,0 +1,41 @@
+import { Dialog } from "@capacitor/dialog";
+import type { IClientDialog, IClientDialogPrompt } from "../types";
+
+export class CapacitorClientDialog implements IClientDialog {
+ public async alert(message: string): Promise<boolean> {
+ try {
+ await Dialog.alert({ message });
+ return true;
+ } catch (e) {
+ return false;
+ };
+ }
+
+ public async confirm(message: string): Promise<boolean> {
+ try {
+ const res = await Dialog.confirm({ message });
+ if (res && typeof res.value === `boolean`) return res.value;
+ return false;
+ } catch (e) {
+ return false;
+ };
+ }
+
+ public async prompt(opts: IClientDialogPrompt): Promise<string | false> {
+ try {
+ const { title, message, ok_button_title: okButtonTitle, cancel_button_title: cancelButtonTitle, input_placeholder: inputPlaceholder, input_text: inputText } = opts;
+ const res = await Dialog.prompt({
+ title,
+ message,
+ okButtonTitle,
+ cancelButtonTitle,
+ inputPlaceholder,
+ inputText
+ });
+ if (res && typeof res.value === `string`) return res.value;
+ return false;
+ } catch (e) {
+ return false;
+ };
+ }
+}
diff --git a/client/src/capacitor/index.ts b/client/src/capacitor/index.ts
@@ -1,8 +1,9 @@
import { Capacitor } from "@capacitor/core";
-import type { IClient, IClientDevice, IClientHaptics, IClientKeystore, IClientNetwork, IClientPlatform, IClientPreferences, IClientShare, IClientWifi } from "../types";
+import type { IClient, IClientDevice, IClientDialog, IClientHaptics, IClientKeystore, IClientNetwork, IClientPlatform, IClientPreferences, IClientShare, IClientWifi } from "../types";
import { parse_platform } from "../utils";
import { CapacitorClientDevice } from "./device";
+import { CapacitorClientDialog } from "./dialog";
import { CapacitorClientHaptics } from "./haptics";
import { CapacitorClientKeystore } from "./keystore";
import { CapacitorClientNetwork } from "./network";
@@ -19,6 +20,7 @@ export class ClientCapacitor implements IClient {
private _preferences: IClientPreferences = new CapacitorClientPreferences();
private _share: IClientShare = new CapacitorClientShare();
private _wifi: IClientWifi = new CapacitorClientWifi();
+ private _dialog: IClientDialog = new CapacitorClientDialog();
public get platform() {
return this._platform;
@@ -51,4 +53,8 @@ export class ClientCapacitor implements IClient {
public get wifi() {
return this._wifi;
}
+
+ public get dialog() {
+ return this._dialog;
+ }
};
\ No newline at end of file
diff --git a/client/src/types.ts b/client/src/types.ts
@@ -10,6 +10,7 @@ export type IClient = {
preferences: IClientPreferences;
share: IClientShare;
wifi: IClientWifi;
+ dialog: IClientDialog;
};
export type IClientPlatform = `androiď` | `ios` | `web`;
@@ -77,5 +78,21 @@ export type IClientWifi = {
current: () => Promise<IClientWifiCurrentResult | undefined>;
connect: (ssid: string, password: string) => Promise<IClientWifiConnectResult | undefined>;
connect_prefix: (ssidPrefix: string, password: string) => Promise<IClientWifiConnectResult | undefined>;
- disconnect: () => Promise<void>
-};
-\ No newline at end of file
+ disconnect: () => Promise<void>;
+};
+
+export type IClientDialogPrompt = {
+ title?: string;
+ message: string;
+ ok_button_title?: string;
+ cancel_button_title?: string;
+ input_placeholder?: string;
+ input_text?: string;
+};
+
+
+export type IClientDialog = {
+ alert(message: string): Promise<boolean>;
+ confirm(message: string): Promise<boolean>;
+ prompt(opts: IClientDialogPrompt): Promise<string | false>;
+};