From 0b48cebcefde31e7b0ba3686a28f6d1121699601 Mon Sep 17 00:00:00 2001 From: jihyunniiii Date: Mon, 11 Mar 2024 22:00:40 +0900 Subject: [PATCH] =?UTF-8?q?[chore]=20#243=20=EA=B2=80=EC=83=89=20+=20?= =?UTF-8?q?=EC=A7=80=EB=8F=84=20=EB=B7=B0=EC=97=90=EC=84=9C=20=EC=B9=A9=20?= =?UTF-8?q?=ED=95=84=ED=84=B0=EB=A7=81=EC=9D=84=20=EC=A7=84=ED=96=89?= =?UTF-8?q?=ED=95=98=EB=8A=94=20=EA=B2=BD=EC=9A=B0=20=EA=B2=80=EC=83=89=20?= =?UTF-8?q?=EA=B2=B0=EA=B3=BC=EA=B0=80=20=EC=97=86=EC=96=B4=EB=8F=84=20?= =?UTF-8?q?=EC=A7=80=EB=8F=84=EB=B7=B0=20=EC=9C=A0=EC=A7=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../presentation/ui/main/home/HomeFragment.kt | 11 ++++---- .../ui/main/home/HomeViewModel.kt | 25 ++++++++++++++----- .../ui/main/home/map/MapFragment.kt | 25 +++++++++++-------- 3 files changed, 40 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/org/sopt/pingle/presentation/ui/main/home/HomeFragment.kt b/app/src/main/java/org/sopt/pingle/presentation/ui/main/home/HomeFragment.kt index 2d9be3f1..dcb19f00 100644 --- a/app/src/main/java/org/sopt/pingle/presentation/ui/main/home/HomeFragment.kt +++ b/app/src/main/java/org/sopt/pingle/presentation/ui/main/home/HomeFragment.kt @@ -145,13 +145,14 @@ class HomeFragment : BindingFragment(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 { diff --git a/app/src/main/java/org/sopt/pingle/presentation/ui/main/home/HomeViewModel.kt b/app/src/main/java/org/sopt/pingle/presentation/ui/main/home/HomeViewModel.kt index 9cedd56c..97d88836 100644 --- a/app/src/main/java/org/sopt/pingle/presentation/ui/main/home/HomeViewModel.kt +++ b/app/src/main/java/org/sopt/pingle/presentation/ui/main/home/HomeViewModel.kt @@ -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() } @@ -69,7 +73,11 @@ class HomeViewModel @Inject constructor( private var _searchWord = MutableStateFlow(null) val searchWord get() = _searchWord.asStateFlow() - private val _pinEntityListState = MutableStateFlow>>(UiState.Empty) + private var _lastSearchWord: String? = null + val lastSearchWord get() = _lastSearchWord + + private val _pinEntityListState = + MutableStateFlow>>>(UiState.Empty) val pinEntityListState get() = _pinEntityListState.asStateFlow() private var _markerModelData = @@ -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) { @@ -250,11 +263,11 @@ 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( @@ -262,7 +275,7 @@ class HomeViewModel @Inject constructor( 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) diff --git a/app/src/main/java/org/sopt/pingle/presentation/ui/main/home/map/MapFragment.kt b/app/src/main/java/org/sopt/pingle/presentation/ui/main/home/map/MapFragment.kt index 597dc846..691419e3 100644 --- a/app/src/main/java/org/sopt/pingle/presentation/ui/main/home/map/MapFragment.kt +++ b/app/src/main/java/org/sopt/pingle/presentation/ui/main/home/map/MapFragment.kt @@ -209,20 +209,25 @@ class MapFragment : BindingFragment(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) + } } } }