Skip to content

Commit

Permalink
feat: 받은 봉투 편집 동작 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
jinukeu committed Jan 31, 2024
1 parent 6517b26 commit 8fe39d4
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import com.susu.core.model.Envelope
import com.susu.core.model.Relationship
import com.susu.core.ui.base.SideEffect
import com.susu.core.ui.base.UiState
import com.susu.feature.received.envelopeadd.content.relationship.RelationShipSideEffect
import kotlinx.collections.immutable.PersistentList
import kotlinx.collections.immutable.persistentListOf

Expand All @@ -15,9 +14,15 @@ data class ReceivedEnvelopeEditState(
val showDateBottomSheet: Boolean = false,
val isRelationSaved: Boolean = false,
) : UiState {
val buttonEnabled = when {
envelope.friend.name.isEmpty() -> false
envelope.relationship.id == relationshipConfig.last().id && isRelationSaved.not() -> false
else -> true
}
}

sealed interface ReceivedEnvelopeEditSideEffect : SideEffect {
data object FocusCustomRelation : ReceivedEnvelopeEditSideEffect
data object PopBackStack : ReceivedEnvelopeEditSideEffect
data class HandleException(val throwable: Throwable, val retry: () -> Unit) : ReceivedEnvelopeEditSideEffect
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,8 @@ import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusRequester
Expand All @@ -34,7 +31,6 @@ import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.susu.core.designsystem.component.appbar.SusuDefaultAppBar
import com.susu.core.designsystem.component.appbar.icon.BackIcon
import com.susu.core.designsystem.component.bottomsheet.datepicker.SusuDatePickerBottomSheet
import com.susu.core.designsystem.component.bottomsheet.datepicker.SusuLimitDatePickerBottomSheet
import com.susu.core.designsystem.component.button.AddConditionButton
import com.susu.core.designsystem.component.button.FilledButtonColor
import com.susu.core.designsystem.component.button.MediumButtonStyle
Expand All @@ -48,14 +44,14 @@ import com.susu.core.designsystem.component.textfieldbutton.style.SmallTextField
import com.susu.core.designsystem.theme.Gray30
import com.susu.core.designsystem.theme.Gray40
import com.susu.core.designsystem.theme.Gray70
import com.susu.core.designsystem.theme.Gray80
import com.susu.core.designsystem.theme.SusuTheme
import com.susu.core.model.Relationship
import com.susu.core.ui.extension.collectWithLifecycle
import com.susu.core.ui.extension.susuClickable
import com.susu.core.ui.util.AnnotatedText
import com.susu.feature.received.R
import com.susu.feature.received.envelopeedit.component.EditDetailItem
import kotlinx.coroutines.android.awaitFrame
import kotlinx.coroutines.launch

@Composable
fun ReceivedEnvelopeEditRoute(
Expand All @@ -76,6 +72,10 @@ fun ReceivedEnvelopeEditRoute(
)

ReceivedEnvelopeEditSideEffect.PopBackStack -> popBackStack()
ReceivedEnvelopeEditSideEffect.FocusCustomRelation -> scope.launch {
awaitFrame()
focusRequester.requestFocus()
}
}
}

Expand All @@ -88,6 +88,22 @@ fun ReceivedEnvelopeEditRoute(
uiState = uiState,
focusRequester = focusRequester,
onClickBackIcon = viewModel::popBackStack,
onClickSave = {},
onTextChangeMoney = viewModel::updateMoney,
onTextChangeName = viewModel::updateName,
onTextChangeRelation = viewModel::updateCustomRelation,
onClickRelation = viewModel::updateRelation,
onClickCustomRelationClear = { viewModel.updateCustomRelation("") },
onClickCustomRelationClose = viewModel::closeCustomRelation,
onClickRelationInnerButton = viewModel::toggleRelationSaved,
onClickAddConditionButton = viewModel::showCustomRelation,
onClickDate = viewModel::showDateBottomSheet,
onClickHasVisited = viewModel::updateHasVisited,
onTextChangeGift = viewModel::updateGift,
onTextChangePhoneNumber = viewModel::updatePhoneNumber,
onTextChangeMemo = viewModel::updateMemo,
onDismissDateBottomSheet = viewModel::hideDateBottomSheet,
onItemSelectedDateBottomSheet = viewModel::updateDate,
)
}

Expand All @@ -100,6 +116,7 @@ fun ReceivedEnvelopeEditScreen(
onClickSave: () -> Unit = {},
onTextChangeMoney: (String) -> Unit = {},
onTextChangeName: (String) -> Unit = {},
onTextChangeRelation: (String) -> Unit = {},
onClickRelation: (Relationship) -> Unit = {},
onClickCustomRelationClear: () -> Unit = {},
onClickCustomRelationClose: () -> Unit = {},
Expand Down Expand Up @@ -173,10 +190,10 @@ fun ReceivedEnvelopeEditScreen(
if (uiState.showCustomRelationButton) {
SusuTextFieldWrapContentButton(
focusRequester = focusRequester,
onTextChange = {},
onTextChange = onTextChangeRelation,
color = TextFieldButtonColor.Orange,
style = SmallTextFieldButtonStyle.height32,
text = uiState.envelope.relationship.customRelation ?: "",
text = uiState.relationshipConfig.last().customRelation ?: "",
isFocused = uiState.relationshipConfig.last().id == uiState.envelope.relationship.id,
isSaved = uiState.isRelationSaved,
onClickClearIcon = onClickCustomRelationClear,
Expand Down Expand Up @@ -285,6 +302,8 @@ fun ReceivedEnvelopeEditScreen(
color = FilledButtonColor.Black,
style = MediumButtonStyle.height60,
shape = RectangleShape,
isActive = uiState.buttonEnabled,
isClickable = uiState.buttonEnabled,
text = stringResource(com.susu.core.ui.R.string.word_save),
onClick = onClickSave,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package com.susu.feature.received.envelopeedit
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.viewModelScope
import com.susu.core.model.Envelope
import com.susu.core.model.Friend
import com.susu.core.model.Relationship
import com.susu.core.ui.base.BaseViewModel
import com.susu.core.ui.base.SideEffect
import com.susu.core.ui.extension.decodeFromUri
import com.susu.core.ui.extension.encodeToUri
import com.susu.domain.usecase.envelope.DeleteEnvelopeUseCase
Expand All @@ -13,8 +16,10 @@ import com.susu.feature.received.navigation.ReceivedRoute
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.collections.immutable.toPersistentList
import kotlinx.coroutines.launch
import kotlinx.datetime.toKotlinLocalDateTime
import kotlinx.serialization.json.Json
import timber.log.Timber
import java.time.LocalDateTime
import javax.inject.Inject

@HiltViewModel
Expand All @@ -39,7 +44,10 @@ class ReceivedEnvelopeEditViewModel @Inject constructor(
intent {
copy(
envelope = envelope,
relationshipConfig = it.toPersistentList(),
relationshipConfig = it.map {
if (it.id == envelope.relationship.id) it.copy(customRelation = envelope.relationship.customRelation)
else it
}.toPersistentList(),
showCustomRelationButton = it.last().id == envelope.relationship.id,
isRelationSaved = it.last().id == envelope.relationship.id,
)
Expand All @@ -50,4 +58,121 @@ class ReceivedEnvelopeEditViewModel @Inject constructor(

fun popBackStack() = postSideEffect(ReceivedEnvelopeEditSideEffect.PopBackStack)

fun updateMoney(money: String) = intent {
copy(
envelope = envelope.copy(amount = money.toLongOrNull() ?: 0L),
)
}

fun updateName(name: String) = intent {
copy(
envelope = envelope.copy(friend = Friend(name = name)),
)
}

fun updateRelation(relationship: Relationship) = intent {
copy(
envelope = envelope.copy(relationship = relationship),
)
}

fun updateCustomRelation(customRelation: String?) = intent {
copy(
envelope = envelope.copy(relationship = envelope.relationship.copy(customRelation = customRelation)),
relationshipConfig = relationshipConfig.map {
if (it.id == envelope.relationship.id) {
it.copy(customRelation = customRelation)
} else it
}.toPersistentList(),
)
}

fun closeCustomRelation() = intent {
copy(
envelope = if (envelope.relationship.id == relationshipConfig.last().id) {
envelope.copy(relationship = relationshipConfig.first())
} else {
envelope
},
relationshipConfig = relationshipConfig.map {
it.copy(customRelation = null)
}.toPersistentList(),
isRelationSaved = false,
showCustomRelationButton = false,
)
}

fun toggleRelationSaved() = intent {
copy(
isRelationSaved = !isRelationSaved,
)
}

fun showCustomRelation() = intent {
postSideEffect(ReceivedEnvelopeEditSideEffect.FocusCustomRelation)
copy(
isRelationSaved = false,
showCustomRelationButton = true,
envelope = envelope.copy(
relationship = relationshipConfig.last(),
),
)
}

fun showDateBottomSheet() = intent {
copy(
showDateBottomSheet = true,
)
}

fun updateHasVisited(visited: Boolean) = intent {
copy(
envelope = envelope.copy(
hasVisited = if (visited == envelope.hasVisited) null else visited,
),
)
}

fun updateGift(gift: String) = intent {
copy(
envelope = envelope.copy(
gift = gift.ifEmpty { null },
),
)
}

fun updatePhoneNumber(phoneNumber: String) = intent {
copy(
envelope = envelope.copy(
friend = envelope.friend.copy(
phoneNumber = phoneNumber,
),
),
)
}

fun updateMemo(memo: String) = intent {
copy(
envelope = envelope.copy(
memo = memo.ifEmpty { null },
),
)
}

fun hideDateBottomSheet(year: Int, month: Int, day: Int) = intent {
copy(
envelope = envelope.copy(
handedOverAt = LocalDateTime.of(year, month, day, 0, 0).toKotlinLocalDateTime(),
),
showDateBottomSheet = false,
)
}

fun updateDate(year: Int, month: Int, day: Int) = intent {
copy(
envelope = envelope.copy(
handedOverAt = LocalDateTime.of(year, month, day, 0, 0).toKotlinLocalDateTime(),
),
)
}
}

0 comments on commit 8fe39d4

Please sign in to comment.