Skip to content

Commit

Permalink
feat: 장부 삭제 예외 처리
Browse files Browse the repository at this point in the history
  • Loading branch information
jinukeu committed Jan 16, 2024
1 parent 38fc742 commit 2a4da61
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ sealed interface MainSideEffect : SideEffect {
data object NavigateLogin : MainSideEffect
data object NavigateSignup : MainSideEffect
data object NavigateSent : MainSideEffect
data class ShowNetworkErrorSnackbar(val retry: () -> Unit) : MainSideEffect
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import com.susu.core.designsystem.component.dialog.SusuDialog
import com.susu.core.designsystem.component.navigation.SusuNavigationBar
import com.susu.core.designsystem.component.navigation.SusuNavigationItem
import com.susu.core.designsystem.component.snackbar.SusuSnackbar
import com.susu.core.ui.SnackbarToken
import com.susu.core.ui.extension.collectWithLifecycle
import com.susu.feature.community.navigation.communityNavGraph
import com.susu.feature.loginsignup.navigation.loginSignupNavGraph
Expand All @@ -43,6 +44,16 @@ internal fun MainScreen(
MainSideEffect.NavigateLogin -> navigator.navigateLogin()
MainSideEffect.NavigateSent -> navigator.navigateSent()
MainSideEffect.NavigateSignup -> navigator.navigateSignup()
is MainSideEffect.ShowNetworkErrorSnackbar -> {
viewModel.onShowSnackbar(
SnackbarToken(
message = "네트워크 오류가 발생했어요",
onClickActionButton = sideEffect.retry,
actionIcon = R.drawable.ic_refresh,
actionIconContentDescription = "새로고침 아이콘"
),
)
}
}
}

Expand Down Expand Up @@ -97,6 +108,7 @@ internal fun MainScreen(
navigateLedgerAdd = navigator::navigateLedgerAdd,
onShowSnackbar = viewModel::onShowSnackbar,
onShowDialog = viewModel::onShowDialog,
handleException = viewModel::handleException,
)

statisticsNavGraph(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.susu.feature.navigator

import androidx.lifecycle.viewModelScope
import com.susu.core.android.throwUnknownException
import com.susu.core.model.exception.NetworkException
import com.susu.core.ui.DialogToken
import com.susu.core.ui.SnackbarToken
import com.susu.core.ui.base.BaseViewModel
Expand All @@ -12,6 +14,7 @@ import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import java.net.UnknownHostException
import javax.inject.Inject

@HiltViewModel
Expand Down Expand Up @@ -43,6 +46,12 @@ class MainViewModel @Inject constructor(
intent { copy(dialogVisible = false) }
}

fun handleException(throwable: Throwable, retry: () -> Unit) = when(throwable) {
is NetworkException -> postSideEffect(MainSideEffect.ShowNetworkErrorSnackbar(retry))
is UnknownHostException -> postSideEffect(MainSideEffect.ShowNetworkErrorSnackbar(retry))
else -> throwUnknownException(throwable)
}

fun initCategoryConfig() = viewModelScope.launch {
getCategoryConfigUseCase()
.onFailure { }
Expand Down
9 changes: 9 additions & 0 deletions feature/navigator/src/main/res/drawable/ic_refresh.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:pathData="M12,20C9.767,20 7.875,19.225 6.325,17.675C4.775,16.125 4,14.233 4,12C4,9.767 4.775,7.875 6.325,6.325C7.875,4.775 9.767,4 12,4C13.15,4 14.25,4.237 15.3,4.712C16.35,5.187 17.25,5.866 18,6.75V5C18,4.717 18.096,4.479 18.288,4.288C18.48,4.097 18.717,4.001 19,4C19.283,4 19.521,4.096 19.713,4.288C19.905,4.48 20.001,4.717 20,5V10C20,10.283 19.904,10.521 19.712,10.713C19.52,10.905 19.283,11.001 19,11H14C13.717,11 13.479,10.904 13.288,10.712C13.097,10.52 13.001,10.283 13,10C13,9.717 13.096,9.479 13.288,9.288C13.48,9.097 13.717,9.001 14,9H17.2C16.667,8.067 15.938,7.333 15.013,6.8C14.088,6.267 13.084,6 12,6C10.333,6 8.917,6.583 7.75,7.75C6.583,8.917 6,10.333 6,12C6,13.667 6.583,15.083 7.75,16.25C8.917,17.417 10.333,18 12,18C13.133,18 14.171,17.713 15.113,17.138C16.055,16.563 16.784,15.792 17.3,14.825C17.433,14.592 17.621,14.429 17.863,14.338C18.105,14.247 18.351,14.242 18.6,14.325C18.867,14.408 19.058,14.583 19.175,14.85C19.292,15.117 19.283,15.367 19.15,15.6C18.467,16.933 17.492,18 16.225,18.8C14.958,19.6 13.55,20 12,20Z"
android:fillColor="#88C1FF"/>
</vector>
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ sealed interface LedgerDetailSideEffect : SideEffect {
data class PopBackStackWithDeleteLedgerId(val ledgerId: Int) : LedgerDetailSideEffect
data class ShowDeleteDialog(val onConfirmRequest: () -> Unit) : LedgerDetailSideEffect
data object ShowDeleteSuccessSnackbar : LedgerDetailSideEffect
data class ShowSnackbar(val msg: String) : LedgerDetailSideEffect
data class HandleException(val throwable: Throwable, val retry: () -> Unit) : LedgerDetailSideEffect
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ fun LedgerDetailRoute(
popBackStackWithDeleteLedgerId: (Int) -> Unit,
onShowSnackbar: (SnackbarToken) -> Unit,
onShowDialog: (DialogToken) -> Unit,
handleException: (Throwable, () -> Unit) -> Unit,
) {
val uiState = viewModel.uiState.collectAsStateWithLifecycle().value
val context = LocalContext.current
Expand Down Expand Up @@ -84,6 +85,8 @@ fun LedgerDetailRoute(
}

is LedgerDetailSideEffect.PopBackStackWithDeleteLedgerId -> popBackStackWithDeleteLedgerId(sideEffect.ledgerId)
is LedgerDetailSideEffect.HandleException -> handleException(sideEffect.throwable, sideEffect.retry)
is LedgerDetailSideEffect.ShowSnackbar -> onShowSnackbar(SnackbarToken(message = sideEffect.msg))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.susu.feature.received.ledgerdetail
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.viewModelScope
import com.susu.core.model.Ledger
import com.susu.core.model.exception.NotFoundLedgerException
import com.susu.core.ui.base.BaseViewModel
import com.susu.core.ui.extension.decodeFromUri
import com.susu.core.ui.extension.encodeToUri
Expand Down Expand Up @@ -73,8 +74,11 @@ class LedgerDetailViewModel @Inject constructor(
LedgerDetailSideEffect.PopBackStackWithDeleteLedgerId(ledger.id),
)
}
.onFailure {

.onFailure { throwable ->
when(throwable) {
is NotFoundLedgerException -> postSideEffect(LedgerDetailSideEffect.ShowSnackbar(throwable.message))
else -> postSideEffect(LedgerDetailSideEffect.HandleException(throwable, ::deleteLedger))
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ fun NavGraphBuilder.receivedNavGraph(
navigateLedgerAdd: () -> Unit,
onShowSnackbar: (SnackbarToken) -> Unit,
onShowDialog: (DialogToken) -> Unit,
handleException: (Throwable, () -> Unit) -> Unit,
) {
composable(route = ReceivedRoute.route) { navBackStackEntry ->
val ledger = navBackStackEntry.savedStateHandle.get<String>(ReceivedRoute.LEDGER_ARGUMENT_NAME)
Expand Down Expand Up @@ -86,6 +87,7 @@ fun NavGraphBuilder.receivedNavGraph(
popBackStackWithDeleteLedgerId = popBackStackWithDeleteLedgerId,
onShowSnackbar = onShowSnackbar,
onShowDialog = onShowDialog,
handleException = handleException,
)
}

Expand Down

0 comments on commit 2a4da61

Please sign in to comment.