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

Adding alt_text and custom_thumbnail to post form. Fixes #1513 #1528

Merged
merged 3 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ local.properties
.project
.settings
.classpath
.kotlin
3 changes: 2 additions & 1 deletion app/src/main/java/com/jerboa/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,7 @@ fun validatePostName(
fun validateUrl(
ctx: Context,
url: String,
label: String = ctx.getString(R.string.url),
): InputField {
return if (url.isNotEmpty() && !PatternsCompat.WEB_URL.matcher(url).matches()) {
InputField(
Expand All @@ -667,7 +668,7 @@ fun validateUrl(
)
} else {
InputField(
label = ctx.getString(R.string.url),
label = label,
hasError = false,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ fun PictrsThumbnailImage(
blur: Boolean,
roundBottomEndCorner: Boolean,
modifier: Modifier = Modifier,
contentDescription: String? = null,
) {
val ctx = LocalContext.current
val imageRequest =
Expand All @@ -148,7 +149,7 @@ fun PictrsThumbnailImage(
AsyncImage(
model = imageRequest,
placeholder = painterResource(R.drawable.ic_launcher_foreground),
contentDescription = null,
contentDescription = contentDescription,
contentScale = ContentScale.Crop,
modifier =
modifier
Expand All @@ -172,6 +173,7 @@ fun PictrsUrlImage(
url: String,
blur: Boolean,
modifier: Modifier = Modifier,
contentDescription: String? = null,
) {
val ctx = LocalContext.current
val imageRequest =
Expand All @@ -187,7 +189,7 @@ fun PictrsUrlImage(
AsyncImage(
model = imageRequest,
placeholder = painterResource(R.drawable.ic_launcher_foreground),
contentDescription = null,
contentDescription = contentDescription,
contentScale = ContentScale.FillWidth,
modifier =
modifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ fun PostTitleAndImageLink(
PictrsUrlImage(
url = cUrl,
blur = blurNSFW.needBlur(postView),
contentDescription = postView.post.alt_text,
modifier =
Modifier
.combinedClickable(
Expand Down Expand Up @@ -1499,6 +1500,7 @@ private fun ThumbnailTile(
thumbnail = thumbnail,
blur = blurNSFW.needBlur(postView),
roundBottomEndCorner = postType != PostType.Link,
contentDescription = postView.post.alt_text,
modifier = postLinkPicMod,
)
} ?: run {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ fun CreateEditPostBody(
url: String,
urlField: InputField,
onUrlChange: (url: String) -> Unit,
altText: String,
onAltTextChange: (altText: String) -> Unit,
customThumbnailField: InputField,
customThumbnail: String,
onCustomThumbnailChange: (customThumbnail: String) -> Unit,
sharedImage: Uri? = null,
onImagePicked: (image: Uri) -> Unit,
isUploadingImage: Boolean = false,
Expand Down Expand Up @@ -141,9 +146,36 @@ fun CreateEditPostBody(
PictrsUrlImage(
url = url,
blur = false,
contentDescription = altText,
)

/**
* Alt text field. Only show if its a url image.
*/
OutlinedTextField(
MV-GH marked this conversation as resolved.
Show resolved Hide resolved
value = altText,
onValueChange = onAltTextChange,
label = {
Text(text = stringResource(R.string.alt_text))
},
modifier = Modifier.fillMaxWidth(),
)
}

/**
* Post custom thumbnail
*/
OutlinedTextField(
value = customThumbnail,
onValueChange = onCustomThumbnailChange,
label = {
Text(text = customThumbnailField.label)
},
isError = customThumbnailField.hasError,
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Uri),
modifier = Modifier.fillMaxWidth(),
)

/**
* Markdown field for post body
*/
Expand Down Expand Up @@ -240,6 +272,11 @@ fun CreatePostBodyPreview() {
url = "",
urlField = InputField(label = "", hasError = false),
onUrlChange = {},
altText = "",
onAltTextChange = {},
customThumbnailField = InputField(label = "", hasError = false),
customThumbnail = "",
onCustomThumbnailChange = {},
onImagePicked = {},
account = AnonAccount,
padding = PaddingValues(),
Expand Down Expand Up @@ -269,6 +306,11 @@ fun CreatePostBodyPreviewNoCommunity() {
url = "",
urlField = InputField(label = "", hasError = false),
onUrlChange = {},
altText = "",
onAltTextChange = {},
customThumbnailField = InputField(label = "", hasError = false),
customThumbnail = "",
onCustomThumbnailChange = {},
onImagePicked = {},
suggestedTitle = stringResource(R.string.create_post_a_title_here),
suggestedTitleLoading = false,
Expand Down Expand Up @@ -299,6 +341,11 @@ fun EditPostBodyPreview() {
onNameChange = {},
onImagePicked = {},
onUrlChange = {},
altText = "",
onAltTextChange = {},
customThumbnailField = InputField(label = "", hasError = false),
customThumbnail = "",
onCustomThumbnailChange = {},
account = AnonAccount,
isNsfw = false,
onIsNsfwChange = {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,13 @@ fun CreatePostActivity(
)
}
var isUploadingImage by rememberSaveable { mutableStateOf(false) }
var altText by rememberSaveable { mutableStateOf("") }
var customThumbnail by rememberSaveable { mutableStateOf("") }

val nameField = validatePostName(ctx, name)
val urlField = validateUrl(ctx, url)
val formValid = !nameField.hasError && !urlField.hasError && (selectedCommunity !== null)
val customThumbnailField = validateUrl(ctx, customThumbnail, ctx.getString(R.string.custom_thumbnail))
val formValid = !nameField.hasError && !urlField.hasError && !customThumbnailField.hasError && (selectedCommunity !== null)

LaunchedEffect(initialUrl) {
if (initialUrl.isNotEmpty()) {
Expand Down Expand Up @@ -137,6 +140,8 @@ fun CreatePostActivity(
name = name,
body = body,
url = url,
altText = altText,
customThumbnail = customThumbnail,
isNsfw = isNsfw,
selectedCommunity = selectedCommunity,
createPostViewModel = createPostViewModel,
Expand Down Expand Up @@ -179,6 +184,11 @@ fun CreatePostActivity(
}
}
},
altText = altText,
onAltTextChange = { altText = it },
customThumbnailField = customThumbnailField,
customThumbnail = customThumbnail,
onCustomThumbnailChange = { customThumbnail = it },
suggestedTitle = suggestedTitle,
suggestedTitleLoading = suggestedTitleLoading,
sharedImage = initialImage,
Expand Down Expand Up @@ -217,6 +227,8 @@ fun onSubmitClick(
name: String,
body: TextFieldValue,
url: String,
altText: String,
customThumbnail: String,
isNsfw: Boolean,
selectedCommunity: Community?,
createPostViewModel: CreatePostViewModel,
Expand All @@ -227,11 +239,15 @@ fun onSubmitClick(
val nameOut = name.trim()
val bodyOut = body.text.trim().ifEmpty { null }
val urlOut = url.trim().ifEmpty { null }?.padUrlWithHttps()
val altTextOut = altText.trim().ifEmpty { null }
val customThumbnailOut = customThumbnail.trim().ifEmpty { null }
createPostViewModel.createPost(
CreatePost(
name = nameOut,
community_id = it,
url = urlOut,
alt_text = altTextOut,
custom_thumbnail = customThumbnailOut,
body = bodyOut,
nsfw = isNsfw,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,13 @@ fun PostEditActivity(
)
}
var isUploadingImage by rememberSaveable { mutableStateOf(false) }
var altText by rememberSaveable { mutableStateOf(postView.post.alt_text.orEmpty()) }
var customThumbnail by rememberSaveable { mutableStateOf("") }

val nameField = validatePostName(ctx, name)
val urlField = validateUrl(ctx, url)
val formValid = !nameField.hasError && !urlField.hasError
val customThumbnailField = validateUrl(ctx, customThumbnail, ctx.getString(R.string.custom_thumbnail))
val formValid = !nameField.hasError && !urlField.hasError && !customThumbnailField.hasError

Scaffold(
topBar = {
Expand All @@ -88,6 +91,8 @@ fun PostEditActivity(
name = name,
body = body,
url = url,
altText = altText,
customThumbnail = customThumbnail,
postEditViewModel = postEditViewModel,
isNsfw = isNsfw,
appState = appState,
Expand All @@ -110,6 +115,11 @@ fun PostEditActivity(
url = url,
urlField = urlField,
onUrlChange = { url = it },
altText = altText,
onAltTextChange = { altText = it },
customThumbnailField = customThumbnailField,
customThumbnail = customThumbnail,
onCustomThumbnailChange = { customThumbnail = it },
onImagePicked = { uri ->
if (!account.isAnon()) {
val imageIs = imageInputStreamFromUri(ctx, uri)
Expand Down Expand Up @@ -140,6 +150,8 @@ fun onSubmitClick(
name: String,
body: TextFieldValue,
url: String,
altText: String,
customThumbnail: String,
postEditViewModel: PostEditViewModel,
isNsfw: Boolean,
appState: JerboaAppState,
Expand All @@ -148,13 +160,17 @@ fun onSubmitClick(
val nameOut = name.trim()
val bodyOut = body.text.trim().ifEmpty { null }
val urlOut = url.trim().ifEmpty { null }
val altTextOut = altText.trim().ifEmpty { null }
val customThumbnailOut = customThumbnail.trim().ifEmpty { null }

postEditViewModel.editPost(
form =
EditPost(
post_id = postId,
name = nameOut,
url = urlOut,
alt_text = altTextOut,
custom_thumbnail = customThumbnailOut,
body = bodyOut,
nsfw = isNsfw,
),
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -469,4 +469,6 @@
<string name="site_legal_info_name">%1$s legal info</string>
<string name="moderators">Moderators</string>
<string name="admins">Admins</string>
<string name="custom_thumbnail">Custom Thumbnail</string>
<string name="alt_text">Alt Text</string>
</resources>