From 75d58677e4368221e5730c2d32cef0582c8da522 Mon Sep 17 00:00:00 2001 From: Niels Andriesse Date: Wed, 23 Jun 2021 14:48:29 +1000 Subject: [PATCH] Add scroll to bottom button plumming --- .../conversation/v2/ConversationActivityV2.kt | 19 +++++++++++---- .../v2/ConversationRecyclerView.kt | 23 +++++++++++++++++++ 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt index 27ba986214..a0591f8c5c 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt @@ -6,11 +6,13 @@ import android.content.res.Resources import android.database.Cursor import android.graphics.Rect import android.os.Bundle +import android.util.Log import android.view.* import android.widget.RelativeLayout import androidx.loader.app.LoaderManager import androidx.loader.content.Loader import androidx.recyclerview.widget.LinearLayoutManager +import androidx.recyclerview.widget.RecyclerView import kotlinx.android.synthetic.main.activity_conversation_v2.* import kotlinx.android.synthetic.main.activity_conversation_v2.view.* import kotlinx.android.synthetic.main.activity_conversation_v2_action_bar.* @@ -34,11 +36,12 @@ import org.thoughtcrime.securesms.database.DraftDatabase.Drafts import org.thoughtcrime.securesms.database.model.MessageRecord import org.thoughtcrime.securesms.loki.utilities.toPx import org.thoughtcrime.securesms.mms.GlideApp -import kotlin.math.abs -import kotlin.math.roundToInt -import kotlin.math.sqrt +import kotlin.math.* -class ConversationActivityV2 : PassphraseRequiredActionBarActivity(), InputBarDelegate, InputBarRecordingViewDelegate { +class ConversationActivityV2 : PassphraseRequiredActionBarActivity(), InputBarDelegate, + InputBarRecordingViewDelegate, ConversationRecyclerViewDelegate { + private val scrollButtonFullVisibilityThreshold by lazy { toPx(120.0f, resources) } + private val scrollButtonNoVisibilityThreshold by lazy { toPx(20.0f, resources) } private val screenWidth = Resources.getSystem().displayMetrics.widthPixels private var threadID: Long = -1 private var actionMode: ActionMode? = null @@ -102,6 +105,7 @@ class ConversationActivityV2 : PassphraseRequiredActionBarActivity(), InputBarDe conversationRecyclerView.adapter = adapter val layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, true) conversationRecyclerView.layoutManager = layoutManager + conversationRecyclerView.delegate = this // Workaround for the fact that CursorRecyclerViewAdapter doesn't auto-update automatically (even though it says it will) LoaderManager.getInstance(this).restartLoader(0, null, object : LoaderManager.LoaderCallbacks { @@ -304,6 +308,13 @@ class ConversationActivityV2 : PassphraseRequiredActionBarActivity(), InputBarDe } animation.start() } + + override fun handleConversationRecyclerViewBottomOffsetChanged(bottomOffset: Int) { + val rawAlpha = (bottomOffset.toFloat() - scrollButtonNoVisibilityThreshold) / + (scrollButtonFullVisibilityThreshold - scrollButtonNoVisibilityThreshold) + val alpha = max(min(rawAlpha, 1.0f), 0.0f) + Log.d("Test", "$alpha") + } // endregion // region Interaction diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationRecyclerView.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationRecyclerView.kt index ea10f0176e..246b84668f 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationRecyclerView.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationRecyclerView.kt @@ -2,17 +2,21 @@ package org.thoughtcrime.securesms.conversation.v2 import android.content.Context import android.util.AttributeSet +import android.util.Log import android.view.MotionEvent import android.view.VelocityTracker import androidx.recyclerview.widget.RecyclerView +import kotlinx.android.synthetic.main.activity_conversation_v2.* import org.thoughtcrime.securesms.loki.utilities.disableClipping import org.thoughtcrime.securesms.loki.utilities.toPx import kotlin.math.abs +import kotlin.math.max class ConversationRecyclerView : RecyclerView { private val maxLongPressVelocityY = toPx(10, resources) private val minSwipeVelocityX = toPx(10, resources) private var velocityTracker: VelocityTracker? = null + var delegate: ConversationRecyclerViewDelegate? = null constructor(context: Context) : super(context) { initialize() } constructor(context: Context, attrs: AttributeSet) : super(context, attrs) { initialize() } @@ -20,6 +24,20 @@ class ConversationRecyclerView : RecyclerView { private fun initialize() { disableClipping() + addOnScrollListener(object : RecyclerView.OnScrollListener() { + private var maxScrollOffset = 0 + + override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) { + // Do nothing + } + + override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) { + val scrollOffset = recyclerView.computeVerticalScrollOffset() + maxScrollOffset = max(maxScrollOffset, scrollOffset) + val bottomOffset = (maxScrollOffset - scrollOffset) + delegate?.handleConversationRecyclerViewBottomOffsetChanged(bottomOffset) + } + }) } override fun onInterceptTouchEvent(e: MotionEvent): Boolean { @@ -48,4 +66,9 @@ class ConversationRecyclerView : RecyclerView { velocityTracker?.addMovement(e) return super.dispatchTouchEvent(e) } +} + +interface ConversationRecyclerViewDelegate { + + fun handleConversationRecyclerViewBottomOffsetChanged(bottomOffset: Int) } \ No newline at end of file