Skip to content

Commit

Permalink
[chore] #243 검색 + 지도 뷰에서 칩 필터링을 진행하는 경우 검색 결과가 없어도 지도뷰 유지
Browse files Browse the repository at this point in the history
  • Loading branch information
jihyunniiii committed Mar 11, 2024
1 parent 1775e47 commit 0b48ceb
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,14 @@ class HomeFragment : BindingFragment<FragmentHomeBinding>(R.layout.fragment_home
homeViewModel.searchWord.flowWithLifecycle(viewLifecycleOwner.lifecycle).distinctUntilChanged(),
homeViewModel.mainListOrderType.flowWithLifecycle(viewLifecycleOwner.lifecycle).distinctUntilChanged(),
homeViewModel.homeViewType.flowWithLifecycle(viewLifecycleOwner.lifecycle).distinctUntilChanged()
) { _, _, _, homeViewType ->
homeViewType
}.onEach { homeViewType ->
when (homeViewType) {
) { _, searchWord, _, homeViewType ->
Pair(searchWord != homeViewModel.lastSearchWord, homeViewType)
}.onEach { searchStatusAndHomeViewType ->
when (searchStatusAndHomeViewType.second) {
HomeViewType.MAIN_LIST -> homeViewModel.getMainListPingleList()
HomeViewType.MAP -> homeViewModel.getPinListWithoutFilter()
HomeViewType.MAP -> homeViewModel.getPinListWithoutFilter(isSearching = searchStatusAndHomeViewType.first)
}
homeViewModel.setLastSearchWord(homeViewModel.searchWord.value)
}.launchIn(viewLifecycleOwner.lifecycleScope)

homeViewModel.homeViewType.flowWithLifecycle(viewLifecycleOwner.lifecycle).onEach {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,15 @@ class HomeViewModel @Inject constructor(
}

init {
localStorage.sharedPreference.registerOnSharedPreferenceChangeListener(sharedPreferenceChangeListener)
localStorage.sharedPreference.registerOnSharedPreferenceChangeListener(
sharedPreferenceChangeListener
)
}

override fun onCleared() {
localStorage.sharedPreference.unregisterOnSharedPreferenceChangeListener(sharedPreferenceChangeListener)
localStorage.sharedPreference.unregisterOnSharedPreferenceChangeListener(
sharedPreferenceChangeListener
)
super.onCleared()
}

Expand All @@ -69,7 +73,11 @@ class HomeViewModel @Inject constructor(
private var _searchWord = MutableStateFlow<String?>(null)
val searchWord get() = _searchWord.asStateFlow()

private val _pinEntityListState = MutableStateFlow<UiState<List<PinEntity>>>(UiState.Empty)
private var _lastSearchWord: String? = null
val lastSearchWord get() = _lastSearchWord

private val _pinEntityListState =
MutableStateFlow<UiState<Pair<Boolean, List<PinEntity>>>>(UiState.Empty)
val pinEntityListState get() = _pinEntityListState.asStateFlow()

private var _markerModelData =
Expand Down Expand Up @@ -115,6 +123,11 @@ class HomeViewModel @Inject constructor(

fun clearSearchWord() {
_searchWord.value = null
_lastSearchWord = null
}

fun setLastSearchWord(searchWord: String?) {
_lastSearchWord = searchWord
}

private fun setMarkerModelListIsSelected(position: Int) {
Expand Down Expand Up @@ -250,19 +263,19 @@ class HomeViewModel @Inject constructor(
}
}

fun getPinListWithoutFilter() {
fun getPinListWithoutFilter(isSearching: Boolean = false) {
viewModelScope.launch {
_pinEntityListState.value = UiState.Loading
if (_searchWord.value?.isBlank() == true) {
_pinEntityListState.emit(UiState.Success(emptyList()))
_pinEntityListState.emit(UiState.Success(Pair(isSearching, emptyList())))
} else {
runCatching {
getPinListWithoutFilteringUseCase(
teamId = localStorage.groupId.toLong(),
category = _category.value?.name,
searchWord = _searchWord.value
).collect() { pinList ->
_pinEntityListState.value = UiState.Success(pinList)
_pinEntityListState.value = UiState.Success(Pair(isSearching, pinList))
}
}.onFailure { exception: Throwable ->
_pinEntityListState.value = UiState.Error(exception.message)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,20 +209,25 @@ class MapFragment : BindingFragment<FragmentMapBinding>(R.layout.fragment_map),
when (uiState) {
is UiState.Success -> {
if (::naverMap.isInitialized) {
makeMarkers(uiState.data)
makeMarkers(uiState.data.second)
mapCardAdapter.clearData()
homeViewModel.clearSelectedMarkerPosition()
}

homeViewModel.searchWord.value?.let { searchWord ->
AmplitudeUtils.trackEventWithProperty(
eventName = COMPLETE_SEARCH_MAP,
propertyName = KEYWORD,
propertyValue = searchWord
)
when {
uiState.data.isEmpty() -> homeViewModel.setHomeViewType(HomeViewType.MAIN_LIST)
else -> moveMapCamera(homeViewModel.markerModelData.value.second[FIRST_INDEX].marker.position)
if (uiState.data.first) {
homeViewModel.searchWord.value?.let { searchWord ->
AmplitudeUtils.trackEventWithProperty(
eventName = COMPLETE_SEARCH_MAP,
propertyName = KEYWORD,
propertyValue = searchWord
)
when {
uiState.data.second.isEmpty() -> homeViewModel.setHomeViewType(
HomeViewType.MAIN_LIST
)

else -> moveMapCamera(homeViewModel.markerModelData.value.second[FIRST_INDEX].marker.position)
}
}
}
}
Expand Down

0 comments on commit 0b48ceb

Please sign in to comment.