Skip to content

Commit 00b929c

Browse files
committed
feat : Worker 알람 시간 변경 로직 구현
1 parent 88b805b commit 00b929c

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

presentation/src/main/java/com/stop/LastTimeCheckWorker.kt

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import androidx.work.ForegroundInfo
1010
import androidx.work.WorkManager
1111
import androidx.work.WorkerParameters
1212
import com.stop.domain.usecase.nearplace.GetNearPlacesUseCase
13+
import com.stop.ui.alarmsetting.AlarmSettingFragment.Companion.ALARM_CODE
1314
import com.stop.ui.alarmsetting.AlarmSettingFragment.Companion.ALARM_TIME
1415
import com.stop.ui.alarmsetting.AlarmSettingFragment.Companion.LAST_TIME
1516
import dagger.assisted.Assisted
@@ -20,26 +21,30 @@ import kotlinx.coroutines.delay
2021
class LastTimeCheckWorker @AssistedInject constructor(
2122
@Assisted context: Context,
2223
@Assisted workerParameters: WorkerParameters,
23-
private val getNearPlacesUseCase: GetNearPlacesUseCase
24+
private val getNearPlacesUseCase: GetNearPlacesUseCase,
25+
private val alarmFunctions: AlarmFunctions
2426
) : CoroutineWorker(context, workerParameters) {
2527

2628
private val notificationManager =
2729
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
2830

29-
private val lastTime by lazy {
30-
inputData.getString(LAST_TIME)
31-
}
31+
private var lastTime = ""
3232
private val alarmTime by lazy {
3333
inputData.getInt(ALARM_TIME, 0)
3434
}
3535

3636
override suspend fun doWork(): Result {
37+
initData()
3738
setForeground(createForegroundInfo())
3839
checkLastTransportTime()
3940

4041
return Result.success()
4142
}
4243

44+
private fun initData(){
45+
lastTime = inputData.getString(LAST_TIME) ?: ""
46+
}
47+
4348
private fun createForegroundInfo(): ForegroundInfo {
4449
val id = applicationContext.getString(R.string.notification_channel_id)
4550
val title = applicationContext.getString(R.string.notification_title)
@@ -74,15 +79,22 @@ class LastTimeCheckWorker @AssistedInject constructor(
7479
}
7580

7681
private suspend fun checkLastTransportTime() {
77-
//TODO 막차시간 갱신시 알람 삭제 후 등록 로직필요
78-
7982
while (isStopped.not()) {
83+
//TODO 막차시간 가져오는 API로 변경 해야합니다.
8084
getNearPlacesUseCase.getNearPlaces(
8185
"아남타워",
8286
126.969652,
8387
37.553836
8488
)
8589

90+
val resultLastTime = "21:04:00"
91+
92+
if(lastTime != resultLastTime && resultLastTime != null){
93+
lastTime = resultLastTime
94+
alarmFunctions.cancelAlarm(ALARM_CODE)
95+
alarmFunctions.callAlarm(resultLastTime, alarmTime, ALARM_CODE)
96+
}
97+
8698
val delayTime = getDelayTime()
8799
if (delayTime == 0L) {
88100
this.onStopped()
@@ -93,7 +105,7 @@ class LastTimeCheckWorker @AssistedInject constructor(
93105
}
94106

95107
private fun getDelayTime(): Long {
96-
val fullLastTimeMillis = makeFullTime(lastTime ?: "").timeInMillis
108+
val fullLastTimeMillis = makeFullTime(lastTime).timeInMillis
97109
val currentTimeMillis = System.currentTimeMillis()
98110

99111
return when (val diffTimeMillis =

0 commit comments

Comments
 (0)