Skip to content

Commit

Permalink
[FIX] 마이 페이지 화면에서 서버 에러 발생시, 로그인 화면으로 이동
Browse files Browse the repository at this point in the history
토큰이 만료된 경우에 대해서, 서버 에러 다이얼로그를 띄워, 재시도를 하는 로직이 현재 상황에선 의미가 없고, 불필요한 API 호출을 야기시키기 때문에, 서버 에러가 발생할 경우 사용자에게 로그인 세션이 만료되었음을 알리고, 로그인 화면으로 이동하는 로직으로 임시 대응

clear 라는 함수명이 추상적이므로 clearAuthToken 으로 좀 더 직관적으로 변경
  • Loading branch information
easyhooon committed Mar 26, 2024
1 parent 5f0bdce commit b75b693
Show file tree
Hide file tree
Showing 13 changed files with 55 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class TokenRepositoryImpl @Inject constructor(
return dataSource.getUUID()
}

override suspend fun clear() {
dataSource.clear()
override suspend fun clearAuthToken() {
dataSource.clearAuthToken()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ interface TokenDataSource {
suspend fun getAccessToken(): String
suspend fun getRefreshToken(): String
suspend fun getUUID(): Long
suspend fun clear()
suspend fun clearAuthToken()
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class TokenDataSourceImpl @Inject constructor(
else throw exception
}.first()[KEY_REFRESH_TOKEN] ?: ""

override suspend fun clear() {
override suspend fun clearAuthToken() {
dataStore.edit { it.clear() }
}
}
1 change: 1 addition & 0 deletions core/designsystem/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<string name="mypage_name_postfix">님</string>
<string name="mypage_dropdown_delete">삭제하기</string>
<string name="mypage_dropdown_share">공유하기</string>
<string name="mypage_logout">로그인 세션이 종료되어 다시 로그인이 필요합니다.</string>

<!-- Setting -->
<string name="setting_title">설정</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ interface TokenRepository {
suspend fun getAccessToken(): String
suspend fun getRefreshToken(): String
suspend fun getUUID(): Long
suspend fun clear()
suspend fun clearAuthToken()
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class IntroViewModel @Inject constructor(

fun autoLoginFail() = intent {
viewModelScope.launch {
repository.clear()
repository.clearAuthToken()
postSideEffect(IntroSideEffect.AutoLoginFail)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class LoginViewModel @Inject constructor(
}
.onFailure { exception ->
Timber.e(exception)
tokenRepository.clear()
tokenRepository.clearAuthToken()
postSideEffect(LoginSideEffect.LoginFail(exception))
}
reduce {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ internal fun MainScreen(
padding = padding,
onSettingClick = navigator::navigateToSetting,
onNavigateToMyAlbum = navigator::navigateToMyAlbum,
onNavigateToLogin = onNavigateToLogin,
)

myAlbumNavGraph(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.nexters.ilab.android.feature.mypage
import android.content.ClipData
import android.content.Intent
import android.net.Uri
import android.widget.Toast
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.Image
Expand Down Expand Up @@ -48,6 +49,7 @@ import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.res.vectorResource
Expand All @@ -72,7 +74,6 @@ import com.nexters.ilab.core.ui.component.ILabTopAppBar
import com.nexters.ilab.core.ui.component.LoadingIndicator
import com.nexters.ilab.core.ui.component.NetworkErrorDialog
import com.nexters.ilab.core.ui.component.NetworkImage
import com.nexters.ilab.core.ui.component.ServerErrorDialog
import com.nexters.ilab.core.ui.component.TopAppBarNavigationType
import kotlinx.collections.immutable.ImmutableList

Expand All @@ -81,9 +82,11 @@ internal fun MyPageRoute(
padding: PaddingValues,
onSettingClick: () -> Unit,
onNavigateToMyAlbum: (String, List<String>) -> Unit,
onNavigateToLogin: () -> Unit,
viewModel: MyPageViewModel = hiltViewModel(),
) {
val uiState by viewModel.container.stateFlow.collectAsStateWithLifecycle()
val context = LocalContext.current

val launcher = rememberLauncherForActivityResult(ActivityResultContracts.StartActivityForResult()) { _ ->
viewModel.deleteCacheDir()
Expand All @@ -100,7 +103,7 @@ internal fun MyPageRoute(
putParcelableArrayListExtra(Intent.EXTRA_STREAM, uriList)
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
// https://stackoverflow.com/questions/57689792/permission-denial-while-sharing-file-with-fileprovider
val clipData = ClipData.newRawUri("", uriList.get(0)).apply {
val clipData = ClipData.newRawUri("", uriList[0]).apply {
for (i in 1 until uriList.size) {
addItem(ClipData.Item(uriList[i]))
}
Expand All @@ -119,6 +122,10 @@ internal fun MyPageRoute(
sideEffect.imageUrlList,
)
}

is MyPageSideEffect.ShowToast -> {
Toast.makeText(context, sideEffect.message.asString(context), Toast.LENGTH_SHORT).show()
}
}
}
}
Expand All @@ -131,8 +138,10 @@ internal fun MyPageRoute(
onShareBtnClick = viewModel::shareMyAlbum,
onDeleteBtnClick = viewModel::deleteMyAlbum,
onAlbumClick = viewModel::onAlbumClick,
dismissServerErrorDialog = viewModel::dismissServerErrorDialog,
// dismissServerErrorDialog = viewModel::dismissServerErrorDialog,
dismissNetworkErrorDialog = viewModel::dismissNetworkErrorDialog,
onNavigateToLogin = onNavigateToLogin,
clearAuthToken = viewModel::clearAuthToken,
)
}

Expand All @@ -145,8 +154,10 @@ internal fun MyPageScreen(
onShareBtnClick: () -> Unit,
onDeleteBtnClick: (Int) -> Unit,
onAlbumClick: (Int) -> Unit,
dismissServerErrorDialog: () -> Unit,
// dismissServerErrorDialog: () -> Unit,
dismissNetworkErrorDialog: () -> Unit,
onNavigateToLogin: () -> Unit,
clearAuthToken: () -> Unit,
) {
Column(
modifier = Modifier
Expand All @@ -158,12 +169,14 @@ internal fun MyPageScreen(
LoadingIndicator(modifier = Modifier.fillMaxSize())
}
if (uiState.isServerErrorDialogVisible) {
ServerErrorDialog(
onRetryClick = {
dismissServerErrorDialog()
getUserInfo()
},
)
// ServerErrorDialog(
// onRetryClick = {
// dismissServerErrorDialog()
// getUserInfo()
// },
// )
clearAuthToken()
onNavigateToLogin()
}
if (uiState.isNetworkErrorDialogVisible) {
NetworkErrorDialog(
Expand Down Expand Up @@ -442,14 +455,16 @@ fun MyPageScreenPreview() {
onAlbumClick = {},
getUserInfo = {},
dismissNetworkErrorDialog = {},
dismissServerErrorDialog = {},
// dismissServerErrorDialog = {},
onNavigateToLogin = {},
clearAuthToken = {},
)
}
}

@DevicePreview
@Composable
fun MyPageScreenTestPreview() {
fun MyPageScreenEmptyPreview() {
ILabTheme {
MyPageContentEmpty()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ fun NavGraphBuilder.myPageNavGraph(
padding: PaddingValues,
onSettingClick: () -> Unit,
onNavigateToMyAlbum: (String, List<String>) -> Unit,
onNavigateToLogin: () -> Unit,
) {
composable(route = MY_PAGE_ROUTE) {
MyPageRoute(
padding = padding,
onSettingClick = onSettingClick,
onNavigateToMyAlbum = onNavigateToMyAlbum,
onNavigateToLogin = onNavigateToLogin,
)
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package com.nexters.ilab.android.feature.mypage.viewmodel

import com.nexters.ilab.android.core.common.UiText

sealed interface MyPageSideEffect {
data class ShareMyAlbum(val imageUriList: List<String>) : MyPageSideEffect
data class NavigateToMyAlbum(
val imageUrlList: List<String>,
val imageStyle: String,
) : MyPageSideEffect

data class ShowToast(val message: UiText) : MyPageSideEffect
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ package com.nexters.ilab.android.feature.mypage.viewmodel
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.nexters.ilab.android.core.common.ErrorHandlerActions
import com.nexters.ilab.android.core.common.UiText
import com.nexters.ilab.android.core.common.handleException
import com.nexters.ilab.android.core.designsystem.R
import com.nexters.ilab.android.core.domain.repository.AuthRepository
import com.nexters.ilab.android.core.domain.repository.DeleteMyAlbumRepository
import com.nexters.ilab.android.core.domain.repository.FileRepository
import com.nexters.ilab.android.core.domain.repository.TokenRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.collections.immutable.toImmutableList
import kotlinx.coroutines.launch
Expand All @@ -23,6 +26,7 @@ class MyPageViewModel @Inject constructor(
private val authRepository: AuthRepository,
private val fileRepository: FileRepository,
private val deleteMyAlbumRepository: DeleteMyAlbumRepository,
private val tokenRepository: TokenRepository,
) : ViewModel(), ContainerHost<MyPageState, MyPageSideEffect>, ErrorHandlerActions {
override val container = container<MyPageState, MyPageSideEffect>(MyPageState())

Expand Down Expand Up @@ -135,8 +139,12 @@ class MyPageViewModel @Inject constructor(
}
}

override fun onCleared() {
super.onCleared()
Timber.d("MyPageViewModel is cleared")
fun clearAuthToken() = intent {
viewModelScope.launch {
tokenRepository.clearAuthToken()
postSideEffect(
MyPageSideEffect.ShowToast(UiText.StringResource(R.string.mypage_logout)),
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class SettingViewModel @Inject constructor(
reduce { state.copy(isLoading = true) }
authRepository.signOut()
.onSuccess {
tokenRepository.clear()
tokenRepository.clearAuthToken()
postSideEffect(SettingSideEffect.LogoutSuccess)
}.onFailure { exception ->
Timber.d("$exception")
Expand All @@ -54,7 +54,7 @@ class SettingViewModel @Inject constructor(
reduce { state.copy(isLoading = true) }
authRepository.deleteAccount()
.onSuccess {
tokenRepository.clear()
tokenRepository.clearAuthToken()
postSideEffect(SettingSideEffect.LogoutSuccess)
}.onFailure { exception ->
Timber.d("$exception")
Expand Down

0 comments on commit b75b693

Please sign in to comment.