cpd_2022_zi/lib/providers/input_provider.dart

69 lines
2.0 KiB
Dart
Raw Permalink 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
2023-03-04 18:02:27 +01:00
enum SleepTimes {
wokeUpAt,
sleptAt,
}
2023-02-26 14:59:37 +01:00
class InputProvider extends ChangeNotifier {
double _sliderValue = 50;
final TextEditingController _textController = TextEditingController(text: '');
2023-03-04 18:02:27 +01:00
final Map<SleepTimes, TimeOfDay> _times = {
SleepTimes.wokeUpAt: const TimeOfDay(hour: 8, minute: 0),
SleepTimes.sleptAt: const TimeOfDay(hour: 22, minute: 0),
2023-02-26 17:07:38 +01:00
};
2023-03-03 12:51:25 +01:00
String relapseCategory = '';
2023-02-26 14:59:37 +01:00
double get sliderValue => _sliderValue;
TextEditingController get textController => _textController;
2023-02-26 14:59:37 +01:00
set sliderValue(double newValue) {
2023-03-06 21:04:35 +01:00
if (_sliderValue > 0 && _sliderValue < 100) {
_sliderValue = newValue;
}
2023-02-26 14:59:37 +01:00
notifyListeners();
}
2023-03-04 18:02:27 +01:00
TimeOfDay getTimeEntry(SleepTimes key) {
2023-02-26 17:07:38 +01:00
return _times[key] ?? const TimeOfDay(hour: 12, minute: 0);
}
2023-03-04 18:02:27 +01:00
void setTime(SleepTimes key, TimeOfDay time) {
_times[key] = time;
2023-02-26 17:07:38 +01:00
notifyListeners();
}
void _resetFields() {
_sliderValue = 50;
_textController.text = '';
2023-03-04 18:02:27 +01:00
setTime(SleepTimes.wokeUpAt, const TimeOfDay(hour: 8, minute: 0));
setTime(SleepTimes.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 =
2023-03-03 12:51:25 +01:00
Relapse(relapseCategory, _textController.text, DateTime.now());
2023-02-27 02:28:26 +01:00
_resetFields();
return globals.databaseService.addRelapse(relapse);
2023-02-27 02:28:26 +01:00
}
2023-03-04 18:02:27 +01:00
Future<int> saveSleep(SleepTimes wokeUpKey, SleepTimes 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
}