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.
This commit is contained in:
parent
6032e9fd07
commit
6b4ee77cba
2 changed files with 44 additions and 2 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue