Skip to content

Commit c1b2f7c

Browse files
authored
Merge pull request #40 from boostcampwm-2022/8-feature-button
[PR] ํ˜„์žฌ ์œ„์น˜ ํด๋ฆญ ์‹œ flow ํ†ตํ•ด ์ด๋ฒคํŠธ ๋ฐœ์ƒ์‹œ์ผœ ๋งˆ์ปค ๋ฐ ํŒจ๋„ ๋„์šฐ๊ธฐ ๊ตฌํ˜„
2 parents a08a949 + 0c4a797 commit c1b2f7c

File tree

6 files changed

+54
-19
lines changed

6 files changed

+54
-19
lines changed

โ€Ždata/src/main/java/com/stop/data/repository/NearPlaceRepositoryImpl.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ internal class NearPlaceRepositoryImpl @Inject constructor(
1717
centerLon: Double,
1818
centerLat: Double,
1919
appKey: String
20-
): Flow<List<Place>> = flow<List<Place>> {
20+
): Flow<List<Place>> = flow {
2121
emit(
2222
nearPlaceRemoteDataSource.getNearPlaces(
2323
version,
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
package com.stop.model
22

3-
open class Event<out T>(private val content : T){
3+
class Event<out T>(private val content: T) {
44

55
var hasBeenHandled = false
66
private set
77

8-
fun getContentIfNotHandled() : T?{
9-
return if (hasBeenHandled){
8+
fun getContentIfNotHandled(): T? {
9+
return if (hasBeenHandled) {
1010
null
11-
}else {
11+
} else {
1212
hasBeenHandled = true
1313
content
1414
}
1515
}
1616

17-
fun peekContent() : T = content
17+
fun peekContent(): T = content
1818

1919
}

โ€Žpresentation/src/main/java/com/stop/ui/map/MapFragment.kt

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package com.stop.ui.map
22

33
import android.Manifest.permission
44
import android.os.Bundle
5-
import android.util.Log
65
import android.view.LayoutInflater
76
import android.view.View
87
import android.view.ViewGroup
@@ -11,6 +10,8 @@ import androidx.core.content.ContextCompat
1110
import androidx.core.graphics.drawable.toBitmap
1211
import androidx.fragment.app.Fragment
1312
import androidx.fragment.app.activityViewModels
13+
import androidx.lifecycle.flowWithLifecycle
14+
import androidx.lifecycle.lifecycleScope
1415
import androidx.navigation.findNavController
1516
import com.skt.tmap.TMapData
1617
import com.skt.tmap.TMapGpsManager
@@ -158,13 +159,8 @@ class MapFragment : Fragment() {
158159
this.tMapPoint = location
159160
}
160161

161-
try {
162-
tMapView.removeTMapMarkerItem(MARKER)
163-
tMapView.addTMapMarkerItem(marker)
164-
}catch (e : Exception){
165-
Log.e("ABC", e.printStackTrace().toString())
166-
}
167-
162+
tMapView.removeTMapMarkerItem(MARKER)
163+
tMapView.addTMapMarkerItem(marker)
168164
}
169165

170166
private fun initBinding() {
@@ -196,6 +192,7 @@ class MapFragment : Fragment() {
196192
requestPermissionsLauncher.launch(PERMISSIONS)
197193

198194
observeClickPlace()
195+
observeClickCurrentLocation()
199196
}
200197
tMapView.setOnEnableScrollWithZoomLevelListener { _, _ ->
201198
isTracking = false
@@ -260,6 +257,28 @@ class MapFragment : Fragment() {
260257
}
261258
}
262259

260+
private fun observeClickCurrentLocation() {
261+
viewLifecycleOwner.lifecycleScope.launch {
262+
launch {
263+
placeSearchViewModel.clickCurrentLocation
264+
.flowWithLifecycle(viewLifecycleOwner.lifecycle)
265+
.collect {
266+
val currentLocation = placeSearchViewModel.currentLocation
267+
val currentTmapPoint = TMapPoint(currentLocation.latitude, currentLocation.longitude)
268+
269+
tMapView.setCenterPoint(currentTmapPoint.latitude, currentTmapPoint.longitude)
270+
271+
setPanel(currentTmapPoint)
272+
273+
makeMarker(
274+
R.drawable.ic_baseline_location_on_32,
275+
currentTmapPoint
276+
)
277+
}
278+
}
279+
}
280+
}
281+
263282
override fun onDestroyView() {
264283
_binding = null
265284
super.onDestroyView()

โ€Žpresentation/src/main/java/com/stop/ui/nearplace/PlaceSearchFragment.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,20 @@ class PlaceSearchFragment : Fragment() {
5555
nearPlaceAdapter.onItemClick = {
5656
placeSearchViewModel.setClickPlace(it)
5757
placeSearchViewModel.setNearPlaceListEmpty()
58+
5859
binding.root.findNavController().navigate(R.id.action_placeSearchFragment_to_mapFragment)
5960
}
6061
}
6162

6263
private fun buttonClick() {
6364
with(binding) {
6465
textViewCurrentLocation.setOnClickListener {
65-
root.findNavController().navigate(R.id.action_placeSearchFragment_to_mapFragment)
66+
placeSearchViewModel.setClickCurrentLocation()
67+
binding.root.findNavController().navigate(R.id.action_placeSearchFragment_to_mapFragment)
6668
}
6769

6870
textViewSelectMap.setOnClickListener {
69-
root.findNavController().navigate(R.id.action_placeSearchFragment_to_mapFragment)
71+
binding.root.findNavController().navigate(R.id.action_placeSearchFragment_to_mapFragment)
7072
}
7173
}
7274
}

โ€Žpresentation/src/main/java/com/stop/ui/nearplace/PlaceSearchViewModel.kt

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,14 @@ class PlaceSearchViewModel @Inject constructor(
2727
private val _nearPlaceList = MutableLiveData<List<Place>>()
2828
val nearPlaceList: LiveData<List<Place>> = _nearPlaceList
2929

30+
private val errorMessageChannel = Channel<String>()
31+
val errorMessage = errorMessageChannel.receiveAsFlow()
32+
3033
private val _clickPlace = MutableLiveData<Event<Place>>()
3134
val clickPlace: LiveData<Event<Place>> = _clickPlace
3235

33-
private val eventChannel = Channel<String>()
34-
val errorMessage = eventChannel.receiveAsFlow()
36+
private val clickCurrentLocationChannel = Channel<Boolean>()
37+
val clickCurrentLocation = clickCurrentLocationChannel.receiveAsFlow()
3538

3639

3740
fun afterTextChanged(s: Editable?) {
@@ -64,7 +67,7 @@ class PlaceSearchViewModel @Inject constructor(
6467
}
6568
} catch (e: Exception) {
6669
setNearPlaceListEmpty()
67-
eventChannel.send(e.message ?: "something is wrong")
70+
errorMessageChannel.send(e.message ?: "something is wrong")
6871
}
6972
}
7073
}
@@ -77,6 +80,12 @@ class PlaceSearchViewModel @Inject constructor(
7780
_clickPlace.value = Event(place)
7881
}
7982

83+
fun setClickCurrentLocation() {
84+
viewModelScope.launch {
85+
clickCurrentLocationChannel.send(true)
86+
}
87+
}
88+
8089
companion object {
8190
private const val TMAP_VERSION = 1
8291
}

โ€Žpresentation/src/main/res/navigation/nav_graph.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
<action
1414
android:id="@+id/action_mapFragment_to_placeSearchFragment"
1515
app:destination="@id/placeSearchFragment" />
16+
<argument
17+
android:name="currentLocation"
18+
app:argType="string"
19+
app:nullable="true"
20+
android:defaultValue="@null"/>
1621
</fragment>
1722
<fragment
1823
android:id="@+id/placeSearchFragment"

0 commit comments

Comments
ย (0)