From 4e5d9a3a4cf4621f6e6308064b590160c33c69f5 Mon Sep 17 00:00:00 2001 From: "sultanmyrza@gmail.com" Date: Sat, 12 Feb 2022 13:09:37 +0800 Subject: [PATCH] refactor(go-pro-bluetooth.service.ts): use preferenceManager instead of Plugins.Storage --- .../services/go-pro-bluetooth.service.ts | 37 ++++++++++++------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/src/app/features/settings/go-pro/services/go-pro-bluetooth.service.ts b/src/app/features/settings/go-pro/services/go-pro-bluetooth.service.ts index f688844d2..ca5adbdba 100644 --- a/src/app/features/settings/go-pro/services/go-pro-bluetooth.service.ts +++ b/src/app/features/settings/go-pro/services/go-pro-bluetooth.service.ts @@ -6,11 +6,9 @@ import { ScanResult, } from '@capacitor-community/bluetooth-le'; import { Wifi } from '@capacitor-community/wifi'; -import { Plugins } from '@capacitor/core'; import { isPlatform } from '@ionic/core'; import { isEqual } from 'lodash-es'; - -const { Storage } = Plugins; +import { PreferenceManager } from '../../../../shared/preference-manager/preference-manager.service'; interface GoProWiFiCreds { wifiPASS: string; @@ -58,6 +56,12 @@ export class GoProBluetoothService { private hasInitialized = false; + readonly id = 'GoProBluetoothService'; + + private readonly preferences = this.preferenceManager.getPreferences(this.id); + + constructor(private readonly preferenceManager: PreferenceManager) {} + private async initialize() { if (this.hasInitialized) { return; @@ -122,24 +126,27 @@ export class GoProBluetoothService { private async getConnectedDeviceFromStorage(): Promise< ScanResult | undefined > { - const result = await Storage.get({ - key: this.GO_PRO_BLUETOOTH_STORAGE_KEY, - }); - if (result.value) { - return JSON.parse(result.value) as ScanResult; + const res = await this.preferences.getString( + PrefKeys.LAST_CONNECTED_BLUETOOTH_DEVICE + ); + if (res !== '') { + return JSON.parse(res) as ScanResult; } } async saveConnectedDeviceToStorage(scanResult: ScanResult) { - await Storage.set({ - key: this.GO_PRO_BLUETOOTH_STORAGE_KEY, - value: JSON.stringify(scanResult), - }); + await this.preferences.setString( + PrefKeys.LAST_CONNECTED_BLUETOOTH_DEVICE, + JSON.stringify(scanResult) + ); } // eslint-disable-next-line @typescript-eslint/no-unused-vars async removeConnectedDeviceFromStorage(scanResult: ScanResult) { - await Storage.remove({ key: this.GO_PRO_BLUETOOTH_STORAGE_KEY }); + await this.preferences.setString( + PrefKeys.LAST_CONNECTED_BLUETOOTH_DEVICE, + '' + ); } async getConnectedDevice(): Promise { @@ -241,3 +248,7 @@ export class GoProBluetoothService { await this.getGoProWiFiCreds(); } } + +const enum PrefKeys { + LAST_CONNECTED_BLUETOOTH_DEVICE = 'GO_PRO_LAST_CONNECTED_BLUETOOTH_DEVICE', +}