Skip to content

Commit

Permalink
Update #11: mypage mvi
Browse files Browse the repository at this point in the history
  • Loading branch information
0se0 committed Dec 17, 2024
1 parent 86a7e37 commit 0593832
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 36 deletions.
37 changes: 17 additions & 20 deletions app/src/main/java/org/sopt/and/feature/mypage/MyPageRoute.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.navigation.NavController
import org.sopt.and.R
import org.sopt.and.core.common.modifier.defaultScreenBackground
import org.sopt.and.core.common.modifier.noRippleClickable
Expand All @@ -32,53 +31,46 @@ import org.sopt.and.ui.theme.DarkGray2

@Composable
fun MyPageRoute(
navController: NavController,
onLogout: () -> Unit,
viewModel: MyPageViewModel
) {
val uiState by viewModel.uiState.collectAsStateWithLifecycle()
val state by viewModel.uiState.collectAsStateWithLifecycle()

MyPageScreen(
state = uiState,
onLogout = {
viewModel.logout()
navController.navigate("login") {
popUpTo("mypage") { inclusive = true }
}
}
state = state,
onIntent = viewModel::handleIntent,
onLogout = onLogout
)
}

@Composable
fun MyPageScreen(
state: MyPageState,
onLogout: () -> Unit,
viewHistoryList: List<Int> = emptyList(),
interestedProgramList: List<Int> = emptyList(),
interestedMovieList: List<Int> = emptyList(),
editorPickList: List<Int> = emptyList()
onIntent: (MyPageIntent) -> Unit,
onLogout: () -> Unit
) {
val interestsSections = listOf(
InterestsSectionData(
titleResId = R.string.full_view_history,
emptyMessageResId = R.string.no_full_view_history,
interests = viewHistoryList,
interests = state.viewHistoryList,
maxItems = 20
),
InterestsSectionData(
titleResId = R.string.interested_program,
emptyMessageResId = R.string.no_interested_program,
interests = interestedProgramList,
interests = state.interestedProgramList,
maxItems = 1
),
InterestsSectionData(
titleResId = R.string.interested_movie,
emptyMessageResId = R.string.no_interested_movie,
interests = interestedMovieList
interests = state.interestedMovieList
),
InterestsSectionData(
titleResId = R.string.interested_editor_pick,
emptyMessageResId = R.string.empty_editor_pick,
interests = editorPickList,
interests = state.editorPickList,
showMoreButton = false
)
)
Expand Down Expand Up @@ -108,7 +100,12 @@ fun MyPageScreen(
}

item {
LogoutButton(onClick = onLogout)
LogoutButton(
onClick = {
onIntent(MyPageIntent.Logout)
onLogout()
}
)
}
}
}
Expand Down
37 changes: 21 additions & 16 deletions app/src/main/java/org/sopt/and/feature/mypage/MyPageViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,24 @@ class MyPageViewModel(
val uiState: StateFlow<MyPageState> = _uiState

init {
loadHobby()
loadUserInfo()
handleIntent(MyPageIntent.LoadProfile)
}

private fun loadHobby() {
fun handleIntent(intent: MyPageIntent) {
when (intent) {
is MyPageIntent.LoadProfile -> {
loadProfile()
}
is MyPageIntent.ClickMoreHistory -> {
// Handle
}
is MyPageIntent.Logout -> {
logout()
}
}
}

private fun loadProfile() {
viewModelScope.launch {
_uiState.update { it.copy(isLoading = true) }

Expand All @@ -35,32 +48,24 @@ class MyPageViewModel(

userRepository.getHobby(token)
.onSuccess { hobby ->
_uiState.update {
it.copy(hobby = hobby)
}
_uiState.update { it.copy(hobby = hobby) }
}
.onFailure { error ->
_uiState.update {
it.copy(hobby = error.message ?: "네트워크 오류가 발생했습니다")
}
}

_uiState.update { it.copy(isLoading = false) }
}
}

private fun loadUserInfo() {
viewModelScope.launch {
userRepository.getUserInfo()
.onSuccess { userEntity ->
_uiState.update {
it.copy(username = userEntity.username)
}
_uiState.update { it.copy(username = userEntity.username) }
}

_uiState.update { it.copy(isLoading = false) }
}
}

fun logout() {
private fun logout() {
viewModelScope.launch {
userRepository.logout()
_uiState.update {
Expand Down

0 comments on commit 0593832

Please sign in to comment.