web_lib

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

commit 040708a7567bf08145e1f2a024daa091eba4343b
parent 34f18c3ee0b85c453c5a073f94549bbdd467ef80
Author: triesap <137732411+triesap@users.noreply.github.com>
Date:   Fri, 23 Aug 2024 10:36:06 +0000

client: add capacitor network api

Diffstat:
Mclient/package.json | 1+
Mclient/src/capacitor/index.ts | 8+++++++-
Aclient/src/capacitor/network.ts | 20++++++++++++++++++++
Mclient/src/types.ts | 19++++++++++++++++---
4 files changed, 44 insertions(+), 4 deletions(-)

diff --git a/client/package.json b/client/package.json @@ -11,6 +11,7 @@ "@capacitor/core": "^6.1.2", "@capacitor/device": "^6.0.0", "@capacitor/haptics": "^6.0.0", + "@capacitor/network": "^6.0.0", "@radroots/capacitor-secure-storage": "workspace:*" }, "devDependencies": { diff --git a/client/src/capacitor/index.ts b/client/src/capacitor/index.ts @@ -1,16 +1,18 @@ import { Capacitor } from "@capacitor/core"; -import type { IClient, IClientDevice, IClientHaptics, IClientKeystore, IClientPlatform } from "../types"; +import type { IClient, IClientDevice, IClientHaptics, IClientKeystore, IClientNetwork, IClientPlatform } from "../types"; import { parse_platform } from "../utils"; import { CapacitorClientDevice } from "./device"; import { CapacitorClientHaptics } from "./haptics"; import { CapacitorClientKeystore } from "./keystore"; +import { CapacitorClientNetwork } from "./network"; 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(); + private _network: IClientNetwork = new CapacitorClientNetwork(); public get platform() { return this._platform; @@ -27,4 +29,8 @@ export class ClientCapacitor implements IClient { public get haptics() { return this._haptics; } + + public get network() { + return this._network; + } }; \ No newline at end of file diff --git a/client/src/capacitor/network.ts b/client/src/capacitor/network.ts @@ -0,0 +1,20 @@ +import { Network } from '@capacitor/network'; +import type { IClientNetwork, IClientNetworkConnection } from '../types'; + +export class CapacitorClientNetwork implements IClientNetwork { + public async status(): Promise<IClientNetworkConnection | undefined> { + try { + const { connected, connectionType: connection_type } = await Network.getStatus(); + return { connected, connection_type }; + } catch (e) { }; + } + + public async close(): Promise<boolean> { + try { + await Network.removeAllListeners(); + return true; + } catch (e) { + return false; + }; + } +} diff --git a/client/src/types.ts b/client/src/types.ts @@ -1,13 +1,11 @@ import { type BatteryInfo, type DeviceInfo } from '@capacitor/device'; -export type CapacitorDeviceInfo = DeviceInfo; -export type CapacitorDeviceBatteryInfo = BatteryInfo; - export type IClient = { platform: IClientPlatform; keystore: IClientKeystore; device: IClientDevice; haptics: IClientHaptics; + network: IClientNetwork; }; export type IClientPlatform = `androiď` | `ios` | `web`; @@ -20,6 +18,9 @@ export type IClientKeystore = { remove(key: string): Promise<boolean>; }; +export type CapacitorDeviceInfo = DeviceInfo; +export type CapacitorDeviceBatteryInfo = BatteryInfo; + export type IClientDevice = { info(): Promise<CapacitorDeviceInfo | undefined>; battery(): Promise<CapacitorDeviceBatteryInfo | undefined>; @@ -31,4 +32,16 @@ export type IClientHaptics = { selection_start: () => Promise<void>; selection_changed: () => Promise<void>; selection_end: () => Promise<void>; +}; + +export type IClientNetworkConnectionType = `wifi` | `cellular` | `none` | `unknown`; + +export type IClientNetworkConnection = { + connected: boolean; + connection_type: IClientNetworkConnectionType; +}; + +export type IClientNetwork = { + status(): Promise<IClientNetworkConnection | undefined>; + close(): Promise<boolean>; }; \ No newline at end of file