Merge pull request #562 from Naveen3Singh/scrolling_improvements

Scrolling improvements
This commit is contained in:
Tibor Kaputa 2023-01-16 16:26:07 +01:00 committed by GitHub
commit 9ac2b71172
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 57 additions and 15 deletions

View file

@ -4,6 +4,7 @@ import android.annotation.SuppressLint
import android.app.Activity import android.app.Activity
import android.content.ActivityNotFoundException import android.content.ActivityNotFoundException
import android.content.Intent import android.content.Intent
import android.content.res.ColorStateList
import android.graphics.BitmapFactory import android.graphics.BitmapFactory
import android.graphics.drawable.LayerDrawable import android.graphics.drawable.LayerDrawable
import android.media.MediaMetadataRetriever import android.media.MediaMetadataRetriever
@ -36,6 +37,8 @@ import androidx.annotation.StringRes
import androidx.constraintlayout.widget.ConstraintLayout import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.res.ResourcesCompat import androidx.core.content.res.ResourcesCompat
import androidx.core.view.* import androidx.core.view.*
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.gson.Gson import com.google.gson.Gson
import com.google.gson.reflect.TypeToken import com.google.gson.reflect.TypeToken
import com.simplemobiletools.commons.dialogs.ConfirmationDialog import com.simplemobiletools.commons.dialogs.ConfirmationDialog
@ -79,6 +82,8 @@ class ThreadActivity : SimpleActivity() {
private val TYPE_SEND = 15 private val TYPE_SEND = 15
private val TYPE_DELETE = 16 private val TYPE_DELETE = 16
private val SCROLL_TO_BOTTOM_FAB_LIMIT = 20
private var threadId = 0L private var threadId = 0L
private var currentSIMCardIndex = 0 private var currentSIMCardIndex = 0
private var isActivityVisible = false private var isActivityVisible = false
@ -142,6 +147,7 @@ class ThreadActivity : SimpleActivity() {
} }
setupThread() setupThread()
setupScrollFab()
} }
} else { } else {
finish() finish()
@ -173,12 +179,7 @@ class ThreadActivity : SimpleActivity() {
} }
} }
val bottomBarColor = if (baseConfig.isUsingSystemTheme) { val bottomBarColor = getBottomBarColor()
resources.getColor(R.color.you_bottom_bar_color)
} else {
getBottomNavigationBackgroundColor()
}
thread_send_message_holder.setBackgroundColor(bottomBarColor) thread_send_message_holder.setBackgroundColor(bottomBarColor)
reply_disabled_info_holder.setBackgroundColor(bottomBarColor) reply_disabled_info_holder.setBackgroundColor(bottomBarColor)
updateNavigationBarColor(bottomBarColor) updateNavigationBarColor(bottomBarColor)
@ -394,12 +395,11 @@ class ThreadActivity : SimpleActivity() {
runOnUiThread { runOnUiThread {
refreshMenuItems() refreshMenuItems()
getOrCreateThreadAdapter().apply { getOrCreateThreadAdapter().apply {
val scrollPosition = if (currentList.lastOrNull() != threadItems.lastOrNull()) { val layoutManager = thread_messages_list.layoutManager as LinearLayoutManager
threadItems.lastIndex val lastPosition = itemCount - 1
} else { val lastVisiblePosition = layoutManager.findLastVisibleItemPosition()
-1 val shouldScrollToBottom = currentList.lastOrNull() != threadItems.lastOrNull() && lastPosition - lastVisiblePosition == 1
} updateMessages(threadItems, if (shouldScrollToBottom) lastPosition else -1)
updateMessages(threadItems, scrollPosition)
} }
} }
@ -438,6 +438,22 @@ class ThreadActivity : SimpleActivity() {
} }
} }
private fun setupScrollFab() {
thread_messages_list.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
val layoutManager = thread_messages_list.layoutManager as LinearLayoutManager
val lastVisibleItemPosition = layoutManager.findLastCompletelyVisibleItemPosition()
val isCloseToBottom = lastVisibleItemPosition >= getOrCreateThreadAdapter().itemCount - SCROLL_TO_BOTTOM_FAB_LIMIT
if (isCloseToBottom) {
scroll_to_bottom_fab.hide()
} else {
scroll_to_bottom_fab.show()
}
}
})
}
private fun handleItemClick(any: Any) { private fun handleItemClick(any: Any) {
when { when {
any is Message && any.isScheduled -> showScheduledMessageInfo(any) any is Message && any.isScheduled -> showScheduledMessageInfo(any)
@ -633,6 +649,11 @@ class ThreadActivity : SimpleActivity() {
addAttachment(it) addAttachment(it)
} }
} }
scroll_to_bottom_fab.setOnClickListener {
scrollToBottom()
}
scroll_to_bottom_fab.backgroundTintList = ColorStateList.valueOf(getBottomBarColor())
scroll_to_bottom_fab.applyColorFilter(textColor)
setupScheduleSendUi() setupScheduleSendUi()
} }
@ -1235,7 +1256,7 @@ class ThreadActivity : SimpleActivity() {
val newItems = getThreadItems() val newItems = getThreadItems()
runOnUiThread { runOnUiThread {
getOrCreateThreadAdapter().updateMessages(newItems) getOrCreateThreadAdapter().updateMessages(newItems, newItems.lastIndex)
if (!refreshedSinceSent) { if (!refreshedSinceSent) {
refreshMessages() refreshMessages()
} }
@ -1661,4 +1682,10 @@ class ThreadActivity : SimpleActivity() {
showAttachmentPicker() showAttachmentPicker()
} }
} }
private fun getBottomBarColor() = if (baseConfig.isUsingSystemTheme) {
resources.getColor(R.color.you_bottom_bar_color)
} else {
getBottomNavigationBackgroundColor()
}
} }

View file

@ -227,7 +227,7 @@ class ThreadAdapter(
private fun isThreadDateTime(position: Int) = currentList.getOrNull(position) is ThreadDateTime private fun isThreadDateTime(position: Int) = currentList.getOrNull(position) is ThreadDateTime
fun updateMessages(newMessages: ArrayList<ThreadItem>, scrollPosition: Int = newMessages.lastIndex) { fun updateMessages(newMessages: ArrayList<ThreadItem>, scrollPosition: Int = -1) {
val latestMessages = newMessages.toMutableList() val latestMessages = newMessages.toMutableList()
submitList(latestMessages) { submitList(latestMessages) {
if (scrollPosition != -1) { if (scrollPosition != -1) {

View file

@ -70,7 +70,7 @@ const val FILE_SIZE_600_KB = 614_400L
const val FILE_SIZE_1_MB = 1_048_576L const val FILE_SIZE_1_MB = 1_048_576L
const val FILE_SIZE_2_MB = 2_097_152L const val FILE_SIZE_2_MB = 2_097_152L
const val MESSAGES_LIMIT = 50 const val MESSAGES_LIMIT = 75
// intent launch request codes // intent launch request codes
const val PICK_PHOTO_INTENT = 42 const val PICK_PHOTO_INTENT = 42

View file

@ -0,0 +1,3 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:width="24dp" android:height="24dp" android:viewportWidth="24" android:viewportHeight="24">
<path android:fillColor="@android:color/white" android:pathData="M11 5v11.17l-4.88-4.88c-0.39-0.39-1.03-0.39-1.42 0-0.39 0.39-0.39 1.02 0 1.41l6.59 6.59c0.39 0.39 1.02 0.39 1.41 0l6.59-6.59c0.39-0.39 0.39-1.02 0-1.41-0.39-0.39-1.02-0.39-1.41 0L13 16.17V5c0-0.55-0.45-1-1-1s-1 0.45-1 1z"/>
</vector>

View file

@ -121,6 +121,18 @@
</com.qtalk.recyclerviewfastscroller.RecyclerViewFastScroller> </com.qtalk.recyclerviewfastscroller.RecyclerViewFastScroller>
<com.simplemobiletools.commons.views.MyFloatingActionButton
android:id="@+id/scroll_to_bottom_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/activity_margin"
android:src="@drawable/ic_arrow_down_vector"
android:visibility="invisible"
app:fabSize="mini"
app:layout_constraintBottom_toBottomOf="@id/thread_messages_fastscroller"
app:layout_constraintEnd_toEndOf="@id/thread_messages_fastscroller"
tools:ignore="ContentDescription" />
<include <include
layout="@layout/layout_invalid_short_code_info" layout="@layout/layout_invalid_short_code_info"
android:layout_width="match_parent" android:layout_width="match_parent"