Minor cleanups.

This commit is contained in:
Dain Nilsson 2023-08-18 09:43:13 +02:00
parent 2b011f2ea3
commit 916d9e24db
No known key found for this signature in database
GPG Key ID: F04367096FBA95E8
6 changed files with 47 additions and 77 deletions

View File

@ -20,11 +20,7 @@ import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import '../../app/message.dart';
import '../../oath/keys.dart';
import '../../oath/models.dart';
import '../../oath/views/add_account_page.dart';
import '../../oath/views/add_multi_account_page.dart';
import '../../oath/views/utils.dart';
const _appLinkMethodsChannel = MethodChannel('app.link.methods');
@ -33,35 +29,11 @@ void setupOtpAuthLinkHandler(BuildContext context) {
final args = jsonDecode(call.arguments);
switch (call.method) {
case 'handleOtpAuthLink':
{
final l10n = AppLocalizations.of(context)!;
Navigator.popUntil(context, ModalRoute.withName('/'));
var uri = args['link'];
List<CredentialData> creds =
uri != null ? CredentialData.fromUri(Uri.parse(uri)) : [];
if (creds.isEmpty) {
showMessage(context, l10n.l_qr_not_found);
} else if (creds.length == 1) {
await showBlurDialog(
context: context,
builder: (context) => OathAddAccountPage(
null,
null,
credentials: null,
credentialData: creds[0],
),
);
} else {
await showBlurDialog(
context: context,
builder: (context) => OathAddMultiAccountPage(null, null, creds,
key: migrateAccountAction),
);
}
break;
}
Navigator.popUntil(context, ModalRoute.withName('/'));
final l10n = AppLocalizations.of(context)!;
final uri = args['link'];
await handleUri(context, null, uri, null, null, l10n);
break;
default:
throw PlatformException(
code: 'NotImplemented',

View File

@ -102,9 +102,10 @@ class MainPage extends ConsumerWidget {
if (scanner != null) {
try {
final uri = await scanner.scanQr();
final withContext = ref.read(withContextProvider);
final credentials = ref.read(credentialsProvider);
handleUri(withContext, credentials, uri, null, null, l10n);
final withContext = ref.read(withContextProvider);
await withContext((context) =>
handleUri(context, credentials, uri, null, null, l10n));
} on CancellationException catch (_) {
// ignored - user cancelled
return;

View File

@ -58,9 +58,8 @@ class _AddAccountDialogState extends ConsumerState<AddAccountDialog> {
if (qrScanner != null) {
final b64Image = base64Encode(fileData);
final uri = await qrScanner.scanQr(b64Image);
handleUri(withContext, credentials, uri, widget.devicePath,
widget.state, l10n);
await withContext((context) => handleUri(context, credentials,
uri, widget.devicePath, widget.state, l10n));
}
},
child: Column(
@ -80,8 +79,13 @@ class _AddAccountDialogState extends ConsumerState<AddAccountDialog> {
Navigator.of(context).pop();
if (qrScanner != null) {
final uri = await qrScanner.scanQr();
handleUri(withContext, credentials, uri,
widget.devicePath, widget.state, l10n);
await withContext((context) => handleUri(
context,
credentials,
uri,
widget.devicePath,
widget.state,
l10n));
}
},
),

View File

@ -172,14 +172,7 @@ class _OathAddMultiAccountPageState
));
}
bool isTouchSupported() {
bool touch = true;
if (!(widget.state?.version.isAtLeast(4, 2) ?? true)) {
// Touch not supported
touch = false;
}
return touch;
}
bool isTouchSupported() => widget.state?.version.isAtLeast(4, 2) ?? true;
void checkForDuplicates() {
for (final item in _credStates.entries) {
@ -204,9 +197,10 @@ class _OathAddMultiAccountPageState
}
bool isValid() {
final credsToAdd = _credStates.values.where((element) => element.$1).length;
if (widget.state != null) {
int? capacity = widget.state!.version.isAtLeast(4) ? 32 : null;
final credsToAdd =
_credStates.values.where((element) => element.$1).length;
final capacity = widget.state!.version.isAtLeast(4) ? 32 : null;
return (credsToAdd > 0) &&
(capacity == null || (_numCreds! + credsToAdd <= capacity));
} else {

View File

@ -65,8 +65,8 @@ Widget oathBuildActions(
final qrScanner = ref.read(qrScannerProvider);
if (qrScanner != null) {
final uri = await qrScanner.scanQr();
handleUri(withContext, credentials, uri, devicePath,
oathState, l10n);
await withContext((context) => handleUri(context,
credentials, uri, devicePath, oathState, l10n));
}
} else {
await showBlurDialog(

View File

@ -16,6 +16,7 @@
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import '../../app/message.dart';
@ -61,9 +62,9 @@ String getTextName(OathCredential credential) {
: credential.name;
}
void handleUri(
final withContext,
final credentials,
Future<void> handleUri(
BuildContext context,
List<OathCredential>? credentials,
String? uri,
DevicePath? devicePath,
OathState? state,
@ -71,25 +72,23 @@ void handleUri(
) 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),
);
}
});
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),
);
}
}