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 '../../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<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
.family<OathStateNotifier, OathState, DevicePath>(
_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 '../../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);
},
),
);

View File

@ -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,

View File

@ -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,6 +165,9 @@ class _UnlockedViewState extends ConsumerState<_UnlockedView> {
ActionChip(
label: Text(l10n.s_add_account),
onPressed: () async {
if (isAndroid) {
ref.read(androidAddAccountFlowProvider)(context);
} else {
await showBlurDialog(
context: context,
builder: (context) => AddAccountDialog(
@ -171,6 +175,7 @@ class _UnlockedViewState extends ConsumerState<_UnlockedView> {
widget.oathState,
),
);
}
},
avatar: const Icon(Icons.person_add_alt_1_outlined),
)