cpd_2022_zi/lib/providers/input_provider.dart

69 lines
2.0 KiB
Dart

import 'package:flutter/material.dart';
import 'package:smoke_cess_app/models/mood.dart';
import 'package:smoke_cess_app/models/relapse.dart';
import 'package:smoke_cess_app/models/sleep.dart';
import '../globals.dart' as globals;
enum SleepTimes {
wokeUpAt,
sleptAt,
}
class InputProvider extends ChangeNotifier {
double _sliderValue = 50;
final TextEditingController _textController = TextEditingController(text: '');
final Map<SleepTimes, TimeOfDay> _times = {
SleepTimes.wokeUpAt: const TimeOfDay(hour: 8, minute: 0),
SleepTimes.sleptAt: const TimeOfDay(hour: 22, minute: 0),
};
String relapseCategory = '';
double get sliderValue => _sliderValue;
TextEditingController get textController => _textController;
set sliderValue(double newValue) {
if (_sliderValue > 0 && _sliderValue < 100) {
_sliderValue = newValue;
}
notifyListeners();
}
TimeOfDay getTimeEntry(SleepTimes key) {
return _times[key] ?? const TimeOfDay(hour: 12, minute: 0);
}
void setTime(SleepTimes key, TimeOfDay time) {
_times[key] = time;
notifyListeners();
}
void _resetFields() {
_sliderValue = 50;
_textController.text = '';
setTime(SleepTimes.wokeUpAt, const TimeOfDay(hour: 8, minute: 0));
setTime(SleepTimes.sleptAt, const TimeOfDay(hour: 22, minute: 0));
notifyListeners();
}
Future<int> saveMood() {
Mood mood =
Mood(_sliderValue.toInt(), _textController.text, DateTime.now());
_resetFields();
return globals.databaseService.addMood(mood);
}
Future<int> saveRelapse() {
Relapse relapse =
Relapse(relapseCategory, _textController.text, DateTime.now());
_resetFields();
return globals.databaseService.addRelapse(relapse);
}
Future<int> saveSleep(SleepTimes wokeUpKey, SleepTimes sleptKey) {
Sleep sleep = Sleep(_sliderValue.toInt(), _textController.text,
DateTime.now(), getTimeEntry(sleptKey), getTimeEntry(wokeUpKey));
_resetFields();
return globals.databaseService.addSleep(sleep);
}
}