commit 1807c249cb2361541b33525852136335f16843e8
parent 6cc4fbbf49d1b3ece31e37942637b311455a222b
Author: triesap <137732411+triesap@users.noreply.github.com>
Date: Fri, 23 Aug 2024 10:42:10 +0000
client: add capacitor share api
Diffstat:
4 files changed, 51 insertions(+), 1 deletion(-)
diff --git a/client/package.json b/client/package.json
@@ -13,6 +13,7 @@
"@capacitor/haptics": "^6.0.0",
"@capacitor/network": "^6.0.0",
"@capacitor/preferences": "^6.0.0",
+ "@capacitor/share": "^6.0.0",
"@radroots/capacitor-secure-storage": "workspace:*"
},
"devDependencies": {
diff --git a/client/src/capacitor/index.ts b/client/src/capacitor/index.ts
@@ -1,12 +1,13 @@
import { Capacitor } from "@capacitor/core";
-import type { IClient, IClientDevice, IClientHaptics, IClientKeystore, IClientNetwork, IClientPlatform, IClientPreferences } from "../types";
+import type { IClient, IClientDevice, IClientHaptics, IClientKeystore, IClientNetwork, IClientPlatform, IClientPreferences, IClientShare } 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";
+import { CapacitorClientShare } from "./share";
export class ClientCapacitor implements IClient {
private _platform: IClientPlatform = parse_platform(Capacitor.getPlatform());
@@ -15,6 +16,7 @@ export class ClientCapacitor implements IClient {
private _haptics: IClientHaptics = new CapacitorClientHaptics();
private _network: IClientNetwork = new CapacitorClientNetwork();
private _preferences: IClientPreferences = new CapacitorClientPreferences();
+ private _share: IClientShare = new CapacitorClientShare();
public get platform() {
return this._platform;
@@ -39,4 +41,8 @@ export class ClientCapacitor implements IClient {
public get preferences() {
return this._preferences;
}
+
+ public get share() {
+ return this._share;
+ }
};
\ No newline at end of file
diff --git a/client/src/capacitor/share.ts b/client/src/capacitor/share.ts
@@ -0,0 +1,29 @@
+import { Share } from '@capacitor/share';
+import type { ICapacitorShareOpts, IClientShare } from '../types';
+
+export class CapacitorClientShare implements IClientShare {
+ public async status(): Promise<boolean> {
+ try {
+ const res = await Share.canShare();
+ if (res && typeof res.value === `boolean`) return res.value;
+ return false;
+ } catch (e) {
+ return false;
+ };
+ }
+
+ public async share(opts: ICapacitorShareOpts): Promise<void> {
+ try {
+ const { title, text, url, files, dialog_title: dialogTitle } = opts;
+ await Share.share({
+ title,
+ text,
+ url,
+ files,
+ dialogTitle
+ });
+ } catch (e) { };
+ }
+}
+
+
diff --git a/client/src/types.ts b/client/src/types.ts
@@ -7,6 +7,7 @@ export type IClient = {
haptics: IClientHaptics;
network: IClientNetwork;
preferences: IClientPreferences;
+ share: IClientShare;
};
export type IClientPlatform = `androiď` | `ios` | `web`;
@@ -50,4 +51,17 @@ export type IClientNetwork = {
export type IClientPreferences = {
set(key: string, value: string): Promise<boolean>;
get(key: string): Promise<string | undefined>;
+};
+
+export type ICapacitorShareOpts = {
+ title?: string;
+ text?: string;
+ url?: string;
+ files?: string[];
+ dialog_title?: string;
+};
+
+export type IClientShare = {
+ status(): Promise<boolean>;
+ share(opts: ICapacitorShareOpts): Promise<void>;
};
\ No newline at end of file