cpd_2022_zi/lib/providers/input_provider.dart

68 lines
1.9 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;
class InputProvider extends ChangeNotifier {
double _sliderValue = 50;
final TextEditingController _textController = TextEditingController(text: '');
final Map<String, TimeOfDay> _times = {
'wokeUpAt': const TimeOfDay(hour: 8, minute: 0),
'sleptAt': const TimeOfDay(hour: 22, minute: 0),
};
String _relapseCategory = '';
double get sliderValue => _sliderValue;
TextEditingController get textController => _textController;
String get relapseCategory => _relapseCategory;
set sliderValue(double newValue) {
_sliderValue = newValue;
notifyListeners();
}
set relapseCategory(String newValue) {
_relapseCategory = newValue;
notifyListeners();
}
TimeOfDay getTimeEntry(String key) {
return _times[key] ?? const TimeOfDay(hour: 12, minute: 0);
}
void setTime(String key, TimeOfDay time) {
_times[key] = time;
notifyListeners();
}
void _resetFields() {
_sliderValue = 50;
_textController.text = '';
setTime('wokeUpAt', const TimeOfDay(hour: 8, minute: 0));
setTime('sleptAt', const TimeOfDay(hour: 22, minute: 0));
notifyListeners();
}
void saveMood() {
Mood mood =
Mood(_sliderValue.toInt(), _textController.text, DateTime.now());
globals.databaseService.addMood(mood);
_resetFields();
}
void saveRelapse() {
Relapse relapse =
Relapse(_relapseCategory, _textController.text, DateTime.now());
globals.databaseService.addRelapse(relapse);
_resetFields();
}
void saveSleep(String wokeUpKey, String sleptKey) {
Sleep sleep = Sleep(_sliderValue.toInt(), _textController.text,
DateTime.now(), getTimeEntry(sleptKey), getTimeEntry(wokeUpKey));
globals.databaseService.addSleep(sleep);
_resetFields();
}
}