cpd_2022_zi/lib/services/date_service.dart

115 lines
3.6 KiB
Dart
Raw Permalink Normal View History

2023-02-26 14:10:06 +01:00
import 'package:smoke_cess_app/services/settings_service.dart';
import 'package:timezone/timezone.dart';
2023-03-03 16:38:08 +01:00
import 'pages_service.dart';
2023-03-06 21:04:35 +01:00
const int trainingTime = 6 * 7;
2023-02-26 14:10:06 +01:00
const weekDays = {
"Montag": 1,
"Dienstag": 2,
"Mittwoch": 3,
"Donnerstag": 4,
"Freitag": 5,
"Samstag": 6,
"Sonntag": 7,
};
2023-03-02 18:35:03 +01:00
bool isSameDay(DateTime? dateA, DateTime? dateB) {
return dateA?.year == dateB?.year &&
dateA?.month == dateB?.month &&
dateA?.day == dateB?.day;
}
2023-02-26 14:10:06 +01:00
Future<List<TZDateTime>> getDatesforMood() async {
final List<String>? selectedDays = await getMoodQueryDaysCategories();
final int? selectedHours = await getMoodQueryHours();
final int? selectedMinutes = await getMoodQueryMinutes();
return createTZDateTimes(selectedDays, selectedHours, selectedMinutes);
}
Future<List<TZDateTime>> getDatesforSleep() async {
final List<String>? selectedDays = await getSleepQueryDaysCategories();
final int? selectedHours = await getSleepQueryHours();
final int? selectedMinutes = await getSleepQueryMinutes();
return createTZDateTimes(selectedDays, selectedHours, selectedMinutes);
}
2023-03-02 18:35:03 +01:00
Future<TZDateTime?> getTodayMood() async {
List<TZDateTime> moodDates = await getDatesforMood();
Iterable<TZDateTime> today =
moodDates.where((element) => isSameDay(element, DateTime.now()));
return today.isNotEmpty ? today.first : null;
}
Future<TZDateTime?> getTodaySleep() async {
List<TZDateTime> sleepDates = await getDatesforSleep();
Iterable<TZDateTime> today =
sleepDates.where((element) => isSameDay(element, DateTime.now()));
return today.isNotEmpty ? today.first : null;
}
2023-03-03 16:38:08 +01:00
Future<Duration> getTimeTillNextMood() async {
List<TZDateTime> moodDates = await getDatesforMood();
Iterable<TZDateTime> nextDate =
moodDates.where((element) => element.isAfter(DateTime.now()));
Duration duration = nextDate.isNotEmpty
? nextDate.first.difference(DateTime.now())
: const Duration(seconds: 0);
return duration;
}
Future<Duration> getTimeTillNextSleep() async {
List<TZDateTime> sleepDates = await getDatesforSleep();
Iterable<TZDateTime> nextDate =
sleepDates.where((element) => element.isAfter(DateTime.now()));
Duration duration = nextDate.isNotEmpty
? nextDate.first.difference(DateTime.now())
: const Duration(seconds: 0);
return duration;
}
Future<Duration> 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<Duration> getTimeTill(Pages page) {
switch (page) {
case Pages.mood:
return getTimeTillNextMood();
case Pages.sleep:
return getTimeTillNextSleep();
default:
return getTimeTillNextWorkout();
}
}
2023-02-26 14:10:06 +01:00
List<TZDateTime> createTZDateTimes(
List<String>? selectedDays, int? selectedHours, int? selectedMinutes) {
final List<TZDateTime> tzDateTimes = [];
final DateTime now = DateTime.now();
final Duration offset = now.timeZoneOffset;
if (selectedDays == null ||
selectedHours == null ||
selectedMinutes == null) {
return tzDateTimes;
}
final Iterable<int?> 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));
2023-03-07 09:45:54 +01:00
if (selectedDaysInt.contains(date.weekday)) {
2023-02-26 14:10:06 +01:00
tzDateTimes.add(TZDateTime.local(date.year, date.month, date.day,
selectedHours, selectedMinutes, 0, 0, 0)
.subtract(offset));
}
}
return tzDateTimes;
}