diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/ThreadActivity.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/ThreadActivity.kt index 8dd7fc19..4bc5e631 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/ThreadActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/ThreadActivity.kt @@ -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() 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() diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/adapters/ThreadAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/adapters/ThreadAdapter.kt index 232d43c8..486a8eea 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/adapters/ThreadAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/adapters/ThreadAdapter.kt @@ -231,14 +231,14 @@ class ThreadAdapter( private fun isThreadDateTime(position: Int) = messages.getOrNull(position) is ThreadDateTime - fun updateMessages(newMessages: ArrayList) { + fun updateMessages(newMessages: ArrayList, scrollPosition: Int = messages.size - 1) { val latestMessages = newMessages.clone() as ArrayList val oldHashCode = messages.hashCode() val newHashCode = latestMessages.hashCode() if (newHashCode != oldHashCode) { messages = latestMessages notifyDataSetChanged() - recyclerView.scrollToPosition(messages.size - 1) + recyclerView.scrollToPosition(scrollPosition) } } diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/extensions/Context.kt index 67d0c5db..d3eca1d3 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/extensions/Context.kt @@ -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 { +fun Context.getMessages(threadId: Long, getImageResolutions: Boolean, index: Long = -1): ArrayList { 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() val blockedNumbers = getBlockedNumbers() diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/helpers/Constants.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/helpers/Constants.kt index 6e46f4c9..2e5a2646 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/helpers/Constants.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/helpers/Constants.kt @@ -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()) }