From b825f42c7a7f66dd7f0e8a5a784656968b6e9d2b Mon Sep 17 00:00:00 2001 From: Konrad Pozniak Date: Sat, 22 Jun 2019 08:05:55 +0200 Subject: [PATCH] Improve link preview cards (#1330) * improve card frame * add placeholder for empty image * improve layout, Pleroma compatibility, show author name as description fallback * reset okhttp logging level * remove unneeded statement --- app/build.gradle | 1 + .../adapter/StatusDetailedViewHolder.java | 38 ++++++++++++++++--- .../com/keylesspalace/tusky/entity/Card.kt | 8 +++- .../com/keylesspalace/tusky/entity/Status.kt | 2 +- app/src/main/res/drawable/card_frame.xml | 5 +++ app/src/main/res/drawable/card_frame_dark.xml | 6 --- .../main/res/drawable/card_frame_light.xml | 6 --- .../res/drawable/card_image_placeholder.xml | 10 +++++ .../main/res/layout/item_status_detailed.xml | 11 ++++-- app/src/main/res/values-night/styles.xml | 3 +- app/src/main/res/values/attrs.xml | 3 +- app/src/main/res/values/dimens.xml | 4 +- app/src/main/res/values/styles.xml | 3 +- 13 files changed, 69 insertions(+), 31 deletions(-) create mode 100644 app/src/main/res/drawable/card_frame.xml delete mode 100644 app/src/main/res/drawable/card_frame_dark.xml delete mode 100644 app/src/main/res/drawable/card_frame_light.xml create mode 100644 app/src/main/res/drawable/card_image_placeholder.xml diff --git a/app/build.gradle b/app/build.gradle index 9e2347b1..9886c543 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -154,6 +154,7 @@ dependencies { //Glide implementation 'com.github.bumptech.glide:glide:4.9.0' implementation 'com.github.bumptech.glide:okhttp3-integration:4.9.0' + implementation 'jp.wasabeef:glide-transformations:4.0.1' //Add some useful extensions implementation 'androidx.core:core-ktx:1.2.0-alpha01' diff --git a/app/src/main/java/com/keylesspalace/tusky/adapter/StatusDetailedViewHolder.java b/app/src/main/java/com/keylesspalace/tusky/adapter/StatusDetailedViewHolder.java index 031c2d85..3642ce37 100644 --- a/app/src/main/java/com/keylesspalace/tusky/adapter/StatusDetailedViewHolder.java +++ b/app/src/main/java/com/keylesspalace/tusky/adapter/StatusDetailedViewHolder.java @@ -17,6 +17,7 @@ import android.widget.TextView; import android.widget.Toast; import com.bumptech.glide.Glide; +import com.bumptech.glide.load.resource.bitmap.CenterCrop; import com.keylesspalace.tusky.R; import com.keylesspalace.tusky.entity.Card; import com.keylesspalace.tusky.entity.Status; @@ -31,6 +32,8 @@ import java.util.Date; import androidx.annotation.Nullable; import androidx.recyclerview.widget.RecyclerView; +import jp.wasabeef.glide.transformations.RoundedCornersTransformation; + class StatusDetailedViewHolder extends StatusBaseViewHolder { private TextView reblogs; private TextView favourites; @@ -152,20 +155,32 @@ class StatusDetailedViewHolder extends StatusBaseViewHolder { final Card card = status.getCard(); cardView.setVisibility(View.VISIBLE); cardTitle.setText(card.getTitle()); - cardDescription.setText(card.getDescription()); + if(TextUtils.isEmpty(card.getDescription()) && TextUtils.isEmpty(card.getAuthorName())) { + cardDescription.setVisibility(View.GONE); + } else { + cardDescription.setVisibility(View.VISIBLE); + if(TextUtils.isEmpty(card.getDescription())) { + cardDescription.setText(card.getAuthorName()); + } else { + cardDescription.setText(card.getDescription()); + } + } cardUrl.setText(card.getUrl()); - if (card.getWidth() > 0 && card.getHeight() > 0 && !TextUtils.isEmpty(card.getImage())) { - cardImage.setVisibility(View.VISIBLE); + if (!TextUtils.isEmpty(card.getImage())) { + + RoundedCornersTransformation.CornerType cornertype; if (card.getWidth() > card.getHeight()) { cardView.setOrientation(LinearLayout.VERTICAL); + cardImage.getLayoutParams().height = cardImage.getContext().getResources() .getDimensionPixelSize(R.dimen.card_image_vertical_height); cardImage.getLayoutParams().width = ViewGroup.LayoutParams.MATCH_PARENT; cardInfo.getLayoutParams().height = ViewGroup.LayoutParams.MATCH_PARENT; cardInfo.getLayoutParams().width = ViewGroup.LayoutParams.WRAP_CONTENT; + cornertype = RoundedCornersTransformation.CornerType.TOP; } else { cardView.setOrientation(LinearLayout.HORIZONTAL); cardImage.getLayoutParams().height = ViewGroup.LayoutParams.MATCH_PARENT; @@ -173,17 +188,28 @@ class StatusDetailedViewHolder extends StatusBaseViewHolder { .getDimensionPixelSize(R.dimen.card_image_horizontal_width); cardInfo.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT; cardInfo.getLayoutParams().width = ViewGroup.LayoutParams.MATCH_PARENT; + cornertype = RoundedCornersTransformation.CornerType.LEFT; } - cardView.setClipToOutline(true); + int radius = cardImage.getContext().getResources() + .getDimensionPixelSize(R.dimen.card_radius); + cardView.setClipToOutline(true); Glide.with(cardImage) .load(card.getImage()) - .centerCrop() + .transform(new CenterCrop(), new RoundedCornersTransformation(radius, 0, cornertype)) .into(cardImage); } else { - cardImage.setVisibility(View.GONE); + cardView.setOrientation(LinearLayout.HORIZONTAL); + cardImage.getLayoutParams().height = ViewGroup.LayoutParams.MATCH_PARENT; + cardImage.getLayoutParams().width = cardImage.getContext().getResources() + .getDimensionPixelSize(R.dimen.card_image_horizontal_width); + cardInfo.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT; + cardInfo.getLayoutParams().width = ViewGroup.LayoutParams.MATCH_PARENT; + + cardImage.setImageResource(R.drawable.card_image_placeholder); + } cardView.setOnClickListener(v -> LinkHelper.openLink(card.getUrl(), v.getContext())); diff --git a/app/src/main/java/com/keylesspalace/tusky/entity/Card.kt b/app/src/main/java/com/keylesspalace/tusky/entity/Card.kt index c5bc55f8..c6f82637 100644 --- a/app/src/main/java/com/keylesspalace/tusky/entity/Card.kt +++ b/app/src/main/java/com/keylesspalace/tusky/entity/Card.kt @@ -16,13 +16,17 @@ package com.keylesspalace.tusky.entity import android.os.Parcelable +import android.text.Spanned +import com.google.gson.annotations.SerializedName import kotlinx.android.parcel.Parcelize +import kotlinx.android.parcel.WriteWith @Parcelize data class Card( val url: String, - val title: String, - val description: String, + val title: @WriteWith() Spanned, + val description: @WriteWith() Spanned, + @SerializedName("author_name") val authorName: String, val image: String, val type: String, val width: Int, diff --git a/app/src/main/java/com/keylesspalace/tusky/entity/Status.kt b/app/src/main/java/com/keylesspalace/tusky/entity/Status.kt index 48eff64c..202be598 100644 --- a/app/src/main/java/com/keylesspalace/tusky/entity/Status.kt +++ b/app/src/main/java/com/keylesspalace/tusky/entity/Status.kt @@ -130,7 +130,7 @@ data class Status( data class Application ( val name: String, - val website: String + val website: String? ) companion object { diff --git a/app/src/main/res/drawable/card_frame.xml b/app/src/main/res/drawable/card_frame.xml new file mode 100644 index 00000000..87e28d8b --- /dev/null +++ b/app/src/main/res/drawable/card_frame.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/card_frame_dark.xml b/app/src/main/res/drawable/card_frame_dark.xml deleted file mode 100644 index e68b196b..00000000 --- a/app/src/main/res/drawable/card_frame_dark.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/app/src/main/res/drawable/card_frame_light.xml b/app/src/main/res/drawable/card_frame_light.xml deleted file mode 100644 index a7f15ab5..00000000 --- a/app/src/main/res/drawable/card_frame_light.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/app/src/main/res/drawable/card_image_placeholder.xml b/app/src/main/res/drawable/card_image_placeholder.xml new file mode 100644 index 00000000..1ca515a3 --- /dev/null +++ b/app/src/main/res/drawable/card_image_placeholder.xml @@ -0,0 +1,10 @@ + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/item_status_detailed.xml b/app/src/main/res/layout/item_status_detailed.xml index cd496066..18ae205b 100644 --- a/app/src/main/res/layout/item_status_detailed.xml +++ b/app/src/main/res/layout/item_status_detailed.xml @@ -134,8 +134,10 @@ android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="8dp" - android:background="?attr/card_background" + android:background="@drawable/card_frame" android:clipChildren="true" + android:foreground="?attr/selectableItemBackground" + android:minHeight="80dp" android:orientation="vertical" app:layout_constraintTop_toBottomOf="@+id/status_content" tools:visibility="gone"> @@ -144,7 +146,10 @@ android:id="@+id/card_image" android:layout_width="match_parent" android:layout_height="300dp" - android:background="?attr/card_image_background" /> + android:layout_margin="1dp" + android:background="?attr/card_background_color" + android:importantForAccessibility="no" + android:scaleType="center" /> @color/text_color_primary_dark @color/text_color_secondary_dark - @drawable/card_frame_dark - @color/text_color_tertiary_dark + @color/color_primary_dark @drawable/ic_play_indicator_dark diff --git a/app/src/main/res/values/attrs.xml b/app/src/main/res/values/attrs.xml index 8fc2da67..7477a44b 100644 --- a/app/src/main/res/values/attrs.xml +++ b/app/src/main/res/values/attrs.xml @@ -35,8 +35,7 @@ - - + diff --git a/app/src/main/res/values/dimens.xml b/app/src/main/res/values/dimens.xml index a58bf369..7ba79b6a 100644 --- a/app/src/main/res/values/dimens.xml +++ b/app/src/main/res/values/dimens.xml @@ -14,7 +14,7 @@ 16dp 5dp - 160dp + 210dp 100dp 16dp @@ -40,4 +40,6 @@ 4.5dp 3dp 160dp + + 5dp diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index f34c6324..e9f341da 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -125,8 +125,7 @@ @color/text_color_secondary_dark - @drawable/card_frame_light - @color/text_color_tertiary_light + @color/color_primary_light @drawable/ic_play_indicator_light