unify android add oath account flow

This commit is contained in:
Adam Velebil 2024-02-05 11:50:42 +01:00
parent fb04f2c8d0
commit 83716a6e8e
No known key found for this signature in database
GPG Key ID: C9B1E4A3CBBD2E10
4 changed files with 39 additions and 34 deletions

View File

@ -32,11 +32,34 @@ import '../../exception/cancellation_exception.dart';
import '../../exception/platform_exception_decoder.dart'; import '../../exception/platform_exception_decoder.dart';
import '../../oath/models.dart'; import '../../oath/models.dart';
import '../../oath/state.dart'; import '../../oath/state.dart';
import '../qr_scanner/qr_scanner_provider.dart';
final _log = Logger('android.oath.state'); final _log = Logger('android.oath.state');
const _methods = MethodChannel('android.oath.methods'); const _methods = MethodChannel('android.oath.methods');
// handles adding account on Android
final androidAddAccountFlowProvider =
Provider<void Function(BuildContext)>((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 final androidOathStateProvider = AsyncNotifierProvider.autoDispose
.family<OathStateNotifier, OathState, DevicePath>( .family<OathStateNotifier, OathState, DevicePath>(
_AndroidOathStateNotifier.new); _AndroidOathStateNotifier.new);

View File

@ -19,6 +19,7 @@ import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../android/app_methods.dart'; import '../../android/app_methods.dart';
import '../../android/oath/state.dart';
import '../../android/qr_scanner/qr_scanner_provider.dart'; import '../../android/qr_scanner/qr_scanner_provider.dart';
import '../../android/state.dart'; import '../../android/state.dart';
import '../../core/state.dart'; import '../../core/state.dart';
@ -102,22 +103,7 @@ class MainPage extends ConsumerWidget {
icon: const Icon(Icons.person_add_alt_1), icon: const Icon(Icons.person_add_alt_1),
tooltip: l10n.s_add_account, tooltip: l10n.s_add_account,
onPressed: () async { onPressed: () async {
final withContext = ref.read(withContextProvider); ref.read(androidAddAccountFlowProvider)(context);
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);
}
}, },
), ),
); );

View File

@ -18,6 +18,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../android/oath/state.dart';
import '../../android/qr_scanner/qr_scanner_provider.dart'; import '../../android/qr_scanner/qr_scanner_provider.dart';
import '../../app/message.dart'; import '../../app/message.dart';
import '../../app/models.dart'; import '../../app/models.dart';
@ -59,17 +60,7 @@ Widget oathBuildActions(
? (context) async { ? (context) async {
Navigator.of(context).popUntil((route) => route.isFirst); Navigator.of(context).popUntil((route) => route.isFirst);
if (isAndroid) { if (isAndroid) {
final withContext = ref.read(withContextProvider); ref.read(androidAddAccountFlowProvider)(context);
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);
}
} else { } else {
await showBlurDialog( await showBlurDialog(
context: context, context: context,

View File

@ -22,6 +22,7 @@ import 'package:flutter/services.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../android/oath/state.dart';
import '../../app/message.dart'; import '../../app/message.dart';
import '../../app/models.dart'; import '../../app/models.dart';
import '../../app/shortcuts.dart'; import '../../app/shortcuts.dart';
@ -164,13 +165,17 @@ class _UnlockedViewState extends ConsumerState<_UnlockedView> {
ActionChip( ActionChip(
label: Text(l10n.s_add_account), label: Text(l10n.s_add_account),
onPressed: () async { onPressed: () async {
await showBlurDialog( if (isAndroid) {
context: context, ref.read(androidAddAccountFlowProvider)(context);
builder: (context) => AddAccountDialog( } else {
widget.devicePath, await showBlurDialog(
widget.oathState, context: context,
), builder: (context) => AddAccountDialog(
); widget.devicePath,
widget.oathState,
),
);
}
}, },
avatar: const Icon(Icons.person_add_alt_1_outlined), avatar: const Icon(Icons.person_add_alt_1_outlined),
) )