From 75073c149b1614c4df1ac0ca78626a98e343b45c Mon Sep 17 00:00:00 2001 From: Adam Velebil Date: Fri, 6 Sep 2024 16:58:30 +0200 Subject: [PATCH] refactor --- lib/android/tap_request_dialog.dart | 46 ++-- lib/android/views/nfc/models.dart | 26 +-- lib/android/views/nfc/models.freezed.dart | 212 ++++-------------- .../nfc/nfc_activity_command_listener.dart | 12 +- .../views/nfc/nfc_activity_overlay.dart | 25 +-- .../views/nfc/nfc_auto_close_widget.dart | 8 +- .../nfc/nfc_count_down_close_widget.dart | 7 +- 7 files changed, 92 insertions(+), 244 deletions(-) diff --git a/lib/android/tap_request_dialog.dart b/lib/android/tap_request_dialog.dart index 1258482c..baddb8a5 100755 --- a/lib/android/tap_request_dialog.dart +++ b/lib/android/tap_request_dialog.dart @@ -47,7 +47,8 @@ class _DialogProvider extends Notifier { final viewNotifier = ref.read(nfcViewNotifier.notifier); ref.listen(androidNfcActivityProvider, (previous, current) { - final notifier = ref.read(nfcEventCommandNotifier.notifier); + processingViewTimeout?.cancel(); + final notifier = ref.read(nfcEventNotifier.notifier); if (!explicitAction) { // setup properties for ad-hoc action @@ -57,21 +58,18 @@ class _DialogProvider extends Notifier { switch (current) { case NfcActivity.processingStarted: final timeout = explicitAction ? 300 : 500; - processingViewTimeout?.cancel(); processingViewTimeout = Timer(Duration(milliseconds: timeout), () { - notifier.sendCommand(showScanning()); + notifier.send(showHoldStill()); }); break; case NfcActivity.processingFinished: - processingViewTimeout?.cancel(); - notifier.sendCommand(showDone()); - notifier.sendCommand(hideNfcView(const Duration(milliseconds: 400))); - + notifier.send(showDone()); + notifier + .send(const NfcHideViewEvent(delay: Duration(milliseconds: 400))); explicitAction = false; // next action might not be explicit break; case NfcActivity.processingInterrupted: - processingViewTimeout?.cancel(); - notifier.sendCommand(showFailed()); + notifier.send(showFailed()); break; case NfcActivity.notActive: _log.debug('Received not handled notActive'); @@ -82,11 +80,11 @@ class _DialogProvider extends Notifier { }); _channel.setMethodCallHandler((call) async { - final notifier = ref.read(nfcEventCommandNotifier.notifier); + final notifier = ref.read(nfcEventNotifier.notifier); switch (call.method) { case 'show': explicitAction = true; - notifier.sendCommand(showTapYourYubiKey()); + notifier.send(showTapYourYubiKey()); break; case 'close': @@ -103,34 +101,36 @@ class _DialogProvider extends Notifier { return 0; } - NfcEventCommand showTapYourYubiKey() { + NfcEvent showTapYourYubiKey() { ref .read(nfcViewNotifier.notifier) .setDialogProperties(showCloseButton: true); - return setNfcView(NfcContentWidget( + return NfcSetViewEvent( + child: NfcContentWidget( title: l10n.s_nfc_ready_to_scan, subtitle: l10n.s_nfc_tap_your_yubikey, icon: const NfcIconProgressBar(false), )); } - NfcEventCommand showScanning() { + NfcEvent showHoldStill() { ref .read(nfcViewNotifier.notifier) .setDialogProperties(showCloseButton: false); - return setNfcView(NfcContentWidget( + return NfcSetViewEvent( + child: NfcContentWidget( title: l10n.s_nfc_ready_to_scan, subtitle: l10n.s_nfc_hold_still, icon: const NfcIconProgressBar(true), )); } - NfcEventCommand showDone() { + NfcEvent showDone() { ref .read(nfcViewNotifier.notifier) .setDialogProperties(showCloseButton: true); - return setNfcView( - NfcContentWidget( + return NfcSetViewEvent( + child: NfcContentWidget( title: l10n.s_nfc_ready_to_scan, subtitle: l10n.s_done, icon: const NfcIconSuccess(), @@ -138,12 +138,12 @@ class _DialogProvider extends Notifier { showIfHidden: false); } - NfcEventCommand showFailed() { + NfcEvent showFailed() { ref .read(nfcViewNotifier.notifier) .setDialogProperties(showCloseButton: true); - return setNfcView( - NfcContentWidget( + return NfcSetViewEvent( + child: NfcContentWidget( title: l10n.s_nfc_ready_to_scan, subtitle: l10n.l_nfc_failed_to_scan, icon: const NfcIconFailure(), @@ -152,7 +152,7 @@ class _DialogProvider extends Notifier { } void closeDialog() { - ref.read(nfcEventCommandNotifier.notifier).sendCommand(hideNfcView()); + ref.read(nfcEventNotifier.notifier).send(const NfcHideViewEvent()); } void cancelDialog() async { @@ -166,7 +166,7 @@ class _DialogProvider extends Notifier { Timer.periodic( const Duration(milliseconds: 200), (timer) { - if (!ref.read(nfcViewNotifier.select((s) => s.isShowing))) { + if (ref.read(nfcViewNotifier.select((s) => !s.visible))) { timer.cancel(); completer.complete(); } diff --git a/lib/android/views/nfc/models.dart b/lib/android/views/nfc/models.dart index 71862e7e..5149528c 100644 --- a/lib/android/views/nfc/models.dart +++ b/lib/android/views/nfc/models.dart @@ -24,13 +24,9 @@ class NfcEvent { } class NfcHideViewEvent extends NfcEvent { - final Duration hideAfter; + final Duration delay; - const NfcHideViewEvent({required this.hideAfter}); -} - -class NfcCancelEvent extends NfcEvent { - const NfcCancelEvent(); + const NfcHideViewEvent({this.delay = Duration.zero}); } class NfcSetViewEvent extends NfcEvent { @@ -43,22 +39,8 @@ class NfcSetViewEvent extends NfcEvent { @freezed class NfcView with _$NfcView { factory NfcView({ - required bool isShowing, required Widget child, - bool? showCloseButton, + @Default(false) bool visible, + @Default(false) bool hasCloseButton, }) = _NfcView; } - -@freezed -class NfcEventCommand with _$NfcEventCommand { - factory NfcEventCommand({ - @Default(NfcEvent()) NfcEvent event, - }) = _NfcEventCommand; -} - -NfcEventCommand hideNfcView([Duration hideAfter = Duration.zero]) => - NfcEventCommand(event: NfcHideViewEvent(hideAfter: hideAfter)); - -NfcEventCommand setNfcView(Widget child, {bool showIfHidden = true}) => - NfcEventCommand( - event: NfcSetViewEvent(child: child, showIfHidden: showIfHidden)); diff --git a/lib/android/views/nfc/models.freezed.dart b/lib/android/views/nfc/models.freezed.dart index eb0d46bc..d7dc49d1 100644 --- a/lib/android/views/nfc/models.freezed.dart +++ b/lib/android/views/nfc/models.freezed.dart @@ -16,9 +16,9 @@ final _privateConstructorUsedError = UnsupportedError( /// @nodoc mixin _$NfcView { - bool get isShowing => throw _privateConstructorUsedError; Widget get child => throw _privateConstructorUsedError; - bool? get showCloseButton => throw _privateConstructorUsedError; + bool get visible => throw _privateConstructorUsedError; + bool get hasCloseButton => throw _privateConstructorUsedError; /// Create a copy of NfcView /// with the given fields replaced by the non-null parameter values. @@ -31,7 +31,7 @@ abstract class $NfcViewCopyWith<$Res> { factory $NfcViewCopyWith(NfcView value, $Res Function(NfcView) then) = _$NfcViewCopyWithImpl<$Res, NfcView>; @useResult - $Res call({bool isShowing, Widget child, bool? showCloseButton}); + $Res call({Widget child, bool visible, bool hasCloseButton}); } /// @nodoc @@ -49,23 +49,23 @@ class _$NfcViewCopyWithImpl<$Res, $Val extends NfcView> @pragma('vm:prefer-inline') @override $Res call({ - Object? isShowing = null, Object? child = null, - Object? showCloseButton = freezed, + Object? visible = null, + Object? hasCloseButton = null, }) { return _then(_value.copyWith( - isShowing: null == isShowing - ? _value.isShowing - : isShowing // ignore: cast_nullable_to_non_nullable - as bool, child: null == child ? _value.child : child // ignore: cast_nullable_to_non_nullable as Widget, - showCloseButton: freezed == showCloseButton - ? _value.showCloseButton - : showCloseButton // ignore: cast_nullable_to_non_nullable - as bool?, + visible: null == visible + ? _value.visible + : visible // ignore: cast_nullable_to_non_nullable + as bool, + hasCloseButton: null == hasCloseButton + ? _value.hasCloseButton + : hasCloseButton // ignore: cast_nullable_to_non_nullable + as bool, ) as $Val); } } @@ -77,7 +77,7 @@ abstract class _$$NfcViewImplCopyWith<$Res> implements $NfcViewCopyWith<$Res> { __$$NfcViewImplCopyWithImpl<$Res>; @override @useResult - $Res call({bool isShowing, Widget child, bool? showCloseButton}); + $Res call({Widget child, bool visible, bool hasCloseButton}); } /// @nodoc @@ -93,23 +93,23 @@ class __$$NfcViewImplCopyWithImpl<$Res> @pragma('vm:prefer-inline') @override $Res call({ - Object? isShowing = null, Object? child = null, - Object? showCloseButton = freezed, + Object? visible = null, + Object? hasCloseButton = null, }) { return _then(_$NfcViewImpl( - isShowing: null == isShowing - ? _value.isShowing - : isShowing // ignore: cast_nullable_to_non_nullable - as bool, child: null == child ? _value.child : child // ignore: cast_nullable_to_non_nullable as Widget, - showCloseButton: freezed == showCloseButton - ? _value.showCloseButton - : showCloseButton // ignore: cast_nullable_to_non_nullable - as bool?, + visible: null == visible + ? _value.visible + : visible // ignore: cast_nullable_to_non_nullable + as bool, + hasCloseButton: null == hasCloseButton + ? _value.hasCloseButton + : hasCloseButton // ignore: cast_nullable_to_non_nullable + as bool, )); } } @@ -118,18 +118,20 @@ class __$$NfcViewImplCopyWithImpl<$Res> class _$NfcViewImpl implements _NfcView { _$NfcViewImpl( - {required this.isShowing, required this.child, this.showCloseButton}); + {required this.child, this.visible = false, this.hasCloseButton = false}); - @override - final bool isShowing; @override final Widget child; @override - final bool? showCloseButton; + @JsonKey() + final bool visible; + @override + @JsonKey() + final bool hasCloseButton; @override String toString() { - return 'NfcView(isShowing: $isShowing, child: $child, showCloseButton: $showCloseButton)'; + return 'NfcView(child: $child, visible: $visible, hasCloseButton: $hasCloseButton)'; } @override @@ -137,16 +139,14 @@ class _$NfcViewImpl implements _NfcView { return identical(this, other) || (other.runtimeType == runtimeType && other is _$NfcViewImpl && - (identical(other.isShowing, isShowing) || - other.isShowing == isShowing) && (identical(other.child, child) || other.child == child) && - (identical(other.showCloseButton, showCloseButton) || - other.showCloseButton == showCloseButton)); + (identical(other.visible, visible) || other.visible == visible) && + (identical(other.hasCloseButton, hasCloseButton) || + other.hasCloseButton == hasCloseButton)); } @override - int get hashCode => - Object.hash(runtimeType, isShowing, child, showCloseButton); + int get hashCode => Object.hash(runtimeType, child, visible, hasCloseButton); /// Create a copy of NfcView /// with the given fields replaced by the non-null parameter values. @@ -159,16 +159,16 @@ class _$NfcViewImpl implements _NfcView { abstract class _NfcView implements NfcView { factory _NfcView( - {required final bool isShowing, - required final Widget child, - final bool? showCloseButton}) = _$NfcViewImpl; + {required final Widget child, + final bool visible, + final bool hasCloseButton}) = _$NfcViewImpl; - @override - bool get isShowing; @override Widget get child; @override - bool? get showCloseButton; + bool get visible; + @override + bool get hasCloseButton; /// Create a copy of NfcView /// with the given fields replaced by the non-null parameter values. @@ -177,133 +177,3 @@ abstract class _NfcView implements NfcView { _$$NfcViewImplCopyWith<_$NfcViewImpl> get copyWith => throw _privateConstructorUsedError; } - -/// @nodoc -mixin _$NfcEventCommand { - NfcEvent get event => throw _privateConstructorUsedError; - - /// Create a copy of NfcEventCommand - /// with the given fields replaced by the non-null parameter values. - @JsonKey(includeFromJson: false, includeToJson: false) - $NfcEventCommandCopyWith get copyWith => - throw _privateConstructorUsedError; -} - -/// @nodoc -abstract class $NfcEventCommandCopyWith<$Res> { - factory $NfcEventCommandCopyWith( - NfcEventCommand value, $Res Function(NfcEventCommand) then) = - _$NfcEventCommandCopyWithImpl<$Res, NfcEventCommand>; - @useResult - $Res call({NfcEvent event}); -} - -/// @nodoc -class _$NfcEventCommandCopyWithImpl<$Res, $Val extends NfcEventCommand> - implements $NfcEventCommandCopyWith<$Res> { - _$NfcEventCommandCopyWithImpl(this._value, this._then); - - // ignore: unused_field - final $Val _value; - // ignore: unused_field - final $Res Function($Val) _then; - - /// Create a copy of NfcEventCommand - /// with the given fields replaced by the non-null parameter values. - @pragma('vm:prefer-inline') - @override - $Res call({ - Object? event = null, - }) { - return _then(_value.copyWith( - event: null == event - ? _value.event - : event // ignore: cast_nullable_to_non_nullable - as NfcEvent, - ) as $Val); - } -} - -/// @nodoc -abstract class _$$NfcEventCommandImplCopyWith<$Res> - implements $NfcEventCommandCopyWith<$Res> { - factory _$$NfcEventCommandImplCopyWith(_$NfcEventCommandImpl value, - $Res Function(_$NfcEventCommandImpl) then) = - __$$NfcEventCommandImplCopyWithImpl<$Res>; - @override - @useResult - $Res call({NfcEvent event}); -} - -/// @nodoc -class __$$NfcEventCommandImplCopyWithImpl<$Res> - extends _$NfcEventCommandCopyWithImpl<$Res, _$NfcEventCommandImpl> - implements _$$NfcEventCommandImplCopyWith<$Res> { - __$$NfcEventCommandImplCopyWithImpl( - _$NfcEventCommandImpl _value, $Res Function(_$NfcEventCommandImpl) _then) - : super(_value, _then); - - /// Create a copy of NfcEventCommand - /// with the given fields replaced by the non-null parameter values. - @pragma('vm:prefer-inline') - @override - $Res call({ - Object? event = null, - }) { - return _then(_$NfcEventCommandImpl( - event: null == event - ? _value.event - : event // ignore: cast_nullable_to_non_nullable - as NfcEvent, - )); - } -} - -/// @nodoc - -class _$NfcEventCommandImpl implements _NfcEventCommand { - _$NfcEventCommandImpl({this.event = const NfcEvent()}); - - @override - @JsonKey() - final NfcEvent event; - - @override - String toString() { - return 'NfcEventCommand(event: $event)'; - } - - @override - bool operator ==(Object other) { - return identical(this, other) || - (other.runtimeType == runtimeType && - other is _$NfcEventCommandImpl && - (identical(other.event, event) || other.event == event)); - } - - @override - int get hashCode => Object.hash(runtimeType, event); - - /// Create a copy of NfcEventCommand - /// with the given fields replaced by the non-null parameter values. - @JsonKey(includeFromJson: false, includeToJson: false) - @override - @pragma('vm:prefer-inline') - _$$NfcEventCommandImplCopyWith<_$NfcEventCommandImpl> get copyWith => - __$$NfcEventCommandImplCopyWithImpl<_$NfcEventCommandImpl>( - this, _$identity); -} - -abstract class _NfcEventCommand implements NfcEventCommand { - factory _NfcEventCommand({final NfcEvent event}) = _$NfcEventCommandImpl; - - @override - NfcEvent get event; - - /// Create a copy of NfcEventCommand - /// with the given fields replaced by the non-null parameter values. - @override - @JsonKey(includeFromJson: false, includeToJson: false) - _$$NfcEventCommandImplCopyWith<_$NfcEventCommandImpl> get copyWith => - throw _privateConstructorUsedError; -} diff --git a/lib/android/views/nfc/nfc_activity_command_listener.dart b/lib/android/views/nfc/nfc_activity_command_listener.dart index d821750b..c24f1cb8 100644 --- a/lib/android/views/nfc/nfc_activity_command_listener.dart +++ b/lib/android/views/nfc/nfc_activity_command_listener.dart @@ -37,8 +37,7 @@ class _NfcEventCommandListener { void startListener(BuildContext context) { listener?.close(); - listener = _ref.listen(nfcEventCommandNotifier.select((c) => c.event), - (previous, action) { + listener = _ref.listen(nfcEventNotifier, (previous, action) { _log.debug('Change in command for Overlay: $previous -> $action'); switch (action) { case (NfcSetViewEvent a): @@ -49,11 +48,7 @@ class _NfcEventCommandListener { } break; case (NfcHideViewEvent e): - _hide(context, e.hideAfter); - break; - case (NfcCancelEvent _): - _ref.read(androidDialogProvider.notifier).cancelDialog(); - _hide(context, Duration.zero); + _hide(context, e.delay); break; } }); @@ -88,7 +83,8 @@ class _NfcEventCommandListener { }); } - bool get visible => _ref.read(nfcViewNotifier.select((s) => s.isShowing)); + bool get visible => _ref.read(nfcViewNotifier.select((s) => s.visible)); + set visible(bool showing) => _ref.read(nfcViewNotifier.notifier).setShowing(showing); } diff --git a/lib/android/views/nfc/nfc_activity_overlay.dart b/lib/android/views/nfc/nfc_activity_overlay.dart index c3f2b123..cab4bb99 100644 --- a/lib/android/views/nfc/nfc_activity_overlay.dart +++ b/lib/android/views/nfc/nfc_activity_overlay.dart @@ -20,18 +20,17 @@ import 'package:material_symbols_icons/symbols.dart'; import 'models.dart'; -final nfcEventCommandNotifier = - NotifierProvider<_NfcEventCommandNotifier, NfcEventCommand>( - _NfcEventCommandNotifier.new); +final nfcEventNotifier = + NotifierProvider<_NfcEventNotifier, NfcEvent>(_NfcEventNotifier.new); -class _NfcEventCommandNotifier extends Notifier { +class _NfcEventNotifier extends Notifier { @override - NfcEventCommand build() { - return NfcEventCommand(event: const NfcEvent()); + NfcEvent build() { + return const NfcEvent(); } - void sendCommand(NfcEventCommand command) { - state = command; + void send(NfcEvent event) { + state = event; } } @@ -41,7 +40,7 @@ final nfcViewNotifier = class _NfcViewNotifier extends Notifier { @override NfcView build() { - return NfcView(isShowing: false, child: const SizedBox()); + return NfcView(child: const SizedBox()); } void update(Widget child) { @@ -49,12 +48,12 @@ class _NfcViewNotifier extends Notifier { } void setShowing(bool value) { - state = state.copyWith(isShowing: value); + state = state.copyWith(visible: value); } void setDialogProperties({bool? showCloseButton}) { - state = state.copyWith( - showCloseButton: showCloseButton ?? state.showCloseButton); + state = + state.copyWith(hasCloseButton: showCloseButton ?? state.hasCloseButton); } } @@ -65,7 +64,7 @@ class NfcBottomSheet extends ConsumerWidget { Widget build(BuildContext context, WidgetRef ref) { final widget = ref.watch(nfcViewNotifier.select((s) => s.child)); final showCloseButton = - ref.watch(nfcViewNotifier.select((s) => s.showCloseButton ?? false)); + ref.watch(nfcViewNotifier.select((s) => s.hasCloseButton)); return Column( mainAxisAlignment: MainAxisAlignment.start, mainAxisSize: MainAxisSize.min, diff --git a/lib/android/views/nfc/nfc_auto_close_widget.dart b/lib/android/views/nfc/nfc_auto_close_widget.dart index ccd221db..2b00b07b 100644 --- a/lib/android/views/nfc/nfc_auto_close_widget.dart +++ b/lib/android/views/nfc/nfc_auto_close_widget.dart @@ -22,13 +22,13 @@ import 'models.dart'; import 'nfc_activity_overlay.dart'; import 'nfc_content_widget.dart'; -NfcEventCommand autoClose( +NfcEvent autoClose( {required String title, required String subtitle, required Widget icon, bool showIfHidden = true}) => - setNfcView( - _NfcAutoCloseWidget( + NfcSetViewEvent( + child: _NfcAutoCloseWidget( child: NfcContentWidget( title: title, subtitle: subtitle, @@ -46,7 +46,7 @@ class _NfcAutoCloseWidget extends ConsumerWidget { Widget build(BuildContext context, WidgetRef ref) { ref.listen(androidNfcActivityProvider, (previous, current) { if (current == NfcActivity.ready) { - ref.read(nfcEventCommandNotifier.notifier).sendCommand(hideNfcView()); + ref.read(nfcEventNotifier.notifier).send(const NfcHideViewEvent()); } }); diff --git a/lib/android/views/nfc/nfc_count_down_close_widget.dart b/lib/android/views/nfc/nfc_count_down_close_widget.dart index fa9da011..530f09d6 100644 --- a/lib/android/views/nfc/nfc_count_down_close_widget.dart +++ b/lib/android/views/nfc/nfc_count_down_close_widget.dart @@ -24,13 +24,14 @@ import 'models.dart'; import 'nfc_activity_overlay.dart'; import 'nfc_content_widget.dart'; -NfcEventCommand countDownClose({ +NfcEvent countDownClose({ required String title, required String subtitle, required Widget icon, int closeInSec = 3, }) => - setNfcView(_CountDownCloseWidget( + NfcSetViewEvent( + child: _CountDownCloseWidget( closeInSec: closeInSec, child: NfcContentWidget( title: title, @@ -109,6 +110,6 @@ class _CountDownCloseWidgetState extends ConsumerState<_CountDownCloseWidget> { } void hideNow() { - ref.read(nfcEventCommandNotifier.notifier).sendCommand(hideNfcView()); + ref.read(nfcEventNotifier.notifier).send(const NfcHideViewEvent()); } }