diff --git a/app/src/main/java/org/sopt/and/presentation/mypage/MyPageScreen.kt b/app/src/main/java/org/sopt/and/presentation/mypage/MyPageScreen.kt index 77f1d8e..4940db4 100644 --- a/app/src/main/java/org/sopt/and/presentation/mypage/MyPageScreen.kt +++ b/app/src/main/java/org/sopt/and/presentation/mypage/MyPageScreen.kt @@ -6,7 +6,6 @@ import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.padding -import androidx.compose.material3.CircularProgressIndicator import androidx.compose.material3.SnackbarHost import androidx.compose.material3.SnackbarHostState import androidx.compose.runtime.Composable @@ -14,7 +13,6 @@ import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue import androidx.compose.runtime.remember -import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.platform.LocalContext @@ -31,7 +29,7 @@ import org.sopt.and.presentation.mypage.component.PurchaseText fun MyPageScreen( myPageViewModel: MyPageViewModel = viewModel( factory = MyPageViewModel.provideFactory( - AuthLocalDataSource.getInstance(LocalContext.current) + authLocalDataSource = AuthLocalDataSource.getInstance(LocalContext.current) ) ), modifier: Modifier = Modifier diff --git a/app/src/main/java/org/sopt/and/presentation/mypage/MyPageViewModel.kt b/app/src/main/java/org/sopt/and/presentation/mypage/MyPageViewModel.kt index 092d230..61ff6fd 100644 --- a/app/src/main/java/org/sopt/and/presentation/mypage/MyPageViewModel.kt +++ b/app/src/main/java/org/sopt/and/presentation/mypage/MyPageViewModel.kt @@ -13,7 +13,7 @@ import org.sopt.and.data.ServicePool import org.sopt.and.data.local.AuthLocalDataSource class MyPageViewModel( - private val authDataStore: AuthLocalDataSource + private val authLocalDataSource: AuthLocalDataSource ) : ViewModel() { private val _uiState = MutableStateFlow(MyPageUiState()) val uiState: StateFlow = _uiState.asStateFlow() @@ -32,44 +32,79 @@ class MyPageViewModel( viewModelScope.launch { _uiState.update { it.copy(isLoading = true) } try { - authDataStore.getToken().collectLatest { token -> + authLocalDataSource.getToken().collect { token -> if (token != null) { - val response = ServicePool.authService.getMyHobby(token) - when { - response.isSuccessful && response.body()?.result != null -> { - _uiState.update { - it.copy(hobby = response.body()?.result?.hobby ?: "") + try { + val response = ServicePool.authService.getMyHobby(token) + when { + response.isSuccessful && response.body()?.result != null -> { + _uiState.update { + it.copy( + hobby = response.body()?.result?.hobby ?: "", + isLoading = false + ) + } + } + response.code() == 401 -> { + _uiState.update { + it.copy( + errorMessage = "토큰이 없습니다", + isLoading = false + ) + } + } + response.code() == 403 -> { + _uiState.update { + it.copy( + errorMessage = "유효하지 않은 토큰입니다", + isLoading = false + ) + } + } + else -> { + _uiState.update { + it.copy( + errorMessage = "취미 조회에 실패했습니다", + isLoading = false + ) + } } } - response.code() == 401 -> { - _uiState.update { it.copy(errorMessage = "토큰이 없습니다") } - } - response.code() == 403 -> { - _uiState.update { it.copy(errorMessage = "유효하지 않은 토큰입니다") } - } - else -> { - _uiState.update { it.copy(errorMessage = "취미 조회에 실패했습니다") } + } catch (e: Exception) { + _uiState.update { + it.copy( + errorMessage = "네트워크 오류가 발생했습니다", + isLoading = false + ) } } } else { - _uiState.update { it.copy(errorMessage = "로그인이 필요합니다") } + _uiState.update { + it.copy( + errorMessage = "로그인이 필요합니다", + isLoading = false + ) + } } } } catch (e: Exception) { - _uiState.update { it.copy(errorMessage = "네트워크 오류가 발생했습니다") } - } finally { - _uiState.update { it.copy(isLoading = false) } + _uiState.update { + it.copy( + errorMessage = "토큰 조회에 실패했습니다", + isLoading = false + ) + } } } } companion object { fun provideFactory( - authDataStore: AuthLocalDataSource + authLocalDataSource: AuthLocalDataSource ): ViewModelProvider.Factory = object : ViewModelProvider.Factory { @Suppress("UNCHECKED_CAST") override fun create(modelClass: Class): T { - return MyPageViewModel(authDataStore) as T + return MyPageViewModel(authLocalDataSource) as T } } }