Skip to content

Commit

Permalink
[chore]#7 마이페이지 취미 표시 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
angryPodo committed Nov 15, 2024
1 parent ab920d5 commit 28737b3
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@ 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
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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<MyPageUiState> = _uiState.asStateFlow()
Expand All @@ -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 <T : ViewModel> create(modelClass: Class<T>): T {
return MyPageViewModel(authDataStore) as T
return MyPageViewModel(authLocalDataSource) as T
}
}
}
Expand Down

0 comments on commit 28737b3

Please sign in to comment.