From 8d5a2c8e56de21f78f0d6b11c2829b8f65078e12 Mon Sep 17 00:00:00 2001 From: JulianAlcala Date: Wed, 22 Apr 2026 02:26:25 +0200 Subject: [PATCH] refactor(settings): migrate disable_functions to Riverpod --- .../providers/block_phone_controller.g.dart | 2 +- .../disable_functions_screen.dart | 72 ++--- .../disable_functions_controller.dart | 37 +++ .../disable_functions_controller.g.dart | 57 ++++ .../disable_functions_selection_provider.dart | 29 ++ ...isable_functions_selection_provider.g.dart | 79 +++++ .../state/disable_functions_view_model.dart | 84 ----- .../state/disable_functions_view_state.dart | 17 -- .../disable_functions_view_state.freezed.dart | 286 ------------------ .../providers/sos_contacts_controller.g.dart | 2 +- .../disable_functions_controller_test.dart | 91 ++++++ 11 files changed, 332 insertions(+), 424 deletions(-) create mode 100644 modules/legacy/modules/settings/lib/src/features/disable_functions/presentation/providers/disable_functions_controller.dart create mode 100644 modules/legacy/modules/settings/lib/src/features/disable_functions/presentation/providers/disable_functions_controller.g.dart create mode 100644 modules/legacy/modules/settings/lib/src/features/disable_functions/presentation/providers/disable_functions_selection_provider.dart create mode 100644 modules/legacy/modules/settings/lib/src/features/disable_functions/presentation/providers/disable_functions_selection_provider.g.dart delete mode 100644 modules/legacy/modules/settings/lib/src/features/disable_functions/presentation/state/disable_functions_view_model.dart delete mode 100644 modules/legacy/modules/settings/lib/src/features/disable_functions/presentation/state/disable_functions_view_state.dart delete mode 100644 modules/legacy/modules/settings/lib/src/features/disable_functions/presentation/state/disable_functions_view_state.freezed.dart create mode 100644 modules/legacy/modules/settings/test/features/disable_functions/disable_functions_controller_test.dart diff --git a/modules/legacy/modules/settings/lib/src/features/block_phone/presentation/providers/block_phone_controller.g.dart b/modules/legacy/modules/settings/lib/src/features/block_phone/presentation/providers/block_phone_controller.g.dart index 549de320..58f00751 100644 --- a/modules/legacy/modules/settings/lib/src/features/block_phone/presentation/providers/block_phone_controller.g.dart +++ b/modules/legacy/modules/settings/lib/src/features/block_phone/presentation/providers/block_phone_controller.g.dart @@ -34,7 +34,7 @@ final class BlockPhoneControllerProvider } String _$blockPhoneControllerHash() => - r'9992feb7188fa92f3b6fdb569bef89e750a81b0e'; + r'f508a90f1c08214b50ee03c8b328bad1b04d447e'; abstract class _$BlockPhoneController extends $AsyncNotifier { FutureOr build(); diff --git a/modules/legacy/modules/settings/lib/src/features/disable_functions/presentation/disable_functions_screen.dart b/modules/legacy/modules/settings/lib/src/features/disable_functions/presentation/disable_functions_screen.dart index 6a4978bc..250e4f6d 100644 --- a/modules/legacy/modules/settings/lib/src/features/disable_functions/presentation/disable_functions_screen.dart +++ b/modules/legacy/modules/settings/lib/src/features/disable_functions/presentation/disable_functions_screen.dart @@ -1,52 +1,45 @@ import 'package:design_system/design_system.dart'; -import 'package:legacy_theme/legacy_theme.dart'; import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:legacy_device_state/legacy_device_state.dart'; +import 'package:legacy_theme/legacy_theme.dart'; import 'package:legacy_ui/legacy_ui.dart'; +import 'package:settings/src/features/disable_functions/presentation/providers/disable_functions_controller.dart'; +import 'package:settings/src/features/disable_functions/presentation/providers/disable_functions_selection_provider.dart'; import 'package:sf_localizations/sf_localizations.dart'; +import 'package:sf_shared/sf_shared.dart'; import 'package:utils/utils.dart'; -import 'state/disable_functions_view_model.dart'; - class DisableFunctionsScreen extends ConsumerWidget { const DisableFunctionsScreen({super.key}); @override Widget build(BuildContext context, WidgetRef ref) { final primaryColor = context.sfColors.legacyPrimary; - final state = ref.watch(disableFunctionsViewModelProvider); - final vm = ref.read(disableFunctionsViewModelProvider.notifier); - ref.listen(disableFunctionsViewModelProvider.select((s) => s.errorEvent), ( - previous, - next, - ) { - if (next != null) { - showTopSnackbar( - context, - message: context.translate(I18n.errorDisableFunctions), - type: MessageType.error, - ); + ref.listen(disableFunctionsControllerProvider, (prev, next) async { + if (prev == null || !prev.isLoading || next.isLoading) return; + if (next.hasError) { + await next.showErrorOn(context); + return; } + ref.read(disableFunctionsSelectionProvider.notifier).clear(); + await showSuccessDialog(context, I18n.deviceUpdatedSuccess); }); - ref.listen(disableFunctionsViewModelProvider.select((s) => s.saveSuccess), ( - previous, - next, - ) { - if (next && !(previous ?? false)) { - showTopSnackbar( - context, - message: context.translate(I18n.disableFunctionsUpdated), - type: MessageType.success, - ); - } - }); + final device = ref.watch(selectedDeviceProvider).value; + final deviceDefault = ( + keyboard: device?.settings.keyboard ?? true, + location: device?.settings.location ?? true, + ); + final selection = + ref.watch(disableFunctionsSelectionProvider) ?? deviceDefault; + final isSaving = ref + .watch(disableFunctionsControllerProvider.select((s) => s.isLoading)); return LegacyPageLayout( title: context.translate(I18n.disableFunctions), - body: state.isLoading + body: device == null ? const Center(child: CircularProgressIndicator()) : Padding( padding: EdgeInsets.symmetric( @@ -58,16 +51,20 @@ class DisableFunctionsScreen extends ConsumerWidget { _FunctionCard( icon: Icons.dialpad, label: context.translate(I18n.disableFunctionsKeyboard), - value: state.keyboard, - onChanged: vm.toggleKeyboard, + value: selection.keyboard, + onChanged: (value) => ref + .read(disableFunctionsSelectionProvider.notifier) + .setKeyboard(current: deviceDefault, value: value), primaryColor: primaryColor, ), const SizedBox(height: 12), _FunctionCard( icon: Icons.gps_fixed, label: context.translate(I18n.disableFunctionsGps), - value: state.location, - onChanged: vm.toggleLocation, + value: selection.location, + onChanged: (value) => ref + .read(disableFunctionsSelectionProvider.notifier) + .setLocation(current: deviceDefault, value: value), primaryColor: primaryColor, ), ], @@ -76,13 +73,18 @@ class DisableFunctionsScreen extends ConsumerWidget { footer: Padding( padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 10), child: PrimaryButton( - onPressed: state.isSaving + onPressed: isSaving || device == null ? null : () async { if (!await guardDeviceCommand(context, ref)) return; - vm.save(); + if (!context.mounted) return; + ref.read(disableFunctionsControllerProvider.notifier).save( + device: device, + keyboard: selection.keyboard, + location: selection.location, + ); }, - text: state.isSaving ? '...' : context.translate(I18n.save), + text: isSaving ? '...' : context.translate(I18n.save), color: primaryColor, ), ), diff --git a/modules/legacy/modules/settings/lib/src/features/disable_functions/presentation/providers/disable_functions_controller.dart b/modules/legacy/modules/settings/lib/src/features/disable_functions/presentation/providers/disable_functions_controller.dart new file mode 100644 index 00000000..8535a41c --- /dev/null +++ b/modules/legacy/modules/settings/lib/src/features/disable_functions/presentation/providers/disable_functions_controller.dart @@ -0,0 +1,37 @@ +import 'dart:async'; + +import 'package:legacy_device_state/legacy_device_state.dart'; +import 'package:riverpod_annotation/riverpod_annotation.dart'; +import 'package:sf_shared/sf_shared.dart'; +import 'package:sf_tracking/sf_tracking.dart'; + +part 'disable_functions_controller.g.dart'; + +@riverpod +class DisableFunctionsController extends _$DisableFunctionsController { + @override + FutureOr build() {} + + Future save({ + required DeviceEntity device, + required bool keyboard, + required bool location, + }) async { + state = const AsyncLoading(); + state = await AsyncValue.guard(() async { + final updated = device.settings.copyWith( + keyboard: keyboard, + location: location, + ); + await ref + .read(deviceSettingsUpdateProvider) + .updateDeviceSettings(device: device, updatedSettings: updated); + ref.syncDeviceSettings(device, updated); + + final tracking = ref.read(sfTrackingProvider); + unawaited(tracking.legacySettingsDisableFunctionsChanged()); + unawaited(tracking.legacySettingsDisableFunctionsKeyboardToggled(keyboard)); + unawaited(tracking.legacySettingsDisableFunctionsGpsToggled(location)); + }); + } +} diff --git a/modules/legacy/modules/settings/lib/src/features/disable_functions/presentation/providers/disable_functions_controller.g.dart b/modules/legacy/modules/settings/lib/src/features/disable_functions/presentation/providers/disable_functions_controller.g.dart new file mode 100644 index 00000000..7346f7a0 --- /dev/null +++ b/modules/legacy/modules/settings/lib/src/features/disable_functions/presentation/providers/disable_functions_controller.g.dart @@ -0,0 +1,57 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'disable_functions_controller.dart'; + +// ************************************************************************** +// RiverpodGenerator +// ************************************************************************** + +// GENERATED CODE - DO NOT MODIFY BY HAND +// ignore_for_file: type=lint, type=warning + +@ProviderFor(DisableFunctionsController) +const disableFunctionsControllerProvider = + DisableFunctionsControllerProvider._(); + +final class DisableFunctionsControllerProvider + extends $AsyncNotifierProvider { + const DisableFunctionsControllerProvider._() + : super( + from: null, + argument: null, + retry: null, + name: r'disableFunctionsControllerProvider', + isAutoDispose: true, + dependencies: null, + $allTransitiveDependencies: null, + ); + + @override + String debugGetCreateSourceHash() => _$disableFunctionsControllerHash(); + + @$internal + @override + DisableFunctionsController create() => DisableFunctionsController(); +} + +String _$disableFunctionsControllerHash() => + r'bd6c220914255c4b775b8cf7e942f746a57b364b'; + +abstract class _$DisableFunctionsController extends $AsyncNotifier { + FutureOr build(); + @$mustCallSuper + @override + void runBuild() { + build(); + final ref = this.ref as $Ref, void>; + final element = + ref.element + as $ClassProviderElement< + AnyNotifier, void>, + AsyncValue, + Object?, + Object? + >; + element.handleValue(ref, null); + } +} diff --git a/modules/legacy/modules/settings/lib/src/features/disable_functions/presentation/providers/disable_functions_selection_provider.dart b/modules/legacy/modules/settings/lib/src/features/disable_functions/presentation/providers/disable_functions_selection_provider.dart new file mode 100644 index 00000000..9d185da1 --- /dev/null +++ b/modules/legacy/modules/settings/lib/src/features/disable_functions/presentation/providers/disable_functions_selection_provider.dart @@ -0,0 +1,29 @@ +import 'package:riverpod_annotation/riverpod_annotation.dart'; + +part 'disable_functions_selection_provider.g.dart'; + +typedef DisableFunctionsSelectionState = ({bool keyboard, bool location}); + +@riverpod +class DisableFunctionsSelection extends _$DisableFunctionsSelection { + @override + DisableFunctionsSelectionState? build() => null; + + void setKeyboard({ + required DisableFunctionsSelectionState current, + required bool value, + }) { + final base = state ?? current; + state = (keyboard: value, location: base.location); + } + + void setLocation({ + required DisableFunctionsSelectionState current, + required bool value, + }) { + final base = state ?? current; + state = (keyboard: base.keyboard, location: value); + } + + void clear() => state = null; +} diff --git a/modules/legacy/modules/settings/lib/src/features/disable_functions/presentation/providers/disable_functions_selection_provider.g.dart b/modules/legacy/modules/settings/lib/src/features/disable_functions/presentation/providers/disable_functions_selection_provider.g.dart new file mode 100644 index 00000000..97bb37ac --- /dev/null +++ b/modules/legacy/modules/settings/lib/src/features/disable_functions/presentation/providers/disable_functions_selection_provider.g.dart @@ -0,0 +1,79 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'disable_functions_selection_provider.dart'; + +// ************************************************************************** +// RiverpodGenerator +// ************************************************************************** + +// GENERATED CODE - DO NOT MODIFY BY HAND +// ignore_for_file: type=lint, type=warning + +@ProviderFor(DisableFunctionsSelection) +const disableFunctionsSelectionProvider = DisableFunctionsSelectionProvider._(); + +final class DisableFunctionsSelectionProvider + extends + $NotifierProvider< + DisableFunctionsSelection, + DisableFunctionsSelectionState? + > { + const DisableFunctionsSelectionProvider._() + : super( + from: null, + argument: null, + retry: null, + name: r'disableFunctionsSelectionProvider', + isAutoDispose: true, + dependencies: null, + $allTransitiveDependencies: null, + ); + + @override + String debugGetCreateSourceHash() => _$disableFunctionsSelectionHash(); + + @$internal + @override + DisableFunctionsSelection create() => DisableFunctionsSelection(); + + /// {@macro riverpod.override_with_value} + Override overrideWithValue(DisableFunctionsSelectionState? value) { + return $ProviderOverride( + origin: this, + providerOverride: $SyncValueProvider( + value, + ), + ); + } +} + +String _$disableFunctionsSelectionHash() => + r'c72b5a63fe035e800cb8beec2ed33658afd08782'; + +abstract class _$DisableFunctionsSelection + extends $Notifier { + DisableFunctionsSelectionState? build(); + @$mustCallSuper + @override + void runBuild() { + final created = build(); + final ref = + this.ref + as $Ref< + DisableFunctionsSelectionState?, + DisableFunctionsSelectionState? + >; + final element = + ref.element + as $ClassProviderElement< + AnyNotifier< + DisableFunctionsSelectionState?, + DisableFunctionsSelectionState? + >, + DisableFunctionsSelectionState?, + Object?, + Object? + >; + element.handleValue(ref, created); + } +} diff --git a/modules/legacy/modules/settings/lib/src/features/disable_functions/presentation/state/disable_functions_view_model.dart b/modules/legacy/modules/settings/lib/src/features/disable_functions/presentation/state/disable_functions_view_model.dart deleted file mode 100644 index 57ff6eca..00000000 --- a/modules/legacy/modules/settings/lib/src/features/disable_functions/presentation/state/disable_functions_view_model.dart +++ /dev/null @@ -1,84 +0,0 @@ -import 'dart:async'; - -import 'package:flutter_riverpod/flutter_riverpod.dart'; -import 'package:legacy_device_state/legacy_device_state.dart'; -import 'package:sf_shared/sf_shared.dart'; -import 'package:sf_tracking/sf_tracking.dart'; - -import 'disable_functions_view_state.dart'; - -final disableFunctionsViewModelProvider = - NotifierProvider.autoDispose< - DisableFunctionsViewModel, - DisableFunctionsViewState - >(DisableFunctionsViewModel.new); - -class DisableFunctionsViewModel extends Notifier { - late final DeviceSettingsUpdateDatasource _datasource; - late final SfTrackingRepository _tracking; - - @override - DisableFunctionsViewState build() { - _datasource = ref.read(deviceSettingsUpdateProvider); - _tracking = ref.read(sfTrackingProvider); - Future.microtask(_load); - return const DisableFunctionsViewState(); - } - - void _load() { - final device = ref.read(selectedDeviceProvider).value; - if (device == null) return; - - state = state.copyWith( - keyboard: device.settings.keyboard, - location: device.settings.location, - isLoading: false, - ); - } - - void toggleKeyboard(bool value) { - state = state.copyWith(keyboard: value); - } - - void toggleLocation(bool value) { - state = state.copyWith(location: value); - } - - Future save() async { - final device = ref.read(selectedDeviceProvider).value; - if (device == null) return; - - state = state.copyWith( - isSaving: true, - errorEvent: null, - saveSuccess: false, - ); - - try { - final updatedSettings = device.settings.copyWith( - keyboard: state.keyboard, - location: state.location, - ); - await _datasource.updateDeviceSettings( - device: device, - updatedSettings: updatedSettings, - ); - if (!ref.mounted) return; - ref.syncDeviceSettings(device, updatedSettings); - - unawaited(_tracking.legacySettingsDisableFunctionsChanged()); - unawaited( - _tracking.legacySettingsDisableFunctionsKeyboardToggled(state.keyboard), - ); - unawaited(_tracking.legacySettingsDisableFunctionsGpsToggled(state.location)); - - state = state.copyWith(isSaving: false, saveSuccess: true); - } catch (e) { - if (!ref.mounted) return; - state = state.copyWith( - isSaving: false, - errorEvent: DisableFunctionsErrorEvent.update, - ); - } - } -} diff --git a/modules/legacy/modules/settings/lib/src/features/disable_functions/presentation/state/disable_functions_view_state.dart b/modules/legacy/modules/settings/lib/src/features/disable_functions/presentation/state/disable_functions_view_state.dart deleted file mode 100644 index ab5889ef..00000000 --- a/modules/legacy/modules/settings/lib/src/features/disable_functions/presentation/state/disable_functions_view_state.dart +++ /dev/null @@ -1,17 +0,0 @@ -import 'package:freezed_annotation/freezed_annotation.dart'; - -part 'disable_functions_view_state.freezed.dart'; - -enum DisableFunctionsErrorEvent { update } - -@freezed -abstract class DisableFunctionsViewState with _$DisableFunctionsViewState { - const factory DisableFunctionsViewState({ - @Default(true) bool keyboard, - @Default(true) bool location, - @Default(true) bool isLoading, - @Default(false) bool isSaving, - DisableFunctionsErrorEvent? errorEvent, - @Default(false) bool saveSuccess, - }) = _DisableFunctionsViewState; -} diff --git a/modules/legacy/modules/settings/lib/src/features/disable_functions/presentation/state/disable_functions_view_state.freezed.dart b/modules/legacy/modules/settings/lib/src/features/disable_functions/presentation/state/disable_functions_view_state.freezed.dart deleted file mode 100644 index 37a368f3..00000000 --- a/modules/legacy/modules/settings/lib/src/features/disable_functions/presentation/state/disable_functions_view_state.freezed.dart +++ /dev/null @@ -1,286 +0,0 @@ -// GENERATED CODE - DO NOT MODIFY BY HAND -// coverage:ignore-file -// ignore_for_file: type=lint -// ignore_for_file: unused_element, deprecated_member_use, deprecated_member_use_from_same_package, use_function_type_syntax_for_parameters, unnecessary_const, avoid_init_to_null, invalid_override_different_default_values_named, prefer_expression_function_bodies, annotate_overrides, invalid_annotation_target, unnecessary_question_mark - -part of 'disable_functions_view_state.dart'; - -// ************************************************************************** -// FreezedGenerator -// ************************************************************************** - -// dart format off -T _$identity(T value) => value; -/// @nodoc -mixin _$DisableFunctionsViewState { - - bool get keyboard; bool get location; bool get isLoading; bool get isSaving; DisableFunctionsErrorEvent? get errorEvent; bool get saveSuccess; -/// Create a copy of DisableFunctionsViewState -/// with the given fields replaced by the non-null parameter values. -@JsonKey(includeFromJson: false, includeToJson: false) -@pragma('vm:prefer-inline') -$DisableFunctionsViewStateCopyWith get copyWith => _$DisableFunctionsViewStateCopyWithImpl(this as DisableFunctionsViewState, _$identity); - - - -@override -bool operator ==(Object other) { - return identical(this, other) || (other.runtimeType == runtimeType&&other is DisableFunctionsViewState&&(identical(other.keyboard, keyboard) || other.keyboard == keyboard)&&(identical(other.location, location) || other.location == location)&&(identical(other.isLoading, isLoading) || other.isLoading == isLoading)&&(identical(other.isSaving, isSaving) || other.isSaving == isSaving)&&(identical(other.errorEvent, errorEvent) || other.errorEvent == errorEvent)&&(identical(other.saveSuccess, saveSuccess) || other.saveSuccess == saveSuccess)); -} - - -@override -int get hashCode => Object.hash(runtimeType,keyboard,location,isLoading,isSaving,errorEvent,saveSuccess); - -@override -String toString() { - return 'DisableFunctionsViewState(keyboard: $keyboard, location: $location, isLoading: $isLoading, isSaving: $isSaving, errorEvent: $errorEvent, saveSuccess: $saveSuccess)'; -} - - -} - -/// @nodoc -abstract mixin class $DisableFunctionsViewStateCopyWith<$Res> { - factory $DisableFunctionsViewStateCopyWith(DisableFunctionsViewState value, $Res Function(DisableFunctionsViewState) _then) = _$DisableFunctionsViewStateCopyWithImpl; -@useResult -$Res call({ - bool keyboard, bool location, bool isLoading, bool isSaving, DisableFunctionsErrorEvent? errorEvent, bool saveSuccess -}); - - - - -} -/// @nodoc -class _$DisableFunctionsViewStateCopyWithImpl<$Res> - implements $DisableFunctionsViewStateCopyWith<$Res> { - _$DisableFunctionsViewStateCopyWithImpl(this._self, this._then); - - final DisableFunctionsViewState _self; - final $Res Function(DisableFunctionsViewState) _then; - -/// Create a copy of DisableFunctionsViewState -/// with the given fields replaced by the non-null parameter values. -@pragma('vm:prefer-inline') @override $Res call({Object? keyboard = null,Object? location = null,Object? isLoading = null,Object? isSaving = null,Object? errorEvent = freezed,Object? saveSuccess = null,}) { - return _then(_self.copyWith( -keyboard: null == keyboard ? _self.keyboard : keyboard // ignore: cast_nullable_to_non_nullable -as bool,location: null == location ? _self.location : location // ignore: cast_nullable_to_non_nullable -as bool,isLoading: null == isLoading ? _self.isLoading : isLoading // ignore: cast_nullable_to_non_nullable -as bool,isSaving: null == isSaving ? _self.isSaving : isSaving // ignore: cast_nullable_to_non_nullable -as bool,errorEvent: freezed == errorEvent ? _self.errorEvent : errorEvent // ignore: cast_nullable_to_non_nullable -as DisableFunctionsErrorEvent?,saveSuccess: null == saveSuccess ? _self.saveSuccess : saveSuccess // ignore: cast_nullable_to_non_nullable -as bool, - )); -} - -} - - -/// Adds pattern-matching-related methods to [DisableFunctionsViewState]. -extension DisableFunctionsViewStatePatterns on DisableFunctionsViewState { -/// A variant of `map` that fallback to returning `orElse`. -/// -/// It is equivalent to doing: -/// ```dart -/// switch (sealedClass) { -/// case final Subclass value: -/// return ...; -/// case _: -/// return orElse(); -/// } -/// ``` - -@optionalTypeArgs TResult maybeMap(TResult Function( _DisableFunctionsViewState value)? $default,{required TResult orElse(),}){ -final _that = this; -switch (_that) { -case _DisableFunctionsViewState() when $default != null: -return $default(_that);case _: - return orElse(); - -} -} -/// A `switch`-like method, using callbacks. -/// -/// Callbacks receives the raw object, upcasted. -/// It is equivalent to doing: -/// ```dart -/// switch (sealedClass) { -/// case final Subclass value: -/// return ...; -/// case final Subclass2 value: -/// return ...; -/// } -/// ``` - -@optionalTypeArgs TResult map(TResult Function( _DisableFunctionsViewState value) $default,){ -final _that = this; -switch (_that) { -case _DisableFunctionsViewState(): -return $default(_that);case _: - throw StateError('Unexpected subclass'); - -} -} -/// A variant of `map` that fallback to returning `null`. -/// -/// It is equivalent to doing: -/// ```dart -/// switch (sealedClass) { -/// case final Subclass value: -/// return ...; -/// case _: -/// return null; -/// } -/// ``` - -@optionalTypeArgs TResult? mapOrNull(TResult? Function( _DisableFunctionsViewState value)? $default,){ -final _that = this; -switch (_that) { -case _DisableFunctionsViewState() when $default != null: -return $default(_that);case _: - return null; - -} -} -/// A variant of `when` that fallback to an `orElse` callback. -/// -/// It is equivalent to doing: -/// ```dart -/// switch (sealedClass) { -/// case Subclass(:final field): -/// return ...; -/// case _: -/// return orElse(); -/// } -/// ``` - -@optionalTypeArgs TResult maybeWhen(TResult Function( bool keyboard, bool location, bool isLoading, bool isSaving, DisableFunctionsErrorEvent? errorEvent, bool saveSuccess)? $default,{required TResult orElse(),}) {final _that = this; -switch (_that) { -case _DisableFunctionsViewState() when $default != null: -return $default(_that.keyboard,_that.location,_that.isLoading,_that.isSaving,_that.errorEvent,_that.saveSuccess);case _: - return orElse(); - -} -} -/// A `switch`-like method, using callbacks. -/// -/// As opposed to `map`, this offers destructuring. -/// It is equivalent to doing: -/// ```dart -/// switch (sealedClass) { -/// case Subclass(:final field): -/// return ...; -/// case Subclass2(:final field2): -/// return ...; -/// } -/// ``` - -@optionalTypeArgs TResult when(TResult Function( bool keyboard, bool location, bool isLoading, bool isSaving, DisableFunctionsErrorEvent? errorEvent, bool saveSuccess) $default,) {final _that = this; -switch (_that) { -case _DisableFunctionsViewState(): -return $default(_that.keyboard,_that.location,_that.isLoading,_that.isSaving,_that.errorEvent,_that.saveSuccess);case _: - throw StateError('Unexpected subclass'); - -} -} -/// A variant of `when` that fallback to returning `null` -/// -/// It is equivalent to doing: -/// ```dart -/// switch (sealedClass) { -/// case Subclass(:final field): -/// return ...; -/// case _: -/// return null; -/// } -/// ``` - -@optionalTypeArgs TResult? whenOrNull(TResult? Function( bool keyboard, bool location, bool isLoading, bool isSaving, DisableFunctionsErrorEvent? errorEvent, bool saveSuccess)? $default,) {final _that = this; -switch (_that) { -case _DisableFunctionsViewState() when $default != null: -return $default(_that.keyboard,_that.location,_that.isLoading,_that.isSaving,_that.errorEvent,_that.saveSuccess);case _: - return null; - -} -} - -} - -/// @nodoc - - -class _DisableFunctionsViewState implements DisableFunctionsViewState { - const _DisableFunctionsViewState({this.keyboard = true, this.location = true, this.isLoading = true, this.isSaving = false, this.errorEvent, this.saveSuccess = false}); - - -@override@JsonKey() final bool keyboard; -@override@JsonKey() final bool location; -@override@JsonKey() final bool isLoading; -@override@JsonKey() final bool isSaving; -@override final DisableFunctionsErrorEvent? errorEvent; -@override@JsonKey() final bool saveSuccess; - -/// Create a copy of DisableFunctionsViewState -/// with the given fields replaced by the non-null parameter values. -@override @JsonKey(includeFromJson: false, includeToJson: false) -@pragma('vm:prefer-inline') -_$DisableFunctionsViewStateCopyWith<_DisableFunctionsViewState> get copyWith => __$DisableFunctionsViewStateCopyWithImpl<_DisableFunctionsViewState>(this, _$identity); - - - -@override -bool operator ==(Object other) { - return identical(this, other) || (other.runtimeType == runtimeType&&other is _DisableFunctionsViewState&&(identical(other.keyboard, keyboard) || other.keyboard == keyboard)&&(identical(other.location, location) || other.location == location)&&(identical(other.isLoading, isLoading) || other.isLoading == isLoading)&&(identical(other.isSaving, isSaving) || other.isSaving == isSaving)&&(identical(other.errorEvent, errorEvent) || other.errorEvent == errorEvent)&&(identical(other.saveSuccess, saveSuccess) || other.saveSuccess == saveSuccess)); -} - - -@override -int get hashCode => Object.hash(runtimeType,keyboard,location,isLoading,isSaving,errorEvent,saveSuccess); - -@override -String toString() { - return 'DisableFunctionsViewState(keyboard: $keyboard, location: $location, isLoading: $isLoading, isSaving: $isSaving, errorEvent: $errorEvent, saveSuccess: $saveSuccess)'; -} - - -} - -/// @nodoc -abstract mixin class _$DisableFunctionsViewStateCopyWith<$Res> implements $DisableFunctionsViewStateCopyWith<$Res> { - factory _$DisableFunctionsViewStateCopyWith(_DisableFunctionsViewState value, $Res Function(_DisableFunctionsViewState) _then) = __$DisableFunctionsViewStateCopyWithImpl; -@override @useResult -$Res call({ - bool keyboard, bool location, bool isLoading, bool isSaving, DisableFunctionsErrorEvent? errorEvent, bool saveSuccess -}); - - - - -} -/// @nodoc -class __$DisableFunctionsViewStateCopyWithImpl<$Res> - implements _$DisableFunctionsViewStateCopyWith<$Res> { - __$DisableFunctionsViewStateCopyWithImpl(this._self, this._then); - - final _DisableFunctionsViewState _self; - final $Res Function(_DisableFunctionsViewState) _then; - -/// Create a copy of DisableFunctionsViewState -/// with the given fields replaced by the non-null parameter values. -@override @pragma('vm:prefer-inline') $Res call({Object? keyboard = null,Object? location = null,Object? isLoading = null,Object? isSaving = null,Object? errorEvent = freezed,Object? saveSuccess = null,}) { - return _then(_DisableFunctionsViewState( -keyboard: null == keyboard ? _self.keyboard : keyboard // ignore: cast_nullable_to_non_nullable -as bool,location: null == location ? _self.location : location // ignore: cast_nullable_to_non_nullable -as bool,isLoading: null == isLoading ? _self.isLoading : isLoading // ignore: cast_nullable_to_non_nullable -as bool,isSaving: null == isSaving ? _self.isSaving : isSaving // ignore: cast_nullable_to_non_nullable -as bool,errorEvent: freezed == errorEvent ? _self.errorEvent : errorEvent // ignore: cast_nullable_to_non_nullable -as DisableFunctionsErrorEvent?,saveSuccess: null == saveSuccess ? _self.saveSuccess : saveSuccess // ignore: cast_nullable_to_non_nullable -as bool, - )); -} - - -} - -// dart format on diff --git a/modules/legacy/modules/settings/lib/src/features/sos_contacts/presentation/providers/sos_contacts_controller.g.dart b/modules/legacy/modules/settings/lib/src/features/sos_contacts/presentation/providers/sos_contacts_controller.g.dart index 7b413243..ddb92784 100644 --- a/modules/legacy/modules/settings/lib/src/features/sos_contacts/presentation/providers/sos_contacts_controller.g.dart +++ b/modules/legacy/modules/settings/lib/src/features/sos_contacts/presentation/providers/sos_contacts_controller.g.dart @@ -34,7 +34,7 @@ final class SosContactsControllerProvider } String _$sosContactsControllerHash() => - r'f832432d482c01547771414dff25a358baf72438'; + r'fae515bbb9183a099e03b0c7a301b419cb302f9f'; abstract class _$SosContactsController extends $AsyncNotifier { FutureOr build(); diff --git a/modules/legacy/modules/settings/test/features/disable_functions/disable_functions_controller_test.dart b/modules/legacy/modules/settings/test/features/disable_functions/disable_functions_controller_test.dart new file mode 100644 index 00000000..03275721 --- /dev/null +++ b/modules/legacy/modules/settings/test/features/disable_functions/disable_functions_controller_test.dart @@ -0,0 +1,91 @@ +import 'package:flutter_riverpod/flutter_riverpod.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:legacy_device_state/legacy_device_state.dart'; +import 'package:mocktail/mocktail.dart'; +import 'package:settings/src/features/disable_functions/presentation/providers/disable_functions_controller.dart'; +import 'package:sf_infrastructure/sf_infrastructure.dart'; +import 'package:sf_shared/sf_shared.dart'; +import 'package:sf_shared/testing.dart'; +import 'package:sf_tracking/sf_tracking.dart'; + +class MockDeviceSettingsUpdateDatasource extends Mock + implements DeviceSettingsUpdateDatasource {} + +const _device = DeviceEntity( + id: 'device-1', + identificator: 'imei-1', + carrierName: 'Watch', + settings: DeviceSettingsEntity(keyboard: true, location: true), +); + +void main() { + setUpAll(() { + registerFallbackValue(_device); + registerFallbackValue(const DeviceSettingsEntity()); + }); + + ProviderContainer buildContainer(DeviceSettingsUpdateDatasource ds) { + return makeContainer( + overrides: [ + deviceSettingsUpdateProvider.overrideWithValue(ds), + sfTrackingProvider.overrideWithValue( + SfTrackingRepository(clients: const []), + ), + ], + ); + } + + group('DisableFunctionsController.save', () { + test('persists flags and transitions to AsyncData on success', () async { + final ds = MockDeviceSettingsUpdateDatasource(); + when( + () => ds.updateDeviceSettings( + device: any(named: 'device'), + updatedSettings: any(named: 'updatedSettings'), + ), + ).thenAnswer((_) async {}); + + final container = buildContainer(ds); + addTearDown(container.dispose); + + await container + .read(disableFunctionsControllerProvider.notifier) + .save(device: _device, keyboard: false, location: true); + + expect( + container.read(disableFunctionsControllerProvider), + isA>(), + ); + + final captured = verify( + () => ds.updateDeviceSettings( + device: _device, + updatedSettings: captureAny(named: 'updatedSettings'), + ), + ).captured.single as DeviceSettingsEntity; + expect(captured.keyboard, isFalse); + expect(captured.location, isTrue); + }); + + test('exposes AsyncError when datasource fails', () async { + final ds = MockDeviceSettingsUpdateDatasource(); + when( + () => ds.updateDeviceSettings( + device: any(named: 'device'), + updatedSettings: any(named: 'updatedSettings'), + ), + ).thenThrow(const ApiException(message: 'boom', isNetworkError: true)); + + final container = buildContainer(ds); + addTearDown(container.dispose); + + await container + .read(disableFunctionsControllerProvider.notifier) + .save(device: _device, keyboard: false, location: false); + + final state = container.read(disableFunctionsControllerProvider); + expect(state, isA>()); + expect(state.error, isA()); + }); + }); +}