Skip to content

Commit 37cb4fe

Browse files
committed
feat : 막차시간 주기적으로 호출해서 비교하는 워크매니저 구현
1 parent 94445f4 commit 37cb4fe

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package com.stop
2+
3+
import android.app.NotificationChannel
4+
import android.app.NotificationManager
5+
import android.content.Context
6+
import androidx.core.app.NotificationCompat
7+
import androidx.hilt.work.HiltWorker
8+
import androidx.work.CoroutineWorker
9+
import androidx.work.ForegroundInfo
10+
import androidx.work.WorkManager
11+
import androidx.work.WorkerParameters
12+
import com.stop.domain.usecase.nearplace.GetNearPlacesUseCase
13+
import dagger.assisted.Assisted
14+
import dagger.assisted.AssistedInject
15+
import kotlinx.coroutines.delay
16+
17+
@HiltWorker
18+
class LastTimeCheckWorker @AssistedInject constructor(
19+
@Assisted context: Context,
20+
@Assisted workerParameters: WorkerParameters,
21+
private val getNearPlacesUseCase: GetNearPlacesUseCase
22+
) : CoroutineWorker(context, workerParameters) {
23+
24+
private val notificationManager =
25+
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
26+
27+
override suspend fun doWork(): Result {
28+
29+
setForeground(createForegroundInfo())
30+
checkLastTransportTime()
31+
32+
return Result.success()
33+
}
34+
35+
private fun createForegroundInfo(): ForegroundInfo {
36+
val id = applicationContext.getString(R.string.notification_channel_id)
37+
val title = applicationContext.getString(R.string.notification_title)
38+
val cancel = applicationContext.getString(R.string.alarm_cancel_text)
39+
40+
val intent = WorkManager.getInstance(applicationContext)
41+
.createCancelPendingIntent(getId())
42+
43+
createChannel(id)
44+
45+
val notification = NotificationCompat.Builder(applicationContext, id)
46+
.setContentTitle(title)
47+
.setTicker(title)
48+
.setContentText(NOTIFICATION_CONTENT)
49+
.setSmallIcon(R.mipmap.ic_bus)
50+
.setOngoing(true) // 사용자가 지우지 못하도록 막음
51+
.addAction(android.R.drawable.ic_delete, cancel, intent)
52+
.build()
53+
54+
return ForegroundInfo(NOTIFICATION_ID, notification)
55+
}
56+
57+
private fun createChannel(id: String) {
58+
if (isMoreThanOreo()) {
59+
if (notificationManager.getNotificationChannel(id) == null) {
60+
val name = applicationContext.getString(R.string.notification_channel_name)
61+
NotificationChannel(id, name, NotificationManager.IMPORTANCE_DEFAULT).apply {
62+
notificationManager.createNotificationChannel(this)
63+
}
64+
}
65+
}
66+
}
67+
68+
private suspend fun checkLastTransportTime() {
69+
while (isStopped.not()) {
70+
getNearPlacesUseCase.getNearPlaces(
71+
"아남타워",
72+
126.969652,
73+
37.553836
74+
)
75+
76+
val delayTime = getDelayTime("18:18:00")
77+
if (delayTime == 0L) {
78+
this.onStopped()
79+
} else {
80+
delay(delayTime)
81+
}
82+
}
83+
}
84+
85+
private fun getDelayTime(lastTime: String): Long {
86+
val fullLastTimeMillis = makeFullTime(lastTime).timeInMillis
87+
val currentTimeMillis = System.currentTimeMillis()
88+
89+
return when (val diffTimeMillis =
90+
if (fullLastTimeMillis > currentTimeMillis) fullLastTimeMillis - currentTimeMillis
91+
else 0L
92+
) {
93+
in THIRTY_MINUTES until Long.MAX_VALUE -> diffTimeMillis - THIRTY_MINUTES
94+
in TWENTY_MINUTES until THIRTY_MINUTES -> FIVE_MINUTES
95+
in TEN_MINUTES until TWENTY_MINUTES -> TWO_MINUTES
96+
in 0 until TEN_MINUTES -> ONE_MINUTES
97+
else -> 0
98+
}
99+
}
100+
101+
companion object {
102+
const val NOTIFICATION_ID = 12
103+
private const val NOTIFICATION_CONTENT = "막차 시간이 변경되는지 계속 추적중입니다."
104+
private const val THIRTY_MINUTES = 1_800_000L
105+
private const val TWENTY_MINUTES = 1_200_000L
106+
private const val TEN_MINUTES = 600_000L
107+
private const val FIVE_MINUTES = 300_000L
108+
private const val TWO_MINUTES = 120_000L
109+
private const val ONE_MINUTES = 60_000L
110+
}
111+
}

presentation/src/main/res/values/strings.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,9 @@
4444
<string name="alarm_delete_text">알람 삭제</string>
4545
<string name="recent_search_text">최근기록</string>
4646
<string name="delete_recent_search_text">최근기록 삭제</string>
47+
48+
<string name="notification_channel_id">"notification_channel_id</string>
49+
<string name="notification_channel_name">"notification_channel_name"</string>
50+
<string name="notification_title">"막차 알림</string>
51+
<string name="alarm_cancel_text">취소하기</string>
4752
</resources>

0 commit comments

Comments
 (0)