cpd_2022_zi/lib/providers/input_provider.dart

53 lines
1.5 KiB
Dart

import 'package:flutter/material.dart';
import 'package:smoke_cess_app/models/mood.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),
};
double get sliderValue => _sliderValue;
TextEditingController get textController => _textController;
set sliderValue(double newValue) {
_sliderValue = 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 saveSleep(String wokeUpKey, String sleptKey) {
Sleep sleep = Sleep(_sliderValue.toInt(), _textController.text,
DateTime.now(), getTimeEntry(sleptKey), getTimeEntry(wokeUpKey));
globals.databaseService.addSleep(sleep);
_resetFields();
}
}