Skip to content

Commit

Permalink
[Feat] 검색 결과 SideEffect, Intent 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
JaesungLeee committed Oct 3, 2024
1 parent 4559e81 commit b86a326
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 8 deletions.
1 change: 1 addition & 0 deletions app/src/main/java/team/ppac/navigation/FarmemeNavHost.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ fun FarmemeNavHost(
)
searchResultScreen(
navigateBack = { navController.popBackStack() },
navigateToMemeDetail = navigateToDetail
)
myPageScreen(
analyticsHelper = analyticsHelper,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand All @@ -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) }
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -28,6 +34,7 @@ import javax.inject.Inject
class SearchResultViewModel @Inject constructor(
savedStateHandle: SavedStateHandle,
private val searchMemeUseCase: SearchMemeUseCase,
private val watchMemeUseCase: WatchMemeUseCase,
) : BaseViewModel<SearchResultUiState, SearchResultSideEffect, SearchResultIntent>(savedStateHandle) {

init {
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
data object ClickBackButton : SearchResultIntent
data object ClickErrorRetry : SearchResultIntent
data class ClickMeme(val memeId: String) : SearchResultIntent
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -15,6 +16,7 @@ fun NavController.navigateToSearchResult(

fun NavGraphBuilder.searchResultScreen(
navigateBack: () -> Unit,
navigateToMemeDetail: (String) -> Unit,
) {
composable(
route = "$SEARCH_RESULT_ROUTE/{query}",
Expand All @@ -25,7 +27,8 @@ fun NavGraphBuilder.searchResultScreen(
)
) {
SearchResultRoute(
navigateBack = navigateBack
navigateBack = navigateBack,
navigateToMemeDetail = navigateToMemeDetail
)
}
}

0 comments on commit b86a326

Please sign in to comment.