From 6d62ffb7303c25d77731ab81ba6fb1d4ddcd2397 Mon Sep 17 00:00:00 2001 From: Dain Nilsson Date: Fri, 11 Feb 2022 11:44:08 +0100 Subject: [PATCH] Change account trigger behavior. This is closer to how the existing app behaves. --- lib/oath/views/account_view.dart | 34 +++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/lib/oath/views/account_view.dart b/lib/oath/views/account_view.dart index a2506f63..a2f21e00 100755 --- a/lib/oath/views/account_view.dart +++ b/lib/oath/views/account_view.dart @@ -57,15 +57,16 @@ class AccountView extends ConsumerWidget { } } - List _buildPopupMenu(BuildContext context, WidgetRef ref) => [ + List _buildPopupMenu( + BuildContext context, WidgetRef ref, bool trigger) => + [ PopupMenuItem( child: const ListTile( leading: Icon(Icons.copy), title: Text('Copy to clipboard'), ), - enabled: code != null, onTap: () { - _copyToClipboard(context); + _copyToClipboard(context, ref, trigger); }, ), PopupMenuItem( @@ -114,8 +115,15 @@ class AccountView extends ConsumerWidget { ), ]; - _copyToClipboard(BuildContext context) { - Clipboard.setData(ClipboardData(text: code!.value)); + _copyToClipboard(BuildContext context, WidgetRef ref, bool trigger) async { + String value; + if (trigger) { + final updated = await _calculate(context, ref); + value = updated.value; + } else { + value = code!.value; + } + await Clipboard.setData(ClipboardData(text: value)); ScaffoldMessenger.of(context).showSnackBar( const SnackBar( content: Text('Code copied'), @@ -124,7 +132,7 @@ class AccountView extends ConsumerWidget { ); } - _calculate(BuildContext context, WidgetRef ref) async { + Future _calculate(BuildContext context, WidgetRef ref) async { VoidCallback? close; if (credential.touchRequired) { final sbc = ScaffoldMessenger.of(context).showSnackBar( @@ -139,7 +147,7 @@ class AccountView extends ConsumerWidget { close = sbc.close; } try { - await ref + return await ref .read(credentialListProvider(deviceData.node.path).notifier) .calculate(credential); } finally { @@ -150,10 +158,10 @@ class AccountView extends ConsumerWidget { @override Widget build(BuildContext context, WidgetRef ref) { final code = this.code; - final expired = ref.watch(_expireProvider(code?.validTo ?? 0)); final label = credential.issuer != null ? '${credential.issuer} (${credential.name})' : credential.name; + final expired = ref.watch(_expireProvider(code?.validTo ?? 0)); final trigger = code == null || expired && (credential.touchRequired || credential.oathType == OathType.hotp); @@ -161,10 +169,11 @@ class AccountView extends ConsumerWidget { return ListTile( focusNode: focusNode, onTap: () { - if (trigger) { - _calculate(context, ref); + final focus = focusNode; + if (focus != null && focus.hasFocus == false) { + focus.requestFocus(); } else { - _copyToClipboard(context); + _copyToClipboard(context, ref, trigger); } }, leading: CircleAvatar( @@ -208,7 +217,8 @@ class AccountView extends ConsumerWidget { const Spacer(), PopupMenuButton( child: Icon(Icons.adaptive.more), - itemBuilder: (context) => _buildPopupMenu(context, ref), + itemBuilder: (context) => + _buildPopupMenu(context, ref, trigger), ), ], ),