Fix for showing accurate next enabled alarm

This commit is contained in:
Rawlin C
2023-07-05 01:04:52 +05:30
parent d378dd4797
commit 528f0033cb
3 changed files with 90 additions and 4 deletions

View File

@ -193,3 +193,43 @@ fun getAllTimeZones() = arrayListOf(
MyTimeZone(88, "GMT+13:00 Auckland", "Pacific/Auckland"),
MyTimeZone(89, "GMT+13:00 Tongatapu", "Pacific/Tongatapu")
)
fun getTimeUntilNextAlarm(alarmTimeInMinutes: Int, days: Int): Int? {
val calendar = Calendar.getInstance()
calendar.firstDayOfWeek = Calendar.MONDAY
val currentTimeInMinutes = calendar.get(Calendar.HOUR_OF_DAY) * 60 + calendar.get(Calendar.MINUTE)
val currentDayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - Calendar.MONDAY
var minTimeDifferenceInMinutes = Int.MAX_VALUE
for (i in 0..6) {
val alarmDayOfWeek = (currentDayOfWeek + i) % 7
if (isAlarmEnabledForDay(alarmDayOfWeek, days)) {
val timeDifferenceInMinutes = getTimeDifferenceInMinutes(currentTimeInMinutes, alarmTimeInMinutes, i)
if (timeDifferenceInMinutes < minTimeDifferenceInMinutes) {
minTimeDifferenceInMinutes = timeDifferenceInMinutes
}
}
}
return if (minTimeDifferenceInMinutes != Int.MAX_VALUE) {
minTimeDifferenceInMinutes
} else {
null
}
}
fun isAlarmEnabledForDay(day: Int, alarmDays: Int): Boolean {
return ((alarmDays shr day) and 1) == 1
}
fun getTimeDifferenceInMinutes(currentTimeInMinutes: Int, alarmTimeInMinutes: Int, daysUntilAlarm: Int): Int {
val minutesInADay = 24 * 60
val minutesUntilAlarm = daysUntilAlarm * minutesInADay + alarmTimeInMinutes
return if (minutesUntilAlarm > currentTimeInMinutes) {
minutesUntilAlarm - currentTimeInMinutes
} else {
minutesInADay - (currentTimeInMinutes - minutesUntilAlarm)
}
}