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()
|
lastUpdate = System.currentTimeMillis()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getContactsCursor(): Cursor {
|
override fun getContactsCursor(): Cursor? {
|
||||||
return context.contentResolver.query(URI, PROJECTION, null, null, null)
|
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,
|
return context.contentResolver.query(URI, PROJECTION,
|
||||||
"date > $lastSync", null,
|
"date > $lastSync", null,
|
||||||
"date desc")
|
"date desc")
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getConversationCursor(threadId: Long): Cursor {
|
override fun getConversationCursor(threadId: Long): Cursor? {
|
||||||
return context.contentResolver.query(URI, PROJECTION,
|
return context.contentResolver.query(URI, PROJECTION,
|
||||||
"_id = ?", arrayOf(threadId.toString()), null)
|
"_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")
|
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.
|
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()
|
lastUpdate = System.currentTimeMillis()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getRecipientCursor(): Cursor {
|
override fun getRecipientCursor(): Cursor? {
|
||||||
return context.contentResolver.query(URI, null, null, null, null)
|
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)
|
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.Realm
|
||||||
import io.realm.RealmResults
|
import io.realm.RealmResults
|
||||||
import io.realm.Sort
|
import io.realm.Sort
|
||||||
import manager.ExternalBlockingManager
|
|
||||||
import manager.KeyManager
|
import manager.KeyManager
|
||||||
import mapper.CursorToConversation
|
import mapper.CursorToConversation
|
||||||
import mapper.CursorToRecipient
|
import mapper.CursorToRecipient
|
||||||
@@ -598,38 +597,37 @@ class MessageRepositoryImpl @Inject constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun getConversationFromCp(threadId: Long): Conversation? {
|
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()) {
|
val recipients = conversation.recipients
|
||||||
conversation = cursorToConversation.map(cursor)
|
.map { recipient -> recipient.id }
|
||||||
|
.map { id -> cursorToRecipient.getRecipientCursor(id) }
|
||||||
val realm = Realm.getDefaultInstance()
|
.mapNotNull { recipientCursor ->
|
||||||
val contacts = realm.copyFromRealm(realm.where(Contact::class.java).findAll())
|
// Map the recipient cursor to a list of recipients
|
||||||
|
recipientCursor?.use { recipientCursor.map { cursorToRecipient.map(recipientCursor) } }
|
||||||
val recipients = conversation.recipients
|
}
|
||||||
.map { recipient -> recipient.id }
|
.flatten()
|
||||||
.map { id -> cursorToRecipient.getRecipientCursor(id) }
|
.map { recipient ->
|
||||||
.map { recipientCursor -> recipientCursor.map { cursorToRecipient.map(recipientCursor) } }
|
recipient.apply {
|
||||||
.flatten()
|
contact = contacts.firstOrNull {
|
||||||
.map { recipient ->
|
it.numbers.any { PhoneNumberUtils.compare(recipient.address, it.address) }
|
||||||
recipient.apply {
|
}
|
||||||
contact = contacts.firstOrNull {
|
}
|
||||||
it.numbers.any { PhoneNumberUtils.compare(recipient.address, it.address) }
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
conversation.recipients.clear()
|
conversation.recipients.clear()
|
||||||
conversation.recipients.addAll(recipients)
|
conversation.recipients.addAll(recipients)
|
||||||
conversation.insertOrUpdate()
|
conversation.insertOrUpdate()
|
||||||
realm.close()
|
realm.close()
|
||||||
}
|
|
||||||
|
|
||||||
cursor.close()
|
conversation
|
||||||
|
}
|
||||||
return conversation
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -100,35 +100,35 @@ class SyncRepositoryImpl @Inject constructor(
|
|||||||
|
|
||||||
|
|
||||||
// Sync messages
|
// Sync messages
|
||||||
val messageCursor = cursorToMessage.getMessagesCursor()
|
cursorToMessage.getMessagesCursor()?.use { messageCursor ->
|
||||||
val messageColumns = CursorToMessage.MessageColumns(messageCursor)
|
val messageColumns = CursorToMessage.MessageColumns(messageCursor)
|
||||||
val messages = messageCursor.mapWhile(
|
val messages = messageCursor.mapWhile(
|
||||||
{ cursor -> cursorToMessage.map(Pair(cursor, messageColumns)) },
|
{ cursor -> cursorToMessage.map(Pair(cursor, messageColumns)) },
|
||||||
{ message -> message.date > lastSync })
|
{ message -> message.date > lastSync })
|
||||||
realm.insertOrUpdate(messages)
|
realm.insertOrUpdate(messages)
|
||||||
messageCursor.close()
|
}
|
||||||
|
|
||||||
|
|
||||||
// Sync conversations
|
// Sync conversations
|
||||||
val conversationCursor = cursorToConversation.getConversationsCursor()
|
cursorToConversation.getConversationsCursor()?.use { conversationCursor ->
|
||||||
val conversations = conversationCursor
|
val conversations = conversationCursor
|
||||||
.map { cursor -> cursorToConversation.map(cursor) }
|
.map { cursor -> cursorToConversation.map(cursor) }
|
||||||
|
|
||||||
persistedData?.forEach { data ->
|
persistedData?.forEach { data ->
|
||||||
val conversation = conversations.firstOrNull { conversation -> conversation.id == data.id }
|
val conversation = conversations.firstOrNull { conversation -> conversation.id == data.id }
|
||||||
conversation?.archived = data.archived
|
conversation?.archived = data.archived
|
||||||
conversation?.blocked = data.blocked
|
conversation?.blocked = data.blocked
|
||||||
|
}
|
||||||
|
|
||||||
|
realm.insertOrUpdate(conversations)
|
||||||
}
|
}
|
||||||
|
|
||||||
realm.insertOrUpdate(conversations)
|
|
||||||
conversationCursor.close()
|
|
||||||
|
|
||||||
|
|
||||||
// Sync recipients
|
// Sync recipients
|
||||||
val recipientCursor = cursorToRecipient.getRecipientCursor()
|
cursorToRecipient.getRecipientCursor()?.use { recipientCursor ->
|
||||||
val recipients = recipientCursor.map { cursor -> cursorToRecipient.map(cursor) }
|
val recipients = recipientCursor.map { cursor -> cursorToRecipient.map(cursor) }
|
||||||
realm.insertOrUpdate(recipients)
|
realm.insertOrUpdate(recipients)
|
||||||
recipientCursor.close()
|
}
|
||||||
|
|
||||||
realm.commitTransaction()
|
realm.commitTransaction()
|
||||||
realm.close()
|
realm.close()
|
||||||
@@ -178,15 +178,15 @@ class SyncRepositoryImpl @Inject constructor(
|
|||||||
override fun syncContacts() {
|
override fun syncContacts() {
|
||||||
// Load all the contacts
|
// Load all the contacts
|
||||||
var contacts = cursorToContact.getContactsCursor()
|
var contacts = cursorToContact.getContactsCursor()
|
||||||
.map { cursor -> cursorToContact.map(cursor) }
|
?.map { cursor -> cursorToContact.map(cursor) }
|
||||||
.groupBy { contact -> contact.lookupKey }
|
?.groupBy { contact -> contact.lookupKey }
|
||||||
.map { contacts ->
|
?.map { contacts ->
|
||||||
val allNumbers = contacts.value.map { it.numbers }.flatten()
|
val allNumbers = contacts.value.map { it.numbers }.flatten()
|
||||||
contacts.value.first().apply {
|
contacts.value.first().apply {
|
||||||
numbers.clear()
|
numbers.clear()
|
||||||
numbers.addAll(allNumbers)
|
numbers.addAll(allNumbers)
|
||||||
}
|
}
|
||||||
}
|
} ?: listOf()
|
||||||
|
|
||||||
val realm = Realm.getDefaultInstance()
|
val realm = Realm.getDefaultInstance()
|
||||||
val recipients = realm.where(Recipient::class.java).findAll()
|
val recipients = realm.where(Recipient::class.java).findAll()
|
||||||
|
|||||||
@@ -23,6 +23,6 @@ import model.Contact
|
|||||||
|
|
||||||
interface CursorToContact : Mapper<Cursor, Contact> {
|
interface CursorToContact : Mapper<Cursor, Contact> {
|
||||||
|
|
||||||
fun getContactsCursor(): Cursor
|
fun getContactsCursor(): Cursor?
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -23,8 +23,8 @@ import model.Conversation
|
|||||||
|
|
||||||
interface CursorToConversation : Mapper<Cursor, Conversation> {
|
interface CursorToConversation : Mapper<Cursor, Conversation> {
|
||||||
|
|
||||||
fun getConversationsCursor(lastSync: Long = 0): Cursor
|
fun getConversationsCursor(lastSync: Long = 0): Cursor?
|
||||||
|
|
||||||
fun getConversationCursor(threadId: Long): Cursor
|
fun getConversationCursor(threadId: Long): Cursor?
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -26,9 +26,9 @@ import java.util.*
|
|||||||
|
|
||||||
interface CursorToMessage : Mapper<Pair<Cursor, CursorToMessage.MessageColumns>, Message> {
|
interface CursorToMessage : Mapper<Pair<Cursor, CursorToMessage.MessageColumns>, Message> {
|
||||||
|
|
||||||
fun getMessagesCursor(): Cursor
|
fun getMessagesCursor(): Cursor?
|
||||||
|
|
||||||
fun getMessageCursor(id: Long): Cursor
|
fun getMessageCursor(id: Long): Cursor?
|
||||||
|
|
||||||
class MessageColumns(private val cursor: Cursor) {
|
class MessageColumns(private val cursor: Cursor) {
|
||||||
|
|
||||||
|
|||||||
@@ -23,8 +23,8 @@ import model.Recipient
|
|||||||
|
|
||||||
interface CursorToRecipient : Mapper<Cursor, Recipient> {
|
interface CursorToRecipient : Mapper<Cursor, Recipient> {
|
||||||
|
|
||||||
fun getRecipientCursor(): Cursor
|
fun getRecipientCursor(): Cursor?
|
||||||
|
|
||||||
fun getRecipientCursor(id: Long): Cursor
|
fun getRecipientCursor(id: Long): Cursor?
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user