Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Qa/23 투표 유효성 검증 변경 #144

Merged
merged 5 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.susu.feature.community

const val VOTE_CONTENT_MAX_LENGTH = 200
const val VOTE_OPTION_MAX_LENGTH = 10
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ fun NavGraphBuilder.communityNavGraph(
composable(route = CommunityRoute.voteAddRoute) {
VoteAddRoute(
popBackStack = popBackStack,
onShowSnackbar = onShowSnackbar,
handleException = handleException,
popBackStackWithVote = popBackStackWithVote,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ data class VoteAddState(
val content: String = "",
val isLoading: Boolean = false,
) : UiState {
val buttonEnabled = content.length in 1..50 &&
voteOptionStateList.all { it.content.length in 1..10 }
val buttonEnabled = content.isNotBlank() &&
voteOptionStateList.all { it.content.isNotBlank() }
}

data class VoteOptionState(
Expand All @@ -23,6 +23,8 @@ data class VoteOptionState(
)

sealed interface VoteAddSideEffect : SideEffect {
data object ShowInvalidContentSnackbar : VoteAddSideEffect
data object ShowInvalidVoteOptionSnackbar : VoteAddSideEffect
data object PopBackStack : VoteAddSideEffect
data class PopBackStackWithVote(val vote: String) : VoteAddSideEffect
data class HandleException(val throwable: Throwable, val retry: () -> Unit) : VoteAddSideEffect
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
Expand All @@ -41,23 +42,42 @@ import com.susu.core.designsystem.theme.Gray40
import com.susu.core.designsystem.theme.Orange60
import com.susu.core.designsystem.theme.SusuTheme
import com.susu.core.model.Category
import com.susu.core.ui.SnackbarToken
import com.susu.core.ui.extension.collectWithLifecycle
import com.susu.core.ui.extension.susuClickable
import com.susu.feature.community.R
import com.susu.feature.community.VOTE_CONTENT_MAX_LENGTH
import com.susu.feature.community.VOTE_OPTION_MAX_LENGTH

@Composable
fun VoteAddRoute(
viewModel: VoteAddViewModel = hiltViewModel(),
popBackStack: () -> Unit,
popBackStackWithVote: (String) -> Unit,
onShowSnackbar: (SnackbarToken) -> Unit,
handleException: (Throwable, () -> Unit) -> Unit,
) {
val uiState = viewModel.uiState.collectAsStateWithLifecycle().value
val context = LocalContext.current
viewModel.sideEffect.collectWithLifecycle { sideEffect ->
when (sideEffect) {
is VoteAddSideEffect.HandleException -> handleException(sideEffect.throwable, sideEffect.retry)
VoteAddSideEffect.PopBackStack -> popBackStack()
is VoteAddSideEffect.PopBackStackWithVote -> popBackStackWithVote(sideEffect.vote)
VoteAddSideEffect.ShowInvalidContentSnackbar -> onShowSnackbar(
SnackbarToken(
message = context.getString(
R.string.snackbar_invalid_vote_content,
VOTE_CONTENT_MAX_LENGTH,
),
),
)

VoteAddSideEffect.ShowInvalidVoteOptionSnackbar -> onShowSnackbar(
SnackbarToken(
message = context.getString(R.string.snackbar_invalid_vote_option, VOTE_OPTION_MAX_LENGTH),
),
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import com.susu.core.ui.base.BaseViewModel
import com.susu.core.ui.extension.encodeToUri
import com.susu.domain.usecase.vote.CreateVoteUseCase
import com.susu.domain.usecase.vote.GetPostCategoryConfigUseCase
import com.susu.feature.community.VOTE_CONTENT_MAX_LENGTH
import com.susu.feature.community.VOTE_OPTION_MAX_LENGTH
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.collections.immutable.toPersistentList
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -56,14 +58,24 @@ class VoteAddViewModel @Inject constructor(
}

fun updateContent(content: String) = intent {
copy(content = content)
if (content.length > VOTE_CONTENT_MAX_LENGTH) {
postSideEffect(VoteAddSideEffect.ShowInvalidContentSnackbar)
return@intent this
}

copy(content = content.trim())
}

fun updateOptionContent(index: Int, content: String) = intent {
if (content.length > VOTE_OPTION_MAX_LENGTH) {
postSideEffect(VoteAddSideEffect.ShowInvalidVoteOptionSnackbar)
return@intent this
}

copy(
voteOptionStateList = voteOptionStateList.mapIndexed { voteIndex, voteOptionState ->
if (index == voteIndex) {
voteOptionState.copy(content = content)
voteOptionState.copy(content = content.trim())
} else {
voteOptionState
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ data class VoteEditState(
val content: String = "",
val isLoading: Boolean = false,
) : UiState {
val buttonEnabled = content.length in 1..50 &&
voteOptionStateList.all { it.length in 1..10 }
val buttonEnabled = content.isNotBlank()
}

sealed interface VoteEditSideEffect : SideEffect {
data object ShowCanNotChangeOptionSnackbar : VoteEditSideEffect
data object PopBackStack : VoteEditSideEffect
data object ShowInvalidContentSnackbar : VoteEditSideEffect
data class HandleException(val throwable: Throwable, val retry: () -> Unit) : VoteEditSideEffect
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import com.susu.core.ui.SnackbarToken
import com.susu.core.ui.extension.collectWithLifecycle
import com.susu.core.ui.extension.susuClickable
import com.susu.feature.community.R
import com.susu.feature.community.VOTE_CONTENT_MAX_LENGTH
import com.susu.feature.community.votedetail.component.VoteItem

@Composable
Expand All @@ -53,6 +54,15 @@ fun VoteEditRoute(
VoteEditSideEffect.ShowCanNotChangeOptionSnackbar -> onShowSnackbar(
SnackbarToken(message = context.getString(R.string.snackbar_can_not_change_option)),
)

VoteEditSideEffect.ShowInvalidContentSnackbar -> onShowSnackbar(
SnackbarToken(
message = context.getString(
R.string.snackbar_invalid_vote_content,
VOTE_CONTENT_MAX_LENGTH,
),
),
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.susu.core.ui.base.BaseViewModel
import com.susu.core.ui.extension.decodeFromUri
import com.susu.domain.usecase.vote.EditVoteUseCase
import com.susu.domain.usecase.vote.GetPostCategoryConfigUseCase
import com.susu.feature.community.VOTE_CONTENT_MAX_LENGTH
import com.susu.feature.community.navigation.CommunityRoute
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.collections.immutable.toPersistentList
Expand Down Expand Up @@ -80,7 +81,12 @@ class VoteEditViewModel @Inject constructor(
}

fun updateContent(content: String) = intent {
copy(content = content)
if (content.length > VOTE_CONTENT_MAX_LENGTH) {
postSideEffect(VoteEditSideEffect.ShowInvalidContentSnackbar)
return@intent this
}

copy(content = content.trim())
}

fun showCannotChangeOptionSnackbar() = postSideEffect(VoteEditSideEffect.ShowCanNotChangeOptionSnackbar)
Expand Down
2 changes: 2 additions & 0 deletions feature/community/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@
<string name="dialog_report_confirm_text">신고하기</string>
<string name="dialog_report_dismiss_text">닫기</string>
<string name="dialog_report_checkbox_text">작성자도 바로 차단하기</string>
<string name="snackbar_invalid_vote_content">글은 %d자 이내로 등록할 수 있어요</string>
<string name="snackbar_invalid_vote_option">투표 항목은 %d자 이내로 등록할 수 있어요</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ internal class MainNavigator(
in listOf(
ReceivedRoute.ledgerSearchRoute,
ReceivedRoute.ledgerFilterRoute("{${ReceivedRoute.FILTER_LEDGER_ARGUMENT_NAME}}"),
ReceivedRoute.envelopeDetailRoute("{${ReceivedRoute.ENVELOPE_ARGUMENT_NAME}}", "{${ReceivedRoute.LEDGER_ID_ARGUMENT_NAME}}"),
ReceivedRoute.envelopeEditRoute("{${ReceivedRoute.ENVELOPE_ARGUMENT_NAME}}", "{${ReceivedRoute.LEDGER_ID_ARGUMENT_NAME}}"),
ReceivedRoute.envelopeDetailRoute("{${ReceivedRoute.ENVELOPE_ARGUMENT_NAME}}", "{${ReceivedRoute.LEDGER_ARGUMENT_NAME}}"),
ReceivedRoute.envelopeEditRoute("{${ReceivedRoute.ENVELOPE_ARGUMENT_NAME}}", "{${ReceivedRoute.LEDGER_ARGUMENT_NAME}}"),
SentRoute.envelopeFilterRoute("{${SentRoute.FILTER_ENVELOPE_ARGUMENT}}"),
SentRoute.sentEnvelopeRoute("{${SentRoute.FRIEND_ID_ARGUMENT_NAME}}"),
SentRoute.sentEnvelopeDetailRoute("{${SentRoute.ENVELOPE_ID_ARGUMENT_NAME}}"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.imePadding
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.runtime.Composable
Expand Down Expand Up @@ -132,23 +134,20 @@ fun LedgerEditScreen(
.background(SusuTheme.colorScheme.background15)
.fillMaxSize(),
) {
Column(
modifier = Modifier
.background(SusuTheme.colorScheme.background15)
.fillMaxSize(),
) {
Column {
SusuDefaultAppBar(
leftIcon = {
BackIcon(onClickBack)
},
)

Spacer(modifier = Modifier.size(23.dp))

Column(
modifier = Modifier
.verticalScroll(rememberScrollState())
.weight(1f)
.padding(horizontal = SusuTheme.spacing.spacing_m),
) {
Spacer(modifier = Modifier.size(23.dp))

LedgerEditContainer(
name = stringResource(id = com.susu.core.ui.R.string.word_event_name),
verticalAlignment = Alignment.CenterVertically,
Expand Down Expand Up @@ -270,21 +269,20 @@ fun LedgerEditScreen(
},
)
}
}

SusuFilledButton(
modifier = Modifier
.align(Alignment.BottomCenter)
.fillMaxWidth()
.imePadding(),
shape = RectangleShape,
isActive = uiState.saveButtonEnabled,
isClickable = uiState.saveButtonEnabled,
color = FilledButtonColor.Black,
style = MediumButtonStyle.height60,
text = stringResource(id = com.susu.core.ui.R.string.word_save),
onClick = onClickSaveButton,
)
SusuFilledButton(
modifier = Modifier
.fillMaxWidth()
.imePadding(),
shape = RectangleShape,
isActive = uiState.saveButtonEnabled,
isClickable = uiState.saveButtonEnabled,
color = FilledButtonColor.Black,
style = MediumButtonStyle.height60,
text = stringResource(id = com.susu.core.ui.R.string.word_save),
onClick = onClickSaveButton,
)
}

if (uiState.showStartDateBottomSheet) {
SusuLimitDatePickerBottomSheet(
Expand Down
Loading