fix: set community in post creation (#517)

This commit is contained in:
Diego Beraldin 2024-02-09 12:45:21 +01:00
parent 77d7d9c697
commit 5192e4cce6
2 changed files with 34 additions and 35 deletions

View File

@ -53,6 +53,7 @@ import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.KeyboardCapitalization import androidx.compose.ui.text.input.KeyboardCapitalization
import androidx.compose.ui.text.input.KeyboardType import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.input.TextFieldValue import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextDecoration import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import cafe.adriel.voyager.core.screen.Screen import cafe.adriel.voyager.core.screen.Screen
@ -161,7 +162,7 @@ class CreatePostScreen(
LaunchedEffect(model, communityId) { LaunchedEffect(model, communityId) {
communityId?.also { communityId -> communityId?.also { communityId ->
CreatePostMviModel.Intent.SetCommunity(CommunityModel(id = communityId)) model.reduce(CreatePostMviModel.Intent.SetCommunity(CommunityModel(id = communityId)))
} }
} }
LaunchedEffect(model) { LaunchedEffect(model) {
@ -500,22 +501,6 @@ class CreatePostScreen(
color = MaterialTheme.colorScheme.error, color = MaterialTheme.colorScheme.error,
) )
} }
if (uiState.currentUser.isNotEmpty()) {
Text(
text = buildString {
append(stringResource(MR.strings.post_reply_source_account))
append(" ")
append(uiState.currentUser)
if (uiState.currentInstance.isNotEmpty()) {
append("@")
append(uiState.currentInstance)
}
},
color = MaterialTheme.colorScheme.onBackground,
style = MaterialTheme.typography.labelSmall,
textDecoration = TextDecoration.Underline,
)
}
} }
}, },
) )
@ -538,6 +523,27 @@ class CreatePostScreen(
showScores = uiState.showScores, showScores = uiState.showScores,
) )
} }
if (uiState.currentUser.isNotEmpty()) {
Text(
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = Spacing.m),
text = buildString {
append(stringResource(MR.strings.post_reply_source_account))
append(" ")
append(uiState.currentUser)
if (uiState.currentInstance.isNotEmpty()) {
append("@")
append(uiState.currentInstance)
}
},
color = MaterialTheme.colorScheme.onBackground,
style = MaterialTheme.typography.labelSmall,
textDecoration = TextDecoration.Underline,
textAlign = TextAlign.End,
)
}
} }
} }
} }

View File

@ -164,7 +164,8 @@ class CreatePostViewModel(
} }
private fun submit(body: String) { private fun submit(body: String) {
if (uiState.value.loading) { val currentState = uiState.value
if (currentState.loading) {
return return
} }
@ -176,41 +177,33 @@ class CreatePostViewModel(
) )
} }
val communityId = uiState.value.communityId val communityId = currentState.communityId
val title = uiState.value.title val title = currentState.title
val url = uiState.value.url.takeIf { it.isNotEmpty() }?.trim() val url = currentState.url.takeIf { it.isNotEmpty() }?.trim()
val nsfw = uiState.value.nsfw val nsfw = currentState.nsfw
val languageId = uiState.value.currentLanguageId val languageId = currentState.currentLanguageId
var valid = true var valid = true
if (title.isEmpty()) { if (title.isEmpty()) {
updateState { updateState {
it.copy( it.copy(titleError = message_missing_field.desc())
titleError = message_missing_field.desc(),
)
} }
valid = false valid = false
} }
if (body.isEmpty()) { if (body.isEmpty()) {
updateState { updateState {
it.copy( it.copy(bodyError = message_missing_field.desc())
bodyError = message_missing_field.desc(),
)
} }
valid = false valid = false
} }
if (!url.isNullOrEmpty() && !url.isValidUrl()) { if (!url.isNullOrEmpty() && !url.isValidUrl()) {
updateState { updateState {
it.copy( it.copy(urlError = message_invalid_field.desc())
urlError = message_invalid_field.desc(),
)
} }
valid = false valid = false
} }
if (communityId == null) { if (communityId == null) {
updateState { updateState {
it.copy( it.copy(communityError = message_missing_field.desc())
communityError = message_missing_field.desc(),
)
} }
valid = false valid = false
} }