refactor: implement isBetween method for TimeOfDay and simplify sleep timer logic

This commit is contained in:
Dr-Blank 2024-10-02 07:37:14 -04:00
parent 62a3935d18
commit 4abd944a0c
No known key found for this signature in database
GPG key ID: 7452CC63F210A266
2 changed files with 16 additions and 2 deletions

View file

@ -67,6 +67,8 @@ class SleepTimer extends _$SleepTimer {
bool shouldBuildRightNow(Duration autoTurnOnTime, Duration autoTurnOffTime) {
final now = TimeOfDay.now();
return autoTurnOnTime.toTimeOfDay().isBefore(now) &&
autoTurnOffTime.toTimeOfDay().isAfter(now);
return now.isBetween(
autoTurnOnTime.toTimeOfDay(),
autoTurnOffTime.toTimeOfDay(),
);
}

View file

@ -31,4 +31,16 @@ extension TimeOfDayExtension on TimeOfDay {
bool isBefore(TimeOfDay other) => this < other;
bool isAfter(TimeOfDay other) => this > other;
bool isBetween(TimeOfDay start, TimeOfDay end) {
// needs more logic to handle the case where start is after end
//but on the other day
if (start == end) {
return this == start;
}
if (start < end) {
return this >= start && this <= end;
}
return this >= start || this <= end;
}
}