2023-02-26 16:17:57 +01:00
|
|
|
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';
|
2023-02-26 16:17:57 +01:00
|
|
|
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;
|
2023-02-26 16:17:57 +01:00
|
|
|
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;
|
2023-02-26 16:17:57 +01:00
|
|
|
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-02-26 16:17:57 +01:00
|
|
|
|
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-02-26 16:17:57 +01:00
|
|
|
}
|
|
|
|
|
2023-03-04 18:02:27 +01:00
|
|
|
void setTime(SleepTimes key, TimeOfDay time) {
|
2023-02-26 16:17:57 +01:00
|
|
|
_times[key] = time;
|
2023-02-26 17:07:38 +01:00
|
|
|
notifyListeners();
|
2023-02-26 16:17:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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));
|
2023-02-26 16:17:57 +01:00
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
2023-02-27 20:02:15 +01:00
|
|
|
Future<int> saveMood() {
|
2023-02-26 16:17:57 +01:00
|
|
|
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 16:17:57 +01:00
|
|
|
}
|
2023-02-26 17:07:38 +01:00
|
|
|
|
2023-02-27 20:09:11 +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();
|
2023-02-27 20:09:11 +01:00
|
|
|
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();
|
2023-02-27 20:09:11 +01:00
|
|
|
return globals.databaseService.addSleep(sleep);
|
2023-02-26 17:07:38 +01:00
|
|
|
}
|
2023-02-26 14:59:37 +01:00
|
|
|
}
|