insert messages in a local db at fetching
This commit is contained in:
parent
633f7cf5c0
commit
e357b4a659
|
@ -105,6 +105,13 @@ fun Context.getMessages(threadId: Int): ArrayList<Message> {
|
||||||
messages = messages.filter { it.participants.isNotEmpty() }
|
messages = messages.filter { it.participants.isNotEmpty() }
|
||||||
.sortedWith(compareBy<Message> { it.date }.thenBy { it.id }).toMutableList() as ArrayList<Message>
|
.sortedWith(compareBy<Message> { it.date }.thenBy { it.id }).toMutableList() as ArrayList<Message>
|
||||||
|
|
||||||
|
// use a separate thread for saving messages in a db
|
||||||
|
Thread {
|
||||||
|
messages.chunked(30).forEach { currentMessages ->
|
||||||
|
messagesDB.insertMessages(*currentMessages.toTypedArray())
|
||||||
|
}
|
||||||
|
}.start()
|
||||||
|
|
||||||
return messages
|
return messages
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -11,7 +11,7 @@ class Converters {
|
||||||
private val gson = Gson()
|
private val gson = Gson()
|
||||||
private val attachmentType = object : TypeToken<List<Attachment>>() {}.type
|
private val attachmentType = object : TypeToken<List<Attachment>>() {}.type
|
||||||
private val simpleContactType = object : TypeToken<List<SimpleContact>>() {}.type
|
private val simpleContactType = object : TypeToken<List<SimpleContact>>() {}.type
|
||||||
private val messageAttachmentType = object : TypeToken<MessageAttachment>() {}.type
|
private val messageAttachmentType = object : TypeToken<MessageAttachment?>() {}.type
|
||||||
|
|
||||||
@TypeConverter
|
@TypeConverter
|
||||||
fun jsonToAttachmentList(value: String) = gson.fromJson<ArrayList<Attachment>>(value, attachmentType)
|
fun jsonToAttachmentList(value: String) = gson.fromJson<ArrayList<Attachment>>(value, attachmentType)
|
||||||
|
@ -29,5 +29,5 @@ class Converters {
|
||||||
fun jsonToMessageAttachment(value: String) = gson.fromJson<MessageAttachment>(value, messageAttachmentType)
|
fun jsonToMessageAttachment(value: String) = gson.fromJson<MessageAttachment>(value, messageAttachmentType)
|
||||||
|
|
||||||
@TypeConverter
|
@TypeConverter
|
||||||
fun messageAttachmentToJson(messageAttachment: MessageAttachment) = gson.toJson(messageAttachment)
|
fun messageAttachmentToJson(messageAttachment: MessageAttachment?) = gson.toJson(messageAttachment)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,22 @@
|
||||||
package com.simplemobiletools.smsmessenger.interfaces
|
package com.simplemobiletools.smsmessenger.interfaces
|
||||||
|
|
||||||
import androidx.room.Dao
|
import androidx.room.Dao
|
||||||
|
import androidx.room.Insert
|
||||||
|
import androidx.room.OnConflictStrategy
|
||||||
import androidx.room.Query
|
import androidx.room.Query
|
||||||
import com.simplemobiletools.smsmessenger.models.Message
|
import com.simplemobiletools.smsmessenger.models.Message
|
||||||
|
|
||||||
@Dao
|
@Dao
|
||||||
interface MessagesDao {
|
interface MessagesDao {
|
||||||
|
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||||
|
fun insertOrUpdate(message: Message)
|
||||||
|
|
||||||
|
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||||
|
fun insertMessages(vararg message: Message)
|
||||||
|
|
||||||
@Query("SELECT * FROM messages")
|
@Query("SELECT * FROM messages")
|
||||||
fun getAll(): List<Message>
|
fun getAll(): List<Message>
|
||||||
|
|
||||||
|
@Query("SELECT * FROM messages WHERE thread = :threadId")
|
||||||
|
fun getThreadMessages(threadId: Long): List<Message>
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue