62 lines
1.8 KiB
Dart
62 lines
1.8 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;
|
|
|
|
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();
|
|
}
|
|
|
|
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(String wokeUpKey, String sleptKey) {
|
|
Sleep sleep = Sleep(_sliderValue.toInt(), _textController.text,
|
|
DateTime.now(), getTimeEntry(sleptKey), getTimeEntry(wokeUpKey));
|
|
_resetFields();
|
|
return globals.databaseService.addSleep(sleep);
|
|
}
|
|
}
|