Files
quik/presentation/src/main/java/feature/qkreply/QkReplyViewModel.kt
T
Moez Bhatti 1f345da920 #207 - Dual SIM support
commit c3dc3ada558c466be5115cd7ddbf9651a229c217
Author: Moez Bhatti <moez.bhatti@gmail.com>
Date:   Sat Jun 2 02:54:06 2018 -0400

    Dual SIM support for QK Reply

commit 48688dd4720bd1b3abc049d365e208e1dfe5ccb8
Author: Moez Bhatti <moez.bhatti@gmail.com>
Date:   Sat Jun 2 00:37:08 2018 -0400

    #1172 - Load all recipients and subId from threadId when sending message from notification reply bar

commit 0b7195fb134c0a9f2a6b551176c6c25a26bf5838
Author: Moez Bhatti <moez.bhatti@gmail.com>
Date:   Fri Jun 1 23:43:30 2018 -0400

    Fix missing subId for RetrySending params

commit 0d3ec43c0e60bab4acc293947ea9f6d2228c1b19
Author: Moez Bhatti <moez.bhatti@gmail.com>
Date:   Fri Jun 1 23:27:56 2018 -0400

    Switch to appropriate SIM when new message is received

commit 1ae883af890104cf1da9e970571371c95666db66
Author: Moez Bhatti <moez.bhatti@gmail.com>
Date:   Fri Jun 1 23:07:16 2018 -0400

    Fix message grouping for incoming messages

commit 2bf7b2d99ddbfdcafff873f305180d6d38c37d65
Author: Moez Bhatti <moez.bhatti@gmail.com>
Date:   Fri Jun 1 22:56:31 2018 -0400

    Correctly assign subId for incoming SMS

commit 1f6028f8921320358a3053a2dbc2c6e05410964b
Author: Moez Bhatti <moez.bhatti@gmail.com>
Date:   Fri Jun 1 20:52:01 2018 -0400

    Abstract SubscriptionManager usage behind SubscriptionUtils

commit 8b18f16271f19a42800c737cfbd867365c4aa781
Author: Moez Bhatti <moez.bhatti@gmail.com>
Date:   Fri Jun 1 20:05:52 2018 -0400

    Store subId when receiving SMS

commit f0e4ffdf15eb54d8ef78adfe244943ef4cb95d4e
Author: Moez Bhatti <moez.bhatti@gmail.com>
Date:   Fri Jun 1 19:52:40 2018 -0400

    Use appropriate SIM card based on last message

commit b7893d69891e4fb61755b0733a5fab561e86bbf3
Author: Moez Bhatti <moez.bhatti@gmail.com>
Date:   Thu May 31 00:20:42 2018 -0400

    Fix crash when device doesn't support multi-sim

commit bb038068129227e931167976628e1a535efb09a0
Author: Moez Bhatti <moez.bhatti@gmail.com>
Date:   Wed May 30 23:54:14 2018 -0400

    Show sim indicator when subId changes in conversation

commit 952f15f7ebfb85a03feef51373ef30568b76d5eb
Author: Moez Bhatti <moez.bhatti@gmail.com>
Date:   Wed May 30 22:13:57 2018 -0400

    Send message from selected SIM

commit 4cfab7ca9ec9bd9c7ce82b3eb5f228cf511a7c7b
Author: Moez Bhatti <moez.bhatti@gmail.com>
Date:   Wed May 30 21:46:01 2018 -0400

    Fix filename for ActiveSubscriptionObservable

commit 720cb79cb4fbfb98b10e169f2fea84be02fd2f91
Author: Moez Bhatti <moez.bhatti@gmail.com>
Date:   Wed May 30 20:35:30 2018 -0400

    Allow cycling through inserted sims

commit fa95980e9bc8e8c01dab7b4b8e4ed3a1c9a0f0b0
Author: Moez Bhatti <moez.bhatti@gmail.com>
Date:   Wed May 30 18:26:44 2018 -0400

    Show sim card slot in compose bar
2018-06-02 02:58:42 -04:00

226 lines
9.3 KiB
Kotlin

/*
* Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
*
* This file is part of QKSMS.
*
* QKSMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QKSMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
*/
package feature.qkreply
import android.telephony.SmsMessage
import com.moez.QKSMS.R
import com.uber.autodispose.android.lifecycle.scope
import com.uber.autodispose.kotlin.autoDisposable
import common.Navigator
import common.base.QkViewModel
import interactor.MarkRead
import interactor.SendMessage
import io.reactivex.rxkotlin.Observables
import io.reactivex.rxkotlin.plusAssign
import io.reactivex.rxkotlin.withLatestFrom
import io.reactivex.schedulers.Schedulers
import io.reactivex.subjects.BehaviorSubject
import io.reactivex.subjects.Subject
import io.realm.RealmResults
import model.Conversation
import model.Message
import repository.MessageRepository
import util.SubscriptionUtils
import util.extensions.asObservable
import util.extensions.mapNotNull
import java.util.concurrent.TimeUnit
import javax.inject.Inject
import javax.inject.Named
class QkReplyViewModel @Inject constructor(
@Named("threadId") threadId: Long,
private val markRead: MarkRead,
private val messageRepo: MessageRepository,
private val navigator: Navigator,
private val sendMessage: SendMessage,
private val subUtils: SubscriptionUtils
) : QkViewModel<QkReplyView, QkReplyState>(QkReplyState()) {
private val conversation by lazy {
messageRepo.getConversationAsync(threadId)
.asObservable<Conversation>()
.filter { it.isLoaded }
.filter { it.isValid }
.distinctUntilChanged()
}
private val messages: Subject<RealmResults<Message>> =
BehaviorSubject.createDefault(messageRepo.getUnreadMessages(threadId))
init {
disposables += markRead
disposables += sendMessage
// When the set of messages changes, update the state
// If we're ever showing an empty set of messages, then it's time to shut down to activity
disposables += Observables
.combineLatest(messages, conversation) { messages, conversation ->
newState { it.copy(data = Pair(conversation, messages)) }
messages
}
.switchMap { messages -> messages.asObservable() }
.filter { it.isLoaded }
.filter { it.isValid }
.filter { it.isEmpty() }
.subscribe { newState { it.copy(hasError = true) } }
disposables += conversation
.map { conversation -> conversation.getTitle() }
.distinctUntilChanged()
.subscribe { title -> newState { it.copy(title = title) } }
val latestSubId = messages
.map { messages -> messages.lastOrNull()?.subId ?: -1 }
.distinctUntilChanged()
disposables += Observables.combineLatest(latestSubId, subUtils.subscriptionsObservable) { subId, subs ->
val sub = if (subs.size > 1) subs.firstOrNull { it.subscriptionId == subId } ?: subs[0] else null
newState { it.copy(subscription = sub) }
}.subscribe()
}
override fun bindView(view: QkReplyView) {
super.bindView(view)
conversation
.map { conversation -> conversation.draft }
.distinctUntilChanged()
.autoDisposable(view.scope())
.subscribe { draft -> view.setDraft(draft) }
// Mark read
view.menuItemIntent
.filter { id -> id == R.id.read }
.withLatestFrom(conversation, { _, conversation -> conversation })
.map { conversation -> conversation.id }
.autoDisposable(view.scope())
.subscribe { threadId ->
markRead.execute(threadId) { newState { it.copy(hasError = true) } }
}
// Call
view.menuItemIntent
.filter { id -> id == R.id.call }
.withLatestFrom(conversation, { _, conversation -> conversation })
.mapNotNull { conversation -> conversation.recipients.first()?.address }
.doOnNext { address -> navigator.makePhoneCall(address) }
.autoDisposable(view.scope())
.subscribe { newState { it.copy(hasError = true) } }
// Show all messages
view.menuItemIntent
.filter { id -> id == R.id.expand }
.withLatestFrom(conversation, { _, conversation -> conversation })
.map { conversation -> messageRepo.getMessages(conversation.id) }
.doOnNext(messages::onNext)
.autoDisposable(view.scope())
.subscribe { newState { it.copy(expanded = true) } }
// Show unread messages only
view.menuItemIntent
.filter { id -> id == R.id.collapse }
.withLatestFrom(conversation, { _, conversation -> conversation })
.map { conversation -> messageRepo.getUnreadMessages(conversation.id) }
.doOnNext(messages::onNext)
.autoDisposable(view.scope())
.subscribe { newState { it.copy(expanded = false) } }
// View conversation
view.menuItemIntent
.filter { id -> id == R.id.view }
.withLatestFrom(conversation, { _, conversation -> conversation })
.map { conversation -> conversation.id }
.doOnNext { threadId -> navigator.showConversation(threadId) }
.autoDisposable(view.scope())
.subscribe { newState { it.copy(hasError = true) } }
// Enable the send button when there is text input into the new message body or there's
// an attachment, disable otherwise
view.textChangedIntent
.map { text -> text.isNotBlank() }
.autoDisposable(view.scope())
.subscribe { canSend -> newState { it.copy(canSend = canSend) } }
// Show the remaining character counter when necessary
view.textChangedIntent
.observeOn(Schedulers.computation())
.map { draft -> SmsMessage.calculateLength(draft, false) }
.map { array ->
val messages = array[0]
val remaining = array[2]
when {
messages <= 1 && remaining > 10 -> ""
messages <= 1 && remaining <= 10 -> "$remaining"
else -> "$remaining / $messages"
}
}
.distinctUntilChanged()
.autoDisposable(view.scope())
.subscribe { remaining -> newState { it.copy(remaining = remaining) } }
// Update the draft whenever the text is changed
view.textChangedIntent
.debounce(100, TimeUnit.MILLISECONDS)
.map { draft -> draft.toString() }
.observeOn(Schedulers.io())
.withLatestFrom(conversation.map { it.id }, { draft, threadId ->
messageRepo.saveDraft(threadId, draft)
})
.autoDisposable(view.scope())
.subscribe()
// Toggle to the next sim slot
view.changeSimIntent
.withLatestFrom(state) { _, state ->
val subs = subUtils.subscriptions
val subIndex = subs.indexOfFirst { it.subscriptionId == state.subscription?.subscriptionId }
val subscription = when {
subIndex == -1 -> null
subIndex < subs.size - 1 -> subs[subIndex + 1]
else -> subs[0]
}
newState { it.copy(subscription = subscription) }
}
.autoDisposable(view.scope())
.subscribe()
// Send a message when the send button is clicked, and disable editing mode if it's enabled
view.sendIntent
.withLatestFrom(view.textChangedIntent, { _, body -> body })
.map { body -> body.toString() }
.withLatestFrom(state, conversation, { body, state, conversation ->
val subId = state.subscription?.subscriptionId ?: -1
val threadId = conversation.id
val addresses = conversation.recipients.map { it.address }
sendMessage.execute(SendMessage.Params(subId, threadId, addresses, body))
view.setDraft("")
threadId
})
.doOnNext { threadId ->
markRead.execute(threadId) {
newState { it.copy(hasError = true) }
}
}
.autoDisposable(view.scope())
.subscribe()
}
}