cpd_2022_zi/lib/providers/input_provider.dart

68 lines
1.9 KiB
Dart
Raw Normal View History

import 'package:flutter/material.dart';
import 'package:smoke_cess_app/models/mood.dart';
2023-02-27 02:28:26 +01:00
import 'package:smoke_cess_app/models/relapse.dart';
2023-02-26 17:07:38 +01:00
import 'package:smoke_cess_app/models/sleep.dart';
import '../globals.dart' as globals;
2023-02-26 14:59:37 +01:00
class InputProvider extends ChangeNotifier {
double _sliderValue = 50;
final TextEditingController _textController = TextEditingController(text: '');
2023-02-26 17:07:38 +01:00
final Map<String, TimeOfDay> _times = {
'wokeUpAt': const TimeOfDay(hour: 8, minute: 0),
'sleptAt': const TimeOfDay(hour: 22, minute: 0),
};
2023-02-27 02:28:26 +01:00
String _relapseCategory = '';
2023-02-26 14:59:37 +01:00
double get sliderValue => _sliderValue;
TextEditingController get textController => _textController;
2023-02-27 02:28:26 +01:00
String get relapseCategory => _relapseCategory;
2023-02-26 14:59:37 +01:00
set sliderValue(double newValue) {
_sliderValue = newValue;
notifyListeners();
}
2023-02-27 02:28:26 +01:00
set relapseCategory(String newValue) {
_relapseCategory = newValue;
notifyListeners();
}
2023-02-26 17:07:38 +01:00
TimeOfDay getTimeEntry(String key) {
return _times[key] ?? const TimeOfDay(hour: 12, minute: 0);
}
void setTime(String key, TimeOfDay time) {
_times[key] = time;
2023-02-26 17:07:38 +01:00
notifyListeners();
}
void _resetFields() {
_sliderValue = 50;
_textController.text = '';
2023-02-26 17:07:38 +01:00
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();
}
2023-02-26 17:07:38 +01:00
2023-02-27 02:28:26 +01:00
void saveRelapse() {
Relapse relapse =
Relapse(_relapseCategory, _textController.text, DateTime.now());
globals.databaseService.addRelapse(relapse);
_resetFields();
}
2023-02-26 17:07:38 +01:00
void saveSleep(String wokeUpKey, String sleptKey) {
Sleep sleep = Sleep(_sliderValue.toInt(), _textController.text,
DateTime.now(), getTimeEntry(sleptKey), getTimeEntry(wokeUpKey));
globals.databaseService.addSleep(sleep);
_resetFields();
}
2023-02-26 14:59:37 +01:00
}