Skip to content

Commit

Permalink
Fix: Alarm titles show next alarm time from the all days instead of n…
Browse files Browse the repository at this point in the history
…ext day alarm time
  • Loading branch information
SuhasDissa committed Mar 26, 2024
1 parent fc1a560 commit 4c0710b
Showing 1 changed file with 4 additions and 21 deletions.
25 changes: 4 additions & 21 deletions app/src/main/java/com/bnyro/clock/util/AlarmHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.os.Build
import android.util.Log
import androidx.annotation.RequiresApi
import com.bnyro.clock.R
import com.bnyro.clock.obj.Alarm
import com.bnyro.clock.receivers.AlarmReceiver
import com.bnyro.clock.ui.MainActivity
import java.util.Calendar
import java.util.Date
import java.util.GregorianCalendar

object AlarmHelper {
Expand All @@ -23,9 +25,10 @@ object AlarmHelper {
}
val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
val alarmInfo = AlarmManager.AlarmClockInfo(
getAlarmScheduleTime(alarm),
getAlarmTime(alarm),
getOpenAppIntent(context, alarm)
)
Log.d("AlarmHelper", "Scheduling alarm time: ${Date(getAlarmTime(alarm))}")
alarmManager.setAlarmClock(alarmInfo, getPendingIntent(context, alarm))
}

Expand Down Expand Up @@ -65,26 +68,6 @@ object AlarmHelper {
/**
* Calculate the epoch time for scheduling an alarm
*/
private fun getAlarmScheduleTime(alarm: Alarm): Long {
val calendar = GregorianCalendar()
calendar.time = TimeHelper.currentTime

// reset the calendar time to the start of the day
calendar.set(Calendar.HOUR_OF_DAY, 0)
calendar.set(Calendar.MINUTE, 0)
calendar.set(Calendar.SECOND, 0)
calendar.set(Calendar.MILLISECOND, 0)

// add the milliseconds from the new alarm
calendar.add(Calendar.MILLISECOND, alarm.time.toInt())

// if the event has already passed for the day, schedule for the next day
if (calendar.time.time < TimeHelper.currentTime.time) {
calendar.add(Calendar.HOUR_OF_DAY, 24)
}
return calendar.timeInMillis
}

fun getAlarmTime(alarm: Alarm): Long {
val calendar = GregorianCalendar()
calendar.time = TimeHelper.currentTime
Expand Down

0 comments on commit 4c0710b

Please sign in to comment.