Skip to content

Commit

Permalink
fix: Correctly position preview card images, show card description (#860
Browse files Browse the repository at this point in the history
)

Byline changes inadvertently changed how the preview image is laid out,
breaking the "Image at start, info at end" variant.

Previous code did not always show the card description if the text was
present, fix that.
  • Loading branch information
nikclayton authored Jul 31, 2024
1 parent a0b3b1f commit ff8b71a
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 19 deletions.
60 changes: 47 additions & 13 deletions app/src/main/java/app/pachli/view/PreviewCardView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.constraintlayout.widget.ConstraintLayout
import app.pachli.R
import app.pachli.core.activity.decodeBlurHash
import app.pachli.core.activity.emojify
Expand Down Expand Up @@ -129,7 +130,10 @@ class PreviewCardView @JvmOverloads constructor(
card.description.isNotBlank() -> card.description
card.authorName.isNotBlank() -> card.authorName
else -> null
}?.let { cardDescription.text = it } ?: cardDescription.hide()
}?.let {
cardDescription.text = it
cardDescription.show()
} ?: cardDescription.hide()

previewCardWrapper.setOnClickListener { listener.onClick(card, Target.CARD) }
cardImage.setOnClickListener { listener.onClick(card, Target.IMAGE) }
Expand All @@ -150,7 +154,7 @@ class PreviewCardView @JvmOverloads constructor(
cardImage.shapeAppearanceModel = if (card.width > card.height) {
setTopBottomLayout()
} else {
setLeftRightLayout()
setStartEndLayout()
}.build()

cardImage.scaleType = ImageView.ScaleType.CENTER_CROP
Expand All @@ -167,7 +171,7 @@ class PreviewCardView @JvmOverloads constructor(
}
} else if (statusDisplayOptions.useBlurhash && !card.blurhash.isNullOrBlank()) {
cardImage.show()
cardImage.shapeAppearanceModel = setLeftRightLayout().build()
cardImage.shapeAppearanceModel = setStartEndLayout().build()
cardImage.scaleType = ImageView.ScaleType.CENTER_CROP

Glide.with(cardImage.context)
Expand All @@ -191,23 +195,53 @@ class PreviewCardView @JvmOverloads constructor(
/** Adjusts the layout parameters to place the image above the information views */
private fun setTopBottomLayout() = with(binding) {
val cardImageShape = ShapeAppearanceModel.Builder()
cardImage.layoutParams.height =
cardImage.resources.getDimensionPixelSize(DR.dimen.card_image_vertical_height)
cardImage.layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT

// Move image to top.
with(cardImage.layoutParams as ConstraintLayout.LayoutParams) {
height = cardImage.resources.getDimensionPixelSize(DR.dimen.card_image_vertical_height)
width = ViewGroup.LayoutParams.MATCH_PARENT

bottomToBottom = ConstraintLayout.LayoutParams.UNSET
}

// Move cardInfo below image
with(cardInfo.layoutParams as ConstraintLayout.LayoutParams) {
startToStart = ConstraintLayout.LayoutParams.PARENT_ID
topToBottom = cardImage.id

startToEnd = ConstraintLayout.LayoutParams.UNSET
topToTop = ConstraintLayout.LayoutParams.UNSET
}

cardInfo.layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT
cardImageShape.setTopLeftCorner(CornerFamily.ROUNDED, cardCornerRadius)
cardImageShape.setTopRightCorner(CornerFamily.ROUNDED, cardCornerRadius)
return@with cardImageShape
}

/** Adjusts the layout parameters to place the image on the left, the information on the right */
private fun setLeftRightLayout() = with(binding) {
/**
* Adjusts the layout parameters to place the image at the start, the information at
* the end.
*/
private fun setStartEndLayout() = with(binding) {
val cardImageShape = ShapeAppearanceModel.Builder()
cardImage.layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT
cardImage.layoutParams.width =
cardImage.resources.getDimensionPixelSize(DR.dimen.card_image_horizontal_width)
cardInfo.layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT
cardInfo.layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT

// Move image to start with fixed width to allow space for cardInfo.
with(cardImage.layoutParams as ConstraintLayout.LayoutParams) {
height = ConstraintLayout.LayoutParams.MATCH_CONSTRAINT
width = cardImage.resources.getDimensionPixelSize(DR.dimen.card_image_horizontal_width)
bottomToBottom = ConstraintLayout.LayoutParams.PARENT_ID
}

// Move cardInfo to end of image
with(cardInfo.layoutParams as ConstraintLayout.LayoutParams) {
startToEnd = binding.cardImage.id
topToTop = ConstraintLayout.LayoutParams.PARENT_ID

startToStart = ConstraintLayout.LayoutParams.UNSET
topToBottom = ConstraintLayout.LayoutParams.UNSET
}

cardImageShape.setTopLeftCorner(CornerFamily.ROUNDED, cardCornerRadius)
cardImageShape.setBottomLeftCorner(CornerFamily.ROUNDED, cardCornerRadius)
return@with cardImageShape
Expand Down
13 changes: 7 additions & 6 deletions app/src/main/res/layout/preview_card.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,15 @@

<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/card_info"
android:layout_width="match_parent"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/card_image"
app:layout_constraintStart_toStartOf="parent"
android:paddingLeft="6dp"
android:paddingTop="6dp"
android:paddingRight="6dp"
android:paddingBottom="6dp">
android:paddingBottom="6dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/card_image">

<TextView
android:id="@+id/card_title"
Expand Down Expand Up @@ -102,8 +103,8 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
app:layout_constraintTop_toBottomOf="@id/card_link"
app:layout_constraintStart_toStartOf="parent" />
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/card_link" />

<TextView
android:id="@+id/author_info"
Expand Down

0 comments on commit ff8b71a

Please sign in to comment.