minor nfc views refactor

This commit is contained in:
Adam Velebil 2024-09-06 08:37:34 +02:00
parent 2de217ddf0
commit 9f183bc745
No known key found for this signature in database
GPG Key ID: C9B1E4A3CBBD2E10
12 changed files with 94 additions and 104 deletions

View File

@ -360,6 +360,7 @@ class _OathMethodChannelNotifier extends MethodChannelNotifier {
'callArgs': {'password': password, 'remember': remember}, 'callArgs': {'password': password, 'remember': remember},
'operationSuccess': l10n.s_nfc_unlock_success, 'operationSuccess': l10n.s_nfc_unlock_success,
'operationFailure': l10n.s_nfc_unlock_failure, 'operationFailure': l10n.s_nfc_unlock_failure,
'showSuccess': false,
}); });
Future<dynamic> setPassword(String? current, String password) async => Future<dynamic> setPassword(String? current, String password) async =>

View File

@ -39,9 +39,11 @@ final androidDialogProvider =
NotifierProvider<_DialogProvider, int>(_DialogProvider.new); NotifierProvider<_DialogProvider, int>(_DialogProvider.new);
class _DialogProvider extends Notifier<int> { class _DialogProvider extends Notifier<int> {
Timer? processingTimer; Timer? processingViewTimeout;
bool explicitAction = false; bool explicitAction = false;
late final l10n = ref.read(l10nProvider);
@override @override
int build() { int build() {
final l10n = ref.read(l10nProvider); final l10n = ref.read(l10nProvider);
@ -53,6 +55,7 @@ class _DialogProvider extends Notifier<int> {
if (!explicitAction) { if (!explicitAction) {
// setup properties for ad-hoc action // setup properties for ad-hoc action
viewNotifier.setDialogProperties( viewNotifier.setDialogProperties(
operationSuccess: l10n.s_nfc_scan_success,
operationFailure: l10n.l_nfc_read_key_failure, operationFailure: l10n.l_nfc_read_key_failure,
showSuccess: true, showSuccess: true,
showCloseButton: false); showCloseButton: false);
@ -62,45 +65,37 @@ class _DialogProvider extends Notifier<int> {
switch (current) { switch (current) {
case NfcActivity.processingStarted: case NfcActivity.processingStarted:
viewNotifier.setDialogProperties(showCloseButton: false); final timeout = explicitAction ? 300 : 500;
processingTimer?.cancel(); processingViewTimeout?.cancel();
final timeout = explicitAction ? 300 : 200; processingViewTimeout = Timer(Duration(milliseconds: timeout), () {
notifier.sendCommand(showAccessingKeyView());
processingTimer = Timer(Duration(milliseconds: timeout), () {
if (!explicitAction) {
// show the widget
notifier.sendCommand(showNfcView(NfcContentWidget(
title: l10n.s_nfc_accessing_yubikey,
icon: const NfcIconProgressBar(true),
)));
} else {
// the processing view will only be shown if the timer is still active
notifier.sendCommand(updateNfcView(NfcContentWidget(
title: l10n.s_nfc_accessing_yubikey,
icon: const NfcIconProgressBar(true),
)));
}
}); });
break; break;
case NfcActivity.processingFinished: case NfcActivity.processingFinished:
processingTimer?.cancel(); processingViewTimeout?.cancel();
final showSuccess = properties.showSuccess ?? false; final showSuccess = properties.showSuccess ?? false;
allowMessages = !showSuccess; allowMessages = !showSuccess;
if (showSuccess) { if (showSuccess) {
notifier.sendCommand(autoClose( notifier.sendCommand(autoClose(
title: properties.operationSuccess, title: properties.operationSuccess,
subtitle: l10n.s_nfc_remove_key, subtitle: explicitAction ? l10n.s_nfc_remove_key : null,
icon: const NfcIconSuccess(), icon: const NfcIconSuccess(),
)); showIfHidden: false));
// hide
} }
// hide notifier.sendCommand(hideNfcView(Duration(
notifier.sendCommand(hideNfcView(explicitAction ? 5000 : 400)); milliseconds: !showSuccess
? 0
: explicitAction
? 5000
: 400)));
explicitAction = false; // next action might not be explicit explicitAction = false; // next action might not be explicit
break; break;
case NfcActivity.processingInterrupted: case NfcActivity.processingInterrupted:
processingTimer?.cancel(); processingViewTimeout?.cancel();
viewNotifier.setDialogProperties(showCloseButton: true); viewNotifier.setDialogProperties(showCloseButton: true);
notifier.sendCommand(updateNfcView(NfcContentWidget( notifier.sendCommand(setNfcView(NfcContentWidget(
title: properties.operationFailure, title: properties.operationFailure,
subtitle: l10n.s_nfc_scan_again, subtitle: l10n.s_nfc_scan_again,
icon: const NfcIconFailure(), icon: const NfcIconFailure(),
@ -119,14 +114,7 @@ class _DialogProvider extends Notifier<int> {
switch (call.method) { switch (call.method) {
case 'show': case 'show':
explicitAction = true; explicitAction = true;
notifier.sendCommand(showScanKeyView());
// we want to show the close button
viewNotifier.setDialogProperties(showCloseButton: true);
notifier.sendCommand(showNfcView(NfcContentWidget(
subtitle: l10n.s_nfc_scan_yubikey,
icon: const NfcIconProgressBar(false),
)));
break; break;
case 'close': case 'close':
@ -143,6 +131,26 @@ class _DialogProvider extends Notifier<int> {
return 0; return 0;
} }
NfcEventCommand showScanKeyView() {
ref
.read(nfcViewNotifier.notifier)
.setDialogProperties(showCloseButton: true);
return setNfcView(NfcContentWidget(
subtitle: l10n.s_nfc_scan_yubikey,
icon: const NfcIconProgressBar(false),
));
}
NfcEventCommand showAccessingKeyView() {
ref
.read(nfcViewNotifier.notifier)
.setDialogProperties(showCloseButton: false);
return setNfcView(NfcContentWidget(
title: l10n.s_nfc_accessing_yubikey,
icon: const NfcIconProgressBar(true),
));
}
void closeDialog() { void closeDialog() {
ref.read(nfcEventCommandNotifier.notifier).sendCommand(hideNfcView()); ref.read(nfcEventCommandNotifier.notifier).sendCommand(hideNfcView());
} }
@ -168,28 +176,3 @@ class _DialogProvider extends Notifier<int> {
await completer.future; await completer.future;
} }
} }
class MethodChannelHelper {
final ProviderRef _ref;
final MethodChannel _channel;
const MethodChannelHelper(this._ref, this._channel);
Future<dynamic> invoke(String method,
{String? operationSuccess,
String? operationFailure,
bool? showSuccess,
bool? showCloseButton,
Map<String, dynamic> arguments = const {}}) async {
final notifier = _ref.read(nfcViewNotifier.notifier);
notifier.setDialogProperties(
operationSuccess: operationSuccess,
operationFailure: operationFailure,
showSuccess: showSuccess,
showCloseButton: showCloseButton);
final result = await _channel.invokeMethod(method, arguments);
await _ref.read(androidDialogProvider.notifier).waitForDialogClosed();
return result;
}
}

View File

@ -23,26 +23,21 @@ class NfcEvent {
const NfcEvent(); const NfcEvent();
} }
class NfcShowViewEvent extends NfcEvent {
final Widget child;
const NfcShowViewEvent({required this.child});
}
class NfcHideViewEvent extends NfcEvent { class NfcHideViewEvent extends NfcEvent {
final int timeoutMs; final Duration hideAfter;
const NfcHideViewEvent({required this.timeoutMs}); const NfcHideViewEvent({required this.hideAfter});
} }
class NfcCancelEvent extends NfcEvent { class NfcCancelEvent extends NfcEvent {
const NfcCancelEvent(); const NfcCancelEvent();
} }
class NfcUpdateViewEvent extends NfcEvent { class NfcSetViewEvent extends NfcEvent {
final Widget child; final Widget child;
final bool showIfHidden;
const NfcUpdateViewEvent({required this.child}); const NfcSetViewEvent({required this.child, this.showIfHidden = true});
} }
@freezed @freezed
@ -63,11 +58,9 @@ class NfcEventCommand with _$NfcEventCommand {
}) = _NfcEventCommand; }) = _NfcEventCommand;
} }
NfcEventCommand hideNfcView([int timeoutMs = 0]) => NfcEventCommand hideNfcView([Duration hideAfter = Duration.zero]) =>
NfcEventCommand(event: NfcHideViewEvent(timeoutMs: timeoutMs)); NfcEventCommand(event: NfcHideViewEvent(hideAfter: hideAfter));
NfcEventCommand updateNfcView(Widget child) => NfcEventCommand setNfcView(Widget child, {bool showIfHidden = true}) =>
NfcEventCommand(event: NfcUpdateViewEvent(child: child)); NfcEventCommand(
event: NfcSetViewEvent(child: child, showIfHidden: showIfHidden));
NfcEventCommand showNfcView(Widget child) =>
NfcEventCommand(event: NfcShowViewEvent(child: child));

View File

@ -41,14 +41,15 @@ class _NfcEventCommandListener {
(previous, action) { (previous, action) {
_log.debug('Change in command for Overlay: $previous -> $action'); _log.debug('Change in command for Overlay: $previous -> $action');
switch (action) { switch (action) {
case (NfcShowViewEvent a): case (NfcSetViewEvent a):
_show(context, a.child); if (!visible && a.showIfHidden) {
break; _show(context, a.child);
case (NfcUpdateViewEvent a): } else {
_ref.read(nfcViewNotifier.notifier).update(a.child); _ref.read(nfcViewNotifier.notifier).update(a.child);
}
break; break;
case (NfcHideViewEvent e): case (NfcHideViewEvent e):
_hide(context, Duration(milliseconds: e.timeoutMs)); _hide(context, e.hideAfter);
break; break;
case (NfcCancelEvent _): case (NfcCancelEvent _):
_ref.read(androidDialogProvider.notifier).cancelDialog(); _ref.read(androidDialogProvider.notifier).cancelDialog();
@ -61,8 +62,8 @@ class _NfcEventCommandListener {
void _show(BuildContext context, Widget child) async { void _show(BuildContext context, Widget child) async {
final notifier = _ref.read(nfcViewNotifier.notifier); final notifier = _ref.read(nfcViewNotifier.notifier);
notifier.update(child); notifier.update(child);
if (!_ref.read(nfcViewNotifier.select((s) => s.isShowing))) { if (!visible) {
notifier.setShowing(true); visible = true;
final result = await showModalBottomSheet( final result = await showModalBottomSheet(
context: context, context: context,
builder: (BuildContext context) { builder: (BuildContext context) {
@ -72,18 +73,22 @@ class _NfcEventCommandListener {
// the modal sheet was cancelled by Back button, close button or dismiss // the modal sheet was cancelled by Back button, close button or dismiss
_ref.read(androidDialogProvider.notifier).cancelDialog(); _ref.read(androidDialogProvider.notifier).cancelDialog();
} }
notifier.setShowing(false); visible = false;
} }
} }
void _hide(BuildContext context, Duration timeout) { void _hide(BuildContext context, Duration timeout) {
Future.delayed(timeout, () { Future.delayed(timeout, () {
_ref.read(withContextProvider)((context) async { _ref.read(withContextProvider)((context) async {
if (_ref.read(nfcViewNotifier.select((s) => s.isShowing))) { if (visible) {
Navigator.of(context).pop('HIDDEN'); Navigator.of(context).pop('HIDDEN');
_ref.read(nfcViewNotifier.notifier).setShowing(false); visible = false;
} }
}); });
}); });
} }
bool get visible => _ref.read(nfcViewNotifier.select((s) => s.isShowing));
set visible(bool showing) =>
_ref.read(nfcViewNotifier.notifier).setShowing(showing);
} }

View File

@ -22,18 +22,20 @@ import 'models.dart';
import 'nfc_activity_overlay.dart'; import 'nfc_activity_overlay.dart';
import 'nfc_content_widget.dart'; import 'nfc_content_widget.dart';
NfcEventCommand autoClose({ NfcEventCommand autoClose(
String? title, {String? title,
String? subtitle, String? subtitle,
Widget? icon, Widget? icon,
}) => bool showIfHidden = true}) =>
updateNfcView(_NfcAutoCloseWidget( setNfcView(
child: NfcContentWidget( _NfcAutoCloseWidget(
title: title, child: NfcContentWidget(
subtitle: subtitle, title: title,
icon: icon, subtitle: subtitle,
), icon: icon,
)); ),
),
showIfHidden: showIfHidden);
class _NfcAutoCloseWidget extends ConsumerWidget { class _NfcAutoCloseWidget extends ConsumerWidget {
final Widget child; final Widget child;
@ -44,7 +46,7 @@ class _NfcAutoCloseWidget extends ConsumerWidget {
Widget build(BuildContext context, WidgetRef ref) { Widget build(BuildContext context, WidgetRef ref) {
ref.listen(androidNfcActivityProvider, (previous, current) { ref.listen(androidNfcActivityProvider, (previous, current) {
if (current == NfcActivity.ready) { if (current == NfcActivity.ready) {
ref.read(nfcEventCommandNotifier.notifier).sendCommand(hideNfcView(0)); ref.read(nfcEventCommandNotifier.notifier).sendCommand(hideNfcView());
} }
}); });

View File

@ -30,7 +30,7 @@ NfcEventCommand countDownClose({
String? subtitle, String? subtitle,
Widget? icon, Widget? icon,
}) => }) =>
updateNfcView(_CountDownCloseWidget( setNfcView(_CountDownCloseWidget(
closeInSec: closeInSec, closeInSec: closeInSec,
child: NfcContentWidget( child: NfcContentWidget(
title: title, title: title,
@ -109,6 +109,6 @@ class _CountDownCloseWidgetState extends ConsumerState<_CountDownCloseWidget> {
} }
void hideNow() { void hideNow() {
ref.read(nfcEventCommandNotifier.notifier).sendCommand(hideNfcView(0)); ref.read(nfcEventCommandNotifier.notifier).sendCommand(hideNfcView());
} }
} }

View File

@ -949,6 +949,7 @@
"s_nfc_accessing_yubikey": null, "s_nfc_accessing_yubikey": null,
"s_nfc_scan_yubikey": null, "s_nfc_scan_yubikey": null,
"s_nfc_scan_again": null, "s_nfc_scan_again": null,
"s_nfc_scan_success": null,
"c_nfc_unlock": null, "c_nfc_unlock": null,
"s_nfc_unlock_processing": null, "s_nfc_unlock_processing": null,

View File

@ -949,6 +949,7 @@
"s_nfc_accessing_yubikey": "Accessing YubiKey", "s_nfc_accessing_yubikey": "Accessing YubiKey",
"s_nfc_scan_yubikey": "Scan your YubiKey", "s_nfc_scan_yubikey": "Scan your YubiKey",
"s_nfc_scan_again": "Scan again", "s_nfc_scan_again": "Scan again",
"s_nfc_scan_success": "Scanned your Yubikey",
"c_nfc_unlock": "unlock", "c_nfc_unlock": "unlock",
"s_nfc_unlock_processing": "Unlocking", "s_nfc_unlock_processing": "Unlocking",

View File

@ -949,6 +949,7 @@
"s_nfc_accessing_yubikey": null, "s_nfc_accessing_yubikey": null,
"s_nfc_scan_yubikey": null, "s_nfc_scan_yubikey": null,
"s_nfc_scan_again": null, "s_nfc_scan_again": null,
"s_nfc_scan_success": null,
"c_nfc_unlock": null, "c_nfc_unlock": null,
"s_nfc_unlock_processing": null, "s_nfc_unlock_processing": null,

View File

@ -949,6 +949,7 @@
"s_nfc_accessing_yubikey": null, "s_nfc_accessing_yubikey": null,
"s_nfc_scan_yubikey": null, "s_nfc_scan_yubikey": null,
"s_nfc_scan_again": null, "s_nfc_scan_again": null,
"s_nfc_scan_success": null,
"c_nfc_unlock": null, "c_nfc_unlock": null,
"s_nfc_unlock_processing": null, "s_nfc_unlock_processing": null,

View File

@ -949,6 +949,7 @@
"s_nfc_accessing_yubikey": null, "s_nfc_accessing_yubikey": null,
"s_nfc_scan_yubikey": null, "s_nfc_scan_yubikey": null,
"s_nfc_scan_again": null, "s_nfc_scan_again": null,
"s_nfc_scan_success": null,
"c_nfc_unlock": null, "c_nfc_unlock": null,
"s_nfc_unlock_processing": null, "s_nfc_unlock_processing": null,

View File

@ -949,6 +949,7 @@
"s_nfc_accessing_yubikey": null, "s_nfc_accessing_yubikey": null,
"s_nfc_scan_yubikey": null, "s_nfc_scan_yubikey": null,
"s_nfc_scan_again": null, "s_nfc_scan_again": null,
"s_nfc_scan_success": null,
"c_nfc_unlock": null, "c_nfc_unlock": null,
"s_nfc_unlock_processing": null, "s_nfc_unlock_processing": null,