From e0809e5edae3c232fb8e121e0909c03ee7ae0c65 Mon Sep 17 00:00:00 2001 From: nielsandriesse Date: Fri, 4 Jun 2021 14:21:08 +1000 Subject: [PATCH] Add swipe to reply icon --- .../v2/ConversationTouchHelperCallback.kt | 46 ++++++++++++++++--- 1 file changed, 40 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationTouchHelperCallback.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationTouchHelperCallback.kt index c1f6f8650c..83e865818e 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationTouchHelperCallback.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationTouchHelperCallback.kt @@ -2,14 +2,23 @@ package org.thoughtcrime.securesms.conversation.v2 import android.content.Context import android.graphics.Canvas +import android.graphics.Rect import android.view.HapticFeedbackConstants +import androidx.core.content.ContextCompat import androidx.recyclerview.widget.ItemTouchHelper import androidx.recyclerview.widget.RecyclerView +import kotlinx.android.synthetic.main.view_visible_message.view.* +import network.loki.messenger.R +import org.thoughtcrime.securesms.conversation.v2.messages.VisibleMessageView import org.thoughtcrime.securesms.loki.utilities.toDp +import org.thoughtcrime.securesms.loki.utilities.toPx import kotlin.math.abs +import kotlin.math.min +import kotlin.math.roundToInt class ConversationTouchHelperCallback(private val adapter: ConversationAdapter, private val context: Context, private val onSwipe: (Int) -> Unit) : ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT) { + private val background = ContextCompat.getDrawable(context, R.drawable.ic_baseline_reply_24)!! private var previousX: Float = 0.0f companion object { @@ -24,16 +33,41 @@ class ConversationTouchHelperCallback(private val adapter: ConversationAdapter, adapter.notifyItemChanged(viewHolder.adapterPosition) } - override fun onChildDraw(c: Canvas, recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, dX: Float, dY: Float, actionState: Int, isCurrentlyActive: Boolean) { - super.onChildDraw(c, recyclerView, viewHolder, dX / 4 , dY, actionState, isCurrentlyActive) - val x = abs(toDp(dX, context.resources)) + override fun onChildDraw(c: Canvas, recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, + dX: Float, dY: Float, actionState: Int, isCurrentlyActive: Boolean) { + val adjustedDistanceInPx = dX / 4 + super.onChildDraw(c, recyclerView, viewHolder, adjustedDistanceInPx, dY, actionState, isCurrentlyActive) + val absDistanceInDp = abs(toDp(dX, context.resources)) val threshold = ConversationTouchHelperCallback.swipeToReplyThreshold - if (x > threshold && previousX < threshold) { - val view = viewHolder.itemView + val view = viewHolder.itemView + if (view !is VisibleMessageView) { return } + // Draw the background + val messageContentView = view.messageContentView + if (dX < 0) { // Swipe to the left + val alpha = min(absDistanceInDp, threshold) / threshold + background.alpha = (alpha * 255.0f).roundToInt() + val spacing = context.resources.getDimension(R.dimen.medium_spacing).toInt() + val itemViewTop = viewHolder.itemView.top + val itemViewBottom = viewHolder.itemView.bottom + val height = itemViewBottom - itemViewTop + val iconSize = toPx(24, context.resources) + val offset = (height - iconSize) / 2 + background.bounds = Rect( + messageContentView.right + adjustedDistanceInPx.toInt() + spacing, + itemViewTop + offset, + messageContentView.right + adjustedDistanceInPx.toInt() + iconSize + spacing, + itemViewTop + offset + iconSize + ) + } else { + //background.setBounds(0, 0, 0, 0) + } + background.draw(c) + // Perform haptic feedback and invoke onSwipe callback if threshold has been reached + if (absDistanceInDp > threshold && previousX < threshold) { view.isHapticFeedbackEnabled = true view.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS) onSwipe(viewHolder.adapterPosition) } - previousX = x + previousX = absDistanceInDp } } \ No newline at end of file