forked from upstream-mirrors/quik
#1079 - Fix composing new conversations not working on Huawei devices
This commit is contained in:
@@ -62,7 +62,8 @@ class MessageRepositoryImpl @Inject constructor(
|
|||||||
private val context: Context,
|
private val context: Context,
|
||||||
private val messageIds: KeyManager,
|
private val messageIds: KeyManager,
|
||||||
private val imageRepository: ImageRepository,
|
private val imageRepository: ImageRepository,
|
||||||
private val prefs: Preferences) : MessageRepository {
|
private val prefs: Preferences,
|
||||||
|
private val syncRepository: SyncRepository) : MessageRepository {
|
||||||
|
|
||||||
override fun getMessages(threadId: Long, query: String): RealmResults<Message> {
|
override fun getMessages(threadId: Long, query: String): RealmResults<Message> {
|
||||||
return Realm.getDefaultInstance()
|
return Realm.getDefaultInstance()
|
||||||
@@ -336,6 +337,13 @@ class MessageRepositoryImpl @Inject constructor(
|
|||||||
realm.executeTransaction { managedMessage?.takeIf { it.isValid }?.contentId = uri.lastPathSegment.toLong() }
|
realm.executeTransaction { managedMessage?.takeIf { it.isValid }?.contentId = uri.lastPathSegment.toLong() }
|
||||||
realm.close()
|
realm.close()
|
||||||
|
|
||||||
|
// On some devices, we can't obtain a threadId until after the first message is sent in a
|
||||||
|
// conversation. In this case, we need to update the message's threadId after it gets added
|
||||||
|
// to the native ContentProvider
|
||||||
|
if (threadId == 0L) {
|
||||||
|
uri?.let(syncRepository::syncMessage)
|
||||||
|
}
|
||||||
|
|
||||||
return message
|
return message
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -21,12 +21,14 @@ package com.moez.QKSMS.interactor
|
|||||||
import com.moez.QKSMS.model.Attachment
|
import com.moez.QKSMS.model.Attachment
|
||||||
import com.moez.QKSMS.repository.ConversationRepository
|
import com.moez.QKSMS.repository.ConversationRepository
|
||||||
import com.moez.QKSMS.repository.MessageRepository
|
import com.moez.QKSMS.repository.MessageRepository
|
||||||
|
import com.moez.QKSMS.repository.SyncRepository
|
||||||
import io.reactivex.Flowable
|
import io.reactivex.Flowable
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
class SendMessage @Inject constructor(
|
class SendMessage @Inject constructor(
|
||||||
private val conversationRepo: ConversationRepository,
|
private val conversationRepo: ConversationRepository,
|
||||||
private val messageRepo: MessageRepository
|
private val messageRepo: MessageRepository,
|
||||||
|
private val syncRepo: SyncRepository
|
||||||
) : Interactor<SendMessage.Params>() {
|
) : Interactor<SendMessage.Params>() {
|
||||||
|
|
||||||
data class Params(
|
data class Params(
|
||||||
@@ -37,12 +39,19 @@ class SendMessage @Inject constructor(
|
|||||||
val attachments: List<Attachment> = listOf(),
|
val attachments: List<Attachment> = listOf(),
|
||||||
val delay: Int = 0)
|
val delay: Int = 0)
|
||||||
|
|
||||||
override fun buildObservable(params: Params): Flowable<Unit> = Flowable.just(Unit)
|
override fun buildObservable(params: Params): Flowable<*> = Flowable.just(Unit)
|
||||||
.filter { params.addresses.isNotEmpty() }
|
.filter { params.addresses.isNotEmpty() }
|
||||||
.doOnNext { messageRepo.sendMessage(params.subId, params.threadId, params.addresses, params.body, params.attachments, params.delay) }
|
.doOnNext { messageRepo.sendMessage(params.subId, params.threadId, params.addresses, params.body, params.attachments, params.delay) }
|
||||||
// If this was the first message sent in the conversation, the conversation might not exist yet
|
.map {
|
||||||
.doOnNext { conversationRepo.getOrCreateConversation(params.threadId) }
|
// On some manufacturers, we can't obtain a threadId for a new conversation. In
|
||||||
.doOnNext { conversationRepo.updateConversations(params.threadId) }
|
// this case, find the threadId manually now that it contains a message
|
||||||
.doOnNext { conversationRepo.markUnarchived(params.threadId) }
|
if (params.threadId == 0L) {
|
||||||
|
conversationRepo.getOrCreateConversation(params.addresses)?.id ?: 0
|
||||||
|
} else {
|
||||||
|
params.threadId
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.doOnNext { threadId -> conversationRepo.updateConversations(threadId) }
|
||||||
|
.doOnNext { threadId -> conversationRepo.markUnarchived(threadId) }
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -107,7 +107,6 @@ class ComposeViewModel @Inject constructor(
|
|||||||
private val contacts: Observable<List<Contact>> by lazy { contactsRepo.getUnmanagedContacts().toObservable() }
|
private val contacts: Observable<List<Contact>> by lazy { contactsRepo.getUnmanagedContacts().toObservable() }
|
||||||
private val contactsReducer: Subject<(List<Contact>) -> List<Contact>> = PublishSubject.create()
|
private val contactsReducer: Subject<(List<Contact>) -> List<Contact>> = PublishSubject.create()
|
||||||
private val selectedContacts: Subject<List<Contact>> = BehaviorSubject.createDefault(listOf())
|
private val selectedContacts: Subject<List<Contact>> = BehaviorSubject.createDefault(listOf())
|
||||||
private val threadIdSubject: Subject<Long> = BehaviorSubject.create()
|
|
||||||
private val searchResults: Subject<List<Message>> = BehaviorSubject.create()
|
private val searchResults: Subject<List<Message>> = BehaviorSubject.create()
|
||||||
private val searchSelection: Subject<Long> = BehaviorSubject.createDefault(-1)
|
private val searchSelection: Subject<Long> = BehaviorSubject.createDefault(-1)
|
||||||
private val conversation: Subject<Conversation> = BehaviorSubject.create()
|
private val conversation: Subject<Conversation> = BehaviorSubject.create()
|
||||||
@@ -119,28 +118,40 @@ class ComposeViewModel @Inject constructor(
|
|||||||
?.asObservable()
|
?.asObservable()
|
||||||
?: Observable.empty()
|
?: Observable.empty()
|
||||||
|
|
||||||
// Merges two potential conversation sources (threadId from constructor and contact selection) into a single
|
val selectedConversation = selectedContacts
|
||||||
// stream of conversations. If the conversation was deleted, notify the activity to shut down
|
|
||||||
disposables += selectedContacts
|
|
||||||
.skipWhile { it.isEmpty() }
|
.skipWhile { it.isEmpty() }
|
||||||
.map { contacts -> contacts.map { it.numbers.firstOrNull()?.address ?: "" } }
|
.map { contacts -> contacts.map { it.numbers.firstOrNull()?.address ?: "" } }
|
||||||
|
.distinctUntilChanged()
|
||||||
.doOnNext { newState { copy(loading = true) } }
|
.doOnNext { newState { copy(loading = true) } }
|
||||||
.observeOn(Schedulers.computation())
|
.observeOn(Schedulers.io())
|
||||||
.map { addresses -> conversationRepo.getThreadId(addresses) ?: 0 }
|
.map { addresses -> Pair(conversationRepo.getOrCreateConversation(addresses)?.id ?: 0, addresses) }
|
||||||
.observeOn(AndroidSchedulers.mainThread())
|
.observeOn(AndroidSchedulers.mainThread())
|
||||||
.doOnNext { newState { copy(loading = false) } }
|
.doOnNext { newState { copy(loading = false) } }
|
||||||
.mergeWith(threadIdSubject)
|
.switchMap { (threadId, addresses) ->
|
||||||
.switchMap { threadId ->
|
|
||||||
// Query the entire list of conversations and map from there, rather than
|
// If we already have this thread in realm, or were able to obtain it from the
|
||||||
// directly querying the conversation from realm. If querying a single
|
// system, just return that.
|
||||||
// conversation and it doesn't exist yet, the realm query will never update
|
threadId.takeIf { it > 0 }?.let {
|
||||||
|
return@switchMap conversationRepo.getConversationAsync(threadId).asObservable()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Otherwise, we'll monitor the conversations until our expected conversation is created
|
||||||
conversationRepo.getConversations().asObservable()
|
conversationRepo.getConversations().asObservable()
|
||||||
.map { conversations ->
|
.filter { it.isLoaded }
|
||||||
conversations.firstOrNull { it.id == threadId }
|
.observeOn(Schedulers.io())
|
||||||
?: conversationRepo.getOrCreateConversation(threadId)
|
.map { conversationRepo.getOrCreateConversation(addresses)?.id ?: 0 }
|
||||||
?: Conversation(id = threadId)
|
.observeOn(AndroidSchedulers.mainThread())
|
||||||
|
.switchMap { actualThreadId ->
|
||||||
|
when (actualThreadId) {
|
||||||
|
0L -> Observable.just(Conversation(0))
|
||||||
|
else -> conversationRepo.getConversationAsync(actualThreadId).asObservable()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Merges two potential conversation sources (threadId from constructor and contact selection) into a single
|
||||||
|
// stream of conversations. If the conversation was deleted, notify the activity to shut down
|
||||||
|
disposables += selectedConversation
|
||||||
.mergeWith(initialConversation)
|
.mergeWith(initialConversation)
|
||||||
.filter { conversation -> conversation.isLoaded }
|
.filter { conversation -> conversation.isLoaded }
|
||||||
.doOnNext { conversation ->
|
.doOnNext { conversation ->
|
||||||
@@ -601,6 +612,7 @@ class ComposeViewModel @Inject constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
when {
|
when {
|
||||||
|
// Scheduling a message
|
||||||
state.scheduled != 0L -> {
|
state.scheduled != 0L -> {
|
||||||
newState { copy(scheduled = 0) }
|
newState { copy(scheduled = 0) }
|
||||||
val uris = attachments.map { it.getUri() }.map { it.toString() }
|
val uris = attachments.map { it.getUri() }.map { it.toString() }
|
||||||
@@ -609,17 +621,27 @@ class ComposeViewModel @Inject constructor(
|
|||||||
context.makeToast(R.string.compose_scheduled_toast)
|
context.makeToast(R.string.compose_scheduled_toast)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Sending a group message
|
||||||
state.sendAsGroup -> {
|
state.sendAsGroup -> {
|
||||||
val threadId = conversation.id.takeIf { it > 0 }
|
sendMessage.execute(SendMessage.Params(subId, conversation.id, addresses, body, attachments, delay))
|
||||||
?: TelephonyCompat.getOrCreateThreadId(context, addresses).also(threadIdSubject::onNext)
|
}
|
||||||
|
|
||||||
|
// Sending a message to an existing conversation with one recipient
|
||||||
|
conversation.recipients.size == 1 -> {
|
||||||
|
val address = conversation.recipients.map { it.address }
|
||||||
|
sendMessage.execute(SendMessage.Params(subId, threadId, address, body, attachments, delay))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a new conversation with one address
|
||||||
|
addresses.size == 1 -> {
|
||||||
sendMessage.execute(SendMessage.Params(subId, threadId, addresses, body, attachments, delay))
|
sendMessage.execute(SendMessage.Params(subId, threadId, addresses, body, attachments, delay))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Send a message to multiple addresses
|
||||||
else -> {
|
else -> {
|
||||||
addresses.forEach { addr ->
|
addresses.forEach { addr ->
|
||||||
val threadId = TelephonyCompat.getOrCreateThreadId(context, addr)
|
val threadId = tryOrNull(false) { TelephonyCompat.getOrCreateThreadId(context, addr) } ?: 0
|
||||||
val address = listOf(conversationRepo.getConversation(threadId)?.recipients?.firstOrNull()?.address
|
val address = listOf(conversationRepo.getConversation(threadId)?.recipients?.firstOrNull()?.address ?: addr)
|
||||||
?: addr)
|
|
||||||
sendMessage.execute(SendMessage.Params(subId, threadId, address, body, attachments, delay))
|
sendMessage.execute(SendMessage.Params(subId, threadId, address, body, attachments, delay))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user