cpd_2022_zi/lib/services/date_service.dart

64 lines
2.1 KiB
Dart

import 'package:smoke_cess_app/services/settings_service.dart';
import 'package:timezone/timezone.dart';
const int trainingTime = 40;
const weekDays = {
"Montag": 1,
"Dienstag": 2,
"Mittwoch": 3,
"Donnerstag": 4,
"Freitag": 5,
"Samstag": 6,
"Sonntag": 7,
};
Future<List<TZDateTime>> getDatesforAll() async {
List<TZDateTime> allDates = [];
List<TZDateTime> moodDates = await getDatesforMood();
List<TZDateTime> sleepDates = await getDatesforSleep();
allDates.addAll(moodDates);
allDates.addAll(sleepDates);
return allDates;
}
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);
}
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));
if (selectedDaysInt.contains(date.weekday) &&
date.isAfter(DateTime.now())) {
tzDateTimes.add(TZDateTime.local(date.year, date.month, date.day,
selectedHours, selectedMinutes, 0, 0, 0)
.subtract(offset));
}
}
return tzDateTimes;
}