diff --git a/data/src/main/java/com/moez/QKSMS/repository/ContactRepositoryImpl.kt b/data/src/main/java/com/moez/QKSMS/repository/ContactRepositoryImpl.kt index 97e86a54..4989fb21 100644 --- a/data/src/main/java/com/moez/QKSMS/repository/ContactRepositoryImpl.kt +++ b/data/src/main/java/com/moez/QKSMS/repository/ContactRepositoryImpl.kt @@ -113,8 +113,6 @@ class ContactRepositoryImpl @Inject constructor( .filter { it.isLoaded } .filter { it.isValid } .map { realm.copyFromRealm(it) } - .subscribeOn(AndroidSchedulers.mainThread()) - .observeOn(Schedulers.io()) .map { contacts -> if (mobileOnly) { contacts.map { contact -> @@ -140,6 +138,7 @@ class ContactRepositoryImpl @Inject constructor( } } } + .subscribeOn(Schedulers.io()) } override fun getUnmanagedContactGroups(): Observable> { diff --git a/data/src/main/java/com/moez/QKSMS/repository/ConversationRepositoryImpl.kt b/data/src/main/java/com/moez/QKSMS/repository/ConversationRepositoryImpl.kt index 11e3a6b2..5d2e0b22 100644 --- a/data/src/main/java/com/moez/QKSMS/repository/ConversationRepositoryImpl.kt +++ b/data/src/main/java/com/moez/QKSMS/repository/ConversationRepositoryImpl.kt @@ -338,7 +338,10 @@ class ConversationRepositoryImpl @Inject constructor( override fun createConversation(addresses: Collection, sendAsGroup: Boolean) = TelephonyCompat.getOrCreateThreadId(context, addresses.toSet()) .takeIf { it != 0L } - ?.let { providerThreadId -> createConversationFromCp(providerThreadId, sendAsGroup) } + ?.let { providerThreadId -> + createConversationFromCp(providerThreadId, sendAsGroup) ?: + createEmptyConversation(providerThreadId, addresses, sendAsGroup) + } override fun getOrCreateConversation(threadId: Long, sendAsGroup: Boolean) = getConversation(threadId) ?: createConversation(threadId, sendAsGroup) @@ -510,6 +513,7 @@ class ConversationRepositoryImpl @Inject constructor( phoneNumberUtils.compare(it.address, address) } } + ?.let { realm.copyFromRealm(it) } } } @@ -532,4 +536,38 @@ class ConversationRepositoryImpl @Inject constructor( } } + /** + * In some cases [createConversationFromCp] will return null if there are no messages present in the convo. + * In order to allow the conversation to be accessed + * we need to create an empty conversation in Realm to match the conversation created in the content provider. + * + * This is a bit of a hack, but is necessary on devices running HyperOS or variants. + */ + private fun createEmptyConversation(threadId: Long, addresses: Collection, sendAsGroup: Boolean): Conversation { + Realm.getDefaultInstance().use { realm -> + val realmContacts = realm.where(Contact::class.java).findAll() + val matchedRecipients = addresses.map { address -> + Recipient().apply { + this.address = address + contact = realmContacts.firstOrNull { realmContact -> + realmContact.numbers.any { + phoneNumberUtils.compare(it.address, address) + } + } + ?.let { realm.copyFromRealm(it) } + } + } + val conversation = Conversation().apply { + id = threadId + recipients.clear() + recipients.addAll(matchedRecipients) + this.sendAsGroup = + if (recipients.size <= 1) false + else sendAsGroup + } + realm.executeTransaction { it.copyToRealmOrUpdate(conversation) } + return conversation + } + } + } diff --git a/presentation/src/main/java/com/moez/QKSMS/feature/compose/ComposeViewModel.kt b/presentation/src/main/java/com/moez/QKSMS/feature/compose/ComposeViewModel.kt index 477c052d..d0f2349e 100644 --- a/presentation/src/main/java/com/moez/QKSMS/feature/compose/ComposeViewModel.kt +++ b/presentation/src/main/java/com/moez/QKSMS/feature/compose/ComposeViewModel.kt @@ -178,10 +178,15 @@ class ComposeViewModel @Inject constructor( // monitors convos and triggers when wanted convo is present conversationRepo.getConversations(false) .asObservable() - .filter { conversations -> conversations.isLoaded } + .filter { conversations -> conversations.isLoaded && conversations.isValid} .mapNotNull { conversationRepo.getConversation(addresses) } .doOnNext { newState { copy(loading = false) } } - } + } + .doOnError { e -> + Timber.e(e, "Error while resolving conversation") + newState { copy(loading = false) } + } + // Merges two potential conversation sources (constructor threadId and contact selection) // into a single stream of conversations. If the conversation was deleted, notify the