simplify work with hasCamera

This commit is contained in:
Adam Velebil 2022-11-08 14:06:27 +01:00
parent 5ad830f191
commit 80fdb03b34
No known key found for this signature in database
GPG Key ID: AC6D6B9D715FC084
5 changed files with 15 additions and 21 deletions

View File

@ -67,11 +67,11 @@ Future<Widget> initialize() async {
currentAppProvider.overrideWithProvider(androidSubPageProvider), currentAppProvider.overrideWithProvider(androidSubPageProvider),
managementStateProvider.overrideWithProvider(androidManagementState), managementStateProvider.overrideWithProvider(androidManagementState),
currentDeviceProvider.overrideWithProvider(androidCurrentDeviceProvider), currentDeviceProvider.overrideWithProvider(androidCurrentDeviceProvider),
qrScannerProvider.overrideWithProvider(androidQrScannerProvider), qrScannerProvider.overrideWithProvider(
androidQrScannerProvider.call(await getHasCamera())),
windowStateProvider.overrideWithProvider(androidWindowStateProvider), windowStateProvider.overrideWithProvider(androidWindowStateProvider),
clipboardProvider.overrideWithProvider(androidClipboardProvider), clipboardProvider.overrideWithProvider(androidClipboardProvider),
androidSdkVersionProvider.overrideWithValue(await getAndroidSdkVersion()), androidSdkVersionProvider.overrideWithValue(await getAndroidSdkVersion()),
androidHasCameraProvider.overrideWithValue(await getHasCamera()),
supportedThemesProvider supportedThemesProvider
.overrideWithProvider(androidSupportedThemesProvider) .overrideWithProvider(androidSupportedThemesProvider)
], ],

View File

@ -46,6 +46,6 @@ class AndroidQrScanner implements QrScanner {
} }
} }
final androidQrScannerProvider = Provider<QrScanner?>( final androidQrScannerProvider = Provider.family<QrScanner?, bool>(
(ref) => AndroidQrScanner(ref.watch(withContextProvider)), (ref, hasCamera) =>
); hasCamera ? AndroidQrScanner(ref.watch(withContextProvider)) : null);

View File

@ -64,8 +64,6 @@ class _AndroidClipboard extends AppClipboard {
final androidSdkVersionProvider = Provider<int>((ref) => -1); final androidSdkVersionProvider = Provider<int>((ref) => -1);
final androidHasCameraProvider = Provider<bool>((ref) => false);
final androidSupportedThemesProvider = StateProvider<List<ThemeMode>>((ref) { final androidSupportedThemesProvider = StateProvider<List<ThemeMode>>((ref) {
if (ref.read(androidSdkVersionProvider) < 29) { if (ref.read(androidSdkVersionProvider) < 29) {
// the user can select from light or dark theme of the app // the user can select from light or dark theme of the app

View File

@ -16,7 +16,6 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:yubico_authenticator/android/state.dart';
import 'package:yubico_authenticator/cancellation_exception.dart'; import 'package:yubico_authenticator/cancellation_exception.dart';
import 'package:yubico_authenticator/core/state.dart'; import 'package:yubico_authenticator/core/state.dart';
@ -75,18 +74,16 @@ class MainPage extends ConsumerWidget {
tooltip: 'Add account', tooltip: 'Add account',
onPressed: () async { onPressed: () async {
CredentialData? otpauth; CredentialData? otpauth;
if (ref.read(androidHasCameraProvider)) { final scanner = ref.read(qrScannerProvider);
final scanner = ref.read(qrScannerProvider); if (scanner != null) {
if (scanner != null) { try {
try { final url = await scanner.scanQr();
final url = await scanner.scanQr(); if (url != null) {
if (url != null) { otpauth = CredentialData.fromUri(Uri.parse(url));
otpauth = CredentialData.fromUri(Uri.parse(url));
}
} on CancellationException catch (_) {
// ignored - user cancelled
return;
} }
} on CancellationException catch (_) {
// ignored - user cancelled
return;
} }
} }
await showBlurDialog( await showBlurDialog(

View File

@ -20,7 +20,6 @@ import 'package:flutter/material.dart';
import 'package:flutter/services.dart'; 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 'package:yubico_authenticator/android/state.dart';
import 'package:yubico_authenticator/widgets/delayed_visibility.dart'; import 'package:yubico_authenticator/widgets/delayed_visibility.dart';
import '../../app/message.dart'; import '../../app/message.dart';
@ -238,7 +237,7 @@ class _UnlockedViewState extends ConsumerState<_UnlockedView> {
action: capacity == null || capacity > used action: capacity == null || capacity > used
? () async { ? () async {
CredentialData? otpauth; CredentialData? otpauth;
if (Platform.isAndroid && ref.read(androidHasCameraProvider)) { if (Platform.isAndroid) {
final scanner = ref.read(qrScannerProvider); final scanner = ref.read(qrScannerProvider);
if (scanner != null) { if (scanner != null) {
final url = await scanner.scanQr(); final url = await scanner.scanQr();