refactor(settings): migrate disable_functions to Riverpod

This commit is contained in:
2026-04-22 02:26:25 +02:00
parent 71ffc52993
commit 86642b9587
11 changed files with 332 additions and 424 deletions

View File

@@ -34,7 +34,7 @@ final class BlockPhoneControllerProvider
}
String _$blockPhoneControllerHash() =>
r'9992feb7188fa92f3b6fdb569bef89e750a81b0e';
r'f508a90f1c08214b50ee03c8b328bad1b04d447e';
abstract class _$BlockPhoneController extends $AsyncNotifier<void> {
FutureOr<void> build();

View File

@@ -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,
),
),

View File

@@ -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<void> build() {}
Future<void> 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));
});
}
}

View File

@@ -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<DisableFunctionsController, void> {
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<void> {
FutureOr<void> build();
@$mustCallSuper
@override
void runBuild() {
build();
final ref = this.ref as $Ref<AsyncValue<void>, void>;
final element =
ref.element
as $ClassProviderElement<
AnyNotifier<AsyncValue<void>, void>,
AsyncValue<void>,
Object?,
Object?
>;
element.handleValue(ref, null);
}
}

View File

@@ -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;
}

View File

@@ -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<DisableFunctionsSelectionState?>(
value,
),
);
}
}
String _$disableFunctionsSelectionHash() =>
r'c72b5a63fe035e800cb8beec2ed33658afd08782';
abstract class _$DisableFunctionsSelection
extends $Notifier<DisableFunctionsSelectionState?> {
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);
}
}

View File

@@ -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<DisableFunctionsViewState> {
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<void> 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,
);
}
}
}

View File

@@ -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;
}

View File

@@ -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>(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<DisableFunctionsViewState> get copyWith => _$DisableFunctionsViewStateCopyWithImpl<DisableFunctionsViewState>(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 extends Object?>(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 extends Object?>(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 extends Object?>(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 extends Object?>(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 extends Object?>(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 extends Object?>(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

View File

@@ -34,7 +34,7 @@ final class SosContactsControllerProvider
}
String _$sosContactsControllerHash() =>
r'f832432d482c01547771414dff25a358baf72438';
r'fae515bbb9183a099e03b0c7a301b419cb302f9f';
abstract class _$SosContactsController extends $AsyncNotifier<void> {
FutureOr<void> build();

View File

@@ -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<AsyncData<void>>(),
);
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<AsyncError<void>>());
expect(state.error, isA<ApiException>());
});
});
}