allow displaying older messages

This commit is contained in:
Pavel Poley 2022-06-01 17:45:09 +03:00
parent 21dd529846
commit e9c32e7487
4 changed files with 53 additions and 5 deletions

View File

@ -40,6 +40,8 @@ import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.*
import com.simplemobiletools.commons.models.PhoneNumber
import com.simplemobiletools.commons.models.SimpleContact
import com.simplemobiletools.commons.views.MyLinearLayoutManager
import com.simplemobiletools.commons.views.MyRecyclerView
import com.simplemobiletools.smsmessenger.R
import com.simplemobiletools.smsmessenger.adapters.AutoCompleteTextViewAdapter
import com.simplemobiletools.smsmessenger.adapters.ThreadAdapter
@ -75,6 +77,9 @@ class ThreadActivity : SimpleActivity() {
private var attachmentSelections = mutableMapOf<String, AttachmentSelection>()
private val imageCompressor by lazy { ImageCompressor(this) }
private var lastAttachmentUri: String? = null
private var loadingOlderMessages = false
private var allMessagesFetched = false
private var nextMessageId = -1L
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@ -320,6 +325,14 @@ class ThreadActivity : SimpleActivity() {
}.apply {
thread_messages_list.adapter = this
}
thread_messages_list.endlessScrollListener = object : MyRecyclerView.EndlessScrollListener {
override fun updateBottom() {}
override fun updateTop() {
fetchNextMessages()
}
}
} else {
(currAdapter as ThreadAdapter).updateMessages(threadItems)
}
@ -351,6 +364,38 @@ class ThreadActivity : SimpleActivity() {
}
}
private fun fetchNextMessages() {
if (messages.isEmpty() || allMessagesFetched || loadingOlderMessages) return
toast("fetchNextMessages")
val messageId = messages.first().date.toLong()*1000 /*- 1*/
if (nextMessageId == messageId /*|| messageId < 1*/) {
allMessagesFetched = true
return
}
nextMessageId = messageId
loadingOlderMessages = true
ensureBackgroundThread {
val olderMessages = getMessages(threadId, true, nextMessageId)
messages.addAll(0, olderMessages)
allMessagesFetched = olderMessages.size < MESSAGES_LIMIT || olderMessages.size == 0
val lastPosition = (thread_messages_list.layoutManager as MyLinearLayoutManager).findLastVisibleItemPosition()
val adapter = thread_messages_list.adapter as ThreadAdapter
val topItemAtRefresh = adapter.messages[lastPosition]
runOnUiThread {
loadingOlderMessages = false
val itemAtRefreshIndex = messages.indexOfFirst { it == topItemAtRefresh }
adapter.updateMessages(getThreadItems(), itemAtRefreshIndex)
}
}
}
private fun setupButtons() {
updateTextColors(thread_holder)
val textColor = getProperTextColor()

View File

@ -231,14 +231,14 @@ class ThreadAdapter(
private fun isThreadDateTime(position: Int) = messages.getOrNull(position) is ThreadDateTime
fun updateMessages(newMessages: ArrayList<ThreadItem>) {
fun updateMessages(newMessages: ArrayList<ThreadItem>, scrollPosition: Int = messages.size - 1) {
val latestMessages = newMessages.clone() as ArrayList<ThreadItem>
val oldHashCode = messages.hashCode()
val newHashCode = latestMessages.hashCode()
if (newHashCode != oldHashCode) {
messages = latestMessages
notifyDataSetChanged()
recyclerView.scrollToPosition(messages.size - 1)
recyclerView.scrollToPosition(scrollPosition)
}
}

View File

@ -56,7 +56,7 @@ val Context.messageAttachmentsDB: MessageAttachmentsDao get() = getMessagessDB()
val Context.messagesDB: MessagesDao get() = getMessagessDB().MessagesDao()
fun Context.getMessages(threadId: Long, getImageResolutions: Boolean): ArrayList<Message> {
fun Context.getMessages(threadId: Long, getImageResolutions: Boolean, index: Long = -1): ArrayList<Message> {
val uri = Sms.CONTENT_URI
val projection = arrayOf(
Sms._ID,
@ -70,9 +70,10 @@ fun Context.getMessages(threadId: Long, getImageResolutions: Boolean): ArrayList
Sms.STATUS
)
val selection = "${Sms.THREAD_ID} = ?"
val rangeQuery = if (index == -1L) "" else "AND ${Sms.DATE} <= $index"
val selection = "${Sms.THREAD_ID} = ? $rangeQuery"
val selectionArgs = arrayOf(threadId.toString())
val sortOrder = "${Sms._ID} DESC LIMIT 100"
val sortOrder = "${Sms._ID} DESC LIMIT $MESSAGES_LIMIT"
val blockStatus = HashMap<String, Boolean>()
val blockedNumbers = getBlockedNumbers()

View File

@ -54,6 +54,8 @@ const val FILE_SIZE_600_KB = 614_400L
const val FILE_SIZE_1_MB = 1_048_576L
const val FILE_SIZE_2_MB = 2_097_152L
const val MESSAGES_LIMIT = 10
fun refreshMessages() {
EventBus.getDefault().post(Events.RefreshMessages())
}