Skip to content

Commit

Permalink
[feature/#871] change ui state
Browse files Browse the repository at this point in the history
  • Loading branch information
leeeyubin committed Oct 14, 2024
1 parent 9b6b873 commit c487625
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ package org.sopt.official.feature.mypage.model
import androidx.compose.runtime.Immutable
import androidx.compose.runtime.Stable
import org.sopt.official.auth.model.UserActiveState
import org.sopt.official.feature.mypage.mypage.MyPageAction

@Stable
sealed interface MyPageUiState {
Expand All @@ -38,7 +37,6 @@ sealed interface MyPageUiState {
@Immutable
data class Authenticated(
val activeState: UserActiveState,
val action: MyPageAction? = null
) : MyPageUiState

@Immutable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Scaffold
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
Expand Down Expand Up @@ -83,6 +84,7 @@ class MyPageActivity : AppCompatActivity() {
val lifecycleOwner = LocalLifecycleOwner.current

val state by viewModel.state.collectAsStateWithLifecycle()
val myPageAction by viewModel.action.collectAsStateWithLifecycle()
val scrollState = rememberScrollState()

val serviceSectionItems = remember {
Expand Down Expand Up @@ -194,31 +196,6 @@ class MyPageActivity : AppCompatActivity() {
Spacer(modifier = Modifier.height(16.dp))
when (state) {
is MyPageUiState.Authenticated -> {
when ((state as MyPageUiState.Authenticated).action) {
MyPageAction.CLEAR_SOPTAMP -> {
MyPageDialog(
onDismissRequest = viewModel::onDismiss,
title = "미션을 초기화 하실건가요?",
subTitle = "사진, 메모가 삭제되고\n전체 미션이 미완료상태로 초기화됩니다.",
negativeText = "취소",
positiveText = "초기화",
onPositiveButtonClick = viewModel::resetSoptamp
)
}

MyPageAction.LOGOUT -> {
MyPageDialog(
onDismissRequest = viewModel::onDismiss,
title = "로그아웃",
subTitle = "정말 로그아웃을 하실 건가요?",
negativeText = "취소",
positiveText = "로그아웃",
onPositiveButtonClick = viewModel::logOut
)
}

else -> {}
}
MyPageSection(items = notificationSectionItems)
Spacer(modifier = Modifier.height(16.dp))
MyPageSection(items = soptampSectionItems)
Expand All @@ -234,6 +211,14 @@ class MyPageActivity : AppCompatActivity() {
}
Spacer(modifier = Modifier.height(32.dp))
}
if (myPageAction != null) {
ShowMyPageDialog(
action = myPageAction ?: return@Scaffold,
onDismissRequest = viewModel::onDismiss,
onClearSoptampClick = viewModel::resetSoptamp,
onLogoutClick = viewModel::logOut
)
}
}
}
}
Expand All @@ -250,3 +235,35 @@ class MyPageActivity : AppCompatActivity() {
}
}
}

@Composable
private fun ShowMyPageDialog(
action: MyPageAction,
onDismissRequest: () -> Unit,
onClearSoptampClick: () -> Unit,
onLogoutClick: () -> Unit
) {
when (action) {
MyPageAction.CLEAR_SOPTAMP -> {
MyPageDialog(
onDismissRequest = onDismissRequest,
title = "미션을 초기화 하실건가요?",
subTitle = "사진, 메모가 삭제되고\n전체 미션이 미완료상태로 초기화됩니다.",
negativeText = "취소",
positiveText = "초기화",
onPositiveButtonClick = onClearSoptampClick
)
}

MyPageAction.LOGOUT -> {
MyPageDialog(
onDismissRequest = onDismissRequest,
title = "로그아웃",
subTitle = "정말 로그아웃을 하실 건가요?",
negativeText = "취소",
positiveText = "로그아웃",
onPositiveButtonClick = onLogoutClick
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.receiveAsFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import kotlinx.coroutines.tasks.await
import org.sopt.official.auth.model.UserActiveState
Expand All @@ -56,6 +55,9 @@ class MyPageViewModel @Inject constructor(
private val _state: MutableStateFlow<MyPageUiState> = MutableStateFlow(MyPageUiState.UnInitialized)
val state: StateFlow<MyPageUiState> = _state.asStateFlow()

private val _action = MutableStateFlow<MyPageAction?>(null)
val action: StateFlow<MyPageAction?> = _action.asStateFlow()

private val _finish = Channel<Unit>()
val finish = _finish.receiveAsFlow()

Expand Down Expand Up @@ -89,14 +91,10 @@ class MyPageViewModel @Inject constructor(
}

fun showDialogState(action: MyPageAction) {
_state.update { currentState ->
(currentState as MyPageUiState.Authenticated).copy(action = action)
}
_action.value = action
}

fun onDismiss() {
_state.update { currentState ->
(currentState as MyPageUiState.Authenticated).copy(action = null)
}
_action.value = null
}
}

0 comments on commit c487625

Please sign in to comment.