cpd_2022_zi/lib/providers/input_provider.dart

68 lines
2.0 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();
}
2023-02-27 20:02:15 +01:00
Future<int> saveMood() {
Mood mood =
Mood(_sliderValue.toInt(), _textController.text, DateTime.now());
_resetFields();
2023-02-27 20:02:15 +01:00
return globals.databaseService.addMood(mood);
}
2023-02-26 17:07:38 +01:00
Future<int> saveRelapse() {
2023-02-27 02:28:26 +01:00
Relapse relapse =
Relapse(_relapseCategory, _textController.text, DateTime.now());
_resetFields();
return globals.databaseService.addRelapse(relapse);
2023-02-27 02:28:26 +01:00
}
Future<int> saveSleep(String wokeUpKey, String sleptKey) {
2023-02-26 17:07:38 +01:00
Sleep sleep = Sleep(_sliderValue.toInt(), _textController.text,
DateTime.now(), getTimeEntry(sleptKey), getTimeEntry(wokeUpKey));
_resetFields();
return globals.databaseService.addSleep(sleep);
2023-02-26 17:07:38 +01:00
}
2023-02-26 14:59:37 +01:00
}