From 83716a6e8ecb1cf639bcbe9e75e497a435552fe9 Mon Sep 17 00:00:00 2001 From: Adam Velebil Date: Mon, 5 Feb 2024 11:50:42 +0100 Subject: [PATCH] unify android add oath account flow --- lib/android/oath/state.dart | 23 +++++++++++++++++++++++ lib/app/views/main_page.dart | 18 ++---------------- lib/oath/views/key_actions.dart | 13 ++----------- lib/oath/views/oath_screen.dart | 19 ++++++++++++------- 4 files changed, 39 insertions(+), 34 deletions(-) diff --git a/lib/android/oath/state.dart b/lib/android/oath/state.dart index 2da5f472..f8893c02 100755 --- a/lib/android/oath/state.dart +++ b/lib/android/oath/state.dart @@ -32,11 +32,34 @@ import '../../exception/cancellation_exception.dart'; import '../../exception/platform_exception_decoder.dart'; import '../../oath/models.dart'; import '../../oath/state.dart'; +import '../qr_scanner/qr_scanner_provider.dart'; final _log = Logger('android.oath.state'); const _methods = MethodChannel('android.oath.methods'); +// handles adding account on Android +final androidAddAccountFlowProvider = + Provider((ref) => (context) async { + final l10n = AppLocalizations.of(context)!; + final withContext = ref.read(withContextProvider); + final qrScanner = ref.read(qrScannerProvider); + if (qrScanner != null) { + try { + final qrData = await qrScanner.scanQr(); + await AndroidQrScanner.handleScannedData( + qrData, withContext, qrScanner, l10n); + } on CancellationException catch (_) { + //ignored - user cancelled + return; + } + } else { + // no QR scanner - enter data manually + await AndroidQrScanner.showAccountManualEntryDialog( + withContext, l10n); + } + }); + final androidOathStateProvider = AsyncNotifierProvider.autoDispose .family( _AndroidOathStateNotifier.new); diff --git a/lib/app/views/main_page.dart b/lib/app/views/main_page.dart index 40c22b7e..c366fbaf 100755 --- a/lib/app/views/main_page.dart +++ b/lib/app/views/main_page.dart @@ -19,6 +19,7 @@ import 'package:flutter_gen/gen_l10n/app_localizations.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import '../../android/app_methods.dart'; +import '../../android/oath/state.dart'; import '../../android/qr_scanner/qr_scanner_provider.dart'; import '../../android/state.dart'; import '../../core/state.dart'; @@ -102,22 +103,7 @@ class MainPage extends ConsumerWidget { icon: const Icon(Icons.person_add_alt_1), tooltip: l10n.s_add_account, onPressed: () async { - final withContext = ref.read(withContextProvider); - final qrScanner = ref.read(qrScannerProvider); - if (qrScanner != null) { - try { - final qrData = await qrScanner.scanQr(); - await AndroidQrScanner.handleScannedData( - qrData, withContext, qrScanner, l10n); - } on CancellationException catch (_) { - // ignored - user cancelled - return; - } - } else { - // no QR scanner - enter data manually - await AndroidQrScanner.showAccountManualEntryDialog( - withContext, l10n); - } + ref.read(androidAddAccountFlowProvider)(context); }, ), ); diff --git a/lib/oath/views/key_actions.dart b/lib/oath/views/key_actions.dart index dc48caa4..2d2a8517 100755 --- a/lib/oath/views/key_actions.dart +++ b/lib/oath/views/key_actions.dart @@ -18,6 +18,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; +import '../../android/oath/state.dart'; import '../../android/qr_scanner/qr_scanner_provider.dart'; import '../../app/message.dart'; import '../../app/models.dart'; @@ -59,17 +60,7 @@ Widget oathBuildActions( ? (context) async { Navigator.of(context).popUntil((route) => route.isFirst); if (isAndroid) { - final withContext = ref.read(withContextProvider); - final qrScanner = ref.read(qrScannerProvider); - if (qrScanner != null) { - final qrData = await qrScanner.scanQr(); - await AndroidQrScanner.handleScannedData( - qrData, withContext, qrScanner, l10n); - } else { - // no QR scanner - enter data manually - await AndroidQrScanner.showAccountManualEntryDialog( - withContext, l10n); - } + ref.read(androidAddAccountFlowProvider)(context); } else { await showBlurDialog( context: context, diff --git a/lib/oath/views/oath_screen.dart b/lib/oath/views/oath_screen.dart index 4f19cc94..b76ad9d0 100755 --- a/lib/oath/views/oath_screen.dart +++ b/lib/oath/views/oath_screen.dart @@ -22,6 +22,7 @@ import 'package:flutter/services.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; +import '../../android/oath/state.dart'; import '../../app/message.dart'; import '../../app/models.dart'; import '../../app/shortcuts.dart'; @@ -164,13 +165,17 @@ class _UnlockedViewState extends ConsumerState<_UnlockedView> { ActionChip( label: Text(l10n.s_add_account), onPressed: () async { - await showBlurDialog( - context: context, - builder: (context) => AddAccountDialog( - widget.devicePath, - widget.oathState, - ), - ); + if (isAndroid) { + ref.read(androidAddAccountFlowProvider)(context); + } else { + await showBlurDialog( + context: context, + builder: (context) => AddAccountDialog( + widget.devicePath, + widget.oathState, + ), + ); + } }, avatar: const Icon(Icons.person_add_alt_1_outlined), )