Merge branch '42-refactoring-enums-anstatt-strings' into 'main'

Enums instead of Strings

Closes #42

See merge request Crondung/hsma_cpd!31
main
Julian Gegner 2023-03-05 10:22:30 +00:00
commit d2564fedf4
5 changed files with 49 additions and 33 deletions

View File

@ -4,12 +4,17 @@ import 'package:smoke_cess_app/models/relapse.dart';
import 'package:smoke_cess_app/models/sleep.dart'; import 'package:smoke_cess_app/models/sleep.dart';
import '../globals.dart' as globals; import '../globals.dart' as globals;
enum SleepTimes {
wokeUpAt,
sleptAt,
}
class InputProvider extends ChangeNotifier { class InputProvider extends ChangeNotifier {
double _sliderValue = 50; double _sliderValue = 50;
final TextEditingController _textController = TextEditingController(text: ''); final TextEditingController _textController = TextEditingController(text: '');
final Map<String, TimeOfDay> _times = { final Map<SleepTimes, TimeOfDay> _times = {
'wokeUpAt': const TimeOfDay(hour: 8, minute: 0), SleepTimes.wokeUpAt: const TimeOfDay(hour: 8, minute: 0),
'sleptAt': const TimeOfDay(hour: 22, minute: 0), SleepTimes.sleptAt: const TimeOfDay(hour: 22, minute: 0),
}; };
String relapseCategory = ''; String relapseCategory = '';
@ -21,11 +26,11 @@ class InputProvider extends ChangeNotifier {
notifyListeners(); notifyListeners();
} }
TimeOfDay getTimeEntry(String key) { TimeOfDay getTimeEntry(SleepTimes key) {
return _times[key] ?? const TimeOfDay(hour: 12, minute: 0); return _times[key] ?? const TimeOfDay(hour: 12, minute: 0);
} }
void setTime(String key, TimeOfDay time) { void setTime(SleepTimes key, TimeOfDay time) {
_times[key] = time; _times[key] = time;
notifyListeners(); notifyListeners();
} }
@ -33,8 +38,8 @@ class InputProvider extends ChangeNotifier {
void _resetFields() { void _resetFields() {
_sliderValue = 50; _sliderValue = 50;
_textController.text = ''; _textController.text = '';
setTime('wokeUpAt', const TimeOfDay(hour: 8, minute: 0)); setTime(SleepTimes.wokeUpAt, const TimeOfDay(hour: 8, minute: 0));
setTime('sleptAt', const TimeOfDay(hour: 22, minute: 0)); setTime(SleepTimes.sleptAt, const TimeOfDay(hour: 22, minute: 0));
notifyListeners(); notifyListeners();
} }
@ -52,7 +57,7 @@ class InputProvider extends ChangeNotifier {
return globals.databaseService.addRelapse(relapse); return globals.databaseService.addRelapse(relapse);
} }
Future<int> saveSleep(String wokeUpKey, String sleptKey) { Future<int> saveSleep(SleepTimes wokeUpKey, SleepTimes sleptKey) {
Sleep sleep = Sleep(_sliderValue.toInt(), _textController.text, Sleep sleep = Sleep(_sliderValue.toInt(), _textController.text,
DateTime.now(), getTimeEntry(sleptKey), getTimeEntry(wokeUpKey)); DateTime.now(), getTimeEntry(sleptKey), getTimeEntry(wokeUpKey));
_resetFields(); _resetFields();

View File

@ -5,6 +5,13 @@ import 'package:smoke_cess_app/providers/audio_provider.dart';
import 'package:smoke_cess_app/providers/timer_provider.dart'; import 'package:smoke_cess_app/providers/timer_provider.dart';
import '../globals.dart' as globals; import '../globals.dart' as globals;
enum WorkoutPhases {
warmUp,
highIntensity,
lowIntensity,
coolDown,
}
class WorkoutProvider extends ChangeNotifier { class WorkoutProvider extends ChangeNotifier {
final TimerProvider timerProvider; final TimerProvider timerProvider;
final AudioProvider audioProvider; final AudioProvider audioProvider;
@ -15,21 +22,21 @@ class WorkoutProvider extends ChangeNotifier {
int motivationAfter = 50; int motivationAfter = 50;
int _workoutPhaseIndex = 0; int _workoutPhaseIndex = 0;
final List<String> _workoutPhases = [ final List<WorkoutPhases> _workoutPhases = [
'Warm-Up', WorkoutPhases.warmUp,
'High Intensity', WorkoutPhases.highIntensity,
'Low Intensity', WorkoutPhases.lowIntensity,
'High Intensity', WorkoutPhases.highIntensity,
'Low Intensity', WorkoutPhases.lowIntensity,
'High Intensity', WorkoutPhases.highIntensity,
'Low Intensity', WorkoutPhases.lowIntensity,
'High Intensity', WorkoutPhases.highIntensity,
'Cool-down' WorkoutPhases.coolDown,
]; ];
WorkoutProvider(this.timerProvider, this.audioProvider); WorkoutProvider(this.timerProvider, this.audioProvider);
String get currentPhase => _workoutPhases[_workoutPhaseIndex]; WorkoutPhases get currentPhase => _workoutPhases[_workoutPhaseIndex];
Duration get currentPhaseDuration => Duration get currentPhaseDuration =>
_workoutPhaseSettings[currentPhase]!['duration']; _workoutPhaseSettings[currentPhase]!['duration'];
bool get isPhaseComplete => bool get isPhaseComplete =>
@ -37,6 +44,7 @@ class WorkoutProvider extends ChangeNotifier {
Color get currentPhaseColor => _workoutPhaseSettings[currentPhase]!['color']; Color get currentPhaseColor => _workoutPhaseSettings[currentPhase]!['color'];
AssetSource get currentPhaseSource => AssetSource get currentPhaseSource =>
_workoutPhaseSettings[currentPhase]!['source']; _workoutPhaseSettings[currentPhase]!['source'];
String get currentPhaseTitle => _workoutPhaseSettings[currentPhase]!['title'];
void nextPhase() { void nextPhase() {
if (_workoutPhaseIndex < _workoutPhases.length - 1) { if (_workoutPhaseIndex < _workoutPhases.length - 1) {
@ -89,23 +97,27 @@ class WorkoutProvider extends ChangeNotifier {
} }
} }
Map<String, Map<String, dynamic>> _workoutPhaseSettings = { Map<WorkoutPhases, Map<String, dynamic>> _workoutPhaseSettings = {
'Warm-Up': { WorkoutPhases.warmUp: {
'title': 'Warm Up',
'duration': const Duration(seconds: 5), 'duration': const Duration(seconds: 5),
'source': AssetSource('warmUp.mp3'), 'source': AssetSource('warmUp.mp3'),
'color': Colors.green 'color': Colors.green
}, },
'High Intensity': { WorkoutPhases.highIntensity: {
'title': 'High Intensity',
'duration': const Duration(seconds: 4), 'duration': const Duration(seconds: 4),
'source': AssetSource('workout.mp3'), 'source': AssetSource('workout.mp3'),
'color': Colors.red 'color': Colors.red
}, },
'Low Intensity': { WorkoutPhases.lowIntensity: {
'title': 'Low Intensity',
'duration': const Duration(seconds: 3), 'duration': const Duration(seconds: 3),
'source': AssetSource('workout.mp3'), 'source': AssetSource('workout.mp3'),
'color': Colors.orange 'color': Colors.orange
}, },
'Cool-down': { WorkoutPhases.coolDown: {
'title': 'Cool Down',
'duration': const Duration(seconds: 5), 'duration': const Duration(seconds: 5),
'source': AssetSource('cool_down.mp3'), 'source': AssetSource('cool_down.mp3'),
'color': Colors.blue 'color': Colors.blue

View File

@ -17,20 +17,18 @@ class SleepForm extends StatelessWidget {
Widget build(BuildContext context) { Widget build(BuildContext context) {
InputProvider inputModel = context.watch<InputProvider>(); InputProvider inputModel = context.watch<InputProvider>();
TasksProvider tasksModel = context.watch<TasksProvider>(); TasksProvider tasksModel = context.watch<TasksProvider>();
String wokeUpKey = 'wokeUpAt';
String sleptKey = 'sleptAt';
return Column( return Column(
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,
children: [ children: [
ElevatedCard( const ElevatedCard(
title: 'Einschlafzeit', title: 'Einschlafzeit',
child: TimePicker(sleptKey), child: TimePicker(SleepTimes.sleptAt),
), ),
const SizedBox(height: 16), const SizedBox(height: 16),
ElevatedCard( const ElevatedCard(
title: 'Aufwachzeit', title: 'Aufwachzeit',
child: TimePicker(wokeUpKey), child: TimePicker(SleepTimes.wokeUpAt),
), ),
const SizedBox(height: 16), const SizedBox(height: 16),
const ElevatedCard( const ElevatedCard(
@ -46,7 +44,8 @@ class SleepForm extends StatelessWidget {
height: 80, height: 80,
), ),
SubmitFormButton( SubmitFormButton(
submitCallback: () => inputModel.saveSleep(wokeUpKey, sleptKey), submitCallback: () =>
inputModel.saveSleep(SleepTimes.wokeUpAt, SleepTimes.sleptAt),
updateTasks: () => tasksModel.setTaskDone(Pages.mood), updateTasks: () => tasksModel.setTaskDone(Pages.mood),
) )
], ],

View File

@ -3,7 +3,7 @@ import 'package:smoke_cess_app/providers/input_provider.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
class TimePicker extends StatelessWidget { class TimePicker extends StatelessWidget {
final String keyMap; final SleepTimes keyMap;
const TimePicker(this.keyMap, {super.key}); const TimePicker(this.keyMap, {super.key});

View File

@ -62,7 +62,7 @@ class WorkoutTimerWidget extends StatelessWidget {
return Column( return Column(
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,
children: [ children: [
Text(workoutProvider.currentPhase), Text(workoutProvider.currentPhaseTitle),
const SizedBox( const SizedBox(
height: 20, height: 20,
), ),