Skip to content

Commit a228165

Browse files
committed
feat : Searchkeyword flow로 변경하여 debounce 통해 쿼리 쏘기
1 parent 447d9c3 commit a228165

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

presentation/src/main/java/com/stop/ui/placesearch/PlaceSearchFragment.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ import androidx.navigation.findNavController
1717
import com.stop.R
1818
import com.stop.databinding.FragmentPlaceSearchBinding
1919
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
2024
import kotlinx.coroutines.launch
2125

2226
@AndroidEntryPoint
@@ -46,6 +50,7 @@ class PlaceSearchFragment : Fragment() {
4650
initBinding()
4751
listenEditTextChange()
4852
logErrorMessage()
53+
observeSearchKeyword()
4954
}
5055

5156
private fun initAdapter() {
@@ -118,6 +123,19 @@ class PlaceSearchFragment : Fragment() {
118123
}
119124
}
120125

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+
121139
override fun onDestroyView() {
122140
_binding = null
123141

presentation/src/main/java/com/stop/ui/placesearch/PlaceSearchViewModel.kt

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,31 +37,24 @@ class PlaceSearchViewModel @Inject constructor(
3737
private val clickCurrentLocationChannel = Channel<Boolean>()
3838
val clickCurrentLocation = clickCurrentLocationChannel.receiveAsFlow()
3939

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
4642

47-
if (s.toString().isBlank()) {
48-
setNearPlaceListEmpty()
49-
}
43+
fun afterTextChanged(s: Editable?) {
44+
_searchKeyword.value = s.toString()
5045
}
5146

52-
private fun getNearPlaces(
47+
fun getNearPlaces(
5348
searchKeyword: String,
54-
centerLon: Double,
55-
centerLat: Double
5649
) {
5750
viewModelScope.launch {
5851
try {
5952
_nearPlaceList.emit(
6053
getNearPlacesUseCase.getNearPlaces(
6154
TMAP_VERSION,
6255
searchKeyword,
63-
centerLon,
64-
centerLat,
56+
currentLocation.longitude,
57+
currentLocation.latitude,
6558
BuildConfig.TMAP_APP_KEY
6659
)
6760
)

0 commit comments

Comments
 (0)