commit 34f18c3ee0b85c453c5a073f94549bbdd467ef80
parent 0582d226f858d43bd96c282c4abf7c4f20ffeb19
Author: triesap <137732411+triesap@users.noreply.github.com>
Date: Fri, 23 Aug 2024 10:31:27 +0000
client: add capacitor haptics api
Diffstat:
4 files changed, 52 insertions(+), 1 deletion(-)
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/haptics": "^6.0.0",
"@radroots/capacitor-secure-storage": "workspace:*"
},
"devDependencies": {
diff --git a/client/src/capacitor/haptics.ts b/client/src/capacitor/haptics.ts
@@ -0,0 +1,34 @@
+import { Haptics, ImpactStyle } from '@capacitor/haptics';
+import type { IClientHaptics } from '../types';
+
+export class CapacitorClientHaptics implements IClientHaptics {
+ public impact = async (mod?: 'less' | 'more'): Promise<void> => {
+ try {
+ await Haptics.impact({ style: mod ? mod === `more` ? ImpactStyle.Heavy : ImpactStyle.Light : ImpactStyle.Medium });
+ } catch (e) { };
+ };
+
+ public vibrate = async (duration?: number): Promise<void> => {
+ try {
+ await Haptics.vibrate(duration ? { duration } : undefined);
+ } catch (e) { };
+ };
+
+ public selection_start = async (): Promise<void> => {
+ try {
+ await Haptics.selectionStart();
+ } catch (e) { };
+ };
+
+ public selection_changed = async (): Promise<void> => {
+ try {
+ await Haptics.selectionChanged();
+ } catch (e) { };
+ };
+
+ public selection_end = async (): Promise<void> => {
+ try {
+ await Haptics.selectionEnd();
+ } catch (e) { };
+ };
+}
diff --git a/client/src/capacitor/index.ts b/client/src/capacitor/index.ts
@@ -1,14 +1,16 @@
import { Capacitor } from "@capacitor/core";
-import type { IClient, IClientDevice, IClientKeystore, IClientPlatform } from "../types";
+import type { IClient, IClientDevice, IClientHaptics, IClientKeystore, IClientPlatform } from "../types";
import { parse_platform } from "../utils";
import { CapacitorClientDevice } from "./device";
+import { CapacitorClientHaptics } from "./haptics";
import { CapacitorClientKeystore } from "./keystore";
export class ClientCapacitor implements IClient {
private _platform: IClientPlatform = parse_platform(Capacitor.getPlatform());
private _keystore: IClientKeystore = new CapacitorClientKeystore();
private _device: IClientDevice = new CapacitorClientDevice();
+ private _haptics: IClientHaptics = new CapacitorClientHaptics();
public get platform() {
return this._platform;
@@ -21,4 +23,8 @@ export class ClientCapacitor implements IClient {
public get device() {
return this._device;
}
+
+ public get haptics() {
+ return this._haptics;
+ }
};
\ No newline at end of file
diff --git a/client/src/types.ts b/client/src/types.ts
@@ -7,6 +7,7 @@ export type IClient = {
platform: IClientPlatform;
keystore: IClientKeystore;
device: IClientDevice;
+ haptics: IClientHaptics;
};
export type IClientPlatform = `androiď` | `ios` | `web`;
@@ -23,3 +24,11 @@ export type IClientDevice = {
info(): Promise<CapacitorDeviceInfo | undefined>;
battery(): Promise<CapacitorDeviceBatteryInfo | undefined>;
};
+
+export type IClientHaptics = {
+ impact: (mod?: "less" | "more") => Promise<void>;
+ vibrate: (duration?: number) => Promise<void>;
+ selection_start: () => Promise<void>;
+ selection_changed: () => Promise<void>;
+ selection_end: () => Promise<void>;
+};
+\ No newline at end of file