cpd_2022_zi/lib/providers/tasks_provider.dart

73 lines
2.3 KiB
Dart
Raw Normal View History

2023-03-02 19:31:47 +01:00
import 'package:flutter/material.dart';
2023-03-03 12:50:50 +01:00
import 'package:smoke_cess_app/models/relapse.dart';
2023-03-02 19:31:47 +01:00
import 'package:smoke_cess_app/models/sleep.dart';
import 'package:smoke_cess_app/models/workout.dart';
2023-03-02 19:57:24 +01:00
import 'package:smoke_cess_app/providers/settings_provider.dart';
2023-03-02 19:31:47 +01:00
import 'package:smoke_cess_app/services/date_service.dart';
import 'package:smoke_cess_app/services/pages_service.dart';
2023-03-05 17:43:40 +01:00
import 'package:timezone/timezone.dart';
2023-03-02 19:31:47 +01:00
import '../globals.dart' as globals;
import '../models/mood.dart';
class TasksProvider extends ChangeNotifier {
Map<Pages, bool> tasks = {
Pages.mood: true,
Pages.sleep: true,
Pages.timer: true,
};
2023-03-03 12:50:50 +01:00
List<Mood> moodHistory = [];
List<Sleep> sleepHistory = [];
List<Workout> workoutHistory = [];
List<Relapse> relapseHistory = [];
2023-03-02 19:57:24 +01:00
TasksProvider(SettingsProvider? settingsProvider) {
2023-03-02 19:31:47 +01:00
initTasks();
2023-03-03 12:50:50 +01:00
initHistories();
2023-03-02 19:31:47 +01:00
}
2023-03-02 20:33:44 +01:00
void setTaskDone(Pages taskName) {
2023-03-02 19:31:47 +01:00
tasks[taskName] = false;
notifyListeners();
2023-03-03 12:50:50 +01:00
}
2023-03-06 23:46:49 +01:00
Future<void> initHistories() async {
2023-03-03 12:50:50 +01:00
moodHistory = await globals.databaseService.getMoodRecords();
sleepHistory = await globals.databaseService.getSleepRecords();
workoutHistory = await globals.databaseService.getWorkoutRecords();
relapseHistory = await globals.databaseService.getRelapseRecords();
notifyListeners();
2023-03-02 19:31:47 +01:00
}
void initTasks() async {
2023-03-03 16:38:08 +01:00
DateTime now = DateTime.now();
2023-03-02 19:31:47 +01:00
TZDateTime? moodToday = await getTodayMood();
2023-03-07 00:49:54 +01:00
if (moodToday != null && moodToday.isBefore(now)) {
2023-03-02 19:31:47 +01:00
List<Mood> moodList = await globals.databaseService.getMoodRecords();
if (moodList.isNotEmpty) {
Mood mood = moodList.last;
2023-03-07 00:49:54 +01:00
tasks[Pages.mood] = !isSameDay(moodToday, mood.date);
2023-03-02 19:31:47 +01:00
}
} else {
tasks[Pages.mood] = false;
}
2023-03-02 19:57:24 +01:00
TZDateTime? sleepToday = await getTodaySleep();
2023-03-07 00:49:54 +01:00
if (sleepToday != null && sleepToday.isBefore(now)) {
2023-03-02 19:31:47 +01:00
List<Sleep> sleepList = await globals.databaseService.getSleepRecords();
if (sleepList.isNotEmpty) {
Sleep sleep = sleepList.last;
2023-03-07 00:49:54 +01:00
tasks[Pages.sleep] = !isSameDay(sleepToday, sleep.date);
2023-03-02 19:31:47 +01:00
}
} else {
tasks[Pages.sleep] = false;
}
List<Workout> workoutList =
await globals.databaseService.getWorkoutRecords();
if (workoutList.isNotEmpty) {
Workout mood = workoutList.last;
2023-03-02 19:57:24 +01:00
tasks[Pages.timer] = !isSameDay(DateTime.now(), mood.date);
2023-03-02 19:31:47 +01:00
}
notifyListeners();
}
}