Skip to content

Commit f92f024

Browse files
committed
refactor(app): remove redundant code
1 parent 8976638 commit f92f024

File tree

9 files changed

+67
-139
lines changed

9 files changed

+67
-139
lines changed

app/lib/bloc/effects/breathing_bloc.dart

Lines changed: 19 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,47 +6,30 @@ import 'package:rusty_controller/model/led_effects.dart';
66
class BreathingBloc
77
extends SpecificEffectBloc<BreathingEffectEvent, BreathingLedEffect> {
88
BreathingBloc(super.effect) {
9-
on<BreathingColorEvent>(
10-
(event, emit) => emit(state..color = event.currentColor));
11-
on<BreathingTimeEvent>(
12-
(event, emit) => emit(state..timeToPeak = event.timeToPeak));
13-
on<BreathingPeakEvent>((event, emit) => emit(state..peak = event.peak));
14-
on<BreathingFromOffEvent>((event, emit) {
15-
if (event.breatheFromOff) {
16-
emit(state..breatheFromOff = true);
17-
} else {
18-
emit(state
19-
..breatheFromOff = false
20-
..color = state.color.withValue(1.0));
21-
}
22-
});
9+
on<BreathingEffectEvent>((event, emit) => emit(event.toEffect(state)));
2310
}
2411
}
2512

26-
abstract class BreathingEffectEvent {}
13+
class BreathingEffectEvent {
14+
HSVColor? color;
15+
int? timeToPeak;
16+
double? peak;
17+
bool? breatheFromOff;
2718

28-
class BreathingColorEvent extends BreathingEffectEvent {
29-
HSVColor currentColor;
19+
BreathingEffectEvent(
20+
{this.color, this.timeToPeak, this.peak, this.breatheFromOff});
3021

31-
double get initialValue => currentColor.value;
22+
BreathingLedEffect toEffect(BreathingLedEffect currentEffect) {
23+
HSVColor color = this.color ?? currentEffect.color;
3224

33-
BreathingColorEvent(this.currentColor);
34-
}
35-
36-
class BreathingTimeEvent extends BreathingEffectEvent {
37-
int timeToPeak;
38-
39-
BreathingTimeEvent(this.timeToPeak);
40-
}
41-
42-
class BreathingPeakEvent extends BreathingEffectEvent {
43-
double peak;
25+
if (breatheFromOff == true) {
26+
color = color.withValue(1.0);
27+
}
4428

45-
BreathingPeakEvent(this.peak);
46-
}
47-
48-
class BreathingFromOffEvent extends BreathingEffectEvent {
49-
bool breatheFromOff;
50-
51-
BreathingFromOffEvent(this.breatheFromOff);
29+
return BreathingLedEffect(
30+
color: color,
31+
timeToPeak: timeToPeak ?? currentEffect.timeToPeak,
32+
peak: peak ?? currentEffect.peak,
33+
breatheFromOff: breatheFromOff ?? currentEffect.breatheFromOff);
34+
}
5235
}

app/lib/bloc/effects/rainbow_bloc.dart

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,21 @@ import 'package:rusty_controller/model/led_effects.dart';
44
class RainbowBloc
55
extends SpecificEffectBloc<RainbowEffectEvent, RainbowLedEffect> {
66
RainbowBloc(super.effect) {
7-
on<RainbowSaturationEvent>(
8-
(event, emit) => emit(state..saturation = event.saturation));
9-
on<RainbowValueEvent>((event, emit) => emit(state..value = event.value));
10-
on<RainbowTimeEvent>(
11-
(event, emit) => emit(state..timeToComplete = event.timeToComplete));
7+
on<RainbowEffectEvent>((event, emit) => emit(event.toEffect(state)));
128
}
139
}
1410

15-
abstract class RainbowEffectEvent {}
11+
class RainbowEffectEvent {
12+
double? saturation;
13+
double? value;
14+
double? timeToComplete;
1615

17-
class RainbowSaturationEvent extends RainbowEffectEvent {
18-
double saturation;
16+
RainbowEffectEvent({this.saturation, this.value, this.timeToComplete});
1917

20-
RainbowSaturationEvent(this.saturation);
21-
}
22-
23-
class RainbowValueEvent extends RainbowEffectEvent {
24-
double value;
25-
26-
RainbowValueEvent(this.value);
27-
}
28-
29-
class RainbowTimeEvent extends RainbowEffectEvent {
30-
double timeToComplete;
31-
32-
RainbowTimeEvent(this.timeToComplete);
18+
RainbowLedEffect toEffect(RainbowLedEffect currentEffect) {
19+
return RainbowLedEffect(
20+
saturation: saturation ?? currentEffect.saturation,
21+
value: value ?? currentEffect.value,
22+
timeToComplete: timeToComplete ?? currentEffect.timeToComplete);
23+
}
3324
}

app/lib/bloc/effects/static_bloc.dart

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,15 @@ import 'package:rusty_controller/model/led_effects.dart';
55
class StaticBloc
66
extends SpecificEffectBloc<StaticEffectEvent, StaticLedEffect> {
77
StaticBloc(super.effect) {
8-
on<StaticColorEvent>(
9-
(event, emit) => emit(state..color = event.currentColor));
8+
on<StaticEffectEvent>(
9+
(event, emit) => emit(StaticLedEffect(color: event.currentColor)));
1010
}
1111
}
1212

13-
abstract class StaticEffectEvent {}
14-
15-
class StaticColorEvent extends StaticEffectEvent {
13+
class StaticEffectEvent {
1614
HSVColor currentColor;
1715

1816
double get initialValue => currentColor.value;
1917

20-
StaticColorEvent(this.currentColor);
18+
StaticEffectEvent(this.currentColor);
2119
}

app/lib/main.dart

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,6 @@ void setupDependencies() {
7979
defaultValue:
8080
defaultEffects[EffectType.breathing] as BreathingLedEffect);
8181

82-
if (savedBreathing.timeToPeak < minBreathingTime ||
83-
savedBreathing.timeToPeak > maxBreathingTime) {
84-
savedBreathing.timeToPeak = maxBreathingTime;
85-
}
86-
87-
if (savedBreathing.peak < 0.0 || savedBreathing.peak > 1.0) {
88-
savedBreathing.peak = 1.0;
89-
}
90-
9182
return BreathingBloc(savedBreathing);
9283
},
9384
);
@@ -104,19 +95,6 @@ void setupDependencies() {
10495
final savedRainbow = await storeService.get<RainbowLedEffect>(
10596
defaultValue: defaultEffects[EffectType.rainbow] as RainbowLedEffect);
10697

107-
if (savedRainbow.timeToComplete < minRainbowTime ||
108-
savedRainbow.timeToComplete > maxRainbowTime) {
109-
savedRainbow.timeToComplete = maxRainbowTime;
110-
}
111-
112-
if (savedRainbow.saturation < 0.0 || savedRainbow.saturation > 1.0) {
113-
savedRainbow.saturation = 1.0;
114-
}
115-
116-
if (savedRainbow.value < 0.0 || savedRainbow.value > 1.0) {
117-
savedRainbow.value = 1.0;
118-
}
119-
12098
return RainbowBloc(savedRainbow);
12199
},
122100
);

app/lib/model/led_effects.dart

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class StaticLedEffect extends LedEffect implements StorableObject {
6666
@override
6767
EffectType get type => EffectType.static;
6868

69-
HSVColor color;
69+
final HSVColor color;
7070

7171
StaticLedEffect({required this.color});
7272

@@ -107,10 +107,10 @@ class BreathingLedEffect extends LedEffect implements StorableObject {
107107
@override
108108
EffectType get type => EffectType.breathing;
109109

110-
HSVColor color;
111-
int timeToPeak;
112-
double peak;
113-
bool breatheFromOff;
110+
final HSVColor color;
111+
final int timeToPeak;
112+
final double peak;
113+
final bool breatheFromOff;
114114

115115
BreathingLedEffect(
116116
{required this.color,
@@ -203,17 +203,18 @@ class CandleLedEffect extends LedEffect implements StorableObject {
203203
Map<String, dynamic> toJson() => _$CandleLedEffectToJson(this);
204204

205205
@override
206-
List<Object?> get props => [hue, saturation, minValue, maxValue, variability, interval];
206+
List<Object?> get props =>
207+
[hue, saturation, minValue, maxValue, variability, interval];
207208
}
208209

209210
@JsonSerializable()
210211
class RainbowLedEffect extends LedEffect implements StorableObject {
211212
@override
212213
EffectType get type => EffectType.rainbow;
213214

214-
double saturation;
215-
double value;
216-
double timeToComplete;
215+
final double saturation;
216+
final double value;
217+
final double timeToComplete;
217218

218219
RainbowLedEffect(
219220
{required this.saturation,

app/lib/widgets/effect_settings/breathing_settings.dart

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,13 @@ import 'package:rusty_controller/model/led_effects.dart';
88
import 'package:rusty_controller/widgets/effect_settings/common/labeled_slider.dart';
99
import 'package:rusty_controller/widgets/effect_settings/common/led_color_picker.dart';
1010

11-
class BreathingSettings extends StatefulWidget {
11+
class BreathingSettings extends StatelessWidget {
1212
const BreathingSettings({super.key});
1313

14-
@override
15-
State<BreathingSettings> createState() => _BreathingSettingsState();
16-
}
17-
18-
class _BreathingSettingsState extends State<BreathingSettings> {
19-
final bloc = serviceLocator.get<BreathingBloc>();
20-
2114
@override
2215
Widget build(BuildContext context) {
16+
final bloc = serviceLocator.get<BreathingBloc>();
17+
2318
return BlocBuilder<BreathingBloc, BreathingLedEffect>(
2419
bloc: bloc,
2520
builder: (ctx, effect) {
@@ -28,30 +23,22 @@ class _BreathingSettingsState extends State<BreathingSettings> {
2823
currentColor: effect.color,
2924
ignoreValue: effect.breatheFromOff,
3025
onColorPick: (color) {
31-
setState(() {
32-
if (!effect.breatheFromOff && isBrightnessOff(effect, color)) {
33-
Get.closeAllSnackbars();
34-
Get.rawSnackbar(
35-
message: 'You need to increase the brightness!',
36-
);
37-
} else {
38-
bloc.add(BreathingColorEvent(color));
39-
40-
if (color.value > effect.peak) {
41-
effect.peak = color.value;
42-
}
43-
}
44-
});
26+
if (!effect.breatheFromOff && isBrightnessOff(effect, color)) {
27+
Get.closeAllSnackbars();
28+
Get.rawSnackbar(
29+
message: 'You need to increase the brightness!',
30+
);
31+
} else {
32+
bloc.add(BreathingEffectEvent(color: color));
33+
}
4534
},
4635
),
4736
Column(
4837
children: [
4938
SwitchListTile.adaptive(
5039
value: effect.breatheFromOff,
5140
onChanged: (fromOff) {
52-
setState(() {
53-
bloc.add(BreathingFromOffEvent(fromOff));
54-
});
41+
bloc.add(BreathingEffectEvent(breatheFromOff: fromOff));
5542
},
5643
title: const Text("Breathe from off")),
5744
LabeledLogSlider(
@@ -60,9 +47,7 @@ class _BreathingSettingsState extends State<BreathingSettings> {
6047
min: minBreathingTime.toDouble(),
6148
max: maxBreathingTime.toDouble(),
6249
onChanged: (time) {
63-
setState(() {
64-
bloc.add(BreathingTimeEvent(time.round()));
65-
});
50+
bloc.add(BreathingEffectEvent(timeToPeak: time.round()));
6651
},
6752
),
6853
LabeledSlider(
@@ -73,9 +58,7 @@ class _BreathingSettingsState extends State<BreathingSettings> {
7358
peak = effect.color.value;
7459
}
7560

76-
setState(() {
77-
bloc.add(BreathingPeakEvent(peak));
78-
});
61+
bloc.add(BreathingEffectEvent(peak: peak));
7962
},
8063
),
8164
],

app/lib/widgets/effect_settings/candle_settings.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class CandleSettings extends StatelessWidget {
2424
children: [
2525
Flexible(
2626
child: Padding(
27-
padding: EdgeInsets.only(bottom: 48.0),
27+
padding: const EdgeInsets.only(bottom: 48.0),
2828
child: Stack(
2929
alignment: Alignment.center,
3030
children: [

app/lib/widgets/effect_settings/rainbow_settings.dart

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,13 @@ import 'package:rusty_controller/main.dart';
66
import 'package:rusty_controller/model/led_effects.dart';
77
import 'package:rusty_controller/widgets/effect_settings/common/labeled_slider.dart';
88

9-
class RainbowSettings extends StatefulWidget {
9+
class RainbowSettings extends StatelessWidget {
1010
const RainbowSettings({super.key});
1111

12-
@override
13-
State<RainbowSettings> createState() => _RainbowSettingsState();
14-
}
15-
16-
class _RainbowSettingsState extends State<RainbowSettings> {
17-
final bloc = serviceLocator.get<RainbowBloc>();
18-
1912
@override
2013
Widget build(BuildContext context) {
14+
final bloc = serviceLocator.get<RainbowBloc>();
15+
2116
return BlocBuilder<RainbowBloc, RainbowLedEffect>(
2217
bloc: bloc,
2318
builder: (ctx, effect) {
@@ -30,7 +25,7 @@ class _RainbowSettingsState extends State<RainbowSettings> {
3025
min: minRainbowTime,
3126
max: maxRainbowTime,
3227
onChanged: (time) {
33-
setState(() => bloc.add(RainbowTimeEvent(time)));
28+
bloc.add(RainbowEffectEvent(timeToComplete: time));
3429
},
3530
),
3631
Row(
@@ -40,8 +35,7 @@ class _RainbowSettingsState extends State<RainbowSettings> {
4035
label: 'Saturation',
4136
value: effect.saturation,
4237
onChanged: (saturation) {
43-
setState(
44-
() => bloc.add(RainbowSaturationEvent(saturation)));
38+
bloc.add(RainbowEffectEvent(saturation: saturation));
4539
},
4640
),
4741
),
@@ -50,7 +44,7 @@ class _RainbowSettingsState extends State<RainbowSettings> {
5044
label: 'Brightness',
5145
value: effect.value,
5246
onChanged: (value) {
53-
setState(() => bloc.add(RainbowValueEvent(value)));
47+
bloc.add(RainbowEffectEvent(value: value));
5448
},
5549
),
5650
),

app/lib/widgets/effect_settings/static_settings.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class StaticSettings extends StatelessWidget {
1616
bloc: bloc,
1717
builder: (_, effect) => LedColorPicker(
1818
currentColor: effect.color,
19-
onColorPick: (color) => bloc.add(StaticColorEvent(color)),
19+
onColorPick: (color) => bloc.add(StaticEffectEvent(color)),
2020
),
2121
);
2222
}

0 commit comments

Comments
 (0)