mirror of
https://github.com/SimpleMobileTools/Simple-Clock.git
synced 2025-06-05 22:19:17 +02:00
Fix for showing accurate next enabled alarm
This commit is contained in:
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user