From b86a3263b66318ef30878abd400b5c02da590e77 Mon Sep 17 00:00:00 2001 From: JaesungLeee Date: Thu, 3 Oct 2024 16:43:33 +0900 Subject: [PATCH] =?UTF-8?q?[Feat]=20=EA=B2=80=EC=83=89=20=EA=B2=B0?= =?UTF-8?q?=EA=B3=BC=20SideEffect,=20Intent=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../team/ppac/navigation/FarmemeNavHost.kt | 1 + .../ppac/search/result/SearchResultRoute.kt | 13 +++++---- .../search/result/SearchResultViewModel.kt | 29 ++++++++++++++++++- .../search/result/mvi/SearchResultIntent.kt | 4 ++- .../result/mvi/SearchResultSideEffect.kt | 1 + .../navigation/SearchResultNavigation.kt | 5 +++- 6 files changed, 45 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/team/ppac/navigation/FarmemeNavHost.kt b/app/src/main/java/team/ppac/navigation/FarmemeNavHost.kt index 921f1c6d..f9fb0fc0 100644 --- a/app/src/main/java/team/ppac/navigation/FarmemeNavHost.kt +++ b/app/src/main/java/team/ppac/navigation/FarmemeNavHost.kt @@ -54,6 +54,7 @@ fun FarmemeNavHost( ) searchResultScreen( navigateBack = { navController.popBackStack() }, + navigateToMemeDetail = navigateToDetail ) myPageScreen( analyticsHelper = analyticsHelper, diff --git a/feature/search/src/main/java/team/ppac/search/result/SearchResultRoute.kt b/feature/search/src/main/java/team/ppac/search/result/SearchResultRoute.kt index c68f8f9d..8633d648 100644 --- a/feature/search/src/main/java/team/ppac/search/result/SearchResultRoute.kt +++ b/feature/search/src/main/java/team/ppac/search/result/SearchResultRoute.kt @@ -6,21 +6,24 @@ import androidx.compose.ui.Modifier import androidx.hilt.navigation.compose.hiltViewModel import team.ppac.common.android.base.BaseComposable import team.ppac.search.result.mvi.ClickBackButton +import team.ppac.search.result.mvi.ClickErrorRetry +import team.ppac.search.result.mvi.ClickMeme import team.ppac.search.result.mvi.NavigateBack -import timber.log.Timber +import team.ppac.search.result.mvi.NavigateToMemeDetail @Composable internal fun SearchResultRoute( modifier: Modifier = Modifier, viewModel: SearchResultViewModel = hiltViewModel(), navigateBack: () -> Unit, + navigateToMemeDetail: (String) -> Unit, ) { BaseComposable(viewModel = viewModel) { uiState -> - Timber.e("$uiState") LaunchedEffect(key1 = viewModel) { viewModel.sideEffect.collect { sideEffect -> when (sideEffect) { - NavigateBack -> navigateBack() + is NavigateBack -> navigateBack() + is NavigateToMemeDetail -> navigateToMemeDetail(sideEffect.memeId) } } } @@ -31,8 +34,8 @@ internal fun SearchResultRoute( onQueryChange = viewModel::updateSearchQuery, onInputDone = viewModel::getSearchResults, handleLoadStates = viewModel::handleLoadErrorStates, - onRetryClick = {}, - onMemeClick = {}, + onRetryClick = { viewModel.intent(ClickErrorRetry) }, + onMemeClick = { viewModel.intent(ClickMeme(it)) }, onCopyClick = { _, _ -> }, onBackClick = { viewModel.intent(ClickBackButton) } ) diff --git a/feature/search/src/main/java/team/ppac/search/result/SearchResultViewModel.kt b/feature/search/src/main/java/team/ppac/search/result/SearchResultViewModel.kt index 1ea1e7da..029bc569 100644 --- a/feature/search/src/main/java/team/ppac/search/result/SearchResultViewModel.kt +++ b/feature/search/src/main/java/team/ppac/search/result/SearchResultViewModel.kt @@ -12,12 +12,18 @@ import kotlinx.coroutines.delay import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.map import team.ppac.common.android.base.BaseViewModel +import team.ppac.domain.model.MemeWatchType import team.ppac.domain.usecase.SearchMemeUseCase +import team.ppac.domain.usecase.WatchMemeUseCase import team.ppac.errorhandling.FarmemeNetworkException import team.ppac.search.detail.model.SearchResultUiModel import team.ppac.search.detail.model.toSearchResultUiModel +import team.ppac.search.detail.mvi.SearchDetailSideEffect import team.ppac.search.result.mvi.ClickBackButton +import team.ppac.search.result.mvi.ClickErrorRetry +import team.ppac.search.result.mvi.ClickMeme import team.ppac.search.result.mvi.NavigateBack +import team.ppac.search.result.mvi.NavigateToMemeDetail import team.ppac.search.result.mvi.SearchResultIntent import team.ppac.search.result.mvi.SearchResultSideEffect import team.ppac.search.result.mvi.SearchResultUiState @@ -28,6 +34,7 @@ import javax.inject.Inject class SearchResultViewModel @Inject constructor( savedStateHandle: SavedStateHandle, private val searchMemeUseCase: SearchMemeUseCase, + private val watchMemeUseCase: WatchMemeUseCase, ) : BaseViewModel(savedStateHandle) { init { @@ -42,10 +49,30 @@ class SearchResultViewModel @Inject constructor( override suspend fun handleIntent(intent: SearchResultIntent) { when (intent) { - ClickBackButton -> postNavigateBackEffect() + is ClickBackButton -> postNavigateBackEffect() + is ClickErrorRetry -> { + getSearchResults(currentState.query) + updateErrorState(isError = false) + } + is ClickMeme -> { + runCatching { + watchMemeUseCase( + memeId = intent.memeId, + watchType = MemeWatchType.SEARCH + ) + }.onSuccess { + postNavigateToMemeDetailEffect(intent.memeId) + }.onFailure { + // 에러 처리 + } + } } } + private fun postNavigateToMemeDetailEffect(memeId: String) { + postSideEffect(NavigateToMemeDetail(memeId = memeId)) + } + override fun handleClientException(throwable: Throwable) {} private fun postNavigateBackEffect() { diff --git a/feature/search/src/main/java/team/ppac/search/result/mvi/SearchResultIntent.kt b/feature/search/src/main/java/team/ppac/search/result/mvi/SearchResultIntent.kt index 8ddf5bc0..92295cd9 100644 --- a/feature/search/src/main/java/team/ppac/search/result/mvi/SearchResultIntent.kt +++ b/feature/search/src/main/java/team/ppac/search/result/mvi/SearchResultIntent.kt @@ -3,4 +3,6 @@ package team.ppac.search.result.mvi import team.ppac.common.android.base.UiIntent sealed interface SearchResultIntent : UiIntent -data object ClickBackButton : SearchResultIntent \ No newline at end of file +data object ClickBackButton : SearchResultIntent +data object ClickErrorRetry : SearchResultIntent +data class ClickMeme(val memeId: String) : SearchResultIntent \ No newline at end of file diff --git a/feature/search/src/main/java/team/ppac/search/result/mvi/SearchResultSideEffect.kt b/feature/search/src/main/java/team/ppac/search/result/mvi/SearchResultSideEffect.kt index 1b90eafd..4926b451 100644 --- a/feature/search/src/main/java/team/ppac/search/result/mvi/SearchResultSideEffect.kt +++ b/feature/search/src/main/java/team/ppac/search/result/mvi/SearchResultSideEffect.kt @@ -4,3 +4,4 @@ import team.ppac.common.android.base.UiSideEffect sealed interface SearchResultSideEffect : UiSideEffect data object NavigateBack : SearchResultSideEffect +data class NavigateToMemeDetail(val memeId: String) : SearchResultSideEffect diff --git a/feature/search/src/main/java/team/ppac/search/result/navigation/SearchResultNavigation.kt b/feature/search/src/main/java/team/ppac/search/result/navigation/SearchResultNavigation.kt index 04fb6c86..f879cedc 100644 --- a/feature/search/src/main/java/team/ppac/search/result/navigation/SearchResultNavigation.kt +++ b/feature/search/src/main/java/team/ppac/search/result/navigation/SearchResultNavigation.kt @@ -6,6 +6,7 @@ import androidx.navigation.NavType import androidx.navigation.compose.composable import androidx.navigation.navArgument import team.ppac.search.result.SearchResultRoute +import team.ppac.search.result.mvi.NavigateToMemeDetail const val SEARCH_RESULT_ROUTE = "search_result" @@ -15,6 +16,7 @@ fun NavController.navigateToSearchResult( fun NavGraphBuilder.searchResultScreen( navigateBack: () -> Unit, + navigateToMemeDetail: (String) -> Unit, ) { composable( route = "$SEARCH_RESULT_ROUTE/{query}", @@ -25,7 +27,8 @@ fun NavGraphBuilder.searchResultScreen( ) ) { SearchResultRoute( - navigateBack = navigateBack + navigateBack = navigateBack, + navigateToMemeDetail = navigateToMemeDetail ) } } \ No newline at end of file