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 tasks = { Pages.mood: true, Pages.sleep: true, Pages.timer: true, }; List moodHistory = []; List sleepHistory = []; List workoutHistory = []; List relapseHistory = []; TasksProvider(SettingsProvider? settingsProvider) { initTasks(); initHistories(); } void setTaskDone(Pages taskName) { tasks[taskName] = false; notifyListeners(); } Future 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 && moodToday.isBefore(now)) { List moodList = await globals.databaseService.getMoodRecords(); if (moodList.isNotEmpty) { Mood mood = moodList.last; tasks[Pages.mood] = !isSameDay(moodToday, mood.date); } } else { tasks[Pages.mood] = false; } TZDateTime? sleepToday = await getTodaySleep(); if (sleepToday != null && sleepToday.isBefore(now)) { List sleepList = await globals.databaseService.getSleepRecords(); if (sleepList.isNotEmpty) { Sleep sleep = sleepList.last; tasks[Pages.sleep] = !isSameDay(sleepToday, sleep.date); } } else { tasks[Pages.sleep] = false; } List workoutList = await globals.databaseService.getWorkoutRecords(); if (workoutList.isNotEmpty) { Workout mood = workoutList.last; tasks[Pages.timer] = !isSameDay(DateTime.now(), mood.date); } notifyListeners(); } }