From 21835800ff208e253ccb3d17a47dc47028e922a1 Mon Sep 17 00:00:00 2001 From: jubb Date: Fri, 25 Jun 2021 12:18:52 +1000 Subject: [PATCH] feat: wiring up click listeners in AlbumThumbnailView.kt --- .../components/AlbumThumbnailView.java | 18 +++---- .../conversation/v2/AlbumThumbnailView.kt | 53 +++++++++++++++++-- .../v2/messages/VisibleMessageContentView.kt | 18 ++++++- .../v2/utilities/KThumbnailView.kt | 19 +++++-- .../main/res/layout/album_thumbnail_view.xml | 38 ++++++++++--- 5 files changed, 120 insertions(+), 26 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/components/AlbumThumbnailView.java b/app/src/main/java/org/thoughtcrime/securesms/components/AlbumThumbnailView.java index 8274e463fa..df88367710 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/components/AlbumThumbnailView.java +++ b/app/src/main/java/org/thoughtcrime/securesms/components/AlbumThumbnailView.java @@ -1,27 +1,27 @@ package org.thoughtcrime.securesms.components; import android.content.Context; -import androidx.annotation.ColorInt; -import androidx.annotation.IdRes; -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; import android.util.AttributeSet; import android.view.ViewGroup; import android.widget.FrameLayout; import android.widget.TextView; -import network.loki.messenger.R; +import androidx.annotation.ColorInt; +import androidx.annotation.IdRes; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; -import org.thoughtcrime.securesms.conversation.v2.utilities.ThumbnailView; +import org.session.libsession.utilities.Stub; +import org.thoughtcrime.securesms.conversation.v2.utilities.KThumbnailView; import org.thoughtcrime.securesms.mms.GlideRequests; - import org.thoughtcrime.securesms.mms.Slide; import org.thoughtcrime.securesms.mms.SlideClickListener; import org.thoughtcrime.securesms.mms.SlidesClickedListener; -import org.session.libsession.utilities.Stub; import java.util.List; +import network.loki.messenger.R; + public class AlbumThumbnailView extends FrameLayout { private @Nullable SlideClickListener thumbnailClickListener; @@ -149,7 +149,7 @@ public class AlbumThumbnailView extends FrameLayout { } private void setSlide(@NonNull GlideRequests glideRequests, @NonNull Slide slide, @IdRes int id) { - ThumbnailView cell = findViewById(id); + KThumbnailView cell = findViewById(id); cell.setImageResource(glideRequests, slide, false, false); cell.setThumbnailClickListener(defaultThumbnailClickListener); cell.setOnLongClickListener(defaultLongClickListener); diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/AlbumThumbnailView.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/AlbumThumbnailView.kt index ecd23e0b03..013f21fe59 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/AlbumThumbnailView.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/AlbumThumbnailView.kt @@ -4,6 +4,7 @@ import android.content.Context import android.graphics.Canvas import android.util.AttributeSet import android.view.LayoutInflater +import android.view.View import android.view.ViewGroup import android.widget.FrameLayout import androidx.core.view.isVisible @@ -11,21 +12,27 @@ import kotlinx.android.synthetic.main.album_thumbnail_view.view.* import network.loki.messenger.R import org.thoughtcrime.securesms.components.CornerMask import org.thoughtcrime.securesms.conversation.v2.utilities.KThumbnailView -import org.thoughtcrime.securesms.conversation.v2.utilities.ThumbnailView import org.thoughtcrime.securesms.database.model.MmsMessageRecord import org.thoughtcrime.securesms.mms.GlideRequests +import org.thoughtcrime.securesms.mms.Slide +import org.thoughtcrime.securesms.mms.SlideClickListener +import org.thoughtcrime.securesms.mms.SlidesClickedListener -class AlbumThumbnailView: FrameLayout { +class AlbumThumbnailView: FrameLayout, SlideClickListener, SlidesClickedListener, View.OnClickListener { // region Lifecycle constructor(context: Context) : super(context) { initialize() } constructor(context: Context, attrs: AttributeSet) : super(context, attrs) { initialize() } constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) { initialize() } + private var slideClickListener: ((Slide) -> Unit)? = null + private var downloadClickListener: ((Slide) -> Unit)? = null + private var readMoreListener: (() -> Unit)? = null private val cornerMask by lazy { CornerMask(this) } private fun initialize() { LayoutInflater.from(context).inflate(R.layout.album_thumbnail_view, this) + albumCellBodyTextReadMore.setOnClickListener(this) } override fun dispatchDraw(canvas: Canvas?) { @@ -37,7 +44,34 @@ class AlbumThumbnailView: FrameLayout { // region Interaction - fun bind(glideRequests: GlideRequests, message: MmsMessageRecord, isStart: Boolean, isEnd: Boolean) { + override fun onClick(v: View?) { + // clicked the view or one of its children + if (v === albumCellBodyTextReadMore) { + readMoreListener?.invoke() + } + } + + override fun onClick(v: View?, slide: Slide?) { + // slide thumbnail clicked + if (slide==null) return + slideClickListener?.invoke(slide) + } + + override fun onClick(v: View?, slides: MutableList?) { + // slide download clicked + if (slides.isNullOrEmpty()) return + slides.firstOrNull().let { slide -> + if (slide == null) return@let + downloadClickListener?.invoke(slide) + } + } + + fun bind(glideRequests: GlideRequests, message: MmsMessageRecord, + clickListener: (Slide)->Unit, downloadClickListener: (Slide)->Unit, readMoreListener: ()->Unit, + isStart: Boolean, isEnd: Boolean) { + this.slideClickListener = clickListener + this.downloadClickListener = downloadClickListener + this.readMoreListener = readMoreListener // TODO: optimize for same size val slides = message.slideDeck.thumbnailSlides if (slides.isEmpty()) { @@ -49,10 +83,21 @@ class AlbumThumbnailView: FrameLayout { LayoutInflater.from(context).inflate(layoutRes(slides.size), albumCellContainer) // iterate slides.take(5).forEachIndexed { position, slide -> - val imageResource = getThumbnailView(position).setImageResource(glideRequests, slide, showControls = false, isPreview = false) + val thumbnailView = getThumbnailView(position) + thumbnailView.thumbnailClickListener = this + thumbnailView.setImageResource(glideRequests, slide, showControls = false, isPreview = false) + thumbnailView.setDownloadClickListener(this) } albumCellBodyParent.isVisible = message.body.isNotEmpty() albumCellBodyText.text = message.body + post { + // post to await layout of text + albumCellBodyText.layout?.let { layout -> + val maxEllipsis = (0 until layout.lineCount).maxByOrNull { lineNum -> layout.getEllipsisCount(lineNum) } ?: 0 + // show read more text if at least one line is ellipsized + albumCellBodyTextReadMore.isVisible = maxEllipsis > 0 + } + } } // endregion diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/VisibleMessageContentView.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/VisibleMessageContentView.kt index 6cd959c5ed..3ab7ad065f 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/VisibleMessageContentView.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/VisibleMessageContentView.kt @@ -4,6 +4,7 @@ import android.content.Context import android.graphics.drawable.Drawable import android.text.util.Linkify import android.util.AttributeSet +import android.util.Log import android.util.TypedValue import android.view.LayoutInflater import android.widget.LinearLayout @@ -88,7 +89,22 @@ class VisibleMessageContentView : LinearLayout { mainContainer.addView(albumThumbnailView) // isStart and isEnd of cluster needed for calculating the mask for full bubble image groups // bind after add view because views are inflated and calculated during bind - albumThumbnailView.bind(glide, message, isStartOfMessageCluster, isEndOfMessageCluster) + albumThumbnailView.bind( + glideRequests = glide, + message = message, + isStart = isStartOfMessageCluster, + isEnd = isEndOfMessageCluster, + clickListener = { slide -> + Log.d("Loki-UI","clicked to display the slide $slide") + }, + downloadClickListener = { slide -> + // trigger download of content? + Log.d("Loki-UI","clicked to download the slide $slide") + }, + readMoreListener = { + Log.d("Loki-UI", "clicked to read more the message $message") + } + ) } else if (message.isOpenGroupInvitation) { val openGroupInvitationView = OpenGroupInvitationView(context) openGroupInvitationView.bind(message, VisibleMessageContentView.getTextColor(context, message)) diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/utilities/KThumbnailView.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/utilities/KThumbnailView.kt index ceec1ae8f8..e0320c234e 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/utilities/KThumbnailView.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/utilities/KThumbnailView.kt @@ -5,6 +5,7 @@ import android.graphics.Bitmap import android.graphics.drawable.Drawable import android.net.Uri import android.util.AttributeSet +import android.view.View import android.widget.FrameLayout import android.widget.ProgressBar import androidx.core.view.isVisible @@ -26,7 +27,7 @@ import org.thoughtcrime.securesms.components.TransferControlView import org.thoughtcrime.securesms.mms.* import org.thoughtcrime.securesms.mms.DecryptableStreamUriLoader.DecryptableUri -open class KThumbnailView: FrameLayout { +open class KThumbnailView: FrameLayout, View.OnClickListener { companion object { private const val WIDTH = 0 @@ -65,9 +66,9 @@ open class KThumbnailView: FrameLayout { typedArray.recycle() } + setOnClickListener(this) } - // region Lifecycle override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { val adjustedDimens = dimensDelegate.resourceSize() if (adjustedDimens[WIDTH] == 0 && adjustedDimens[HEIGHT] == 0) { @@ -85,10 +86,16 @@ open class KThumbnailView: FrameLayout { private fun getDefaultWidth() = maxOf(layoutParams?.width ?: 0, 0) private fun getDefaultHeight() = maxOf(layoutParams?.height ?: 0, 0) - // endregion // region Interaction + + override fun onClick(v: View?) { + if (v === this) { + thumbnailClickListener?.onClick(v, slide) + } + } + fun setImageResource(glide: GlideRequests, slide: Slide, showControls: Boolean, isPreview: Boolean): ListenableFuture { return setImageResource(glide, slide, showControls, isPreview, 0, 0) } @@ -216,7 +223,11 @@ open class KThumbnailView: FrameLayout { } fun setDownloadClickListener(listener: SlidesClickedListener) { - + transferControls.setDownloadClickListener { + slide?.let { slide -> + listener.onClick(it, listOf(slide)) + } + } } // endregion diff --git a/app/src/main/res/layout/album_thumbnail_view.xml b/app/src/main/res/layout/album_thumbnail_view.xml index eb262f7e2d..5920486972 100644 --- a/app/src/main/res/layout/album_thumbnail_view.xml +++ b/app/src/main/res/layout/album_thumbnail_view.xml @@ -1,6 +1,7 @@ - @@ -17,7 +18,8 @@ android:layout_gravity="center" android:layout="@layout/transfer_controls_stub" /> - + android:layout_height="0dp" + app:layout_constraintTop_toTopOf="@+id/albumCellBodyTextParent" + app:layout_constraintBottom_toBottomOf="parent" + /> - + + \ No newline at end of file