v0.3.4: add PollingAlarmReceiver.kt

This commit is contained in:
sanjeok77 committed 2026-08-25 01:58:33 +00:00
1 parent 5de2b56c04
commit 3fe0121bef
1 file changed
+59
@@ -0,0 +1,59 @@
package com.hotdeal.alarm.receiver
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.util.Log
import com.hotdeal.alarm.data.local.preferences.AppSettings
import com.hotdeal.alarm.worker.AlarmScheduler
import com.hotdeal.alarm.worker.WorkerScheduler
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
import javax.inject.Inject
/**
* 주기적 알람 수신 시 백그라운드 스크래핑을 실행하고 다음 알람을 예약하는 리시버
*/
@AndroidEntryPoint
class PollingAlarmReceiver : BroadcastReceiver() {
companion object {
private const val TAG = "PollingAlarmReceiver"
}
@Inject
lateinit var workerScheduler: WorkerScheduler
@Inject
lateinit var alarmScheduler: AlarmScheduler
@Inject
lateinit var appSettings: AppSettings
private val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
override fun onReceive(context: Context, intent: Intent) {
if (intent.action == AlarmScheduler.ACTION_POLL_ALARM) {
Log.d(TAG, "⏰ 폴링 알람 수신 -> 핫딜 스크래핑 1회 즉시 실행 및 다음 알람 예약")
// 1. 핫딜 스크래핑 실행
workerScheduler.executeOnce()
// 2. 다음 주기 알람 예약 (Self-rescheduling)
scope.launch {
try {
val interval = appSettings.pollingInterval.first().toLong()
alarmScheduler.scheduleAlarm(interval)
Log.d(TAG, "다음 폴링 알람 예약 완료: ${interval}분 후")
} catch (e: Exception) {
Log.e(TAG, "다음 알람 예약 실패: ${e.message}")
alarmScheduler.scheduleAlarm(WorkerScheduler.DEFAULT_INTERVAL_MINUTES)
}
}
}
}
}