This commit is contained in:
Adam Velebil 2024-09-06 17:21:59 +02:00
parent 75073c149b
commit f14f16eb9f
No known key found for this signature in database
GPG Key ID: C9B1E4A3CBBD2E10
7 changed files with 100 additions and 86 deletions

View File

@ -107,7 +107,7 @@ Future<Widget> initialize() async {
child: DismissKeyboard(
child: YubicoAuthenticatorApp(page: Consumer(
builder: (context, ref, child) {
ref.read(nfcEventCommandListener).startListener(context);
ref.read(nfcEventNotifierListener).startListener(context);
Timer.run(() {
ref.read(featureFlagProvider.notifier)

View File

@ -18,7 +18,6 @@ import 'package:flutter/services.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'tap_request_dialog.dart';
import 'views/nfc/nfc_activity_overlay.dart';
class MethodChannelNotifier extends Notifier<void> {
final MethodChannel _channel;
@ -30,9 +29,6 @@ class MethodChannelNotifier extends Notifier<void> {
Future<dynamic> invoke(String name,
[Map<String, dynamic> params = const {}]) async {
final notifier = ref.read(nfcViewNotifier.notifier);
notifier.setDialogProperties();
final result = await _channel.invokeMethod(name, params['callArgs']);
await ref.read(androidDialogProvider.notifier).waitForDialogClosed();
return result;

View File

@ -44,7 +44,7 @@ class _DialogProvider extends Notifier<int> {
@override
int build() {
final viewNotifier = ref.read(nfcViewNotifier.notifier);
final viewNotifier = ref.read(nfcActivityWidgetPropertiesNotifier.notifier);
ref.listen(androidNfcActivityProvider, (previous, current) {
processingViewTimeout?.cancel();
@ -52,7 +52,7 @@ class _DialogProvider extends Notifier<int> {
if (!explicitAction) {
// setup properties for ad-hoc action
viewNotifier.setDialogProperties(showCloseButton: false);
viewNotifier.update(hasCloseButton: false);
}
switch (current) {
@ -103,8 +103,8 @@ class _DialogProvider extends Notifier<int> {
NfcEvent showTapYourYubiKey() {
ref
.read(nfcViewNotifier.notifier)
.setDialogProperties(showCloseButton: true);
.read(nfcActivityWidgetPropertiesNotifier.notifier)
.update(hasCloseButton: true);
return NfcSetViewEvent(
child: NfcContentWidget(
title: l10n.s_nfc_ready_to_scan,
@ -115,8 +115,8 @@ class _DialogProvider extends Notifier<int> {
NfcEvent showHoldStill() {
ref
.read(nfcViewNotifier.notifier)
.setDialogProperties(showCloseButton: false);
.read(nfcActivityWidgetPropertiesNotifier.notifier)
.update(hasCloseButton: false);
return NfcSetViewEvent(
child: NfcContentWidget(
title: l10n.s_nfc_ready_to_scan,
@ -127,8 +127,8 @@ class _DialogProvider extends Notifier<int> {
NfcEvent showDone() {
ref
.read(nfcViewNotifier.notifier)
.setDialogProperties(showCloseButton: true);
.read(nfcActivityWidgetPropertiesNotifier.notifier)
.update(hasCloseButton: true);
return NfcSetViewEvent(
child: NfcContentWidget(
title: l10n.s_nfc_ready_to_scan,
@ -140,8 +140,8 @@ class _DialogProvider extends Notifier<int> {
NfcEvent showFailed() {
ref
.read(nfcViewNotifier.notifier)
.setDialogProperties(showCloseButton: true);
.read(nfcActivityWidgetPropertiesNotifier.notifier)
.update(hasCloseButton: true);
return NfcSetViewEvent(
child: NfcContentWidget(
title: l10n.s_nfc_ready_to_scan,
@ -166,7 +166,8 @@ class _DialogProvider extends Notifier<int> {
Timer.periodic(
const Duration(milliseconds: 200),
(timer) {
if (ref.read(nfcViewNotifier.select((s) => !s.visible))) {
if (ref.read(
nfcActivityWidgetPropertiesNotifier.select((s) => !s.visible))) {
timer.cancel();
completer.complete();
}

View File

@ -37,10 +37,10 @@ class NfcSetViewEvent extends NfcEvent {
}
@freezed
class NfcView with _$NfcView {
factory NfcView({
class NfcActivityWidgetProperties with _$NfcActivityWidgetProperties {
factory NfcActivityWidgetProperties({
required Widget child,
@Default(false) bool visible,
@Default(false) bool hasCloseButton,
}) = _NfcView;
}) = _NfcActivityWidgetProperties;
}

View File

@ -15,36 +15,41 @@ final _privateConstructorUsedError = UnsupportedError(
'It seems like you constructed your class using `MyClass._()`. This constructor is only meant to be used by freezed and you are not supposed to need it nor use it.\nPlease check the documentation here for more information: https://github.com/rrousselGit/freezed#adding-getters-and-methods-to-our-models');
/// @nodoc
mixin _$NfcView {
mixin _$NfcActivityWidgetProperties {
Widget get child => throw _privateConstructorUsedError;
bool get visible => throw _privateConstructorUsedError;
bool get hasCloseButton => throw _privateConstructorUsedError;
/// Create a copy of NfcView
/// Create a copy of NfcActivityWidgetProperties
/// with the given fields replaced by the non-null parameter values.
@JsonKey(includeFromJson: false, includeToJson: false)
$NfcViewCopyWith<NfcView> get copyWith => throw _privateConstructorUsedError;
$NfcActivityWidgetPropertiesCopyWith<NfcActivityWidgetProperties>
get copyWith => throw _privateConstructorUsedError;
}
/// @nodoc
abstract class $NfcViewCopyWith<$Res> {
factory $NfcViewCopyWith(NfcView value, $Res Function(NfcView) then) =
_$NfcViewCopyWithImpl<$Res, NfcView>;
abstract class $NfcActivityWidgetPropertiesCopyWith<$Res> {
factory $NfcActivityWidgetPropertiesCopyWith(
NfcActivityWidgetProperties value,
$Res Function(NfcActivityWidgetProperties) then) =
_$NfcActivityWidgetPropertiesCopyWithImpl<$Res,
NfcActivityWidgetProperties>;
@useResult
$Res call({Widget child, bool visible, bool hasCloseButton});
}
/// @nodoc
class _$NfcViewCopyWithImpl<$Res, $Val extends NfcView>
implements $NfcViewCopyWith<$Res> {
_$NfcViewCopyWithImpl(this._value, this._then);
class _$NfcActivityWidgetPropertiesCopyWithImpl<$Res,
$Val extends NfcActivityWidgetProperties>
implements $NfcActivityWidgetPropertiesCopyWith<$Res> {
_$NfcActivityWidgetPropertiesCopyWithImpl(this._value, this._then);
// ignore: unused_field
final $Val _value;
// ignore: unused_field
final $Res Function($Val) _then;
/// Create a copy of NfcView
/// Create a copy of NfcActivityWidgetProperties
/// with the given fields replaced by the non-null parameter values.
@pragma('vm:prefer-inline')
@override
@ -71,24 +76,28 @@ class _$NfcViewCopyWithImpl<$Res, $Val extends NfcView>
}
/// @nodoc
abstract class _$$NfcViewImplCopyWith<$Res> implements $NfcViewCopyWith<$Res> {
factory _$$NfcViewImplCopyWith(
_$NfcViewImpl value, $Res Function(_$NfcViewImpl) then) =
__$$NfcViewImplCopyWithImpl<$Res>;
abstract class _$$NfcActivityWidgetPropertiesImplCopyWith<$Res>
implements $NfcActivityWidgetPropertiesCopyWith<$Res> {
factory _$$NfcActivityWidgetPropertiesImplCopyWith(
_$NfcActivityWidgetPropertiesImpl value,
$Res Function(_$NfcActivityWidgetPropertiesImpl) then) =
__$$NfcActivityWidgetPropertiesImplCopyWithImpl<$Res>;
@override
@useResult
$Res call({Widget child, bool visible, bool hasCloseButton});
}
/// @nodoc
class __$$NfcViewImplCopyWithImpl<$Res>
extends _$NfcViewCopyWithImpl<$Res, _$NfcViewImpl>
implements _$$NfcViewImplCopyWith<$Res> {
__$$NfcViewImplCopyWithImpl(
_$NfcViewImpl _value, $Res Function(_$NfcViewImpl) _then)
class __$$NfcActivityWidgetPropertiesImplCopyWithImpl<$Res>
extends _$NfcActivityWidgetPropertiesCopyWithImpl<$Res,
_$NfcActivityWidgetPropertiesImpl>
implements _$$NfcActivityWidgetPropertiesImplCopyWith<$Res> {
__$$NfcActivityWidgetPropertiesImplCopyWithImpl(
_$NfcActivityWidgetPropertiesImpl _value,
$Res Function(_$NfcActivityWidgetPropertiesImpl) _then)
: super(_value, _then);
/// Create a copy of NfcView
/// Create a copy of NfcActivityWidgetProperties
/// with the given fields replaced by the non-null parameter values.
@pragma('vm:prefer-inline')
@override
@ -97,7 +106,7 @@ class __$$NfcViewImplCopyWithImpl<$Res>
Object? visible = null,
Object? hasCloseButton = null,
}) {
return _then(_$NfcViewImpl(
return _then(_$NfcActivityWidgetPropertiesImpl(
child: null == child
? _value.child
: child // ignore: cast_nullable_to_non_nullable
@ -116,8 +125,9 @@ class __$$NfcViewImplCopyWithImpl<$Res>
/// @nodoc
class _$NfcViewImpl implements _NfcView {
_$NfcViewImpl(
class _$NfcActivityWidgetPropertiesImpl
implements _NfcActivityWidgetProperties {
_$NfcActivityWidgetPropertiesImpl(
{required this.child, this.visible = false, this.hasCloseButton = false});
@override
@ -131,14 +141,14 @@ class _$NfcViewImpl implements _NfcView {
@override
String toString() {
return 'NfcView(child: $child, visible: $visible, hasCloseButton: $hasCloseButton)';
return 'NfcActivityWidgetProperties(child: $child, visible: $visible, hasCloseButton: $hasCloseButton)';
}
@override
bool operator ==(Object other) {
return identical(this, other) ||
(other.runtimeType == runtimeType &&
other is _$NfcViewImpl &&
other is _$NfcActivityWidgetPropertiesImpl &&
(identical(other.child, child) || other.child == child) &&
(identical(other.visible, visible) || other.visible == visible) &&
(identical(other.hasCloseButton, hasCloseButton) ||
@ -148,20 +158,22 @@ class _$NfcViewImpl implements _NfcView {
@override
int get hashCode => Object.hash(runtimeType, child, visible, hasCloseButton);
/// Create a copy of NfcView
/// Create a copy of NfcActivityWidgetProperties
/// with the given fields replaced by the non-null parameter values.
@JsonKey(includeFromJson: false, includeToJson: false)
@override
@pragma('vm:prefer-inline')
_$$NfcViewImplCopyWith<_$NfcViewImpl> get copyWith =>
__$$NfcViewImplCopyWithImpl<_$NfcViewImpl>(this, _$identity);
_$$NfcActivityWidgetPropertiesImplCopyWith<_$NfcActivityWidgetPropertiesImpl>
get copyWith => __$$NfcActivityWidgetPropertiesImplCopyWithImpl<
_$NfcActivityWidgetPropertiesImpl>(this, _$identity);
}
abstract class _NfcView implements NfcView {
factory _NfcView(
abstract class _NfcActivityWidgetProperties
implements NfcActivityWidgetProperties {
factory _NfcActivityWidgetProperties(
{required final Widget child,
final bool visible,
final bool hasCloseButton}) = _$NfcViewImpl;
final bool hasCloseButton}) = _$NfcActivityWidgetPropertiesImpl;
@override
Widget get child;
@ -170,10 +182,10 @@ abstract class _NfcView implements NfcView {
@override
bool get hasCloseButton;
/// Create a copy of NfcView
/// Create a copy of NfcActivityWidgetProperties
/// with the given fields replaced by the non-null parameter values.
@override
@JsonKey(includeFromJson: false, includeToJson: false)
_$$NfcViewImplCopyWith<_$NfcViewImpl> get copyWith =>
throw _privateConstructorUsedError;
_$$NfcActivityWidgetPropertiesImplCopyWith<_$NfcActivityWidgetPropertiesImpl>
get copyWith => throw _privateConstructorUsedError;
}

View File

@ -26,25 +26,27 @@ import 'nfc_activity_overlay.dart';
final _log = Logger('android.nfc_activity_command_listener');
final nfcEventCommandListener =
Provider<_NfcEventCommandListener>((ref) => _NfcEventCommandListener(ref));
final nfcEventNotifierListener = Provider<_NfcEventNotifierListener>(
(ref) => _NfcEventNotifierListener(ref));
class _NfcEventCommandListener {
class _NfcEventNotifierListener {
final ProviderRef _ref;
ProviderSubscription<NfcEvent>? listener;
_NfcEventCommandListener(this._ref);
_NfcEventNotifierListener(this._ref);
void startListener(BuildContext context) {
listener?.close();
listener = _ref.listen(nfcEventNotifier, (previous, action) {
_log.debug('Change in command for Overlay: $previous -> $action');
_log.debug('Event change: $previous -> $action');
switch (action) {
case (NfcSetViewEvent a):
if (!visible && a.showIfHidden) {
_show(context, a.child);
} else {
_ref.read(nfcViewNotifier.notifier).update(a.child);
_ref
.read(nfcActivityWidgetPropertiesNotifier.notifier)
.update(child: a.child);
}
break;
case (NfcHideViewEvent e):
@ -55,14 +57,14 @@ class _NfcEventCommandListener {
}
void _show(BuildContext context, Widget child) async {
final notifier = _ref.read(nfcViewNotifier.notifier);
notifier.update(child);
final notifier = _ref.read(nfcActivityWidgetPropertiesNotifier.notifier);
notifier.update(child: child);
if (!visible) {
visible = true;
final result = await showModalBottomSheet(
context: context,
builder: (BuildContext context) {
return const NfcBottomSheet();
return const NfcActivityWidget();
});
if (result == null) {
// the modal sheet was cancelled by Back button, close button or dismiss
@ -83,8 +85,10 @@ class _NfcEventCommandListener {
});
}
bool get visible => _ref.read(nfcViewNotifier.select((s) => s.visible));
bool get visible =>
_ref.read(nfcActivityWidgetPropertiesNotifier.select((s) => s.visible));
set visible(bool showing) =>
_ref.read(nfcViewNotifier.notifier).setShowing(showing);
set visible(bool visible) => _ref
.read(nfcActivityWidgetPropertiesNotifier.notifier)
.update(visible: visible);
}

View File

@ -34,37 +34,38 @@ class _NfcEventNotifier extends Notifier<NfcEvent> {
}
}
final nfcViewNotifier =
NotifierProvider<_NfcViewNotifier, NfcView>(_NfcViewNotifier.new);
final nfcActivityWidgetPropertiesNotifier = NotifierProvider<
_NfcActivityWidgetPropertiesNotifier,
NfcActivityWidgetProperties>(_NfcActivityWidgetPropertiesNotifier.new);
class _NfcViewNotifier extends Notifier<NfcView> {
class _NfcActivityWidgetPropertiesNotifier
extends Notifier<NfcActivityWidgetProperties> {
@override
NfcView build() {
return NfcView(child: const SizedBox());
NfcActivityWidgetProperties build() {
return NfcActivityWidgetProperties(child: const SizedBox());
}
void update(Widget child) {
state = state.copyWith(child: child);
}
void setShowing(bool value) {
state = state.copyWith(visible: value);
}
void setDialogProperties({bool? showCloseButton}) {
state =
state.copyWith(hasCloseButton: showCloseButton ?? state.hasCloseButton);
void update({
Widget? child,
bool? visible,
bool? hasCloseButton,
}) {
state = state.copyWith(
child: child ?? state.child,
visible: visible ?? state.visible,
hasCloseButton: hasCloseButton ?? state.hasCloseButton);
}
}
class NfcBottomSheet extends ConsumerWidget {
const NfcBottomSheet({super.key});
class NfcActivityWidget extends ConsumerWidget {
const NfcActivityWidget({super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
final widget = ref.watch(nfcViewNotifier.select((s) => s.child));
final showCloseButton =
ref.watch(nfcViewNotifier.select((s) => s.hasCloseButton));
final widget =
ref.watch(nfcActivityWidgetPropertiesNotifier.select((s) => s.child));
final showCloseButton = ref.watch(
nfcActivityWidgetPropertiesNotifier.select((s) => s.hasCloseButton));
return Column(
mainAxisAlignment: MainAxisAlignment.start,
mainAxisSize: MainAxisSize.min,