Refactor code for handling uri

This commit is contained in:
Dennis Fokin 2023-08-16 12:38:29 +02:00
parent 0ff06a52e5
commit c82119d726
No known key found for this signature in database
GPG Key ID: 870B88256690D8BC
4 changed files with 58 additions and 77 deletions

View File

@ -17,15 +17,16 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import '../../android/app_methods.dart';
import '../../android/state.dart';
import '../../exception/cancellation_exception.dart';
import '../../core/state.dart';
import '../../fido/views/fido_screen.dart';
import '../../oath/models.dart';
import '../../oath/state.dart';
import '../../oath/views/add_account_page.dart';
import '../../oath/views/oath_screen.dart';
import '../../oath/views/utils.dart';
import '../../piv/views/piv_screen.dart';
import '../../widgets/custom_icons.dart';
import '../message.dart';
@ -104,10 +105,11 @@ class MainPage extends ConsumerWidget {
final scanner = ref.read(qrScannerProvider);
if (scanner != null) {
try {
final url = await scanner.scanQr();
if (url != null) {
otpauth = CredentialData.fromOtpauth(Uri.parse(url));
}
final uri = await scanner.scanQr();
final withContext = ref.read(withContextProvider);
final credentials = ref.read(credentialsProvider);
handleUri(
ref, withContext, credentials, uri, null, null, l10n);
} on CancellationException catch (_) {
// ignored - user cancelled
return;

View File

@ -24,11 +24,10 @@ import 'package:yubico_authenticator/widgets/responsive_dialog.dart';
import '../../app/models.dart';
import '../../widgets/file_drop_target.dart';
import '../keys.dart';
import '../models.dart';
import '../state.dart';
import 'add_account_page.dart';
import 'add_multi_account_page.dart';
import 'utils.dart';
class AddAccountDialog extends ConsumerStatefulWidget {
final DevicePath? devicePath;
@ -59,23 +58,9 @@ class _AddAccountDialogState extends ConsumerState<AddAccountDialog> {
if (qrScanner != null) {
final b64Image = base64Encode(fileData);
final uri = await qrScanner.scanQr(b64Image);
if (uri == null) {
if (!mounted) return;
showMessage(context, l10n.l_qr_not_found);
} else {
final otpauth = CredentialData.fromOtpauth(Uri.parse(uri));
await withContext((context) async {
await showBlurDialog(
context: context,
builder: (context) => OathAddAccountPage(
widget.devicePath,
widget.state,
credentials: credentials,
credentialData: otpauth,
),
);
});
}
handleUri(ref, withContext, credentials, uri, widget.devicePath,
widget.state, l10n);
}
},
child: Column(
@ -95,31 +80,8 @@ class _AddAccountDialogState extends ConsumerState<AddAccountDialog> {
Navigator.of(context).pop();
if (qrScanner != null) {
final uri = await qrScanner.scanQr();
List<CredentialData> creds = uri != null
? CredentialData.fromUri(Uri.parse(uri))
: [];
await withContext((context) async {
if (creds.isEmpty) {
showMessage(context, l10n.l_qr_not_found);
} else if (creds.length == 1) {
await showBlurDialog(
context: context,
builder: (context) => OathAddAccountPage(
widget.devicePath,
widget.state,
credentials: credentials,
credentialData: creds[0],
),
);
} else {
await showBlurDialog(
context: context,
builder: (context) => OathAddMultiAccountPage(
widget.devicePath, widget.state, creds,
key: migrateAccountAction),
);
}
});
handleUri(ref, withContext, credentials, uri,
widget.devicePath, widget.state, l10n);
}
},
),

View File

@ -26,14 +26,12 @@ import '../../app/state.dart';
import '../../app/views/fs_dialog.dart';
import '../../app/views/action_list.dart';
import '../../core/state.dart';
import '../keys.dart';
import '../models.dart';
import '../keys.dart' as keys;
import '../state.dart';
import 'add_account_page.dart';
import 'add_multi_account_page.dart';
import 'manage_password_dialog.dart';
import 'reset_dialog.dart';
import 'utils.dart';
Widget oathBuildActions(
BuildContext context,
@ -67,31 +65,8 @@ Widget oathBuildActions(
final qrScanner = ref.read(qrScannerProvider);
if (qrScanner != null) {
final uri = await qrScanner.scanQr();
List<CredentialData> creds = uri != null
? CredentialData.fromUri(Uri.parse(uri))
: [];
await withContext((context) async {
if (creds.isEmpty) {
showMessage(context, l10n.l_qr_not_found);
} else if (creds.length == 1) {
await showBlurDialog(
context: context,
builder: (context) => OathAddAccountPage(
devicePath,
oathState,
credentials: credentials,
credentialData: creds[0],
),
);
} else {
await showBlurDialog(
context: context,
builder: (context) => OathAddMultiAccountPage(
devicePath, oathState, creds,
key: migrateAccountAction),
);
}
});
handleUri(ref, withContext, credentials, uri,
devicePath, oathState, l10n);
}
} else {
await showBlurDialog(

View File

@ -16,8 +16,16 @@
import 'dart:math';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import '../../app/message.dart';
import '../../app/models.dart';
import '../../widgets/utf8_utils.dart';
import '../keys.dart';
import '../models.dart';
import 'add_account_page.dart';
import 'add_multi_account_page.dart';
/// Calculates the available space for issuer and account name.
///
@ -53,3 +61,37 @@ String getTextName(OathCredential credential) {
? '${credential.issuer} (${credential.name})'
: credential.name;
}
void handleUri(
WidgetRef ref,
final withContext,
final credentials,
String? uri,
DevicePath? devicePath,
OathState? state,
AppLocalizations l10n,
) async {
List<CredentialData> creds =
uri != null ? CredentialData.fromUri(Uri.parse(uri)) : [];
await withContext((context) async {
if (creds.isEmpty) {
showMessage(context, l10n.l_qr_not_found);
} else if (creds.length == 1) {
await showBlurDialog(
context: context,
builder: (context) => OathAddAccountPage(
devicePath,
state,
credentials: credentials,
credentialData: creds[0],
),
);
} else {
await showBlurDialog(
context: context,
builder: (context) => OathAddMultiAccountPage(devicePath, state, creds,
key: migrateAccountAction),
);
}
});
}