Skip to content

Commit 39dc821

Browse files
committed
fix : 장소 검색 초기화 되는 문제 해결
1 parent 556e055 commit 39dc821

File tree

4 files changed

+46
-33
lines changed

4 files changed

+46
-33
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.stop.bindingadapter
2+
3+
import android.text.Editable
4+
import android.text.TextWatcher
5+
import android.widget.EditText
6+
import kotlinx.coroutines.channels.awaitClose
7+
import kotlinx.coroutines.flow.Flow
8+
import kotlinx.coroutines.flow.callbackFlow
9+
import kotlinx.coroutines.flow.onStart
10+
11+
fun EditText.textChangesToFlow(): Flow<CharSequence?> {
12+
return callbackFlow {
13+
val listener = object : TextWatcher {
14+
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) = Unit
15+
override fun afterTextChanged(s: Editable?) = Unit
16+
override fun onTextChanged(text: CharSequence?, start: Int, before: Int, count: Int) {
17+
trySend(text)
18+
}
19+
}
20+
addTextChangedListener(listener)
21+
awaitClose { removeTextChangedListener(listener) }
22+
}.onStart {
23+
emit(text)
24+
}
25+
}

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

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ import androidx.lifecycle.flowWithLifecycle
1515
import androidx.lifecycle.lifecycleScope
1616
import androidx.navigation.findNavController
1717
import com.stop.R
18+
import com.stop.bindingadapter.textChangesToFlow
1819
import com.stop.databinding.FragmentPlaceSearchBinding
1920
import com.stop.domain.model.nearplace.PlaceUseCaseItem
2021
import dagger.hilt.android.AndroidEntryPoint
22+
import kotlinx.coroutines.Dispatchers
2123
import kotlinx.coroutines.FlowPreview
2224
import kotlinx.coroutines.flow.debounce
2325
import kotlinx.coroutines.flow.launchIn
@@ -64,10 +66,10 @@ class PlaceSearchFragment : Fragment() {
6466
}
6567

6668
private fun initAdapter() {
67-
placeSearchAdapter = PlaceSearchAdapter{
69+
placeSearchAdapter = PlaceSearchAdapter {
6870
clickPlace(it)
6971
}
70-
recentPlaceSearchAdapter = RecentPlaceSearchAdapter{
72+
recentPlaceSearchAdapter = RecentPlaceSearchAdapter {
7173
clickPlace(it)
7274
}
7375

@@ -136,16 +138,17 @@ class PlaceSearchFragment : Fragment() {
136138
}
137139

138140
@OptIn(FlowPreview::class)
139-
private fun observeSearchKeyword(){
140-
placeSearchViewModel.searchKeyword.debounce(100)
141-
.onEach {
142-
if(it.isBlank()){
143-
placeSearchViewModel.setNearPlacesEmpty()
144-
}else{
145-
placeSearchViewModel.getNearPlaces(it)
141+
private fun observeSearchKeyword() {
142+
lifecycleScope.launch(Dispatchers.IO) {
143+
val editTextFlow = binding.textInputEditTextPlaceSearch.textChangesToFlow()
144+
145+
editTextFlow
146+
.debounce(500)
147+
.onEach {
148+
placeSearchViewModel.getNearPlaces(it.toString())
146149
}
147-
}
148-
.launchIn(lifecycleScope)
150+
.launchIn(this)
151+
}
149152
}
150153

151154
override fun onDestroyView() {

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

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.stop.ui.placesearch
22

3-
import android.text.Editable
43
import android.view.View
54
import androidx.lifecycle.LiveData
65
import androidx.lifecycle.MutableLiveData
@@ -28,12 +27,13 @@ import kotlin.math.round
2827
class PlaceSearchViewModel @Inject constructor(
2928
private val getNearPlacesUseCase: GetNearPlacesUseCase,
3029
private val geoLocationUseCase: GeoLocationUseCase,
31-
private val getRecentPlaceSearchUseCase: GetRecentPlaceSearchUseCase,
30+
getRecentPlaceSearchUseCase: GetRecentPlaceSearchUseCase,
3231
private val deleteRecentPlaceSearchUseCase: DeleteRecentPlaceSearchUseCase,
3332
private val insertRecentPlaceSearchUseCase: InsertRecentPlaceSearchUseCase
3433
) : ViewModel() {
3534

36-
var currentLocation = Location(0.0, 0.0)
35+
// 기본 주소로 서울역 주소 지정
36+
var currentLocation = Location(37.553836, 126.969652)
3737

3838
var panelInfo: com.stop.model.route.Place? = null
3939

@@ -52,9 +52,6 @@ class PlaceSearchViewModel @Inject constructor(
5252
private val clickCurrentLocationChannel = Channel<Boolean>()
5353
val clickCurrentLocation = clickCurrentLocationChannel.receiveAsFlow()
5454

55-
private val _searchKeyword = MutableStateFlow("")
56-
val searchKeyword: StateFlow<String> = _searchKeyword
57-
5855
private val _geoLocation = MutableLiveData<GeoLocationInfo>()
5956
val geoLocation: LiveData<GeoLocationInfo> = _geoLocation
6057

@@ -64,17 +61,9 @@ class PlaceSearchViewModel @Inject constructor(
6461
private val _distance = MutableLiveData<Float>()
6562
val distance: LiveData<Float> = _distance
6663

67-
val recentPlaceSearch: StateFlow<List<PlaceUseCaseItem>> =
68-
getRecentPlaceSearchUseCase.getAllRecentPlaceSearch()
69-
.stateIn(
70-
scope = viewModelScope,
71-
started = SharingStarted.WhileSubscribed(),
72-
initialValue = emptyList()
73-
)
74-
75-
fun afterTextChanged(s: Editable?) {
76-
_searchKeyword.value = s.toString()
77-
}
64+
val recentPlaceSearch: StateFlow<List<PlaceUseCaseItem>> = getRecentPlaceSearchUseCase.getAllRecentPlaceSearch().stateIn(
65+
scope = viewModelScope, started = SharingStarted.WhileSubscribed(), initialValue = emptyList()
66+
)
7867

7968
fun getNearPlaces(
8069
searchKeyword: String,
@@ -83,9 +72,7 @@ class PlaceSearchViewModel @Inject constructor(
8372
try {
8473
_nearPlaces.emit(
8574
getNearPlacesUseCase.getNearPlaces(
86-
searchKeyword,
87-
currentLocation.longitude,
88-
currentLocation.latitude
75+
searchKeyword, currentLocation.longitude, currentLocation.latitude
8976
)
9077
)
9178

@@ -99,7 +86,6 @@ class PlaceSearchViewModel @Inject constructor(
9986

10087
fun setNearPlacesEmpty() {
10188
_nearPlaces.value = emptyList()
102-
_searchKeyword.value = ""
10389
_isNearPlacesNotEmpty.value = false
10490
}
10591

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@
5555
android:id="@+id/text_input_edit_text_place_search"
5656
android:layout_width="match_parent"
5757
android:layout_height="wrap_content"
58-
android:afterTextChanged="@{viewModel::afterTextChanged}"
5958
android:hint="@string/location_address_search_text"
6059
android:imeOptions="actionSearch"
6160
android:inputType="text" />

0 commit comments

Comments
 (0)