import 'package:smoke_cess_app/services/settings_service.dart'; import 'package:timezone/timezone.dart'; import 'pages_service.dart'; const int trainingTime = 6 * 7; const weekDays = { "Montag": 1, "Dienstag": 2, "Mittwoch": 3, "Donnerstag": 4, "Freitag": 5, "Samstag": 6, "Sonntag": 7, }; bool isSameDay(DateTime? dateA, DateTime? dateB) { return dateA?.year == dateB?.year && dateA?.month == dateB?.month && dateA?.day == dateB?.day; } Future> getDatesforMood() async { final List? selectedDays = await getMoodQueryDaysCategories(); final int? selectedHours = await getMoodQueryHours(); final int? selectedMinutes = await getMoodQueryMinutes(); return createTZDateTimes(selectedDays, selectedHours, selectedMinutes); } Future> getDatesforSleep() async { final List? selectedDays = await getSleepQueryDaysCategories(); final int? selectedHours = await getSleepQueryHours(); final int? selectedMinutes = await getSleepQueryMinutes(); return createTZDateTimes(selectedDays, selectedHours, selectedMinutes); } Future getTodayMood() async { List moodDates = await getDatesforMood(); Iterable today = moodDates.where((element) => isSameDay(element, DateTime.now())); return today.isNotEmpty ? today.first : null; } Future getTodaySleep() async { List sleepDates = await getDatesforSleep(); Iterable today = sleepDates.where((element) => isSameDay(element, DateTime.now())); return today.isNotEmpty ? today.first : null; } Future getTimeTillNextMood() async { List moodDates = await getDatesforMood(); Iterable nextDate = moodDates.where((element) => element.isAfter(DateTime.now())); Duration duration = nextDate.isNotEmpty ? nextDate.first.difference(DateTime.now()) : const Duration(seconds: 0); return duration; } Future getTimeTillNextSleep() async { List sleepDates = await getDatesforSleep(); Iterable nextDate = sleepDates.where((element) => element.isAfter(DateTime.now())); Duration duration = nextDate.isNotEmpty ? nextDate.first.difference(DateTime.now()) : const Duration(seconds: 0); return duration; } Future getTimeTillNextWorkout() async { DateTime now = DateTime.now(); DateTime tomorrow = DateTime(now.year, now.month, now.day).add(const Duration(days: 1)); Duration duration = tomorrow.difference(now); return duration; } Future getTimeTill(Pages page) { switch (page) { case Pages.mood: return getTimeTillNextMood(); case Pages.sleep: return getTimeTillNextSleep(); default: return getTimeTillNextWorkout(); } } List createTZDateTimes( List? selectedDays, int? selectedHours, int? selectedMinutes) { final List tzDateTimes = []; final DateTime now = DateTime.now(); final Duration offset = now.timeZoneOffset; if (selectedDays == null || selectedHours == null || selectedMinutes == null) { return tzDateTimes; } final Iterable selectedDaysInt = selectedDays.map((day) => weekDays[day]); for (int i = 0; i < trainingTime; i++) { final DateTime date = DateTime(now.year, now.month, now.day, selectedHours, selectedMinutes, 0, 0, 0) .add(Duration(days: i)); if (selectedDaysInt.contains(date.weekday)) { tzDateTimes.add(TZDateTime.local(date.year, date.month, date.day, selectedHours, selectedMinutes, 0, 0, 0) .subtract(offset)); } } return tzDateTimes; }