cpd_2022_zi/lib/providers/tasks_provider.dart

75 lines
2.4 KiB
Dart

import 'package:flutter/material.dart';
import 'package:smoke_cess_app/models/relapse.dart';
import 'package:smoke_cess_app/models/sleep.dart';
import 'package:smoke_cess_app/models/workout.dart';
import 'package:smoke_cess_app/providers/settings_provider.dart';
import 'package:smoke_cess_app/services/date_service.dart';
import 'package:smoke_cess_app/services/pages_service.dart';
import 'package:timezone/timezone.dart';
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,
};
List<Mood> moodHistory = [];
List<Sleep> sleepHistory = [];
List<Workout> workoutHistory = [];
List<Relapse> relapseHistory = [];
TasksProvider(SettingsProvider? settingsProvider) {
initTasks();
initHistories();
}
void setTaskDone(Pages taskName) {
tasks[taskName] = false;
notifyListeners();
}
void initHistories() async {
moodHistory = await globals.databaseService.getMoodRecords();
sleepHistory = await globals.databaseService.getSleepRecords();
workoutHistory = await globals.databaseService.getWorkoutRecords();
relapseHistory = await globals.databaseService.getRelapseRecords();
notifyListeners();
}
void initTasks() async {
DateTime now = DateTime.now();
TZDateTime? moodToday = await getTodayMood();
if (moodToday != null) {
List<Mood> moodList = await globals.databaseService.getMoodRecords();
if (moodList.isNotEmpty) {
Mood mood = moodList.last;
tasks[Pages.mood] =
!isSameDay(moodToday, mood.date) && moodToday.isBefore(now);
}
} else {
tasks[Pages.mood] = false;
}
TZDateTime? sleepToday = await getTodaySleep();
if (sleepToday != null) {
List<Sleep> sleepList = await globals.databaseService.getSleepRecords();
if (sleepList.isNotEmpty) {
Sleep sleep = sleepList.last;
tasks[Pages.sleep] =
!isSameDay(sleepToday, sleep.date) && sleepToday.isBefore(now);
}
} else {
tasks[Pages.sleep] = false;
}
List<Workout> workoutList =
await globals.databaseService.getWorkoutRecords();
if (workoutList.isNotEmpty) {
Workout mood = workoutList.last;
tasks[Pages.timer] = !isSameDay(DateTime.now(), mood.date);
}
notifyListeners();
}
}