From cbaee6d5971525ba3516a2751d54790ca0c5280b Mon Sep 17 00:00:00 2001 From: JulianAlcala Date: Wed, 22 Apr 2026 21:26:18 +0200 Subject: [PATCH] refactor(device_management): migrate volume_control to Riverpod --- .../providers/volume_control_controller.dart | 57 ++++ .../volume_control_controller.g.dart | 56 +++ .../providers/volume_selection_provider.dart | 37 ++ .../volume_selection_provider.g.dart | 63 ++++ .../state/volume_control_view_model.dart | 122 ------- .../state/volume_control_view_state.dart | 20 -- .../volume_control_view_state.freezed.dart | 322 ------------------ .../presentation/volume_control_screen.dart | 175 ++++++---- .../volume_control_controller_test.dart | 100 ++++++ 9 files changed, 415 insertions(+), 537 deletions(-) create mode 100644 modules/legacy/modules/device_management/lib/src/features/volume_control/presentation/providers/volume_control_controller.dart create mode 100644 modules/legacy/modules/device_management/lib/src/features/volume_control/presentation/providers/volume_control_controller.g.dart create mode 100644 modules/legacy/modules/device_management/lib/src/features/volume_control/presentation/providers/volume_selection_provider.dart create mode 100644 modules/legacy/modules/device_management/lib/src/features/volume_control/presentation/providers/volume_selection_provider.g.dart delete mode 100644 modules/legacy/modules/device_management/lib/src/features/volume_control/presentation/state/volume_control_view_model.dart delete mode 100644 modules/legacy/modules/device_management/lib/src/features/volume_control/presentation/state/volume_control_view_state.dart delete mode 100644 modules/legacy/modules/device_management/lib/src/features/volume_control/presentation/state/volume_control_view_state.freezed.dart create mode 100644 modules/legacy/modules/device_management/test/features/volume_control/volume_control_controller_test.dart diff --git a/modules/legacy/modules/device_management/lib/src/features/volume_control/presentation/providers/volume_control_controller.dart b/modules/legacy/modules/device_management/lib/src/features/volume_control/presentation/providers/volume_control_controller.dart new file mode 100644 index 00000000..76438ed0 --- /dev/null +++ b/modules/legacy/modules/device_management/lib/src/features/volume_control/presentation/providers/volume_control_controller.dart @@ -0,0 +1,57 @@ +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 'volume_control_controller.g.dart'; + +@riverpod +class VolumeControlController extends _$VolumeControlController { + @override + FutureOr build() {} + + Future save({ + required DeviceEntity device, + required int media, + required int ringtone, + required int alarm, + }) async { + state = const AsyncLoading(); + state = await AsyncValue.guard(() async { + final updated = device.settings.copyWith( + volume: DeviceVolumeEntity( + media: media, + ringtone: ringtone, + alarm: alarm, + ), + ); + await ref + .read(deviceSettingsUpdateProvider) + .updateDeviceSettings(device: device, updatedSettings: updated); + ref.syncDeviceSettings(device, updated); + + final previous = device.settings.volume; + final tracking = ref.read(sfTrackingProvider); + if (previous.media != media) { + unawaited(tracking.legacyDeviceVolumeControlChanged( + type: 'media', + level: media, + )); + } + if (previous.ringtone != ringtone) { + unawaited(tracking.legacyDeviceVolumeControlChanged( + type: 'ringtone', + level: ringtone, + )); + } + if (previous.alarm != alarm) { + unawaited(tracking.legacyDeviceVolumeControlChanged( + type: 'alarm', + level: alarm, + )); + } + }); + } +} diff --git a/modules/legacy/modules/device_management/lib/src/features/volume_control/presentation/providers/volume_control_controller.g.dart b/modules/legacy/modules/device_management/lib/src/features/volume_control/presentation/providers/volume_control_controller.g.dart new file mode 100644 index 00000000..fdc86e5e --- /dev/null +++ b/modules/legacy/modules/device_management/lib/src/features/volume_control/presentation/providers/volume_control_controller.g.dart @@ -0,0 +1,56 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'volume_control_controller.dart'; + +// ************************************************************************** +// RiverpodGenerator +// ************************************************************************** + +// GENERATED CODE - DO NOT MODIFY BY HAND +// ignore_for_file: type=lint, type=warning + +@ProviderFor(VolumeControlController) +const volumeControlControllerProvider = VolumeControlControllerProvider._(); + +final class VolumeControlControllerProvider + extends $AsyncNotifierProvider { + const VolumeControlControllerProvider._() + : super( + from: null, + argument: null, + retry: null, + name: r'volumeControlControllerProvider', + isAutoDispose: true, + dependencies: null, + $allTransitiveDependencies: null, + ); + + @override + String debugGetCreateSourceHash() => _$volumeControlControllerHash(); + + @$internal + @override + VolumeControlController create() => VolumeControlController(); +} + +String _$volumeControlControllerHash() => + r'ec90f60855fa13113edd2f848d3f591a588aa8be'; + +abstract class _$VolumeControlController 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/device_management/lib/src/features/volume_control/presentation/providers/volume_selection_provider.dart b/modules/legacy/modules/device_management/lib/src/features/volume_control/presentation/providers/volume_selection_provider.dart new file mode 100644 index 00000000..088229b8 --- /dev/null +++ b/modules/legacy/modules/device_management/lib/src/features/volume_control/presentation/providers/volume_selection_provider.dart @@ -0,0 +1,37 @@ +import 'package:riverpod_annotation/riverpod_annotation.dart'; + +part 'volume_selection_provider.g.dart'; + +typedef VolumeSelectionState = ({int media, int ringtone, int alarm}); + +@riverpod +class VolumeSelection extends _$VolumeSelection { + @override + VolumeSelectionState? build() => null; + + void setMedia({ + required VolumeSelectionState current, + required int value, + }) { + final base = state ?? current; + state = (media: value, ringtone: base.ringtone, alarm: base.alarm); + } + + void setRingtone({ + required VolumeSelectionState current, + required int value, + }) { + final base = state ?? current; + state = (media: base.media, ringtone: value, alarm: base.alarm); + } + + void setAlarm({ + required VolumeSelectionState current, + required int value, + }) { + final base = state ?? current; + state = (media: base.media, ringtone: base.ringtone, alarm: value); + } + + void clear() => state = null; +} diff --git a/modules/legacy/modules/device_management/lib/src/features/volume_control/presentation/providers/volume_selection_provider.g.dart b/modules/legacy/modules/device_management/lib/src/features/volume_control/presentation/providers/volume_selection_provider.g.dart new file mode 100644 index 00000000..b9556fa9 --- /dev/null +++ b/modules/legacy/modules/device_management/lib/src/features/volume_control/presentation/providers/volume_selection_provider.g.dart @@ -0,0 +1,63 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'volume_selection_provider.dart'; + +// ************************************************************************** +// RiverpodGenerator +// ************************************************************************** + +// GENERATED CODE - DO NOT MODIFY BY HAND +// ignore_for_file: type=lint, type=warning + +@ProviderFor(VolumeSelection) +const volumeSelectionProvider = VolumeSelectionProvider._(); + +final class VolumeSelectionProvider + extends $NotifierProvider { + const VolumeSelectionProvider._() + : super( + from: null, + argument: null, + retry: null, + name: r'volumeSelectionProvider', + isAutoDispose: true, + dependencies: null, + $allTransitiveDependencies: null, + ); + + @override + String debugGetCreateSourceHash() => _$volumeSelectionHash(); + + @$internal + @override + VolumeSelection create() => VolumeSelection(); + + /// {@macro riverpod.override_with_value} + Override overrideWithValue(VolumeSelectionState? value) { + return $ProviderOverride( + origin: this, + providerOverride: $SyncValueProvider(value), + ); + } +} + +String _$volumeSelectionHash() => r'e28e4062bc284d8376a674b4aa38d1880b598e3f'; + +abstract class _$VolumeSelection extends $Notifier { + VolumeSelectionState? build(); + @$mustCallSuper + @override + void runBuild() { + final created = build(); + final ref = this.ref as $Ref; + final element = + ref.element + as $ClassProviderElement< + AnyNotifier, + VolumeSelectionState?, + Object?, + Object? + >; + element.handleValue(ref, created); + } +} diff --git a/modules/legacy/modules/device_management/lib/src/features/volume_control/presentation/state/volume_control_view_model.dart b/modules/legacy/modules/device_management/lib/src/features/volume_control/presentation/state/volume_control_view_model.dart deleted file mode 100644 index 3c0fcadb..00000000 --- a/modules/legacy/modules/device_management/lib/src/features/volume_control/presentation/state/volume_control_view_model.dart +++ /dev/null @@ -1,122 +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 'volume_control_view_state.dart'; - -final volumeControlViewModelProvider = - NotifierProvider.autoDispose< - VolumeControlViewModel, - VolumeControlViewState - >(VolumeControlViewModel.new); - -class VolumeControlViewModel extends Notifier { - late final DeviceSettingsUpdateDatasource _datasource; - late final SfTrackingRepository _tracking; - - @override - VolumeControlViewState build() { - _datasource = ref.read(deviceSettingsUpdateProvider); - _tracking = ref.read(sfTrackingProvider); - Future.microtask(() => _load()); - return const VolumeControlViewState(); - } - - Future _load() async { - try { - final device = ref.read(selectedDeviceProvider).value; - if (device == null) return; - - final volume = device.settings.volume; - final capVolume = device.capabilities?.volume; - final maxMedia = capVolume?.media ?? 10; - final maxRingtone = capVolume?.ringtone ?? 10; - final maxAlarm = capVolume?.alarm ?? 10; - - state = state.copyWith( - isLoading: false, - device: device, - media: volume.media.clamp(0, maxMedia), - ringtone: volume.ringtone.clamp(0, maxRingtone), - alarm: volume.alarm.clamp(0, maxAlarm), - maxMedia: maxMedia, - maxRingtone: maxRingtone, - maxAlarm: maxAlarm, - ); - } catch (e) { - if (!ref.mounted) return; - state = state.copyWith(isLoading: false, errorMessage: e.toString()); - } - } - - void setMedia(int value) => state = state.copyWith(media: value); - void setRingtone(int value) => state = state.copyWith(ringtone: value); - void setAlarm(int value) => state = state.copyWith(alarm: value); - - Future submit() async { - final device = state.device; - if (device == null) return; - - final previous = device.settings.volume; - final mediaChanged = previous.media != state.media; - final ringtoneChanged = previous.ringtone != state.ringtone; - final alarmChanged = previous.alarm != state.alarm; - - try { - state = state.copyWith( - isLoading: true, - isComplete: false, - errorMessage: '', - ); - - final updatedSettings = device.settings.copyWith( - volume: DeviceVolumeEntity( - media: state.media, - ringtone: state.ringtone, - alarm: state.alarm, - ), - ); - - await _datasource.updateDeviceSettings( - device: device, - updatedSettings: updatedSettings, - ); - - if (!ref.mounted) return; - ref.syncDeviceSettings(device, updatedSettings); - - if (mediaChanged) { - unawaited( - _tracking.legacyDeviceVolumeControlChanged( - type: 'media', - level: state.media, - ), - ); - } - if (ringtoneChanged) { - unawaited( - _tracking.legacyDeviceVolumeControlChanged( - type: 'ringtone', - level: state.ringtone, - ), - ); - } - if (alarmChanged) { - unawaited( - _tracking.legacyDeviceVolumeControlChanged( - type: 'alarm', - level: state.alarm, - ), - ); - } - - state = state.copyWith(isLoading: false, isComplete: true); - } catch (e) { - if (!ref.mounted) return; - state = state.copyWith(isLoading: false, errorMessage: e.toString()); - } - } -} diff --git a/modules/legacy/modules/device_management/lib/src/features/volume_control/presentation/state/volume_control_view_state.dart b/modules/legacy/modules/device_management/lib/src/features/volume_control/presentation/state/volume_control_view_state.dart deleted file mode 100644 index 1a019706..00000000 --- a/modules/legacy/modules/device_management/lib/src/features/volume_control/presentation/state/volume_control_view_state.dart +++ /dev/null @@ -1,20 +0,0 @@ -import 'package:freezed_annotation/freezed_annotation.dart'; -import 'package:sf_shared/sf_shared.dart'; - -part 'volume_control_view_state.freezed.dart'; - -@freezed -abstract class VolumeControlViewState with _$VolumeControlViewState { - const factory VolumeControlViewState({ - @Default(true) bool isLoading, - @Default(false) bool isComplete, - DeviceEntity? device, - @Default(5) int media, - @Default(5) int ringtone, - @Default(5) int alarm, - @Default(10) int maxMedia, - @Default(10) int maxRingtone, - @Default(10) int maxAlarm, - @Default('') String errorMessage, - }) = _VolumeControlViewState; -} diff --git a/modules/legacy/modules/device_management/lib/src/features/volume_control/presentation/state/volume_control_view_state.freezed.dart b/modules/legacy/modules/device_management/lib/src/features/volume_control/presentation/state/volume_control_view_state.freezed.dart deleted file mode 100644 index 5ac7a745..00000000 --- a/modules/legacy/modules/device_management/lib/src/features/volume_control/presentation/state/volume_control_view_state.freezed.dart +++ /dev/null @@ -1,322 +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 'volume_control_view_state.dart'; - -// ************************************************************************** -// FreezedGenerator -// ************************************************************************** - -// dart format off -T _$identity(T value) => value; -/// @nodoc -mixin _$VolumeControlViewState { - - bool get isLoading; bool get isComplete; DeviceEntity? get device; int get media; int get ringtone; int get alarm; int get maxMedia; int get maxRingtone; int get maxAlarm; String get errorMessage; -/// Create a copy of VolumeControlViewState -/// with the given fields replaced by the non-null parameter values. -@JsonKey(includeFromJson: false, includeToJson: false) -@pragma('vm:prefer-inline') -$VolumeControlViewStateCopyWith get copyWith => _$VolumeControlViewStateCopyWithImpl(this as VolumeControlViewState, _$identity); - - - -@override -bool operator ==(Object other) { - return identical(this, other) || (other.runtimeType == runtimeType&&other is VolumeControlViewState&&(identical(other.isLoading, isLoading) || other.isLoading == isLoading)&&(identical(other.isComplete, isComplete) || other.isComplete == isComplete)&&(identical(other.device, device) || other.device == device)&&(identical(other.media, media) || other.media == media)&&(identical(other.ringtone, ringtone) || other.ringtone == ringtone)&&(identical(other.alarm, alarm) || other.alarm == alarm)&&(identical(other.maxMedia, maxMedia) || other.maxMedia == maxMedia)&&(identical(other.maxRingtone, maxRingtone) || other.maxRingtone == maxRingtone)&&(identical(other.maxAlarm, maxAlarm) || other.maxAlarm == maxAlarm)&&(identical(other.errorMessage, errorMessage) || other.errorMessage == errorMessage)); -} - - -@override -int get hashCode => Object.hash(runtimeType,isLoading,isComplete,device,media,ringtone,alarm,maxMedia,maxRingtone,maxAlarm,errorMessage); - -@override -String toString() { - return 'VolumeControlViewState(isLoading: $isLoading, isComplete: $isComplete, device: $device, media: $media, ringtone: $ringtone, alarm: $alarm, maxMedia: $maxMedia, maxRingtone: $maxRingtone, maxAlarm: $maxAlarm, errorMessage: $errorMessage)'; -} - - -} - -/// @nodoc -abstract mixin class $VolumeControlViewStateCopyWith<$Res> { - factory $VolumeControlViewStateCopyWith(VolumeControlViewState value, $Res Function(VolumeControlViewState) _then) = _$VolumeControlViewStateCopyWithImpl; -@useResult -$Res call({ - bool isLoading, bool isComplete, DeviceEntity? device, int media, int ringtone, int alarm, int maxMedia, int maxRingtone, int maxAlarm, String errorMessage -}); - - -$DeviceEntityCopyWith<$Res>? get device; - -} -/// @nodoc -class _$VolumeControlViewStateCopyWithImpl<$Res> - implements $VolumeControlViewStateCopyWith<$Res> { - _$VolumeControlViewStateCopyWithImpl(this._self, this._then); - - final VolumeControlViewState _self; - final $Res Function(VolumeControlViewState) _then; - -/// Create a copy of VolumeControlViewState -/// with the given fields replaced by the non-null parameter values. -@pragma('vm:prefer-inline') @override $Res call({Object? isLoading = null,Object? isComplete = null,Object? device = freezed,Object? media = null,Object? ringtone = null,Object? alarm = null,Object? maxMedia = null,Object? maxRingtone = null,Object? maxAlarm = null,Object? errorMessage = null,}) { - return _then(_self.copyWith( -isLoading: null == isLoading ? _self.isLoading : isLoading // ignore: cast_nullable_to_non_nullable -as bool,isComplete: null == isComplete ? _self.isComplete : isComplete // ignore: cast_nullable_to_non_nullable -as bool,device: freezed == device ? _self.device : device // ignore: cast_nullable_to_non_nullable -as DeviceEntity?,media: null == media ? _self.media : media // ignore: cast_nullable_to_non_nullable -as int,ringtone: null == ringtone ? _self.ringtone : ringtone // ignore: cast_nullable_to_non_nullable -as int,alarm: null == alarm ? _self.alarm : alarm // ignore: cast_nullable_to_non_nullable -as int,maxMedia: null == maxMedia ? _self.maxMedia : maxMedia // ignore: cast_nullable_to_non_nullable -as int,maxRingtone: null == maxRingtone ? _self.maxRingtone : maxRingtone // ignore: cast_nullable_to_non_nullable -as int,maxAlarm: null == maxAlarm ? _self.maxAlarm : maxAlarm // ignore: cast_nullable_to_non_nullable -as int,errorMessage: null == errorMessage ? _self.errorMessage : errorMessage // ignore: cast_nullable_to_non_nullable -as String, - )); -} -/// Create a copy of VolumeControlViewState -/// with the given fields replaced by the non-null parameter values. -@override -@pragma('vm:prefer-inline') -$DeviceEntityCopyWith<$Res>? get device { - if (_self.device == null) { - return null; - } - - return $DeviceEntityCopyWith<$Res>(_self.device!, (value) { - return _then(_self.copyWith(device: value)); - }); -} -} - - -/// Adds pattern-matching-related methods to [VolumeControlViewState]. -extension VolumeControlViewStatePatterns on VolumeControlViewState { -/// 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( _VolumeControlViewState value)? $default,{required TResult orElse(),}){ -final _that = this; -switch (_that) { -case _VolumeControlViewState() 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( _VolumeControlViewState value) $default,){ -final _that = this; -switch (_that) { -case _VolumeControlViewState(): -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( _VolumeControlViewState value)? $default,){ -final _that = this; -switch (_that) { -case _VolumeControlViewState() 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 isLoading, bool isComplete, DeviceEntity? device, int media, int ringtone, int alarm, int maxMedia, int maxRingtone, int maxAlarm, String errorMessage)? $default,{required TResult orElse(),}) {final _that = this; -switch (_that) { -case _VolumeControlViewState() when $default != null: -return $default(_that.isLoading,_that.isComplete,_that.device,_that.media,_that.ringtone,_that.alarm,_that.maxMedia,_that.maxRingtone,_that.maxAlarm,_that.errorMessage);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 isLoading, bool isComplete, DeviceEntity? device, int media, int ringtone, int alarm, int maxMedia, int maxRingtone, int maxAlarm, String errorMessage) $default,) {final _that = this; -switch (_that) { -case _VolumeControlViewState(): -return $default(_that.isLoading,_that.isComplete,_that.device,_that.media,_that.ringtone,_that.alarm,_that.maxMedia,_that.maxRingtone,_that.maxAlarm,_that.errorMessage);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 isLoading, bool isComplete, DeviceEntity? device, int media, int ringtone, int alarm, int maxMedia, int maxRingtone, int maxAlarm, String errorMessage)? $default,) {final _that = this; -switch (_that) { -case _VolumeControlViewState() when $default != null: -return $default(_that.isLoading,_that.isComplete,_that.device,_that.media,_that.ringtone,_that.alarm,_that.maxMedia,_that.maxRingtone,_that.maxAlarm,_that.errorMessage);case _: - return null; - -} -} - -} - -/// @nodoc - - -class _VolumeControlViewState implements VolumeControlViewState { - const _VolumeControlViewState({this.isLoading = true, this.isComplete = false, this.device, this.media = 5, this.ringtone = 5, this.alarm = 5, this.maxMedia = 10, this.maxRingtone = 10, this.maxAlarm = 10, this.errorMessage = ''}); - - -@override@JsonKey() final bool isLoading; -@override@JsonKey() final bool isComplete; -@override final DeviceEntity? device; -@override@JsonKey() final int media; -@override@JsonKey() final int ringtone; -@override@JsonKey() final int alarm; -@override@JsonKey() final int maxMedia; -@override@JsonKey() final int maxRingtone; -@override@JsonKey() final int maxAlarm; -@override@JsonKey() final String errorMessage; - -/// Create a copy of VolumeControlViewState -/// with the given fields replaced by the non-null parameter values. -@override @JsonKey(includeFromJson: false, includeToJson: false) -@pragma('vm:prefer-inline') -_$VolumeControlViewStateCopyWith<_VolumeControlViewState> get copyWith => __$VolumeControlViewStateCopyWithImpl<_VolumeControlViewState>(this, _$identity); - - - -@override -bool operator ==(Object other) { - return identical(this, other) || (other.runtimeType == runtimeType&&other is _VolumeControlViewState&&(identical(other.isLoading, isLoading) || other.isLoading == isLoading)&&(identical(other.isComplete, isComplete) || other.isComplete == isComplete)&&(identical(other.device, device) || other.device == device)&&(identical(other.media, media) || other.media == media)&&(identical(other.ringtone, ringtone) || other.ringtone == ringtone)&&(identical(other.alarm, alarm) || other.alarm == alarm)&&(identical(other.maxMedia, maxMedia) || other.maxMedia == maxMedia)&&(identical(other.maxRingtone, maxRingtone) || other.maxRingtone == maxRingtone)&&(identical(other.maxAlarm, maxAlarm) || other.maxAlarm == maxAlarm)&&(identical(other.errorMessage, errorMessage) || other.errorMessage == errorMessage)); -} - - -@override -int get hashCode => Object.hash(runtimeType,isLoading,isComplete,device,media,ringtone,alarm,maxMedia,maxRingtone,maxAlarm,errorMessage); - -@override -String toString() { - return 'VolumeControlViewState(isLoading: $isLoading, isComplete: $isComplete, device: $device, media: $media, ringtone: $ringtone, alarm: $alarm, maxMedia: $maxMedia, maxRingtone: $maxRingtone, maxAlarm: $maxAlarm, errorMessage: $errorMessage)'; -} - - -} - -/// @nodoc -abstract mixin class _$VolumeControlViewStateCopyWith<$Res> implements $VolumeControlViewStateCopyWith<$Res> { - factory _$VolumeControlViewStateCopyWith(_VolumeControlViewState value, $Res Function(_VolumeControlViewState) _then) = __$VolumeControlViewStateCopyWithImpl; -@override @useResult -$Res call({ - bool isLoading, bool isComplete, DeviceEntity? device, int media, int ringtone, int alarm, int maxMedia, int maxRingtone, int maxAlarm, String errorMessage -}); - - -@override $DeviceEntityCopyWith<$Res>? get device; - -} -/// @nodoc -class __$VolumeControlViewStateCopyWithImpl<$Res> - implements _$VolumeControlViewStateCopyWith<$Res> { - __$VolumeControlViewStateCopyWithImpl(this._self, this._then); - - final _VolumeControlViewState _self; - final $Res Function(_VolumeControlViewState) _then; - -/// Create a copy of VolumeControlViewState -/// with the given fields replaced by the non-null parameter values. -@override @pragma('vm:prefer-inline') $Res call({Object? isLoading = null,Object? isComplete = null,Object? device = freezed,Object? media = null,Object? ringtone = null,Object? alarm = null,Object? maxMedia = null,Object? maxRingtone = null,Object? maxAlarm = null,Object? errorMessage = null,}) { - return _then(_VolumeControlViewState( -isLoading: null == isLoading ? _self.isLoading : isLoading // ignore: cast_nullable_to_non_nullable -as bool,isComplete: null == isComplete ? _self.isComplete : isComplete // ignore: cast_nullable_to_non_nullable -as bool,device: freezed == device ? _self.device : device // ignore: cast_nullable_to_non_nullable -as DeviceEntity?,media: null == media ? _self.media : media // ignore: cast_nullable_to_non_nullable -as int,ringtone: null == ringtone ? _self.ringtone : ringtone // ignore: cast_nullable_to_non_nullable -as int,alarm: null == alarm ? _self.alarm : alarm // ignore: cast_nullable_to_non_nullable -as int,maxMedia: null == maxMedia ? _self.maxMedia : maxMedia // ignore: cast_nullable_to_non_nullable -as int,maxRingtone: null == maxRingtone ? _self.maxRingtone : maxRingtone // ignore: cast_nullable_to_non_nullable -as int,maxAlarm: null == maxAlarm ? _self.maxAlarm : maxAlarm // ignore: cast_nullable_to_non_nullable -as int,errorMessage: null == errorMessage ? _self.errorMessage : errorMessage // ignore: cast_nullable_to_non_nullable -as String, - )); -} - -/// Create a copy of VolumeControlViewState -/// with the given fields replaced by the non-null parameter values. -@override -@pragma('vm:prefer-inline') -$DeviceEntityCopyWith<$Res>? get device { - if (_self.device == null) { - return null; - } - - return $DeviceEntityCopyWith<$Res>(_self.device!, (value) { - return _then(_self.copyWith(device: value)); - }); -} -} - -// dart format on diff --git a/modules/legacy/modules/device_management/lib/src/features/volume_control/presentation/volume_control_screen.dart b/modules/legacy/modules/device_management/lib/src/features/volume_control/presentation/volume_control_screen.dart index f3f794ec..8c120283 100644 --- a/modules/legacy/modules/device_management/lib/src/features/volume_control/presentation/volume_control_screen.dart +++ b/modules/legacy/modules/device_management/lib/src/features/volume_control/presentation/volume_control_screen.dart @@ -1,96 +1,120 @@ import 'package:design_system/design_system.dart'; -import 'package:legacy_theme/legacy_theme.dart'; +import 'package:device_management/src/features/volume_control/presentation/providers/volume_control_controller.dart'; +import 'package:device_management/src/features/volume_control/presentation/providers/volume_selection_provider.dart'; +import 'package:device_management/src/features/volume_control/presentation/widgets/volume_thumb_shape.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:sf_localizations/sf_localizations.dart'; - -import 'state/volume_control_view_model.dart'; -import 'widgets/volume_thumb_shape.dart'; +import 'package:sf_shared/sf_shared.dart'; class VolumeControlScreen extends ConsumerWidget { const VolumeControlScreen({super.key}); @override Widget build(BuildContext context, WidgetRef ref) { - final vm = ref.read(volumeControlViewModelProvider.notifier); - final state = ref.watch(volumeControlViewModelProvider); - - ref.listen(volumeControlViewModelProvider.select((s) => s.errorMessage), ( - _, - msg, - ) { - if (msg.isNotEmpty) { - showTopSnackbar(context, message: msg, type: MessageType.error); - } - }); - - ref.listen(volumeControlViewModelProvider.select((s) => s.isComplete), ( - _, - done, - ) { - if (done) Navigator.pop(context); - }); - final primaryColor = context.sfColors.legacyPrimary; + final device = ref.watch(selectedDeviceProvider).value; + + ref.listen(volumeControlControllerProvider, (prev, next) async { + if (prev == null || !prev.isLoading || next.isLoading) return; + if (next.hasError) { + await next.showErrorOn(context); + return; + } + ref.read(volumeSelectionProvider.notifier).clear(); + await showSuccessDialog(context, I18n.deviceUpdatedSuccess); + }); + + if (device == null) { + return LegacyPageLayout( + title: context.translate(I18n.volumeControl), + body: const Center(child: CircularProgressIndicator()), + ); + } + + final capVolume = device.capabilities?.volume; + final maxMedia = capVolume?.media ?? 10; + final maxRingtone = capVolume?.ringtone ?? 10; + final maxAlarm = capVolume?.alarm ?? 10; + + final deviceDefault = ( + media: device.settings.volume.media.clamp(0, maxMedia), + ringtone: device.settings.volume.ringtone.clamp(0, maxRingtone), + alarm: device.settings.volume.alarm.clamp(0, maxAlarm), + ); + final selection = ref.watch(volumeSelectionProvider) ?? deviceDefault; + final isSaving = + ref.watch(volumeControlControllerProvider.select((s) => s.isLoading)); return LegacyPageLayout( title: context.translate(I18n.volumeControl), - body: state.isLoading - ? const Center(child: CircularProgressIndicator()) - : SingleChildScrollView( - padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12), - child: Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - _VolumeCard( - label: context.translate(I18n.volumeMedia), - value: state.media, - max: state.maxMedia, - color: primaryColor, - onChanged: vm.setMedia, - ), - const SizedBox(height: 12), - _VolumeCard( - label: context.translate(I18n.volumeRingtone), - value: state.ringtone, - max: state.maxRingtone, - color: primaryColor, - onChanged: vm.setRingtone, - ), - const SizedBox(height: 12), - _VolumeCard( - label: context.translate(I18n.volumeAlarm), - value: state.alarm, - max: state.maxAlarm, - color: primaryColor, - onChanged: vm.setAlarm, - ), - const SizedBox(height: 20), - Text( - context.translate(I18n.volumeHint), - style: TextStyle( - fontSize: 13, - color: Theme.of(context).colorScheme.onSurfaceVariant, - height: 1.4, - ), - ), - ], + body: SingleChildScrollView( + padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + _VolumeCard( + label: context.translate(I18n.volumeMedia), + value: selection.media, + max: maxMedia, + color: primaryColor, + onChanged: (v) => ref + .read(volumeSelectionProvider.notifier) + .setMedia(current: deviceDefault, value: v), + ), + const SizedBox(height: 12), + _VolumeCard( + label: context.translate(I18n.volumeRingtone), + value: selection.ringtone, + max: maxRingtone, + color: primaryColor, + onChanged: (v) => ref + .read(volumeSelectionProvider.notifier) + .setRingtone(current: deviceDefault, value: v), + ), + const SizedBox(height: 12), + _VolumeCard( + label: context.translate(I18n.volumeAlarm), + value: selection.alarm, + max: maxAlarm, + color: primaryColor, + onChanged: (v) => ref + .read(volumeSelectionProvider.notifier) + .setAlarm(current: deviceDefault, value: v), + ), + const SizedBox(height: 20), + Text( + context.translate(I18n.volumeHint), + style: TextStyle( + fontSize: 13, + color: Theme.of(context).colorScheme.onSurfaceVariant, + height: 1.4, ), ), + ], + ), + ), footer: Padding( padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 10), - child: state.isLoading - ? const Center(child: CircularProgressIndicator()) - : PrimaryButton( - onPressed: () async { + child: PrimaryButton( + onPressed: isSaving + ? null + : () async { if (!await guardDeviceCommand(context, ref)) return; - vm.submit(); + if (!context.mounted) return; + ref.read(volumeControlControllerProvider.notifier).save( + device: device, + media: selection.media, + ringtone: selection.ringtone, + alarm: selection.alarm, + ); }, - text: context.translate(I18n.volumeSend), - color: primaryColor, - ), + text: context.translate(I18n.volumeSend), + color: primaryColor, + ), ), ); } @@ -136,14 +160,19 @@ class _VolumeCard extends StatelessWidget { const SizedBox(height: 4), Row( children: [ - Icon(Icons.volume_up, color: Theme.of(context).colorScheme.outline, size: 22), + Icon( + Icons.volume_up, + color: Theme.of(context).colorScheme.outline, + size: 22, + ), Expanded( child: SliderTheme( data: SliderThemeData( activeTrackColor: color, thumbColor: Colors.white, thumbShape: VolumeThumbShape(color: color), - inactiveTrackColor: Theme.of(context).colorScheme.outlineVariant, + inactiveTrackColor: + Theme.of(context).colorScheme.outlineVariant, overlayColor: color.withValues(alpha: 0.1), trackHeight: 6, ), diff --git a/modules/legacy/modules/device_management/test/features/volume_control/volume_control_controller_test.dart b/modules/legacy/modules/device_management/test/features/volume_control/volume_control_controller_test.dart new file mode 100644 index 00000000..c807c7d0 --- /dev/null +++ b/modules/legacy/modules/device_management/test/features/volume_control/volume_control_controller_test.dart @@ -0,0 +1,100 @@ +import 'package:device_management/src/features/volume_control/presentation/providers/volume_control_controller.dart'; +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: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( + volume: DeviceVolumeEntity(media: 3, ringtone: 5, alarm: 7), + ), +); + +void main() { + setUpAll(() { + registerFallbackValue(_device); + registerFallbackValue(const DeviceSettingsEntity()); + }); + + ProviderContainer buildContainer(DeviceSettingsUpdateDatasource ds) { + return makeContainer( + overrides: [ + deviceSettingsUpdateProvider.overrideWithValue(ds), + sfTrackingProvider.overrideWithValue( + SfTrackingRepository(clients: const []), + ), + ], + ); + } + + group('VolumeControlController.save', () { + test('persists volumes 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(volumeControlControllerProvider.notifier).save( + device: _device, + media: 6, + ringtone: 8, + alarm: 2, + ); + + expect( + container.read(volumeControlControllerProvider), + isA>(), + ); + final captured = verify( + () => ds.updateDeviceSettings( + device: _device, + updatedSettings: captureAny(named: 'updatedSettings'), + ), + ).captured.single as DeviceSettingsEntity; + expect(captured.volume.media, 6); + expect(captured.volume.ringtone, 8); + expect(captured.volume.alarm, 2); + }); + + 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(volumeControlControllerProvider.notifier).save( + device: _device, + media: 0, + ringtone: 0, + alarm: 0, + ); + + expect( + container.read(volumeControlControllerProvider), + isA>(), + ); + }); + }); +}