diff --git a/data/src/main/java/mapper/CursorToContactImpl.kt b/data/src/main/java/mapper/CursorToContactImpl.kt index 92fb0d19..60d20a06 100644 --- a/data/src/main/java/mapper/CursorToContactImpl.kt +++ b/data/src/main/java/mapper/CursorToContactImpl.kt @@ -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) } diff --git a/data/src/main/java/mapper/CursorToConversationImpl.kt b/data/src/main/java/mapper/CursorToConversationImpl.kt index 59968714..8e40873b 100644 --- a/data/src/main/java/mapper/CursorToConversationImpl.kt +++ b/data/src/main/java/mapper/CursorToConversationImpl.kt @@ -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) } diff --git a/data/src/main/java/mapper/CursorToMessageImpl.kt b/data/src/main/java/mapper/CursorToMessageImpl.kt index 48193d74..aed6bd41 100644 --- a/data/src/main/java/mapper/CursorToMessageImpl.kt +++ b/data/src/main/java/mapper/CursorToMessageImpl.kt @@ -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. } diff --git a/data/src/main/java/mapper/CursorToRecipienImpl.kt b/data/src/main/java/mapper/CursorToRecipienImpl.kt index 95098368..15651e12 100644 --- a/data/src/main/java/mapper/CursorToRecipienImpl.kt +++ b/data/src/main/java/mapper/CursorToRecipienImpl.kt @@ -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) } diff --git a/data/src/main/java/repository/MessageRepositoryImpl.kt b/data/src/main/java/repository/MessageRepositoryImpl.kt index 1391e063..4e3b4197 100644 --- a/data/src/main/java/repository/MessageRepositoryImpl.kt +++ b/data/src/main/java/repository/MessageRepositoryImpl.kt @@ -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 + } } } \ No newline at end of file diff --git a/data/src/main/java/repository/SyncRepositoryImpl.kt b/data/src/main/java/repository/SyncRepositoryImpl.kt index 5558d64a..66734bcc 100644 --- a/data/src/main/java/repository/SyncRepositoryImpl.kt +++ b/data/src/main/java/repository/SyncRepositoryImpl.kt @@ -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() diff --git a/domain/src/main/java/mapper/CursorToContact.kt b/domain/src/main/java/mapper/CursorToContact.kt index 324d993b..17035758 100644 --- a/domain/src/main/java/mapper/CursorToContact.kt +++ b/domain/src/main/java/mapper/CursorToContact.kt @@ -23,6 +23,6 @@ import model.Contact interface CursorToContact : Mapper { - fun getContactsCursor(): Cursor + fun getContactsCursor(): Cursor? } \ No newline at end of file diff --git a/domain/src/main/java/mapper/CursorToConversation.kt b/domain/src/main/java/mapper/CursorToConversation.kt index 2509ddec..6a86b520 100644 --- a/domain/src/main/java/mapper/CursorToConversation.kt +++ b/domain/src/main/java/mapper/CursorToConversation.kt @@ -23,8 +23,8 @@ import model.Conversation interface CursorToConversation : Mapper { - fun getConversationsCursor(lastSync: Long = 0): Cursor + fun getConversationsCursor(lastSync: Long = 0): Cursor? - fun getConversationCursor(threadId: Long): Cursor + fun getConversationCursor(threadId: Long): Cursor? } \ No newline at end of file diff --git a/domain/src/main/java/mapper/CursorToMessage.kt b/domain/src/main/java/mapper/CursorToMessage.kt index 0de555b5..5b00bf5c 100644 --- a/domain/src/main/java/mapper/CursorToMessage.kt +++ b/domain/src/main/java/mapper/CursorToMessage.kt @@ -26,9 +26,9 @@ import java.util.* interface CursorToMessage : Mapper, Message> { - fun getMessagesCursor(): Cursor + fun getMessagesCursor(): Cursor? - fun getMessageCursor(id: Long): Cursor + fun getMessageCursor(id: Long): Cursor? class MessageColumns(private val cursor: Cursor) { diff --git a/domain/src/main/java/mapper/CursorToRecipient.kt b/domain/src/main/java/mapper/CursorToRecipient.kt index 88b97325..4587a6f5 100644 --- a/domain/src/main/java/mapper/CursorToRecipient.kt +++ b/domain/src/main/java/mapper/CursorToRecipient.kt @@ -23,8 +23,8 @@ import model.Recipient interface CursorToRecipient : Mapper { - fun getRecipientCursor(): Cursor + fun getRecipientCursor(): Cursor? - fun getRecipientCursor(id: Long): Cursor + fun getRecipientCursor(id: Long): Cursor? } \ No newline at end of file