Skip to content

Commit 38cc30a

Browse files
committed
feat : 미션 종료 버튼 클릭시 workmanager 종료
1 parent 8d3a53e commit 38cc30a

File tree

4 files changed

+44
-45
lines changed

4 files changed

+44
-45
lines changed

presentation/src/main/java/com/stop/ui/mission/MissionFragment.kt

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ class MissionFragment : Fragment(), MissionHandler {
3434

3535
private lateinit var tMap: MissionTMap
3636

37-
private var state = State.FOREGROUND
38-
3937
override fun onCreateView(
4038
inflater: LayoutInflater, container: ViewGroup?,
4139
savedInstanceState: Bundle?
@@ -57,19 +55,6 @@ class MissionFragment : Fragment(), MissionHandler {
5755

5856
}
5957

60-
override fun onResume() {
61-
super.onResume()
62-
Log.d("MissionWorker", "onResume")
63-
64-
state = State.FOREGROUND
65-
}
66-
67-
override fun onStop() {
68-
super.onStop()
69-
Log.d("MissionWorker", "onStop")
70-
state = State.BACKGROUND
71-
}
72-
7358
override fun onDestroyView() {
7459
_binding = null
7560

@@ -182,24 +167,38 @@ class MissionFragment : Fragment(), MissionHandler {
182167
lateinit var beforeLocation: Location
183168
lifecycleScope.launch {
184169
viewModel.userLocation.collect { userLocation ->
185-
if (first < 2) {
186-
beforeLocation = userLocation
187-
first += 1
188-
} else {
189-
val nowLocation = TMapPoint(userLocation.latitude, userLocation.longitude)
190-
tMap.drawMoveLine(
191-
nowLocation,
192-
TMapPoint(beforeLocation.latitude, beforeLocation.longitude),
193-
Marker.PERSON_LINE + PERSON_LINE_NUM.toString(),
194-
Marker.PERSON_LINE_COLOR
195-
)
196-
tMap.addMarker(Marker.PERSON_MARKER, Marker.PERSON_MARKER_IMG, nowLocation)
197-
viewModel.personCurrentLocation = userLocation
198-
if (tMap.isTracking) {
170+
when (first) {
171+
0 -> {
172+
first += 1
173+
}
174+
1 -> {
175+
beforeLocation = userLocation
199176
tMap.tMapView.setCenterPoint(userLocation.latitude, userLocation.longitude)
177+
tMap.addMarker(
178+
Marker.PERSON_MARKER,
179+
Marker.PERSON_MARKER_IMG,
180+
TMapPoint(userLocation.latitude, userLocation.longitude)
181+
)
182+
viewModel.personCurrentLocation = userLocation
183+
first += 1
184+
}
185+
else -> {
186+
Log.d("MissionWorker", "그리는 중 $userLocation $beforeLocation")
187+
val nowLocation = TMapPoint(userLocation.latitude, userLocation.longitude)
188+
tMap.drawMoveLine(
189+
nowLocation,
190+
TMapPoint(beforeLocation.latitude, beforeLocation.longitude),
191+
Marker.PERSON_LINE + PERSON_LINE_NUM.toString(),
192+
Marker.PERSON_LINE_COLOR
193+
)
194+
tMap.addMarker(Marker.PERSON_MARKER, Marker.PERSON_MARKER_IMG, nowLocation)
195+
viewModel.personCurrentLocation = userLocation
196+
if (tMap.isTracking) {
197+
tMap.tMapView.setCenterPoint(userLocation.latitude, userLocation.longitude)
198+
}
199+
beforeLocation = userLocation
200+
PERSON_LINE_NUM += 1
200201
}
201-
beforeLocation = userLocation
202-
PERSON_LINE_NUM += 1
203202
}
204203

205204
}

presentation/src/main/java/com/stop/ui/mission/MissionViewModel.kt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.stop.ui.mission
22

3+
import android.util.Log
34
import androidx.lifecycle.*
45
import androidx.work.OneTimeWorkRequestBuilder
56
import androidx.work.WorkManager
@@ -9,13 +10,14 @@ import com.stop.model.Location
910
import dagger.hilt.android.lifecycle.HiltViewModel
1011
import kotlinx.coroutines.delay
1112
import kotlinx.coroutines.launch
13+
import java.util.UUID
1214
import javax.inject.Inject
1315
import kotlin.random.Random
1416

1517
@HiltViewModel
1618
class MissionViewModel @Inject constructor(
1719
private val workManager: WorkManager,
18-
private val missionManager: MissionManager
20+
missionManager: MissionManager
1921
) : ViewModel() {
2022

2123
private val random = Random(System.currentTimeMillis())
@@ -56,6 +58,8 @@ class MissionViewModel @Inject constructor(
5658

5759
val userLocation = missionManager.userLocation
5860

61+
lateinit var requestId: UUID
62+
5963
init {
6064
makeMissionWorker()
6165
}
@@ -101,9 +105,16 @@ class MissionViewModel @Inject constructor(
101105
val workRequest = OneTimeWorkRequestBuilder<MissionWorker>()
102106
.build()
103107

108+
requestId = workRequest.id
109+
104110
workManager.enqueue(workRequest)
105111
}
106112

113+
fun cancelMission() {
114+
Log.d("MissionWorker","취소 되나연")
115+
workManager.cancelWorkById(requestId)
116+
}
117+
107118
companion object {
108119
private const val DELAY_TIME = 1000L
109120
private const val TIME_ZERO = 0

presentation/src/main/java/com/stop/ui/mission/MissionWorker.kt

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,6 @@ class MissionWorker @AssistedInject constructor(
5757
) {
5858
return
5959
}
60-
fusedLocationClient.lastLocation
61-
.addOnSuccessListener { location ->
62-
if (location == null) {
63-
Log.d("MissionWorker", "location get fail")
64-
} else {
65-
Log.d("MissionWorker", "initLocation(last) ${location.latitude} , ${location.longitude}")
66-
}
67-
}
68-
.addOnFailureListener {
69-
Log.d("MissionWorker", "location error is ${it.message}")
70-
it.printStackTrace()
71-
}
7260

7361
locationCallback = object : LocationCallback() {
7462
override fun onLocationResult(locationResult: LocationResult) {

presentation/src/main/res/layout/fragment_mission.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@
109109
android:background="@drawable/background_close_button"
110110
app:layout_constraintBottom_toBottomOf="@id/guideline_bottom"
111111
app:layout_constraintEnd_toEndOf="parent"
112-
app:layout_constraintStart_toStartOf="parent">
112+
app:layout_constraintStart_toStartOf="parent"
113+
android:onClick="@{() -> viewModel.cancelMission()}">
113114

114115
<ImageView
115116
android:id="@+id/image_view_mission_close"

0 commit comments

Comments
 (0)