cpd_2022_zi/lib/service/date_service.dart

64 lines
2.1 KiB
Dart
Raw Normal View History

2023-02-24 20:30:51 +01:00
import 'package:smoke_cess_app/service/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) {
2023-02-26 12:44:01 +01:00
final List<TZDateTime> tzDateTimes = [];
final DateTime now = DateTime.now();
final Duration offset = now.timeZoneOffset;
2023-02-24 20:30:51 +01:00
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++) {
2023-02-25 15:52:07 +01:00
final DateTime date = DateTime(now.year, now.month, now.day, selectedHours,
selectedMinutes, 0, 0, 0)
.add(Duration(days: i));
2023-02-25 14:39:05 +01:00
if (selectedDaysInt.contains(date.weekday) &&
date.isAfter(DateTime.now())) {
2023-02-24 20:30:51 +01:00
tzDateTimes.add(TZDateTime.local(date.year, date.month, date.day,
2023-02-26 12:44:01 +01:00
selectedHours, selectedMinutes, 0, 0, 0)
.subtract(offset));
2023-02-24 20:30:51 +01:00
}
}
return tzDateTimes;
}