@@ -10,6 +10,7 @@ import androidx.work.ForegroundInfo
10
10
import androidx.work.WorkManager
11
11
import androidx.work.WorkerParameters
12
12
import com.stop.domain.usecase.nearplace.GetNearPlacesUseCase
13
+ import com.stop.ui.alarmsetting.AlarmSettingFragment.Companion.ALARM_CODE
13
14
import com.stop.ui.alarmsetting.AlarmSettingFragment.Companion.ALARM_TIME
14
15
import com.stop.ui.alarmsetting.AlarmSettingFragment.Companion.LAST_TIME
15
16
import dagger.assisted.Assisted
@@ -20,26 +21,30 @@ import kotlinx.coroutines.delay
20
21
class LastTimeCheckWorker @AssistedInject constructor(
21
22
@Assisted context : Context ,
22
23
@Assisted workerParameters : WorkerParameters ,
23
- private val getNearPlacesUseCase : GetNearPlacesUseCase
24
+ private val getNearPlacesUseCase : GetNearPlacesUseCase ,
25
+ private val alarmFunctions : AlarmFunctions
24
26
) : CoroutineWorker(context, workerParameters) {
25
27
26
28
private val notificationManager =
27
29
context.getSystemService(Context .NOTIFICATION_SERVICE ) as NotificationManager
28
30
29
- private val lastTime by lazy {
30
- inputData.getString(LAST_TIME )
31
- }
31
+ private var lastTime = " "
32
32
private val alarmTime by lazy {
33
33
inputData.getInt(ALARM_TIME , 0 )
34
34
}
35
35
36
36
override suspend fun doWork (): Result {
37
+ initData()
37
38
setForeground(createForegroundInfo())
38
39
checkLastTransportTime()
39
40
40
41
return Result .success()
41
42
}
42
43
44
+ private fun initData (){
45
+ lastTime = inputData.getString(LAST_TIME ) ? : " "
46
+ }
47
+
43
48
private fun createForegroundInfo (): ForegroundInfo {
44
49
val id = applicationContext.getString(R .string.notification_channel_id)
45
50
val title = applicationContext.getString(R .string.notification_title)
@@ -74,15 +79,22 @@ class LastTimeCheckWorker @AssistedInject constructor(
74
79
}
75
80
76
81
private suspend fun checkLastTransportTime () {
77
- // TODO 막차시간 갱신시 알람 삭제 후 등록 로직필요
78
-
79
82
while (isStopped.not ()) {
83
+ // TODO 막차시간 가져오는 API로 변경 해야합니다.
80
84
getNearPlacesUseCase.getNearPlaces(
81
85
" 아남타워" ,
82
86
126.969652 ,
83
87
37.553836
84
88
)
85
89
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
+
86
98
val delayTime = getDelayTime()
87
99
if (delayTime == 0L ) {
88
100
this .onStopped()
@@ -93,7 +105,7 @@ class LastTimeCheckWorker @AssistedInject constructor(
93
105
}
94
106
95
107
private fun getDelayTime (): Long {
96
- val fullLastTimeMillis = makeFullTime(lastTime ? : " " ).timeInMillis
108
+ val fullLastTimeMillis = makeFullTime(lastTime).timeInMillis
97
109
val currentTimeMillis = System .currentTimeMillis()
98
110
99
111
return when (val diffTimeMillis =
0 commit comments