Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Alarm titles show next alarm time from the all days #324

Merged
merged 1 commit into from
Mar 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading