115 lines
3.6 KiB
Dart
115 lines
3.6 KiB
Dart
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<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);
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
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();
|
|
}
|
|
}
|
|
|
|
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)) {
|
|
tzDateTimes.add(TZDateTime.local(date.year, date.month, date.day,
|
|
selectedHours, selectedMinutes, 0, 0, 0)
|
|
.subtract(offset));
|
|
}
|
|
}
|
|
return tzDateTimes;
|
|
}
|