From 4b9bd5742c664df000599c1b82a2acf38b43c536 Mon Sep 17 00:00:00 2001 From: David Gomes <10091092+davidgomesdev@users.noreply.github.com> Date: Sun, 14 Jan 2024 16:43:25 +0000 Subject: [PATCH] refactor(app): remove redundant code --- app/lib/bloc/effects/breathing_bloc.dart | 55 +++++++------------ app/lib/bloc/effects/rainbow_bloc.dart | 33 ++++------- app/lib/bloc/effects/static_bloc.dart | 10 ++-- app/lib/main.dart | 22 -------- app/lib/model/led_effects.dart | 19 ++++--- .../effect_settings/breathing_settings.dart | 45 +++++---------- .../effect_settings/candle_settings.dart | 2 +- .../effect_settings/rainbow_settings.dart | 18 ++---- .../effect_settings/static_settings.dart | 2 +- 9 files changed, 67 insertions(+), 139 deletions(-) diff --git a/app/lib/bloc/effects/breathing_bloc.dart b/app/lib/bloc/effects/breathing_bloc.dart index 16f58d8..6babca3 100644 --- a/app/lib/bloc/effects/breathing_bloc.dart +++ b/app/lib/bloc/effects/breathing_bloc.dart @@ -6,47 +6,30 @@ import 'package:rusty_controller/model/led_effects.dart'; class BreathingBloc extends SpecificEffectBloc { BreathingBloc(super.effect) { - on( - (event, emit) => emit(state..color = event.currentColor)); - on( - (event, emit) => emit(state..timeToPeak = event.timeToPeak)); - on((event, emit) => emit(state..peak = event.peak)); - on((event, emit) { - if (event.breatheFromOff) { - emit(state..breatheFromOff = true); - } else { - emit(state - ..breatheFromOff = false - ..color = state.color.withValue(1.0)); - } - }); + on((event, emit) => emit(event.toEffect(state))); } } -abstract class BreathingEffectEvent {} +class BreathingEffectEvent { + HSVColor? color; + int? timeToPeak; + double? peak; + bool? breatheFromOff; -class BreathingColorEvent extends BreathingEffectEvent { - HSVColor currentColor; + BreathingEffectEvent( + {this.color, this.timeToPeak, this.peak, this.breatheFromOff}); - double get initialValue => currentColor.value; + BreathingLedEffect toEffect(BreathingLedEffect currentEffect) { + HSVColor color = this.color ?? currentEffect.color; - BreathingColorEvent(this.currentColor); -} - -class BreathingTimeEvent extends BreathingEffectEvent { - int timeToPeak; - - BreathingTimeEvent(this.timeToPeak); -} - -class BreathingPeakEvent extends BreathingEffectEvent { - double peak; + if (breatheFromOff == true) { + color = color.withValue(1.0); + } - BreathingPeakEvent(this.peak); -} - -class BreathingFromOffEvent extends BreathingEffectEvent { - bool breatheFromOff; - - BreathingFromOffEvent(this.breatheFromOff); + return BreathingLedEffect( + color: color, + timeToPeak: timeToPeak ?? currentEffect.timeToPeak, + peak: peak ?? currentEffect.peak, + breatheFromOff: breatheFromOff ?? currentEffect.breatheFromOff); + } } diff --git a/app/lib/bloc/effects/rainbow_bloc.dart b/app/lib/bloc/effects/rainbow_bloc.dart index 556568b..69a7314 100644 --- a/app/lib/bloc/effects/rainbow_bloc.dart +++ b/app/lib/bloc/effects/rainbow_bloc.dart @@ -4,30 +4,21 @@ import 'package:rusty_controller/model/led_effects.dart'; class RainbowBloc extends SpecificEffectBloc { RainbowBloc(super.effect) { - on( - (event, emit) => emit(state..saturation = event.saturation)); - on((event, emit) => emit(state..value = event.value)); - on( - (event, emit) => emit(state..timeToComplete = event.timeToComplete)); + on((event, emit) => emit(event.toEffect(state))); } } -abstract class RainbowEffectEvent {} +class RainbowEffectEvent { + double? saturation; + double? value; + double? timeToComplete; -class RainbowSaturationEvent extends RainbowEffectEvent { - double saturation; + RainbowEffectEvent({this.saturation, this.value, this.timeToComplete}); - RainbowSaturationEvent(this.saturation); -} - -class RainbowValueEvent extends RainbowEffectEvent { - double value; - - RainbowValueEvent(this.value); -} - -class RainbowTimeEvent extends RainbowEffectEvent { - double timeToComplete; - - RainbowTimeEvent(this.timeToComplete); + RainbowLedEffect toEffect(RainbowLedEffect currentEffect) { + return RainbowLedEffect( + saturation: saturation ?? currentEffect.saturation, + value: value ?? currentEffect.value, + timeToComplete: timeToComplete ?? currentEffect.timeToComplete); + } } diff --git a/app/lib/bloc/effects/static_bloc.dart b/app/lib/bloc/effects/static_bloc.dart index 8e8782f..6b2d35a 100644 --- a/app/lib/bloc/effects/static_bloc.dart +++ b/app/lib/bloc/effects/static_bloc.dart @@ -5,17 +5,15 @@ import 'package:rusty_controller/model/led_effects.dart'; class StaticBloc extends SpecificEffectBloc { StaticBloc(super.effect) { - on( - (event, emit) => emit(state..color = event.currentColor)); + on( + (event, emit) => emit(StaticLedEffect(color: event.currentColor))); } } -abstract class StaticEffectEvent {} - -class StaticColorEvent extends StaticEffectEvent { +class StaticEffectEvent { HSVColor currentColor; double get initialValue => currentColor.value; - StaticColorEvent(this.currentColor); + StaticEffectEvent(this.currentColor); } diff --git a/app/lib/main.dart b/app/lib/main.dart index 722b5d6..494d4e7 100644 --- a/app/lib/main.dart +++ b/app/lib/main.dart @@ -79,15 +79,6 @@ void setupDependencies() { defaultValue: defaultEffects[EffectType.breathing] as BreathingLedEffect); - if (savedBreathing.timeToPeak < minBreathingTime || - savedBreathing.timeToPeak > maxBreathingTime) { - savedBreathing.timeToPeak = maxBreathingTime; - } - - if (savedBreathing.peak < 0.0 || savedBreathing.peak > 1.0) { - savedBreathing.peak = 1.0; - } - return BreathingBloc(savedBreathing); }, ); @@ -104,19 +95,6 @@ void setupDependencies() { final savedRainbow = await storeService.get( defaultValue: defaultEffects[EffectType.rainbow] as RainbowLedEffect); - if (savedRainbow.timeToComplete < minRainbowTime || - savedRainbow.timeToComplete > maxRainbowTime) { - savedRainbow.timeToComplete = maxRainbowTime; - } - - if (savedRainbow.saturation < 0.0 || savedRainbow.saturation > 1.0) { - savedRainbow.saturation = 1.0; - } - - if (savedRainbow.value < 0.0 || savedRainbow.value > 1.0) { - savedRainbow.value = 1.0; - } - return RainbowBloc(savedRainbow); }, ); diff --git a/app/lib/model/led_effects.dart b/app/lib/model/led_effects.dart index 505a4ac..0867259 100644 --- a/app/lib/model/led_effects.dart +++ b/app/lib/model/led_effects.dart @@ -66,7 +66,7 @@ class StaticLedEffect extends LedEffect implements StorableObject { @override EffectType get type => EffectType.static; - HSVColor color; + final HSVColor color; StaticLedEffect({required this.color}); @@ -107,10 +107,10 @@ class BreathingLedEffect extends LedEffect implements StorableObject { @override EffectType get type => EffectType.breathing; - HSVColor color; - int timeToPeak; - double peak; - bool breatheFromOff; + final HSVColor color; + final int timeToPeak; + final double peak; + final bool breatheFromOff; BreathingLedEffect( {required this.color, @@ -203,7 +203,8 @@ class CandleLedEffect extends LedEffect implements StorableObject { Map toJson() => _$CandleLedEffectToJson(this); @override - List get props => [hue, saturation, minValue, maxValue, variability, interval]; + List get props => + [hue, saturation, minValue, maxValue, variability, interval]; } @JsonSerializable() @@ -211,9 +212,9 @@ class RainbowLedEffect extends LedEffect implements StorableObject { @override EffectType get type => EffectType.rainbow; - double saturation; - double value; - double timeToComplete; + final double saturation; + final double value; + final double timeToComplete; RainbowLedEffect( {required this.saturation, diff --git a/app/lib/widgets/effect_settings/breathing_settings.dart b/app/lib/widgets/effect_settings/breathing_settings.dart index abd1d18..6efcfd3 100644 --- a/app/lib/widgets/effect_settings/breathing_settings.dart +++ b/app/lib/widgets/effect_settings/breathing_settings.dart @@ -8,18 +8,13 @@ import 'package:rusty_controller/model/led_effects.dart'; import 'package:rusty_controller/widgets/effect_settings/common/labeled_slider.dart'; import 'package:rusty_controller/widgets/effect_settings/common/led_color_picker.dart'; -class BreathingSettings extends StatefulWidget { +class BreathingSettings extends StatelessWidget { const BreathingSettings({super.key}); - @override - State createState() => _BreathingSettingsState(); -} - -class _BreathingSettingsState extends State { - final bloc = serviceLocator.get(); - @override Widget build(BuildContext context) { + final bloc = serviceLocator.get(); + return BlocBuilder( bloc: bloc, builder: (ctx, effect) { @@ -28,20 +23,14 @@ class _BreathingSettingsState extends State { currentColor: effect.color, ignoreValue: effect.breatheFromOff, onColorPick: (color) { - setState(() { - if (!effect.breatheFromOff && isBrightnessOff(effect, color)) { - Get.closeAllSnackbars(); - Get.rawSnackbar( - message: 'You need to increase the brightness!', - ); - } else { - bloc.add(BreathingColorEvent(color)); - - if (color.value > effect.peak) { - effect.peak = color.value; - } - } - }); + if (!effect.breatheFromOff && isBrightnessOff(effect, color)) { + Get.closeAllSnackbars(); + Get.rawSnackbar( + message: 'You need to increase the brightness!', + ); + } else { + bloc.add(BreathingEffectEvent(color: color)); + } }, ), Column( @@ -49,9 +38,7 @@ class _BreathingSettingsState extends State { SwitchListTile.adaptive( value: effect.breatheFromOff, onChanged: (fromOff) { - setState(() { - bloc.add(BreathingFromOffEvent(fromOff)); - }); + bloc.add(BreathingEffectEvent(breatheFromOff: fromOff)); }, title: const Text("Breathe from off")), LabeledLogSlider( @@ -60,9 +47,7 @@ class _BreathingSettingsState extends State { min: minBreathingTime.toDouble(), max: maxBreathingTime.toDouble(), onChanged: (time) { - setState(() { - bloc.add(BreathingTimeEvent(time.round())); - }); + bloc.add(BreathingEffectEvent(timeToPeak: time.round())); }, ), LabeledSlider( @@ -73,9 +58,7 @@ class _BreathingSettingsState extends State { peak = effect.color.value; } - setState(() { - bloc.add(BreathingPeakEvent(peak)); - }); + bloc.add(BreathingEffectEvent(peak: peak)); }, ), ], diff --git a/app/lib/widgets/effect_settings/candle_settings.dart b/app/lib/widgets/effect_settings/candle_settings.dart index 673c7c0..440b448 100644 --- a/app/lib/widgets/effect_settings/candle_settings.dart +++ b/app/lib/widgets/effect_settings/candle_settings.dart @@ -24,7 +24,7 @@ class CandleSettings extends StatelessWidget { children: [ Flexible( child: Padding( - padding: EdgeInsets.only(bottom: 48.0), + padding: const EdgeInsets.only(bottom: 48.0), child: Stack( alignment: Alignment.center, children: [ diff --git a/app/lib/widgets/effect_settings/rainbow_settings.dart b/app/lib/widgets/effect_settings/rainbow_settings.dart index 38c4062..5f2eb12 100644 --- a/app/lib/widgets/effect_settings/rainbow_settings.dart +++ b/app/lib/widgets/effect_settings/rainbow_settings.dart @@ -6,18 +6,13 @@ import 'package:rusty_controller/main.dart'; import 'package:rusty_controller/model/led_effects.dart'; import 'package:rusty_controller/widgets/effect_settings/common/labeled_slider.dart'; -class RainbowSettings extends StatefulWidget { +class RainbowSettings extends StatelessWidget { const RainbowSettings({super.key}); - @override - State createState() => _RainbowSettingsState(); -} - -class _RainbowSettingsState extends State { - final bloc = serviceLocator.get(); - @override Widget build(BuildContext context) { + final bloc = serviceLocator.get(); + return BlocBuilder( bloc: bloc, builder: (ctx, effect) { @@ -30,7 +25,7 @@ class _RainbowSettingsState extends State { min: minRainbowTime, max: maxRainbowTime, onChanged: (time) { - setState(() => bloc.add(RainbowTimeEvent(time))); + bloc.add(RainbowEffectEvent(timeToComplete: time)); }, ), Row( @@ -40,8 +35,7 @@ class _RainbowSettingsState extends State { label: 'Saturation', value: effect.saturation, onChanged: (saturation) { - setState( - () => bloc.add(RainbowSaturationEvent(saturation))); + bloc.add(RainbowEffectEvent(saturation: saturation)); }, ), ), @@ -50,7 +44,7 @@ class _RainbowSettingsState extends State { label: 'Brightness', value: effect.value, onChanged: (value) { - setState(() => bloc.add(RainbowValueEvent(value))); + bloc.add(RainbowEffectEvent(value: value)); }, ), ), diff --git a/app/lib/widgets/effect_settings/static_settings.dart b/app/lib/widgets/effect_settings/static_settings.dart index 125200a..63cb2aa 100644 --- a/app/lib/widgets/effect_settings/static_settings.dart +++ b/app/lib/widgets/effect_settings/static_settings.dart @@ -16,7 +16,7 @@ class StaticSettings extends StatelessWidget { bloc: bloc, builder: (_, effect) => LedColorPicker( currentColor: effect.color, - onColorPick: (color) => bloc.add(StaticColorEvent(color)), + onColorPick: (color) => bloc.add(StaticEffectEvent(color)), ), ); }