allow displaying older messages

This commit is contained in:
Pavel Poley 2022-06-02 13:01:03 +03:00
parent e9c32e7487
commit c8dbfb4090
3 changed files with 10 additions and 10 deletions

View File

@ -79,7 +79,7 @@ class ThreadActivity : SimpleActivity() {
private var lastAttachmentUri: String? = null private var lastAttachmentUri: String? = null
private var loadingOlderMessages = false private var loadingOlderMessages = false
private var allMessagesFetched = false private var allMessagesFetched = false
private var nextMessageId = -1L private var oldestMessageDate = -1
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
@ -367,19 +367,19 @@ class ThreadActivity : SimpleActivity() {
private fun fetchNextMessages() { private fun fetchNextMessages() {
if (messages.isEmpty() || allMessagesFetched || loadingOlderMessages) return if (messages.isEmpty() || allMessagesFetched || loadingOlderMessages) return
toast("fetchNextMessages") //toast("fetchNextMessages")
val messageId = messages.first().date.toLong()*1000 /*- 1*/ val date = messages.first().date
if (nextMessageId == messageId /*|| messageId < 1*/) { if (oldestMessageDate == date) {
allMessagesFetched = true allMessagesFetched = true
return return
} }
nextMessageId = messageId oldestMessageDate = date
loadingOlderMessages = true loadingOlderMessages = true
ensureBackgroundThread { ensureBackgroundThread {
val olderMessages = getMessages(threadId, true, nextMessageId) val olderMessages = getMessages(threadId, true, oldestMessageDate)
messages.addAll(0, olderMessages) messages.addAll(0, olderMessages)
allMessagesFetched = olderMessages.size < MESSAGES_LIMIT || olderMessages.size == 0 allMessagesFetched = olderMessages.size < MESSAGES_LIMIT || olderMessages.size == 0

View File

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

View File

@ -54,7 +54,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 = 10 const val MESSAGES_LIMIT = 15
fun refreshMessages() { fun refreshMessages() {
EventBus.getDefault().post(Events.RefreshMessages()) EventBus.getDefault().post(Events.RefreshMessages())