File tree Expand file tree Collapse file tree 2 files changed +25
-14
lines changed
presentation/src/main/java/com/stop/ui/placesearch Expand file tree Collapse file tree 2 files changed +25
-14
lines changed Original file line number Diff line number Diff line change @@ -17,6 +17,10 @@ import androidx.navigation.findNavController
17
17
import com.stop.R
18
18
import com.stop.databinding.FragmentPlaceSearchBinding
19
19
import dagger.hilt.android.AndroidEntryPoint
20
+ import kotlinx.coroutines.FlowPreview
21
+ import kotlinx.coroutines.flow.debounce
22
+ import kotlinx.coroutines.flow.launchIn
23
+ import kotlinx.coroutines.flow.onEach
20
24
import kotlinx.coroutines.launch
21
25
22
26
@AndroidEntryPoint
@@ -46,6 +50,7 @@ class PlaceSearchFragment : Fragment() {
46
50
initBinding()
47
51
listenEditTextChange()
48
52
logErrorMessage()
53
+ observeSearchKeyword()
49
54
}
50
55
51
56
private fun initAdapter () {
@@ -118,6 +123,19 @@ class PlaceSearchFragment : Fragment() {
118
123
}
119
124
}
120
125
126
+ @OptIn(FlowPreview ::class )
127
+ private fun observeSearchKeyword (){
128
+ placeSearchViewModel.searchKeyword.debounce(100 )
129
+ .onEach {
130
+ if (it.isBlank()){
131
+ placeSearchViewModel.setNearPlaceListEmpty()
132
+ }else {
133
+ placeSearchViewModel.getNearPlaces(it)
134
+ }
135
+ }
136
+ .launchIn(lifecycleScope)
137
+ }
138
+
121
139
override fun onDestroyView () {
122
140
_binding = null
123
141
Original file line number Diff line number Diff line change @@ -37,31 +37,24 @@ class PlaceSearchViewModel @Inject constructor(
37
37
private val clickCurrentLocationChannel = Channel <Boolean >()
38
38
val clickCurrentLocation = clickCurrentLocationChannel.receiveAsFlow()
39
39
40
- fun afterTextChanged (s : Editable ? ) {
41
- getNearPlaces(
42
- s.toString(),
43
- currentLocation.longitude,
44
- currentLocation.latitude
45
- )
40
+ private val _searchKeyword = MutableStateFlow (" " )
41
+ val searchKeyword : StateFlow <String > = _searchKeyword
46
42
47
- if (s.toString().isBlank()) {
48
- setNearPlaceListEmpty()
49
- }
43
+ fun afterTextChanged (s : Editable ? ) {
44
+ _searchKeyword .value = s.toString()
50
45
}
51
46
52
- private fun getNearPlaces (
47
+ fun getNearPlaces (
53
48
searchKeyword : String ,
54
- centerLon : Double ,
55
- centerLat : Double
56
49
) {
57
50
viewModelScope.launch {
58
51
try {
59
52
_nearPlaceList .emit(
60
53
getNearPlacesUseCase.getNearPlaces(
61
54
TMAP_VERSION ,
62
55
searchKeyword,
63
- centerLon ,
64
- centerLat ,
56
+ currentLocation.longitude ,
57
+ currentLocation.latitude ,
65
58
BuildConfig .TMAP_APP_KEY
66
59
)
67
60
)
You can’t perform that action at this time.
0 commit comments