Skip to content

Commit

Permalink
refactor(go-pro-bluetooth.service.ts):
Browse files Browse the repository at this point in the history
use preferenceManager instead of Plugins.Storage
  • Loading branch information
sultanmyrza committed Feb 12, 2022
1 parent 1688367 commit 4e5d9a3
Showing 1 changed file with 24 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<ScanResult | undefined> {
Expand Down Expand Up @@ -241,3 +248,7 @@ export class GoProBluetoothService {
await this.getGoProWiFiCreds();
}
}

const enum PrefKeys {
LAST_CONNECTED_BLUETOOTH_DEVICE = 'GO_PRO_LAST_CONNECTED_BLUETOOTH_DEVICE',
}

0 comments on commit 4e5d9a3

Please sign in to comment.