forked from upstream-mirrors/quik
Load appropriate conversation based on chip selection
This commit is contained in:
@@ -0,0 +1,32 @@
|
|||||||
|
package com.moez.QKSMS.common.util
|
||||||
|
|
||||||
|
import android.text.TextUtils
|
||||||
|
import android.util.Patterns
|
||||||
|
import java.util.regex.Pattern
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Utils for using Telephony functions on lower API levels
|
||||||
|
*
|
||||||
|
* Adapted from https://android.googlesource.com/platform/packages/apps/Messaging/+/master/src/com/android/messaging/sms/MmsSmsUtils.java
|
||||||
|
*/
|
||||||
|
object MessageUtils {
|
||||||
|
|
||||||
|
val NAME_ADDR_EMAIL_PATTERN = Pattern.compile("\\s*(\"[^\"]*\"|[^<>\"]+)\\s*<([^<>]+)>\\s*")
|
||||||
|
|
||||||
|
fun extractAddrSpec(address: String): String {
|
||||||
|
val match = NAME_ADDR_EMAIL_PATTERN.matcher(address)
|
||||||
|
return if (match.matches()) {
|
||||||
|
match.group(2)
|
||||||
|
} else address
|
||||||
|
}
|
||||||
|
|
||||||
|
fun isEmailAddress(address: String): Boolean {
|
||||||
|
if (TextUtils.isEmpty(address)) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
val s = extractAddrSpec(address)
|
||||||
|
val match = Patterns.EMAIL_ADDRESS.matcher(s)
|
||||||
|
return match.matches()
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -2,6 +2,8 @@ package com.moez.QKSMS.common.util.extensions
|
|||||||
|
|
||||||
import android.database.Cursor
|
import android.database.Cursor
|
||||||
import io.reactivex.Flowable
|
import io.reactivex.Flowable
|
||||||
|
import io.reactivex.Maybe
|
||||||
|
import io.reactivex.subjects.MaybeSubject
|
||||||
|
|
||||||
fun Cursor.forEach(closeOnComplete: Boolean = true, method: (Cursor) -> Unit = {}) {
|
fun Cursor.forEach(closeOnComplete: Boolean = true, method: (Cursor) -> Unit = {}) {
|
||||||
moveToPosition(-1)
|
moveToPosition(-1)
|
||||||
@@ -30,3 +32,19 @@ fun Cursor.asFlowable(): Flowable<Cursor> {
|
|||||||
}
|
}
|
||||||
.doOnComplete { close() }
|
.doOnComplete { close() }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun Cursor.asMaybe(): Maybe<Cursor> {
|
||||||
|
val subject = MaybeSubject.create<Cursor>()
|
||||||
|
|
||||||
|
if (moveToFirst()) {
|
||||||
|
subject.onSuccess(this)
|
||||||
|
} else {
|
||||||
|
subject.onError(IndexOutOfBoundsException("The cursor has no items"))
|
||||||
|
}
|
||||||
|
|
||||||
|
subject.doOnComplete { close() }
|
||||||
|
return subject
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -6,13 +6,16 @@ import android.net.Uri
|
|||||||
import android.provider.BaseColumns
|
import android.provider.BaseColumns
|
||||||
import android.provider.Telephony.Sms
|
import android.provider.Telephony.Sms
|
||||||
import android.provider.Telephony.TextBasedSmsColumns
|
import android.provider.Telephony.TextBasedSmsColumns
|
||||||
|
import com.moez.QKSMS.common.util.MessageUtils
|
||||||
import com.moez.QKSMS.common.util.extensions.asFlowable
|
import com.moez.QKSMS.common.util.extensions.asFlowable
|
||||||
|
import com.moez.QKSMS.common.util.extensions.asMaybe
|
||||||
import com.moez.QKSMS.common.util.extensions.insertOrUpdate
|
import com.moez.QKSMS.common.util.extensions.insertOrUpdate
|
||||||
import com.moez.QKSMS.data.mapper.CursorToMessage
|
import com.moez.QKSMS.data.mapper.CursorToMessage
|
||||||
import com.moez.QKSMS.data.model.Conversation
|
import com.moez.QKSMS.data.model.Conversation
|
||||||
import com.moez.QKSMS.data.model.ConversationMessagePair
|
import com.moez.QKSMS.data.model.ConversationMessagePair
|
||||||
import com.moez.QKSMS.data.model.Message
|
import com.moez.QKSMS.data.model.Message
|
||||||
import io.reactivex.Flowable
|
import io.reactivex.Flowable
|
||||||
|
import io.reactivex.Maybe
|
||||||
import io.reactivex.functions.BiFunction
|
import io.reactivex.functions.BiFunction
|
||||||
import io.reactivex.schedulers.Schedulers
|
import io.reactivex.schedulers.Schedulers
|
||||||
import io.realm.Realm
|
import io.realm.Realm
|
||||||
@@ -66,6 +69,35 @@ class MessageRepository @Inject constructor(
|
|||||||
.findFirst()
|
.findFirst()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets an unmanaged conversation by the address
|
||||||
|
*/
|
||||||
|
fun getOrCreateConversation(address: String) = getOrCreateConversation(listOf(address))
|
||||||
|
|
||||||
|
fun getOrCreateConversation(addresses: List<String>): Maybe<Conversation> {
|
||||||
|
return Maybe.just(addresses)
|
||||||
|
.map { recipients ->
|
||||||
|
recipients.map { address ->
|
||||||
|
when (MessageUtils.isEmailAddress(address)) {
|
||||||
|
true -> MessageUtils.extractAddrSpec(address)
|
||||||
|
false -> address
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.map { recipients ->
|
||||||
|
Uri.parse("content://mms-sms/threadID").buildUpon().apply {
|
||||||
|
recipients.forEach { recipient -> appendQueryParameter("recipient", recipient) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.flatMap { uriBuilder ->
|
||||||
|
context.contentResolver.query(uriBuilder.build(), arrayOf(BaseColumns._ID), null, null, null).asMaybe()
|
||||||
|
}
|
||||||
|
.map { cursor -> cursor.getLong(0) }
|
||||||
|
.filter { threadId -> threadId != 0L }
|
||||||
|
.map { threadId -> getConversation(threadId) ?: Conversation() } // TODO create actual conversation
|
||||||
|
.onErrorReturn { Conversation() }
|
||||||
|
}
|
||||||
|
|
||||||
fun getMessages(threadId: Long): RealmResults<Message> {
|
fun getMessages(threadId: Long): RealmResults<Message> {
|
||||||
return Realm.getDefaultInstance()
|
return Realm.getDefaultInstance()
|
||||||
.where(Message::class.java)
|
.where(Message::class.java)
|
||||||
|
|||||||
@@ -38,27 +38,30 @@ class ComposeViewModel(threadId: Long) : QkViewModel<ComposeView, ComposeState>(
|
|||||||
|
|
||||||
private val contacts: List<Contact>
|
private val contacts: List<Contact>
|
||||||
|
|
||||||
private val selectedContacts: Observable<List<Contact>>
|
|
||||||
private val conversation: Observable<Conversation>
|
private val conversation: Observable<Conversation>
|
||||||
|
|
||||||
init {
|
init {
|
||||||
AppComponentManager.appComponent.inject(this)
|
AppComponentManager.appComponent.inject(this)
|
||||||
|
|
||||||
selectedContacts = contactsReducer
|
// Merges two potential conversation sources (threadId from constructor and contact selection) into a single
|
||||||
|
// stream of conversations
|
||||||
|
val selectedConversation = contactsReducer
|
||||||
.scan(listOf<Contact>(), { previousState, reducer -> reducer(previousState) })
|
.scan(listOf<Contact>(), { previousState, reducer -> reducer(previousState) })
|
||||||
.doOnNext { contacts -> newState { it.copy(selectedContacts = contacts) } }
|
.doOnNext { contacts -> newState { it.copy(selectedContacts = contacts) } }
|
||||||
|
.map { contacts -> contacts.map { it.address } }
|
||||||
|
.flatMapMaybe { addresses -> messageRepo.getOrCreateConversation(addresses) }
|
||||||
|
|
||||||
conversation = messageRepo.getConversationAsync(threadId)
|
conversation = messageRepo.getConversationAsync(threadId)
|
||||||
.asObservable<Conversation>()
|
.asObservable<Conversation>()
|
||||||
.filter { conversation -> conversation.isLoaded }
|
.filter { conversation -> conversation.isLoaded }
|
||||||
.filter { conversation -> conversation.isValid }
|
.filter { conversation -> conversation.isValid }
|
||||||
|
.mergeWith(selectedConversation)
|
||||||
.distinctUntilChanged { conversation -> conversation.id }
|
.distinctUntilChanged { conversation -> conversation.id }
|
||||||
.doOnNext { conversation -> newState { it.copy(title = conversation.getTitle()) } }
|
.doOnNext { conversation -> newState { it.copy(title = conversation.getTitle()) } }
|
||||||
|
|
||||||
disposables += sendMessage
|
disposables += sendMessage
|
||||||
disposables += markRead
|
disposables += markRead
|
||||||
disposables += conversation.subscribe()
|
disposables += conversation.subscribe()
|
||||||
disposables += selectedContacts.subscribe()
|
|
||||||
|
|
||||||
// When the conversation changes, update the messages for the adapter
|
// When the conversation changes, update the messages for the adapter
|
||||||
// When the message list changes, make sure to mark them read
|
// When the message list changes, make sure to mark them read
|
||||||
|
|||||||
Reference in New Issue
Block a user