From 6b4ee77cbad1d2b7f4b4fca68291745d92a4ab24 Mon Sep 17 00:00:00 2001 From: Helios Date: Wed, 18 Feb 2026 16:01:51 +0100 Subject: [PATCH] feat: add /last-alarm endpoint Saves the last fired alarm (hour, minute, label, timestamp) to SharedPreferences when an alarm fires. New GET /last-alarm endpoint returns the last alarm info or 404 if none has fired yet. --- .../service/AlarmRingService.kt | 20 ++++++++++++-- .../helios_alarm_clock/service/KtorService.kt | 26 +++++++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/example/helios_alarm_clock/service/AlarmRingService.kt b/app/src/main/java/com/example/helios_alarm_clock/service/AlarmRingService.kt index 8c4bce4..f56cb60 100644 --- a/app/src/main/java/com/example/helios_alarm_clock/service/AlarmRingService.kt +++ b/app/src/main/java/com/example/helios_alarm_clock/service/AlarmRingService.kt @@ -22,6 +22,7 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.cancel import kotlinx.coroutines.launch +import java.util.Calendar import javax.inject.Inject @AndroidEntryPoint @@ -55,13 +56,17 @@ class AlarmRingService : Service() { startSound() - // Delete the fired alarm from the database + // Save as last alarm and delete from the database if (alarmId.isNotEmpty()) { scope.launch { try { + val alarm = alarmDao.getById(alarmId) + if (alarm != null) { + saveLastAlarm(alarm.hour, alarm.minute, alarm.label) + } alarmDao.deleteById(alarmId) } catch (e: Exception) { - Log.e(TAG, "Failed to delete alarm $alarmId", e) + Log.e(TAG, "Failed to process alarm $alarmId", e) } } } @@ -142,6 +147,17 @@ class AlarmRingService : Service() { .build() } + private fun saveLastAlarm(hour: Int, minute: Int, label: String) { + val prefs = getSharedPreferences("last_alarm", Context.MODE_PRIVATE) + val now = Calendar.getInstance() + prefs.edit() + .putInt("hour", hour) + .putInt("minute", minute) + .putString("label", label) + .putLong("firedAt", now.timeInMillis) + .apply() + } + companion object { private const val TAG = "AlarmRingService" const val NOTIFICATION_ID = 2 diff --git a/app/src/main/java/com/example/helios_alarm_clock/service/KtorService.kt b/app/src/main/java/com/example/helios_alarm_clock/service/KtorService.kt index 5cebd75..a8693a7 100644 --- a/app/src/main/java/com/example/helios_alarm_clock/service/KtorService.kt +++ b/app/src/main/java/com/example/helios_alarm_clock/service/KtorService.kt @@ -181,6 +181,29 @@ class KtorService : Service() { val alarms = alarmDao.getAll() call.respond(alarms) } + + get("/last-alarm") { + val prefs = this@KtorService.getSharedPreferences( + "last_alarm", + Context.MODE_PRIVATE + ) + val firedAt = prefs.getLong("firedAt", -1) + if (firedAt == -1L) { + call.respond( + HttpStatusCode.NotFound, + ErrorResponse("No alarm has fired yet") + ) + } else { + call.respond( + LastAlarmResponse( + hour = prefs.getInt("hour", 0), + minute = prefs.getInt("minute", 0), + label = prefs.getString("label", "") ?: "", + firedAt = firedAt + ) + ) + } + } } }.also { it.start(wait = false) } } @@ -231,3 +254,6 @@ data class StatusResponse(val status: String) @Serializable data class ErrorResponse(val error: String) + +@Serializable +data class LastAlarmResponse(val hour: Int, val minute: Int, val label: String, val firedAt: Long)