Skip to content

Commit

Permalink
refactor(app): remove redundant code
Browse files Browse the repository at this point in the history
  • Loading branch information
davidgomesdev committed Jan 14, 2024
1 parent 8976638 commit f92f024
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 139 deletions.
55 changes: 19 additions & 36 deletions app/lib/bloc/effects/breathing_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,30 @@ import 'package:rusty_controller/model/led_effects.dart';
class BreathingBloc
extends SpecificEffectBloc<BreathingEffectEvent, BreathingLedEffect> {
BreathingBloc(super.effect) {
on<BreathingColorEvent>(
(event, emit) => emit(state..color = event.currentColor));
on<BreathingTimeEvent>(
(event, emit) => emit(state..timeToPeak = event.timeToPeak));
on<BreathingPeakEvent>((event, emit) => emit(state..peak = event.peak));
on<BreathingFromOffEvent>((event, emit) {
if (event.breatheFromOff) {
emit(state..breatheFromOff = true);
} else {
emit(state
..breatheFromOff = false
..color = state.color.withValue(1.0));
}
});
on<BreathingEffectEvent>((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);
}
}
33 changes: 12 additions & 21 deletions app/lib/bloc/effects/rainbow_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,21 @@ import 'package:rusty_controller/model/led_effects.dart';
class RainbowBloc
extends SpecificEffectBloc<RainbowEffectEvent, RainbowLedEffect> {
RainbowBloc(super.effect) {
on<RainbowSaturationEvent>(
(event, emit) => emit(state..saturation = event.saturation));
on<RainbowValueEvent>((event, emit) => emit(state..value = event.value));
on<RainbowTimeEvent>(
(event, emit) => emit(state..timeToComplete = event.timeToComplete));
on<RainbowEffectEvent>((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);
}
}
10 changes: 4 additions & 6 deletions app/lib/bloc/effects/static_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,15 @@ import 'package:rusty_controller/model/led_effects.dart';
class StaticBloc
extends SpecificEffectBloc<StaticEffectEvent, StaticLedEffect> {
StaticBloc(super.effect) {
on<StaticColorEvent>(
(event, emit) => emit(state..color = event.currentColor));
on<StaticEffectEvent>(
(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);
}
22 changes: 0 additions & 22 deletions app/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
},
);
Expand All @@ -104,19 +95,6 @@ void setupDependencies() {
final savedRainbow = await storeService.get<RainbowLedEffect>(
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);
},
);
Expand Down
19 changes: 10 additions & 9 deletions app/lib/model/led_effects.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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});

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -203,17 +203,18 @@ class CandleLedEffect extends LedEffect implements StorableObject {
Map<String, dynamic> toJson() => _$CandleLedEffectToJson(this);

@override
List<Object?> get props => [hue, saturation, minValue, maxValue, variability, interval];
List<Object?> get props =>
[hue, saturation, minValue, maxValue, variability, interval];
}

@JsonSerializable()
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,
Expand Down
45 changes: 14 additions & 31 deletions app/lib/widgets/effect_settings/breathing_settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<BreathingSettings> createState() => _BreathingSettingsState();
}

class _BreathingSettingsState extends State<BreathingSettings> {
final bloc = serviceLocator.get<BreathingBloc>();

@override
Widget build(BuildContext context) {
final bloc = serviceLocator.get<BreathingBloc>();

return BlocBuilder<BreathingBloc, BreathingLedEffect>(
bloc: bloc,
builder: (ctx, effect) {
Expand All @@ -28,30 +23,22 @@ class _BreathingSettingsState extends State<BreathingSettings> {
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(
children: [
SwitchListTile.adaptive(
value: effect.breatheFromOff,
onChanged: (fromOff) {
setState(() {
bloc.add(BreathingFromOffEvent(fromOff));
});
bloc.add(BreathingEffectEvent(breatheFromOff: fromOff));
},
title: const Text("Breathe from off")),
LabeledLogSlider(
Expand All @@ -60,9 +47,7 @@ class _BreathingSettingsState extends State<BreathingSettings> {
min: minBreathingTime.toDouble(),
max: maxBreathingTime.toDouble(),
onChanged: (time) {
setState(() {
bloc.add(BreathingTimeEvent(time.round()));
});
bloc.add(BreathingEffectEvent(timeToPeak: time.round()));
},
),
LabeledSlider(
Expand All @@ -73,9 +58,7 @@ class _BreathingSettingsState extends State<BreathingSettings> {
peak = effect.color.value;
}

setState(() {
bloc.add(BreathingPeakEvent(peak));
});
bloc.add(BreathingEffectEvent(peak: peak));
},
),
],
Expand Down
2 changes: 1 addition & 1 deletion app/lib/widgets/effect_settings/candle_settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down
18 changes: 6 additions & 12 deletions app/lib/widgets/effect_settings/rainbow_settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<RainbowSettings> createState() => _RainbowSettingsState();
}

class _RainbowSettingsState extends State<RainbowSettings> {
final bloc = serviceLocator.get<RainbowBloc>();

@override
Widget build(BuildContext context) {
final bloc = serviceLocator.get<RainbowBloc>();

return BlocBuilder<RainbowBloc, RainbowLedEffect>(
bloc: bloc,
builder: (ctx, effect) {
Expand All @@ -30,7 +25,7 @@ class _RainbowSettingsState extends State<RainbowSettings> {
min: minRainbowTime,
max: maxRainbowTime,
onChanged: (time) {
setState(() => bloc.add(RainbowTimeEvent(time)));
bloc.add(RainbowEffectEvent(timeToComplete: time));
},
),
Row(
Expand All @@ -40,8 +35,7 @@ class _RainbowSettingsState extends State<RainbowSettings> {
label: 'Saturation',
value: effect.saturation,
onChanged: (saturation) {
setState(
() => bloc.add(RainbowSaturationEvent(saturation)));
bloc.add(RainbowEffectEvent(saturation: saturation));
},
),
),
Expand All @@ -50,7 +44,7 @@ class _RainbowSettingsState extends State<RainbowSettings> {
label: 'Brightness',
value: effect.value,
onChanged: (value) {
setState(() => bloc.add(RainbowValueEvent(value)));
bloc.add(RainbowEffectEvent(value: value));
},
),
),
Expand Down
2 changes: 1 addition & 1 deletion app/lib/widgets/effect_settings/static_settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
),
);
}
Expand Down

0 comments on commit f92f024

Please sign in to comment.