Skip to content

Commit 61edb82

Browse files
committed
feat : background일 때 위치 받아오기
1 parent 2b1c7a5 commit 61edb82

File tree

9 files changed

+407
-317
lines changed

9 files changed

+407
-317
lines changed

presentation/src/main/AndroidManifest.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@
3838
android:authorities="${applicationId}.androidx-startup"
3939
tools:node="remove" />
4040

41+
<service
42+
android:name="androidx.work.impl.foreground.SystemForegroundService"
43+
android:foregroundServiceType="location"
44+
tools:node="merge" />
45+
4146
<activity
4247
android:name=".MainActivity"
4348
android:excludeFromRecents="true"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.stop.model
2+
3+
enum class State {
4+
FOREGROUND, BACKGROUND
5+
}

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

Lines changed: 170 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,22 @@ import android.animation.Animator
55
import android.animation.AnimatorListenerAdapter
66
import android.content.ContextWrapper
77
import android.os.Bundle
8+
import android.util.Log
89
import android.view.LayoutInflater
910
import android.view.View
1011
import android.view.ViewGroup
11-
import androidx.activity.result.contract.ActivityResultContracts
1212
import android.widget.Toast
13+
import androidx.activity.result.contract.ActivityResultContracts
1314
import androidx.fragment.app.Fragment
1415
import androidx.fragment.app.viewModels
1516
import androidx.lifecycle.lifecycleScope
1617
import com.skt.tmap.TMapPoint
1718
import com.stop.R
1819
import com.stop.databinding.FragmentMissionBinding
1920
import com.stop.model.Location
21+
import com.stop.model.State
2022
import com.stop.ui.util.Marker
2123
import dagger.hilt.android.AndroidEntryPoint
22-
import kotlinx.coroutines.delay
2324
import kotlinx.coroutines.launch
2425

2526
@AndroidEntryPoint
@@ -35,6 +36,12 @@ class MissionFragment : Fragment(), MissionHandler {
3536

3637
private var beforeLocation = INIT_LOCATION
3738

39+
private var points = arrayListOf<TMapPoint>()
40+
41+
private var backgroundPoints = arrayListOf<TMapPoint>()
42+
43+
private var state = State.FOREGROUND
44+
3845
override fun onCreateView(
3946
inflater: LayoutInflater, container: ViewGroup?,
4047
savedInstanceState: Bundle?
@@ -52,7 +59,21 @@ class MissionFragment : Fragment(), MissionHandler {
5259
initTMap()
5360
initView()
5461
setObserve()
55-
viewModel.makeMissionWorker("23:30:00")
62+
setNotification()
63+
64+
}
65+
66+
override fun onResume() {
67+
super.onResume()
68+
Log.d("MissionWorker", "onResume")
69+
70+
state = State.FOREGROUND
71+
}
72+
73+
override fun onStop() {
74+
super.onStop()
75+
Log.d("MissionWorker", "onStop")
76+
state = State.BACKGROUND
5677
}
5778

5879
override fun onDestroyView() {
@@ -91,18 +112,6 @@ class MissionFragment : Fragment(), MissionHandler {
91112
)
92113

93114
tMap.isTracking = true
94-
tMap.isTransportTracking = false
95-
}
96-
97-
binding.layoutBusCurrent.setOnClickListener {
98-
tMap.tMapView.setCenterPoint(
99-
viewModel.busCurrentLocation.latitude,
100-
viewModel.busCurrentLocation.longitude,
101-
true
102-
)
103-
104-
tMap.isTracking = false
105-
tMap.isTransportTracking = true
106115
}
107116

108117
}
@@ -154,90 +163,89 @@ class MissionFragment : Fragment(), MissionHandler {
154163
}
155164
}
156165

157-
private fun drawBusLocationLine() {
158-
viewModel.busNowLocationInfo.observe(viewLifecycleOwner) { nowLocations ->
159-
val nowLocation = nowLocations.first()
160-
161-
if (beforeLocation != INIT_LOCATION) {
162-
tMap.drawMoveLine(
163-
TMapPoint(nowLocation.latitude.toDouble(), nowLocation.longitude.toDouble()),
164-
TMapPoint(beforeLocation.latitude, beforeLocation.longitude),
165-
Marker.BUS_LINE + BUS_LINE_NUM.toString(),
166-
Marker.BUS_LINE_COLOR
167-
)
168-
BUS_LINE_NUM += 1
169-
}
170-
beforeLocation = Location(nowLocation.latitude.toDouble(), nowLocation.longitude.toDouble())
171-
172-
viewModel.busCurrentLocation = beforeLocation
173-
174-
tMap.addMarker(
175-
Marker.BUS_MARKER,
176-
Marker.BUS_MARKER_IMG,
177-
TMapPoint(nowLocation.latitude.toDouble(), nowLocation.longitude.toDouble())
178-
)
179-
tMap.trackingTransport(beforeLocation)
180-
}
181-
}
182-
183-
private fun drawSubwayLocationLine() {
184-
viewModel.subwayRoute.observe(viewLifecycleOwner) { subwayRoute ->
185-
viewLifecycleOwner.lifecycleScope.launch {
186-
val timeUnit = (subwayRoute.sectionTime * SECOND_UNIT / subwayRoute.line.size).toLong()
187-
subwayRoute.line.forEachIndexed { index, nowLocation ->
188-
if (index == 0) {
189-
return@forEachIndexed
190-
}
191-
192-
val beforeLocation = subwayRoute.line[index - 1]
193-
tMap.drawMoveLine(
194-
TMapPoint(nowLocation.latitude, nowLocation.longitude),
195-
TMapPoint(beforeLocation.latitude, beforeLocation.longitude),
196-
Marker.SUBWAY_LINE + (index - 1).toString(),
197-
Marker.SUBWAY_LINE_COLOR
198-
)
199-
200-
viewModel.busCurrentLocation = Location(nowLocation.latitude, nowLocation.longitude)
201-
202-
tMap.addMarker(
203-
Marker.SUBWAY_MARKER,
204-
Marker.SUBWAY_MARKER_IMG,
205-
TMapPoint(nowLocation.latitude, nowLocation.longitude)
206-
)
207-
208-
delay(timeUnit)
209-
}
210-
}
211-
212-
}
213-
}
166+
// private fun drawBusLocationLine() {
167+
// viewModel.busNowLocationInfo.observe(viewLifecycleOwner) { nowLocations ->
168+
// val nowLocation = nowLocations.first()
169+
//
170+
// if (beforeLocation != INIT_LOCATION) {
171+
// tMap.drawMoveLine(
172+
// TMapPoint(nowLocation.latitude.toDouble(), nowLocation.longitude.toDouble()),
173+
// TMapPoint(beforeLocation.latitude, beforeLocation.longitude),
174+
// Marker.BUS_LINE + BUS_LINE_NUM.toString(),
175+
// Marker.BUS_LINE_COLOR
176+
// )
177+
// BUS_LINE_NUM += 1
178+
// }
179+
// beforeLocation = Location(nowLocation.latitude.toDouble(), nowLocation.longitude.toDouble())
180+
//
181+
// viewModel.busCurrentLocation = beforeLocation
182+
//
183+
// tMap.addMarker(
184+
// Marker.BUS_MARKER,
185+
// Marker.BUS_MARKER_IMG,
186+
// TMapPoint(nowLocation.latitude.toDouble(), nowLocation.longitude.toDouble())
187+
// )
188+
// tMap.trackingTransport(beforeLocation)
189+
// }
190+
// }
191+
//
192+
// private fun drawSubwayLocationLine() {
193+
// viewModel.subwayRoute.observe(viewLifecycleOwner) { subwayRoute ->
194+
// viewLifecycleOwner.lifecycleScope.launch {
195+
// val timeUnit = (subwayRoute.sectionTime * SECOND_UNIT / subwayRoute.line.size).toLong()
196+
// subwayRoute.line.forEachIndexed { index, nowLocation ->
197+
// if (index == 0) {
198+
// return@forEachIndexed
199+
// }
200+
//
201+
// val beforeLocation = subwayRoute.line[index - 1]
202+
// tMap.drawMoveLine(
203+
// TMapPoint(nowLocation.latitude, nowLocation.longitude),
204+
// TMapPoint(beforeLocation.latitude, beforeLocation.longitude),
205+
// Marker.SUBWAY_LINE + (index - 1).toString(),
206+
// Marker.SUBWAY_LINE_COLOR
207+
// )
208+
//
209+
// viewModel.busCurrentLocation = Location(nowLocation.latitude, nowLocation.longitude)
210+
//
211+
// tMap.addMarker(
212+
// Marker.SUBWAY_MARKER,
213+
// Marker.SUBWAY_MARKER_IMG,
214+
// TMapPoint(nowLocation.latitude, nowLocation.longitude)
215+
// )
216+
//
217+
// delay(timeUnit)
218+
// }
219+
// }
220+
//
221+
// }
222+
// }
214223

215224
override fun alertTMapReady() {
216-
//mimicUserMove()
217225
requestPermissionsLauncher.launch(PERMISSIONS)
218226
tMap.setTrackingMode()
219-
drawBusLocationLine()
220-
drawSubwayLocationLine()
227+
// drawBusLocationLine()
228+
// drawSubwayLocationLine()
221229
}
222230

223231
override fun setOnLocationChangeListener(nowLocation: TMapPoint, beforeLocation: TMapPoint, canMakeLine: Boolean) {
224-
if (canMakeLine) {
225-
tMap.drawMoveLine(
226-
nowLocation,
227-
beforeLocation,
228-
Marker.PERSON_LINE + PERSON_LINE_NUM.toString(),
229-
Marker.PERSON_LINE_COLOR
230-
)
231-
PERSON_LINE_NUM += 1
232-
}
233-
viewModel.personCurrentLocation = Location(nowLocation.latitude, nowLocation.longitude)
232+
// if (canMakeLine) {
233+
// tMap.drawMoveLine(
234+
// nowLocation,
235+
// beforeLocation,
236+
// Marker.PERSON_LINE + PERSON_LINE_NUM.toString(),
237+
// Marker.PERSON_LINE_COLOR
238+
// )
239+
// PERSON_LINE_NUM += 1
240+
// }
241+
234242
}
235243

236244
override fun setOnEnableScrollWithZoomLevelListener() {
237245
tMap.apply {
238246
tMapView.setOnEnableScrollWithZoomLevelListener { _, _ ->
239247
isTracking = false
240-
isTransportTracking = false
248+
// isTransportTracking = false
241249
}
242250
}
243251
}
@@ -250,6 +258,82 @@ class MissionFragment : Fragment(), MissionHandler {
250258
}
251259
}
252260

261+
private fun setNotification() {
262+
test()
263+
}
264+
265+
private fun test() {
266+
var first = 0
267+
lifecycleScope.launch {
268+
viewModel.userLocation.collect { userLocation ->
269+
if (first == 0) {
270+
first += 1
271+
} else if (first == 1) {
272+
points.add(userLocation)
273+
first += 1
274+
} else {
275+
tMap.tMapView.removeTMapPolyLine(Marker.PERSON_LINE + PERSON_LINE_NUM.toString())
276+
Log.d("MissionWorker", "drawLine $points")
277+
points.add(userLocation)
278+
tMap.drawMoveLine(
279+
points,
280+
Marker.PERSON_LINE + PERSON_LINE_NUM.toString(),
281+
Marker.PERSON_LINE_COLOR
282+
)
283+
//PERSON_LINE_NUM += 1
284+
285+
}
286+
287+
}
288+
289+
}
290+
}
291+
292+
private fun drawPersonLine() {
293+
lateinit var nowLocation: TMapPoint
294+
lateinit var beforeLocation: TMapPoint
295+
//if (viewModel.userState.value is UserState.Foreground) {
296+
// nowLocation = viewModel.getLocation()
297+
// if (PERSON_LINE_NUM == 0) {
298+
// viewModel.initLocation.observe(viewLifecycleOwner){
299+
// beforeLocation = it
300+
// }
301+
// }
302+
// tMap.drawMoveLine(
303+
// nowLocation,
304+
// beforeLocation,
305+
// Marker.PERSON_LINE + PERSON_LINE_NUM.toString(),
306+
// Marker.PERSON_LINE_COLOR
307+
// )
308+
// PERSON_LINE_NUM += 1
309+
// beforeLocation = nowLocation
310+
// viewModel.personCurrentLocation = Location(nowLocation.latitude, nowLocation.longitude)
311+
//
312+
// Log.d("MissionWorker", "location value ${viewModel.userState.value}")
313+
// }
314+
// else {
315+
// val allLocation = viewModel.getAllLocation()
316+
// allLocation.forEachIndexed { index, nowLocationInAll ->
317+
// if (index == 0) {
318+
// tMap.drawMoveLine(
319+
// nowLocationInAll,
320+
// beforeLocation,
321+
// Marker.PERSON_LINE + PERSON_LINE_NUM.toString(),
322+
// Marker.PERSON_LINE_COLOR
323+
// )
324+
// } else {
325+
// tMap.drawMoveLine(
326+
// nowLocationInAll,
327+
// beforeLocation,
328+
// Marker.PERSON_LINE + PERSON_LINE_NUM.toString(),
329+
// Marker.PERSON_LINE_COLOR
330+
// )
331+
// }
332+
// beforeLocation = nowLocationInAll
333+
// }
334+
// }
335+
}
336+
253337
companion object {
254338

255339
private const val DESTINATION = "구로3동현대아파트"
Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,28 @@
11
package com.stop.ui.mission
22

3-
import com.stop.model.Location
3+
import com.skt.tmap.TMapPoint
4+
import kotlinx.coroutines.flow.MutableStateFlow
5+
import kotlinx.coroutines.flow.StateFlow
6+
import kotlinx.coroutines.flow.asStateFlow
47

8+
class MissionManager {
59

6-
class MissionManager() {
10+
var initLocation = MutableStateFlow(TMapPoint())
11+
var userLocation = MutableStateFlow(TMapPoint())
712

8-
val personMovements = mutableListOf<Location>()
13+
private var _userState = MutableStateFlow<UserState>(UserState.Foreground())
14+
val userState: StateFlow<UserState>
15+
get() = _userState.asStateFlow()
916

17+
fun setLocation(location: TMapPoint) {
18+
_userState.value = UserState.Foreground(location)
19+
}
20+
21+
}
22+
23+
sealed interface UserState {
24+
25+
data class Foreground(val location: TMapPoint = TMapPoint()) : UserState
26+
27+
data class Background(val locations: List<TMapPoint> = emptyList()) : UserState
1028
}

0 commit comments

Comments
 (0)