Skip to content

Commit

Permalink
feat: 보내요 봉투 화면에서 진입 시 이름, 관계, 더보기-전화번호 옵션 표시하지 않음
Browse files Browse the repository at this point in the history
  • Loading branch information
yangsooplus committed Feb 16, 2024
1 parent e7d0ec2 commit e1569d2
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@ sealed interface EnvelopeAddEffect : SideEffect {
data class HandleException(val throwable: Throwable, val retry: () -> Unit) : EnvelopeAddEffect
}

/**
* @param fromEnvelope 보내요 봉투 화면에서 추가 화면으로 진입할 시 true. MORE STEP에서 전화번호를 감추기 위함.
* */
data class EnvelopeAddState(
val isLoading: Boolean = false,
val buttonEnabled: Boolean = false,
val currentStep: EnvelopeAddStep = EnvelopeAddStep.MONEY,
val lastPage: Boolean = false,
val friendName: String = "",
val fromEnvelope: Boolean = false,
) : UiState {
val buttonResId = if (lastPage) {
com.susu.core.ui.R.string.word_done
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ package com.susu.feature.envelopeadd
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.viewModelScope
import com.susu.core.model.Category
import com.susu.core.model.Friend
import com.susu.core.model.Relationship
import com.susu.core.ui.MONEY_MAX_VALUE
import com.susu.core.ui.base.BaseViewModel
import com.susu.core.ui.extension.decodeFromUri
import com.susu.domain.usecase.envelope.CreateSentEnvelopeUseCase
import com.susu.feature.sent.navigation.SentRoute
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import kotlinx.datetime.toKotlinLocalDateTime
import kotlinx.serialization.json.Json
import java.time.LocalDateTime
import javax.inject.Inject

Expand All @@ -19,10 +22,13 @@ class EnvelopeAddViewModel @Inject constructor(
private val createSentEnvelopeUseCase: CreateSentEnvelopeUseCase,
savedStateHandle: SavedStateHandle,
) : BaseViewModel<EnvelopeAddState, EnvelopeAddEffect>(EnvelopeAddState()) {
private val friendArgument = savedStateHandle.get<String>(SentRoute.FRIEND_ARGUMENT_NAME)
private val friendArgument = runCatching {
savedStateHandle.get<String>(SentRoute.FRIEND_ARGUMENT_NAME)?.let {
Json.decodeFromUri<Friend>(it)
}
}.getOrNull()

private var money: Long = 0
private var name: String = ""
private var friendId: Long? = null
private var relationShip: Relationship? = null
private var category: Category? = null
Expand All @@ -34,15 +40,20 @@ class EnvelopeAddViewModel @Inject constructor(
private var memo: String? = null

fun initData() {
println(friendArgument)
friendArgument?.let {
updateFriendId(it.id)
updateName(it.name)
updatePhoneNumber(it.phoneNumber.ifEmpty { null })
intent { copy(fromEnvelope = true) }
} ?: intent { copy(fromEnvelope = false) }
}

private fun createEnvelope() {
viewModelScope.launch {
createSentEnvelopeUseCase(
param = CreateSentEnvelopeUseCase.Param(
friendId = friendId,
friendName = name,
friendName = currentState.friendName,
phoneNumber = phoneNumber,
relationshipId = relationShip?.id,
customRelation = relationShip?.customRelation,
Expand All @@ -63,7 +74,14 @@ class EnvelopeAddViewModel @Inject constructor(

fun goNextStep() {
when (uiState.value.currentStep) {
EnvelopeAddStep.MONEY -> intent { copy(currentStep = EnvelopeAddStep.NAME) }
EnvelopeAddStep.MONEY -> {
if (friendArgument == null) {
intent { copy(currentStep = EnvelopeAddStep.NAME) }
} else {
intent { copy(currentStep = EnvelopeAddStep.EVENT) }
}
}

EnvelopeAddStep.NAME -> {
intent {
if (friendId == null) {
Expand Down Expand Up @@ -97,13 +115,12 @@ class EnvelopeAddViewModel @Inject constructor(
EnvelopeAddStep.NAME -> intent { copy(currentStep = EnvelopeAddStep.MONEY) }
EnvelopeAddStep.RELATIONSHIP -> intent { copy(currentStep = EnvelopeAddStep.NAME) }
EnvelopeAddStep.EVENT -> {
intent {
if (friendId == null) {
copy(currentStep = EnvelopeAddStep.RELATIONSHIP)
} else {
copy(currentStep = EnvelopeAddStep.NAME)
}
val prevStep = when {
friendId == null -> EnvelopeAddStep.RELATIONSHIP
friendArgument != null -> EnvelopeAddStep.MONEY
else -> EnvelopeAddStep.NAME
}
intent { copy(currentStep = prevStep) }
}

EnvelopeAddStep.DATE -> intent { copy(currentStep = EnvelopeAddStep.EVENT) }
Expand Down Expand Up @@ -133,8 +150,7 @@ class EnvelopeAddViewModel @Inject constructor(
}

fun updateName(name: String) = intent {
this@EnvelopeAddViewModel.name = name
copy(buttonEnabled = name.isNotEmpty())
copy(friendName = name, buttonEnabled = name.isNotEmpty())
}

fun updateFriendId(friendId: Long?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,6 @@ fun SentEnvelopeAddRoute(
}
}

var friendName by remember {
mutableStateOf("")
}

var categoryName by remember {
mutableStateOf("")
}
Expand All @@ -78,15 +74,11 @@ fun SentEnvelopeAddRoute(

SentEnvelopeAddScreen(
uiState = uiState,
friendName = friendName,
categoryName = categoryName,
onClickBack = viewModel::goPrevStep,
onClickNext = viewModel::goNextStep,
updateParentMoney = viewModel::updateMoney,
updateParentName = { name ->
viewModel.updateName(name)
friendName = name
},
updateParentName = viewModel::updateName,
updateParentFriendId = viewModel::updateFriendId,
updateParentSelectedRelation = viewModel::updateSelectedRelationShip,
updateParentCategory = { category ->
Expand All @@ -106,7 +98,6 @@ fun SentEnvelopeAddRoute(
@Composable
fun SentEnvelopeAddScreen(
uiState: EnvelopeAddState = EnvelopeAddState(),
friendName: String = "",
onClickBack: () -> Unit = {},
onClickNext: () -> Unit = {},
updateParentMoney: (Long) -> Unit = {},
Expand Down Expand Up @@ -170,11 +161,12 @@ fun SentEnvelopeAddScreen(
)

EnvelopeAddStep.DATE -> DateContentRoute(
friendName = friendName,
friendName = uiState.friendName,
updateParentDate = updateParentDate,
)

EnvelopeAddStep.MORE -> MoreContentRoute(
fromEnvelope = uiState.fromEnvelope,
updateParentMoreStep = updateParentMoreStep,
)

Expand All @@ -189,7 +181,7 @@ fun SentEnvelopeAddScreen(
)

EnvelopeAddStep.PHONE -> PhoneContentRoute(
friendName = friendName,
friendName = uiState.friendName,
updateParentPhone = updateParentPhoneNumber,
onShowSnackbar = onShowSnackbar,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import com.susu.feature.sent.R
@Composable
fun MoreContentRoute(
viewModel: MoreViewModel = hiltViewModel(),
fromEnvelope: Boolean,
updateParentMoreStep: (List<EnvelopeAddStep>) -> Unit,
) {
val uiState = viewModel.uiState.collectAsStateWithLifecycle().value
Expand All @@ -45,6 +46,7 @@ fun MoreContentRoute(

MoreContent(
uiState = uiState,
fromEnvelope = fromEnvelope,
onClickStepButton = viewModel::toggleStep,
)
}
Expand All @@ -57,6 +59,7 @@ fun MoreContent(
vertical = SusuTheme.spacing.spacing_xl,
),
uiState: MoreState = MoreState(),
fromEnvelope: Boolean = false,
onClickStepButton: (EnvelopeAddStep) -> Unit = {},
) {
val scrollState = rememberScrollState()
Expand Down Expand Up @@ -88,7 +91,9 @@ fun MoreContent(
Column(
verticalArrangement = Arrangement.spacedBy(SusuTheme.spacing.spacing_xxs),
) {
moreStep.forEach { (step, stringRes) ->
for ((step, stringRes) in moreStep) {
if (fromEnvelope && step == EnvelopeAddStep.PHONE) continue

if (step in uiState.selectedMoreStop) {
SusuFilledButton(
color = FilledButtonColor.Orange,
Expand Down

0 comments on commit e1569d2

Please sign in to comment.