Skip to content

Commit

Permalink
Adding alt_text and custom_thumbnail to post form. Fixes LemmyNet#1513 (
Browse files Browse the repository at this point in the history
LemmyNet#1528)

* Adding alt_text and custom_thumbnail to post form. Fixes LemmyNet#1513

* Adding feature flag check.
  • Loading branch information
dessalines authored May 31, 2024
1 parent 544ee98 commit e9fd30a
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 25 deletions.
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 @@ -1510,6 +1511,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 @@ -30,6 +30,7 @@ import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.jerboa.InputField
import com.jerboa.R
import com.jerboa.api.API
import com.jerboa.datatypes.sampleCommunity
import com.jerboa.db.entity.Account
import com.jerboa.db.entity.AnonAccount
Expand All @@ -56,6 +57,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 All @@ -68,6 +74,7 @@ fun CreateEditPostBody(
padding: PaddingValues,
error: Throwable?,
) {
val api = API.getInstanceOrNull()
val scrollState = rememberScrollState()

Column(
Expand Down Expand Up @@ -141,6 +148,37 @@ fun CreateEditPostBody(
PictrsUrlImage(
url = url,
blur = false,
contentDescription = altText,
)

/**
* Alt text field. Only show if its a url image.
*/
if (api != null && api.FF.hidePost()) {
OutlinedTextField(
value = altText,
onValueChange = onAltTextChange,
label = {
Text(text = stringResource(R.string.alt_text))
},
modifier = Modifier.fillMaxWidth(),
)
}
}

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

Expand Down Expand Up @@ -240,6 +278,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 +312,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 +347,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 @@ -88,6 +88,7 @@ fun PostOptionsDropdown(
scope: CoroutineScope,
) {
val ctx = LocalContext.current
val api = getInstanceOrNull()
val localClipboardManager = LocalClipboardManager.current
val (featureIcon, unFeatureIcon) = Pair(Icons.Outlined.PushPin, Icons.Outlined.CancelPresentation)

Expand Down Expand Up @@ -315,24 +316,26 @@ fun PostOptionsDropdown(
}

// Hide / unhide post
if (postView.hidden) {
PopupMenuItem(
text = stringResource(R.string.unhide_post),
icon = Icons.Outlined.Visibility,
onClick = {
onDismissRequest()
onHidePostClick(postView)
},
)
} else {
PopupMenuItem(
text = stringResource(R.string.hide_post),
icon = Icons.Outlined.VisibilityOff,
onClick = {
onDismissRequest()
onHidePostClick(postView)
},
)
if (api != null && api.FF.hidePost()) {
if (postView.hidden) {
PopupMenuItem(
text = stringResource(R.string.unhide_post),
icon = Icons.Outlined.Visibility,
onClick = {
onDismissRequest()
onHidePostClick(postView)
},
)
} else {
PopupMenuItem(
text = stringResource(R.string.hide_post),
icon = Icons.Outlined.VisibilityOff,
onClick = {
onDismissRequest()
onHidePostClick(postView)
},
)
}
}

// Only visible from PostActivity
Expand Down Expand Up @@ -423,8 +426,6 @@ fun PostOptionsDropdown(
},
)

val api = getInstanceOrNull()

if (api != null && api.FF.instanceBlock()) {
val instance = getInstanceFromCommunityUrl(postView.community.actor_id)
PopupMenuItem(
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,6 +469,8 @@
<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>
<string name="post_hidden">Post hidden</string>
<string name="post_unhidden">Post unhidden</string>
<string name="hide_post">Hide Post</string>
Expand Down

0 comments on commit e9fd30a

Please sign in to comment.