From 5eae33d457724ae73708da2a28c0362eb734335e Mon Sep 17 00:00:00 2001 From: sockenklaus Date: Fri, 23 Jun 2023 19:55:53 +0200 Subject: [PATCH] Add switch to mark new posts as NSFW / toggle tag on existing posts (#833) * Added a switch do CreatePost.kt and PostEdit.kt to mark a new post as NSFW and change the tag of an existing post. Added logic in the corresponding Activities. Closes #679 * Changed switch to checkbox, changed arrangement to "SpaceBetween", moved composable out of CreatePost.kt and PostEdit.kt to reduce unnecessary redundancy. --- .../post/composables/PostComposables.kt | 32 +++++++++++++++++++ .../ui/components/post/create/CreatePost.kt | 11 +++++++ .../post/create/CreatePostActivity.kt | 4 +++ .../ui/components/post/edit/PostEdit.kt | 9 ++++++ .../components/post/edit/PostEditActivity.kt | 4 +++ app/src/main/res/values-de/strings.xml | 1 + app/src/main/res/values/strings.xml | 1 + 7 files changed, 62 insertions(+) create mode 100644 app/src/main/java/com/jerboa/ui/components/post/composables/PostComposables.kt diff --git a/app/src/main/java/com/jerboa/ui/components/post/composables/PostComposables.kt b/app/src/main/java/com/jerboa/ui/components/post/composables/PostComposables.kt new file mode 100644 index 000000000..aba6fe853 --- /dev/null +++ b/app/src/main/java/com/jerboa/ui/components/post/composables/PostComposables.kt @@ -0,0 +1,32 @@ +package com.jerboa.ui.components.post.composables + +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.material3.Checkbox +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.res.stringResource +import com.jerboa.R + +@Composable +fun CheckboxIsNsfw( + checked: Boolean, + onCheckedChange: (checked: Boolean) -> Unit, +) { + Row( + verticalAlignment = Alignment.CenterVertically, + horizontalArrangement = Arrangement.SpaceBetween, + modifier = Modifier.fillMaxWidth(), + ) { + Text( + text = stringResource(R.string.create_post_tag_nsfw), + ) + Checkbox( + checked = checked, + onCheckedChange = onCheckedChange, + ) + } +} diff --git a/app/src/main/java/com/jerboa/ui/components/post/create/CreatePost.kt b/app/src/main/java/com/jerboa/ui/components/post/create/CreatePost.kt index a987bd0a3..278216177 100644 --- a/app/src/main/java/com/jerboa/ui/components/post/create/CreatePost.kt +++ b/app/src/main/java/com/jerboa/ui/components/post/create/CreatePost.kt @@ -43,6 +43,7 @@ import com.jerboa.db.Account import com.jerboa.ui.components.common.CircularIcon import com.jerboa.ui.components.common.MarkdownTextField import com.jerboa.ui.components.common.PickImage +import com.jerboa.ui.components.post.composables.CheckboxIsNsfw import com.jerboa.ui.theme.ICON_SIZE import com.jerboa.ui.theme.MEDIUM_PADDING import com.jerboa.ui.theme.THUMBNAIL_SIZE @@ -120,6 +121,8 @@ fun CreatePostBody( padding: PaddingValues, suggestedTitle: String?, suggestedTitleLoading: Boolean, + isNsfw: Boolean, + onIsNsfwChange: (isNsfw: Boolean) -> Unit, ) { val nameField = validatePostName(name) val urlField = validateUrl(url) @@ -237,6 +240,10 @@ fun CreatePostBody( }, ) } + CheckboxIsNsfw( + checked = isNsfw, + onCheckedChange = onIsNsfwChange, + ) } } @@ -267,6 +274,8 @@ fun CreatePostBodyPreview() { padding = PaddingValues(), suggestedTitle = null, suggestedTitleLoading = false, + isNsfw = false, + onIsNsfwChange = {}, ) } @@ -286,5 +295,7 @@ fun CreatePostBodyPreviewNoCommunity() { suggestedTitleLoading = false, account = null, padding = PaddingValues(), + isNsfw = false, + onIsNsfwChange = {}, ) } diff --git a/app/src/main/java/com/jerboa/ui/components/post/create/CreatePostActivity.kt b/app/src/main/java/com/jerboa/ui/components/post/create/CreatePostActivity.kt index 7d4f62ec1..92acee8c1 100644 --- a/app/src/main/java/com/jerboa/ui/components/post/create/CreatePostActivity.kt +++ b/app/src/main/java/com/jerboa/ui/components/post/create/CreatePostActivity.kt @@ -60,6 +60,7 @@ fun CreatePostActivity( ), ) } + var isNsfw by rememberSaveable { mutableStateOf(false) } var formValid by rememberSaveable { mutableStateOf(false) } LaunchedEffect(initialUrl) { @@ -107,6 +108,7 @@ fun CreatePostActivity( url = urlOut, body = bodyOut, auth = acct.jwt, + nsfw = isNsfw, ), navController, ) @@ -154,6 +156,8 @@ fun CreatePostActivity( }, account = account, padding = padding, + isNsfw = isNsfw, + onIsNsfwChange = { isNsfw = it }, ) }, ) diff --git a/app/src/main/java/com/jerboa/ui/components/post/edit/PostEdit.kt b/app/src/main/java/com/jerboa/ui/components/post/edit/PostEdit.kt index bbb5aeadd..db0c6fa84 100644 --- a/app/src/main/java/com/jerboa/ui/components/post/edit/PostEdit.kt +++ b/app/src/main/java/com/jerboa/ui/components/post/edit/PostEdit.kt @@ -26,6 +26,7 @@ import com.jerboa.R import com.jerboa.db.Account import com.jerboa.ui.components.common.MarkdownTextField import com.jerboa.ui.components.common.PickImage +import com.jerboa.ui.components.post.composables.CheckboxIsNsfw import com.jerboa.ui.theme.MEDIUM_PADDING import com.jerboa.validatePostName import com.jerboa.validateUrl @@ -91,6 +92,8 @@ fun EditPostBody( formValid: (valid: Boolean) -> Unit, account: Account?, modifier: Modifier = Modifier, + isNsfw: Boolean, + onIsNsfwChange: (isNsfw: Boolean) -> Unit, ) { val nameField = validatePostName(name) val urlField = validateUrl(url) @@ -144,6 +147,10 @@ fun EditPostBody( focusImmediate = false, placeholder = stringResource(R.string.post_edit_body_placeholder), ) + CheckboxIsNsfw( + checked = isNsfw, + onCheckedChange = onIsNsfwChange, + ) } } @@ -160,5 +167,7 @@ fun EditPostBodyPreview() { onPickedImage = {}, onUrlChange = {}, account = null, + isNsfw = false, + onIsNsfwChange = {}, ) } diff --git a/app/src/main/java/com/jerboa/ui/components/post/edit/PostEditActivity.kt b/app/src/main/java/com/jerboa/ui/components/post/edit/PostEditActivity.kt index afd29e216..953fcff6e 100644 --- a/app/src/main/java/com/jerboa/ui/components/post/edit/PostEditActivity.kt +++ b/app/src/main/java/com/jerboa/ui/components/post/edit/PostEditActivity.kt @@ -50,6 +50,7 @@ fun PostEditActivity( val pv = postEditViewModel.postView var name by rememberSaveable { mutableStateOf(pv?.post?.name.orEmpty()) } var url by rememberSaveable { mutableStateOf(pv?.post?.url.orEmpty()) } + var isNsfw by rememberSaveable { mutableStateOf(pv?.post?.nsfw ?: false) } var body by rememberSaveable(stateSaver = TextFieldValue.Saver) { mutableStateOf( TextFieldValue( @@ -84,6 +85,7 @@ fun PostEditActivity( url = urlOut, body = bodyOut, auth = acct.jwt, + nsfw = isNsfw, ), navController = navController, postViewModel = postViewModel, @@ -120,6 +122,8 @@ fun PostEditActivity( modifier = Modifier .padding(padding) .imePadding(), + isNsfw = isNsfw, + onIsNsfwChange = { isNsfw = it }, ) }, ) diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml index 966cedf60..14a0604e8 100644 --- a/app/src/main/res/values-de/strings.xml +++ b/app/src/main/res/values-de/strings.xml @@ -312,4 +312,5 @@ Erwähnungen Nachrichten Gespeichert + Markiere Post als NSFW diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index ce5c1e5d2..de7e04f19 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -117,6 +117,7 @@ Community copy suggested title: %1$s Create post + Mark post as NSFW Back Report Type your reason