forked from upstream-mirrors/quik
Return nullable cursors when mapping from native contentprovider
This commit is contained in:
@@ -47,7 +47,7 @@ class CursorToContactImpl @Inject constructor(private val context: Context) : Cu
|
||||
lastUpdate = System.currentTimeMillis()
|
||||
}
|
||||
|
||||
override fun getContactsCursor(): Cursor {
|
||||
override fun getContactsCursor(): Cursor? {
|
||||
return context.contentResolver.query(URI, PROJECTION, null, null, null)
|
||||
}
|
||||
|
||||
|
||||
@@ -61,13 +61,13 @@ class CursorToConversationImpl @Inject constructor(private val context: Context)
|
||||
}
|
||||
}
|
||||
|
||||
override fun getConversationsCursor(lastSync: Long): Cursor {
|
||||
override fun getConversationsCursor(lastSync: Long): Cursor? {
|
||||
return context.contentResolver.query(URI, PROJECTION,
|
||||
"date > $lastSync", null,
|
||||
"date desc")
|
||||
}
|
||||
|
||||
override fun getConversationCursor(threadId: Long): Cursor {
|
||||
override fun getConversationCursor(threadId: Long): Cursor? {
|
||||
return context.contentResolver.query(URI, PROJECTION,
|
||||
"_id = ?", arrayOf(threadId.toString()), null)
|
||||
}
|
||||
|
||||
@@ -119,11 +119,11 @@ class CursorToMessageImpl @Inject constructor(
|
||||
}
|
||||
}
|
||||
|
||||
override fun getMessagesCursor(): Cursor {
|
||||
override fun getMessagesCursor(): Cursor? {
|
||||
return context.contentResolver.query(URI, PROJECTION, null, null, "normalized_date desc")
|
||||
}
|
||||
|
||||
override fun getMessageCursor(id: Long): Cursor {
|
||||
override fun getMessageCursor(id: Long): Cursor? {
|
||||
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
|
||||
}
|
||||
|
||||
|
||||
@@ -39,11 +39,11 @@ class CursorToRecipienImpl @Inject constructor(private val context: Context) : C
|
||||
lastUpdate = System.currentTimeMillis()
|
||||
}
|
||||
|
||||
override fun getRecipientCursor(): Cursor {
|
||||
override fun getRecipientCursor(): Cursor? {
|
||||
return context.contentResolver.query(URI, null, null, null, null)
|
||||
}
|
||||
|
||||
override fun getRecipientCursor(id: Long): Cursor {
|
||||
override fun getRecipientCursor(id: Long): Cursor? {
|
||||
return context.contentResolver.query(URI, null, "_id = ?", arrayOf(id.toString()), null)
|
||||
}
|
||||
|
||||
|
||||
@@ -37,7 +37,6 @@ import io.reactivex.schedulers.Schedulers
|
||||
import io.realm.Realm
|
||||
import io.realm.RealmResults
|
||||
import io.realm.Sort
|
||||
import manager.ExternalBlockingManager
|
||||
import manager.KeyManager
|
||||
import mapper.CursorToConversation
|
||||
import mapper.CursorToRecipient
|
||||
@@ -598,38 +597,37 @@ class MessageRepositoryImpl @Inject constructor(
|
||||
}
|
||||
|
||||
private fun getConversationFromCp(threadId: Long): Conversation? {
|
||||
var conversation: Conversation? = null
|
||||
return cursorToConversation.getConversationCursor(threadId)
|
||||
?.takeIf { cursor -> cursor.moveToFirst() }
|
||||
?.use { cursor ->
|
||||
val conversation = cursorToConversation.map(cursor)
|
||||
|
||||
val cursor = cursorToConversation.getConversationCursor(threadId)
|
||||
val realm = Realm.getDefaultInstance()
|
||||
val contacts = realm.copyFromRealm(realm.where(Contact::class.java).findAll())
|
||||
|
||||
if (cursor.moveToFirst()) {
|
||||
conversation = cursorToConversation.map(cursor)
|
||||
|
||||
val realm = Realm.getDefaultInstance()
|
||||
val contacts = realm.copyFromRealm(realm.where(Contact::class.java).findAll())
|
||||
|
||||
val recipients = conversation.recipients
|
||||
.map { recipient -> recipient.id }
|
||||
.map { id -> cursorToRecipient.getRecipientCursor(id) }
|
||||
.map { recipientCursor -> recipientCursor.map { cursorToRecipient.map(recipientCursor) } }
|
||||
.flatten()
|
||||
.map { recipient ->
|
||||
recipient.apply {
|
||||
contact = contacts.firstOrNull {
|
||||
it.numbers.any { PhoneNumberUtils.compare(recipient.address, it.address) }
|
||||
val recipients = conversation.recipients
|
||||
.map { recipient -> recipient.id }
|
||||
.map { id -> cursorToRecipient.getRecipientCursor(id) }
|
||||
.mapNotNull { recipientCursor ->
|
||||
// Map the recipient cursor to a list of recipients
|
||||
recipientCursor?.use { recipientCursor.map { cursorToRecipient.map(recipientCursor) } }
|
||||
}
|
||||
.flatten()
|
||||
.map { recipient ->
|
||||
recipient.apply {
|
||||
contact = contacts.firstOrNull {
|
||||
it.numbers.any { PhoneNumberUtils.compare(recipient.address, it.address) }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
conversation.recipients.clear()
|
||||
conversation.recipients.addAll(recipients)
|
||||
conversation.insertOrUpdate()
|
||||
realm.close()
|
||||
}
|
||||
conversation.recipients.clear()
|
||||
conversation.recipients.addAll(recipients)
|
||||
conversation.insertOrUpdate()
|
||||
realm.close()
|
||||
|
||||
cursor.close()
|
||||
|
||||
return conversation
|
||||
conversation
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -100,35 +100,35 @@ class SyncRepositoryImpl @Inject constructor(
|
||||
|
||||
|
||||
// Sync messages
|
||||
val messageCursor = cursorToMessage.getMessagesCursor()
|
||||
val messageColumns = CursorToMessage.MessageColumns(messageCursor)
|
||||
val messages = messageCursor.mapWhile(
|
||||
{ cursor -> cursorToMessage.map(Pair(cursor, messageColumns)) },
|
||||
{ message -> message.date > lastSync })
|
||||
realm.insertOrUpdate(messages)
|
||||
messageCursor.close()
|
||||
cursorToMessage.getMessagesCursor()?.use { messageCursor ->
|
||||
val messageColumns = CursorToMessage.MessageColumns(messageCursor)
|
||||
val messages = messageCursor.mapWhile(
|
||||
{ cursor -> cursorToMessage.map(Pair(cursor, messageColumns)) },
|
||||
{ message -> message.date > lastSync })
|
||||
realm.insertOrUpdate(messages)
|
||||
}
|
||||
|
||||
|
||||
// Sync conversations
|
||||
val conversationCursor = cursorToConversation.getConversationsCursor()
|
||||
val conversations = conversationCursor
|
||||
.map { cursor -> cursorToConversation.map(cursor) }
|
||||
cursorToConversation.getConversationsCursor()?.use { conversationCursor ->
|
||||
val conversations = conversationCursor
|
||||
.map { cursor -> cursorToConversation.map(cursor) }
|
||||
|
||||
persistedData?.forEach { data ->
|
||||
val conversation = conversations.firstOrNull { conversation -> conversation.id == data.id }
|
||||
conversation?.archived = data.archived
|
||||
conversation?.blocked = data.blocked
|
||||
persistedData?.forEach { data ->
|
||||
val conversation = conversations.firstOrNull { conversation -> conversation.id == data.id }
|
||||
conversation?.archived = data.archived
|
||||
conversation?.blocked = data.blocked
|
||||
}
|
||||
|
||||
realm.insertOrUpdate(conversations)
|
||||
}
|
||||
|
||||
realm.insertOrUpdate(conversations)
|
||||
conversationCursor.close()
|
||||
|
||||
|
||||
// Sync recipients
|
||||
val recipientCursor = cursorToRecipient.getRecipientCursor()
|
||||
val recipients = recipientCursor.map { cursor -> cursorToRecipient.map(cursor) }
|
||||
realm.insertOrUpdate(recipients)
|
||||
recipientCursor.close()
|
||||
cursorToRecipient.getRecipientCursor()?.use { recipientCursor ->
|
||||
val recipients = recipientCursor.map { cursor -> cursorToRecipient.map(cursor) }
|
||||
realm.insertOrUpdate(recipients)
|
||||
}
|
||||
|
||||
realm.commitTransaction()
|
||||
realm.close()
|
||||
@@ -178,15 +178,15 @@ class SyncRepositoryImpl @Inject constructor(
|
||||
override fun syncContacts() {
|
||||
// Load all the contacts
|
||||
var contacts = cursorToContact.getContactsCursor()
|
||||
.map { cursor -> cursorToContact.map(cursor) }
|
||||
.groupBy { contact -> contact.lookupKey }
|
||||
.map { contacts ->
|
||||
?.map { cursor -> cursorToContact.map(cursor) }
|
||||
?.groupBy { contact -> contact.lookupKey }
|
||||
?.map { contacts ->
|
||||
val allNumbers = contacts.value.map { it.numbers }.flatten()
|
||||
contacts.value.first().apply {
|
||||
numbers.clear()
|
||||
numbers.addAll(allNumbers)
|
||||
}
|
||||
}
|
||||
} ?: listOf()
|
||||
|
||||
val realm = Realm.getDefaultInstance()
|
||||
val recipients = realm.where(Recipient::class.java).findAll()
|
||||
|
||||
Reference in New Issue
Block a user