From a1e3749ff8842d5f9b1ed0d7c4a222bd8bf6cd91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20Baz=C3=A1n?= Date: Mon, 8 Jan 2018 12:51:45 -0300 Subject: [PATCH] [V4] Fix: touch id provider --- .../wallet-settings/wallet-settings.ts | 4 +- src/providers/app/app.ts | 3 - src/providers/touchid/touchid.ts | 84 ++++++++++++------- 3 files changed, 55 insertions(+), 36 deletions(-) diff --git a/src/pages/settings/wallet-settings/wallet-settings.ts b/src/pages/settings/wallet-settings/wallet-settings.ts index d318aacf7..a7ec12235 100644 --- a/src/pages/settings/wallet-settings/wallet-settings.ts +++ b/src/pages/settings/wallet-settings/wallet-settings.ts @@ -55,7 +55,9 @@ export class WalletSettingsPage { this.needsBackup = this.wallet.needsBackup; this.hiddenBalance = this.wallet.balanceHidden; this.encryptEnabled = this.walletProvider.isEncrypted(this.wallet); - this.touchIdAvailable = this.touchIdProvider.isAvailable(); + this.touchIdProvider.isAvailable().then((isAvailable: boolean) => { + this.touchIdAvailable = isAvailable; + }); this.config = this.configProvider.get(); this.touchIdEnabled = this.config.touchIdFor ? this.config.touchIdFor[this.wallet.credentials.walletId] : null; if (this.wallet.credentials && !this.wallet.credentials.mnemonicEncrypted && !this.wallet.credentials.mnemonic) diff --git a/src/providers/app/app.ts b/src/providers/app/app.ts index e496a9cc2..6e89cc2ae 100644 --- a/src/providers/app/app.ts +++ b/src/providers/app/app.ts @@ -4,7 +4,6 @@ import { Logger } from '@nsalaun/ng-logger'; import { LanguageProvider } from '../../providers/language/language'; import { ConfigProvider } from '../../providers/config/config'; -import { TouchIdProvider } from '../../providers/touchid/touchid'; import { PersistenceProvider } from '../../providers/persistence/persistence'; /* TODO: implement interface propertly @@ -52,7 +51,6 @@ export class AppProvider { private logger: Logger, private language: LanguageProvider, private config: ConfigProvider, - private touchid: TouchIdProvider, private persistence: PersistenceProvider ) { this.logger.info('AppProvider initialized.'); @@ -63,7 +61,6 @@ export class AppProvider { this.persistence.load(); this.config.load().then(() => { this.language.load(); - this.touchid.init(); this.getServicesInfo().subscribe((infoServices) => { this.servicesInfo = infoServices; this.getInfo().subscribe((infoApp) => { diff --git a/src/providers/touchid/touchid.ts b/src/providers/touchid/touchid.ts index d8b17dc73..0f1e26d10 100644 --- a/src/providers/touchid/touchid.ts +++ b/src/providers/touchid/touchid.ts @@ -9,8 +9,6 @@ import { AndroidFingerprintAuth } from '@ionic-native/android-fingerprint-auth'; @Injectable() export class TouchIdProvider { - private _isAvailable: boolean = false; - constructor( private touchId: TouchID, private androidFingerprintAuth: AndroidFingerprintAuth, @@ -19,28 +17,51 @@ export class TouchIdProvider { private logger: Logger ) { } - public init(): void { - if (this.platform.isCordova) { - if (this.platform.isAndroid) this.checkAndroid(); - if (this.platform.isIOS) this.checkIOS(); - } + public isAvailable(): Promise { + return new Promise((resolve, reject) => { + if (this.platform.isAndroid) { + this.checkAndroid().then((isAvailable) => { + return resolve(isAvailable); + }); + } + else if (this.platform.isIOS) { + this.checkIOS().then((isAvailable) => { + return resolve(isAvailable); + }); + } + else { + return resolve(false); + } + }); } - private checkIOS(): void { - this.touchId.isAvailable() - .then( - res => this._isAvailable = true, - err => this.logger.debug("Fingerprint is not available") - ); + private checkIOS(): Promise { + return new Promise((resolve, reject) => { + this.touchId.isAvailable() + .then( + res => { + return resolve(true); + }, + err => { + console.log("Fingerprint is not available"); + return resolve(false); + } + ); + }); } - private checkAndroid(): void { - this.androidFingerprintAuth.isAvailable() - .then( - res => { - if (res.isAvailable) this._isAvailable = true - else this.logger.debug("Fingerprint is not available") - }); + private checkAndroid() { + return new Promise((resolve, reject) => { + this.androidFingerprintAuth.isAvailable() + .then( + res => { + if (res.isAvailable) return resolve(true); + else { + console.log("Fingerprint is not available"); + return resolve(false); + } + }); + }); } private verifyIOSFingerprint(): Promise { @@ -76,13 +97,8 @@ export class TouchIdProvider { }); } - public isAvailable(): boolean { - return this._isAvailable; - } - public check(): Promise { return new Promise((resolve, reject) => { - if (!this.isAvailable()) reject(); if (this.platform.isIOS) { this.verifyIOSFingerprint() .then(() => { @@ -112,14 +128,18 @@ export class TouchIdProvider { public checkWallet(wallet: any): Promise { return new Promise((resolve, reject) => { - if (!this.isAvailable()) return resolve(); - if (this.isNeeded(wallet)) { - this.check().then(() => { + this.isAvailable().then((isAvailable: boolean) => { + if (!isAvailable) return resolve(); + if (this.isNeeded(wallet)) { + this.check().then(() => { + return resolve(); + }).catch(() => { + return reject(); + }); + } else { return resolve(); - }).catch(() => { - return reject(); - }); - }; + } + }) }); } }