#1351 - Fix new conversation not being created on Huawei devices

This commit is contained in:
moezbhatti
2019-07-27 14:04:22 -04:00
parent 04ea57ebb0
commit da28edc782
4 changed files with 57 additions and 43 deletions
@@ -18,6 +18,9 @@
*/
package com.moez.QKSMS.interactor
import android.content.Context
import com.moez.QKSMS.compat.TelephonyCompat
import com.moez.QKSMS.extensions.mapNotNull
import com.moez.QKSMS.model.Attachment
import com.moez.QKSMS.repository.ConversationRepository
import com.moez.QKSMS.repository.MessageRepository
@@ -25,6 +28,7 @@ import io.reactivex.Flowable
import javax.inject.Inject
class SendMessage @Inject constructor(
private val context: Context,
private val conversationRepo: ConversationRepository,
private val messageRepo: MessageRepository
) : Interactor<SendMessage.Params>() {
@@ -41,16 +45,20 @@ class SendMessage @Inject constructor(
override fun buildObservable(params: Params): Flowable<*> = Flowable.just(Unit)
.filter { params.addresses.isNotEmpty() }
.doOnNext {
messageRepo.sendMessage(params.subId, params.threadId, params.addresses, params.body,
params.attachments, params.delay)
// If a threadId isn't provided, try to obtain one
val threadId = when (params.threadId) {
0L -> TelephonyCompat.getOrCreateThreadId(context, params.addresses.toSet())
else -> params.threadId
}
messageRepo.sendMessage(params.subId, threadId, params.addresses, params.body, params.attachments,
params.delay)
}
.map {
// On some manufacturers, we can't obtain a threadId for a new conversation. In
// this case, find the threadId manually now that it contains a message
if (params.threadId == 0L) {
conversationRepo.getOrCreateConversation(params.addresses)?.id ?: 0
} else {
params.threadId
.mapNotNull {
// If the threadId wasn't provided, then it's probably because it doesn't exist in Realm.
// Sync it now and get the id
when (params.threadId) {
0L -> conversationRepo.getOrCreateConversation(params.addresses)?.id
else -> params.threadId
}
}
.doOnNext { threadId -> conversationRepo.updateConversations(threadId) }
@@ -23,6 +23,6 @@ import com.moez.QKSMS.model.Conversation
interface CursorToConversation : Mapper<Cursor, Conversation> {
fun getConversationsCursor(lastSync: Long = 0): Cursor?
fun getConversationsCursor(): Cursor?
}