mirror of
https://github.com/Dr-Blank/Vaani.git
synced 2026-07-07 01:41:33 +00:00
refactor: implement isBetween method for TimeOfDay and simplify sleep timer logic
This commit is contained in:
parent
62a3935d18
commit
4abd944a0c
2 changed files with 16 additions and 2 deletions
|
|
@ -67,6 +67,8 @@ class SleepTimer extends _$SleepTimer {
|
||||||
|
|
||||||
bool shouldBuildRightNow(Duration autoTurnOnTime, Duration autoTurnOffTime) {
|
bool shouldBuildRightNow(Duration autoTurnOnTime, Duration autoTurnOffTime) {
|
||||||
final now = TimeOfDay.now();
|
final now = TimeOfDay.now();
|
||||||
return autoTurnOnTime.toTimeOfDay().isBefore(now) &&
|
return now.isBetween(
|
||||||
autoTurnOffTime.toTimeOfDay().isAfter(now);
|
autoTurnOnTime.toTimeOfDay(),
|
||||||
|
autoTurnOffTime.toTimeOfDay(),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -31,4 +31,16 @@ extension TimeOfDayExtension on TimeOfDay {
|
||||||
|
|
||||||
bool isBefore(TimeOfDay other) => this < other;
|
bool isBefore(TimeOfDay other) => this < other;
|
||||||
bool isAfter(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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue