diff --git a/lib/android/init.dart b/lib/android/init.dart index 73c8bf9d..05bc6274 100644 --- a/lib/android/init.dart +++ b/lib/android/init.dart @@ -64,7 +64,6 @@ Future initialize() async { currentDeviceDataProvider.overrideWith( (ref) => ref.watch(androidDeviceDataProvider), ), - addOathAccount.overrideWith((ref) => ref.read(androidAddOathAccount)), oathStateProvider.overrideWithProvider(androidOathStateProvider.call), credentialListProvider .overrideWithProvider(androidCredentialListProvider.call), diff --git a/lib/android/oath/state.dart b/lib/android/oath/state.dart index 25aad714..2da5f472 100755 --- a/lib/android/oath/state.dart +++ b/lib/android/oath/state.dart @@ -32,37 +32,11 @@ 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 androidAddOathAccount = Provider< - void Function( - BuildContext, [ - DevicePath? devicePath, - OathState? oathState, - ])>((ref) => (context, [devicePath, oathState]) 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 58eec23a..0e1b4a9c 100755 --- a/lib/app/views/main_page.dart +++ b/lib/app/views/main_page.dart @@ -25,7 +25,7 @@ import '../../fido/views/fingerprints_screen.dart'; import '../../fido/views/passkeys_screen.dart'; import '../../fido/views/webauthn_page.dart'; import '../../management/views/management_screen.dart'; -import '../../oath/state.dart'; +import '../../oath/utils.dart'; import '../../oath/views/oath_screen.dart'; import '../../otp/views/otp_screen.dart'; import '../../piv/views/piv_screen.dart'; @@ -101,7 +101,7 @@ class MainPage extends ConsumerWidget { icon: const Icon(Icons.person_add_alt_1), tooltip: l10n.s_add_account, onPressed: () async { - ref.read(addOathAccount)(context); + addOathAccount(context, ref); }, ), ); diff --git a/lib/desktop/init.dart b/lib/desktop/init.dart index b9909762..4fdc6058 100755 --- a/lib/desktop/init.dart +++ b/lib/desktop/init.dart @@ -214,7 +214,6 @@ Future initialize(List argv) async { (ref) => ref.watch(desktopDeviceDataProvider), ), // OATH - addOathAccount.overrideWith((ref) => ref.read(desktopAddOathAccount)), oathStateProvider.overrideWithProvider(desktopOathState.call), credentialListProvider .overrideWithProvider(desktopOathCredentialListProvider.call), diff --git a/lib/desktop/oath/state.dart b/lib/desktop/oath/state.dart index 73521527..b2e9185c 100755 --- a/lib/desktop/oath/state.dart +++ b/lib/desktop/oath/state.dart @@ -24,30 +24,16 @@ import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:logging/logging.dart'; import '../../app/logging.dart'; -import '../../app/message.dart'; import '../../app/models.dart'; import '../../app/state.dart'; import '../../app/views/user_interaction.dart'; import '../../oath/models.dart'; import '../../oath/state.dart'; -import '../../oath/views/add_account_dialog.dart'; import '../rpc.dart'; import '../state.dart'; final _log = Logger('desktop.oath.state'); -final desktopAddOathAccount = Provider< - void Function( - BuildContext, [ - DevicePath? devicePath, - OathState? oathState, - ])>((ref) => (context, [devicePath, oathState]) async { - await showBlurDialog( - context: context, - builder: (context) => AddAccountDialog(devicePath, oathState), - ); - }); - final _sessionProvider = Provider.autoDispose.family( (ref, devicePath) => RpcNodeSession( diff --git a/lib/oath/state.dart b/lib/oath/state.dart index 6dbf6046..9a5c0684 100755 --- a/lib/oath/state.dart +++ b/lib/oath/state.dart @@ -26,13 +26,6 @@ import '../app/state.dart'; import '../core/state.dart'; import 'models.dart'; -final addOathAccount = Provider< - void Function( - BuildContext, [ - DevicePath? devicePath, - OathState? oathState, - ])>((ref) => throw UnimplementedError()); - final searchProvider = StateNotifierProvider((ref) => SearchNotifier()); diff --git a/lib/oath/utils.dart b/lib/oath/utils.dart new file mode 100644 index 00000000..fbd1e1b4 --- /dev/null +++ b/lib/oath/utils.dart @@ -0,0 +1,55 @@ +/* + * Copyright (C) 2024 Yubico. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import 'package:flutter/material.dart'; +import 'package:flutter_gen/gen_l10n/app_localizations.dart'; +import 'package:flutter_riverpod/flutter_riverpod.dart'; + +import '../android/qr_scanner/qr_scanner_provider.dart'; +import '../app/message.dart'; +import '../app/models.dart'; +import '../app/state.dart'; +import '../core/state.dart'; +import '../exception/cancellation_exception.dart'; +import 'models.dart'; +import 'views/add_account_dialog.dart'; + +void addOathAccount(BuildContext context, WidgetRef ref, + [DevicePath? devicePath, OathState? oathState]) async { + if (isAndroid) { + 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); + } + } else { + await showBlurDialog( + context: context, + builder: (context) => AddAccountDialog(devicePath, oathState), + ); + } +} diff --git a/lib/oath/views/key_actions.dart b/lib/oath/views/key_actions.dart index 7b248bda..eaa76845 100755 --- a/lib/oath/views/key_actions.dart +++ b/lib/oath/views/key_actions.dart @@ -26,7 +26,7 @@ import '../features.dart' as features; import '../icon_provider/icon_pack_dialog.dart'; import '../keys.dart' as keys; import '../models.dart'; -import '../state.dart'; +import '../utils.dart'; import 'manage_password_dialog.dart'; Widget oathBuildActions( @@ -56,7 +56,7 @@ Widget oathBuildActions( onTap: used != null && (capacity == null || capacity > used) ? (context) async { Navigator.of(context).popUntil((route) => route.isFirst); - ref.read(addOathAccount)(context, devicePath, oathState); + addOathAccount(context, ref, devicePath, oathState); } : null), ]), diff --git a/lib/oath/views/oath_screen.dart b/lib/oath/views/oath_screen.dart index 776f90db..21cb3cf1 100755 --- a/lib/oath/views/oath_screen.dart +++ b/lib/oath/views/oath_screen.dart @@ -40,6 +40,7 @@ import '../features.dart' as features; import '../keys.dart' as keys; import '../models.dart'; import '../state.dart'; +import '../utils.dart'; import 'account_dialog.dart'; import 'account_helper.dart'; import 'account_list.dart'; @@ -163,8 +164,9 @@ class _UnlockedViewState extends ConsumerState<_UnlockedView> { ActionChip( label: Text(l10n.s_add_account), onPressed: () async { - ref.read(addOathAccount)( + addOathAccount( context, + ref, widget.devicePath, widget.oathState, );