forked from upstream-mirrors/quik
Close qk reply when there are no messages
This commit is contained in:
@@ -106,6 +106,10 @@ class QkReplyActivity : QkThemedActivity<QkReplyViewModel>(), QkReplyView {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun render(state: QkReplyState) {
|
override fun render(state: QkReplyState) {
|
||||||
|
if (state.hasError) {
|
||||||
|
finish()
|
||||||
|
}
|
||||||
|
|
||||||
threadId.onNext(state.data?.first?.id ?: 0)
|
threadId.onNext(state.data?.first?.id ?: 0)
|
||||||
|
|
||||||
title = state.title
|
title = state.title
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ import model.Conversation
|
|||||||
import model.Message
|
import model.Message
|
||||||
|
|
||||||
data class QkReplyState(
|
data class QkReplyState(
|
||||||
|
val hasError: Boolean = false,
|
||||||
val title: String = "",
|
val title: String = "",
|
||||||
val expanded: Boolean = false,
|
val expanded: Boolean = false,
|
||||||
val data: Pair<Conversation, RealmResults<Message>>? = null,
|
val data: Pair<Conversation, RealmResults<Message>>? = null,
|
||||||
|
|||||||
@@ -31,7 +31,11 @@ import interactor.SendMessage
|
|||||||
import io.reactivex.rxkotlin.plusAssign
|
import io.reactivex.rxkotlin.plusAssign
|
||||||
import io.reactivex.rxkotlin.withLatestFrom
|
import io.reactivex.rxkotlin.withLatestFrom
|
||||||
import io.reactivex.schedulers.Schedulers
|
import io.reactivex.schedulers.Schedulers
|
||||||
|
import io.reactivex.subjects.PublishSubject
|
||||||
|
import io.reactivex.subjects.Subject
|
||||||
|
import io.realm.RealmResults
|
||||||
import model.Conversation
|
import model.Conversation
|
||||||
|
import model.Message
|
||||||
import repository.MessageRepository
|
import repository.MessageRepository
|
||||||
import util.extensions.asObservable
|
import util.extensions.asObservable
|
||||||
import util.extensions.mapNotNull
|
import util.extensions.mapNotNull
|
||||||
@@ -53,17 +57,31 @@ class QkReplyViewModel(intent: Intent) : QkViewModel<QkReplyView, QkReplyState>(
|
|||||||
.distinctUntilChanged()
|
.distinctUntilChanged()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private val messages: Subject<RealmResults<Message>> = PublishSubject.create()
|
||||||
|
|
||||||
init {
|
init {
|
||||||
appComponent.inject(this)
|
appComponent.inject(this)
|
||||||
|
|
||||||
disposables += markRead
|
disposables += markRead
|
||||||
disposables += sendMessage
|
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 += messages
|
||||||
|
.withLatestFrom(conversation, { messages, conversation -> Pair(conversation, messages) })
|
||||||
|
.doOnNext { data -> newState { it.copy(data = data) } }
|
||||||
|
.map { data -> data.second }
|
||||||
|
.switchMap { messages -> messages.asObservable() }
|
||||||
|
.filter { it.isLoaded }
|
||||||
|
.filter { it.isValid }
|
||||||
|
.filter { it.isEmpty() }
|
||||||
|
.subscribe { newState { it.copy(hasError = true) } }
|
||||||
|
|
||||||
disposables += conversation
|
disposables += conversation
|
||||||
.doOnNext { conversation -> newState { it.copy(title = conversation.getTitle()) } }
|
.doOnNext { conversation -> newState { it.copy(title = conversation.getTitle()) } }
|
||||||
.distinctUntilChanged { conversation -> conversation.id } // We only need to set the messages once
|
.distinctUntilChanged { conversation -> conversation.id } // We only need to set the messages once
|
||||||
.map { conversation -> Pair(conversation, messageRepo.getUnreadMessages(conversation.id)) }
|
.map { conversation -> messageRepo.getUnreadMessages(conversation.id) }
|
||||||
.subscribe { data -> newState { it.copy(data = data) } }
|
.subscribe { messages.onNext(it) }
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun bindView(view: QkReplyView) {
|
override fun bindView(view: QkReplyView) {
|
||||||
@@ -81,7 +99,9 @@ class QkReplyViewModel(intent: Intent) : QkViewModel<QkReplyView, QkReplyState>(
|
|||||||
.withLatestFrom(conversation, { _, conversation -> conversation })
|
.withLatestFrom(conversation, { _, conversation -> conversation })
|
||||||
.map { conversation -> conversation.id }
|
.map { conversation -> conversation.id }
|
||||||
.autoDisposable(view.scope())
|
.autoDisposable(view.scope())
|
||||||
.subscribe { threadId -> markRead.execute(threadId) { view.finish() } }
|
.subscribe { threadId ->
|
||||||
|
markRead.execute(threadId) { newState { it.copy(hasError = true) } }
|
||||||
|
}
|
||||||
|
|
||||||
// Call
|
// Call
|
||||||
view.menuItemIntent
|
view.menuItemIntent
|
||||||
@@ -90,23 +110,25 @@ class QkReplyViewModel(intent: Intent) : QkViewModel<QkReplyView, QkReplyState>(
|
|||||||
.mapNotNull { conversation -> conversation.recipients.first()?.address }
|
.mapNotNull { conversation -> conversation.recipients.first()?.address }
|
||||||
.doOnNext { address -> navigator.makePhoneCall(address) }
|
.doOnNext { address -> navigator.makePhoneCall(address) }
|
||||||
.autoDisposable(view.scope())
|
.autoDisposable(view.scope())
|
||||||
.subscribe { view.finish() }
|
.subscribe { newState { it.copy(hasError = true) } }
|
||||||
|
|
||||||
// Show all messages
|
// Show all messages
|
||||||
view.menuItemIntent
|
view.menuItemIntent
|
||||||
.filter { id -> id == R.id.expand }
|
.filter { id -> id == R.id.expand }
|
||||||
.withLatestFrom(conversation, { _, conversation -> conversation })
|
.withLatestFrom(conversation, { _, conversation -> conversation })
|
||||||
.map { conversation -> Pair(conversation, messageRepo.getMessages(conversation.id)) }
|
.map { conversation -> messageRepo.getMessages(conversation.id) }
|
||||||
|
.doOnNext { messages.onNext(it) }
|
||||||
.autoDisposable(view.scope())
|
.autoDisposable(view.scope())
|
||||||
.subscribe { data -> newState { it.copy(expanded = true, data = data) } }
|
.subscribe { newState { it.copy(expanded = true) } }
|
||||||
|
|
||||||
// Show unread messages only
|
// Show unread messages only
|
||||||
view.menuItemIntent
|
view.menuItemIntent
|
||||||
.filter { id -> id == R.id.collapse }
|
.filter { id -> id == R.id.collapse }
|
||||||
.withLatestFrom(conversation, { _, conversation -> conversation })
|
.withLatestFrom(conversation, { _, conversation -> conversation })
|
||||||
.map { conversation -> Pair(conversation, messageRepo.getUnreadMessages(conversation.id)) }
|
.map { conversation -> messageRepo.getUnreadMessages(conversation.id) }
|
||||||
|
.doOnNext { messages.onNext(it) }
|
||||||
.autoDisposable(view.scope())
|
.autoDisposable(view.scope())
|
||||||
.subscribe { data -> newState { it.copy(expanded = false, data = data) } }
|
.subscribe { newState { it.copy(expanded = false) } }
|
||||||
|
|
||||||
// View conversation
|
// View conversation
|
||||||
view.menuItemIntent
|
view.menuItemIntent
|
||||||
@@ -115,7 +137,7 @@ class QkReplyViewModel(intent: Intent) : QkViewModel<QkReplyView, QkReplyState>(
|
|||||||
.map { conversation -> conversation.id }
|
.map { conversation -> conversation.id }
|
||||||
.doOnNext { threadId -> navigator.showConversation(threadId) }
|
.doOnNext { threadId -> navigator.showConversation(threadId) }
|
||||||
.autoDisposable(view.scope())
|
.autoDisposable(view.scope())
|
||||||
.subscribe { view.finish() }
|
.subscribe { newState { it.copy(hasError = true) } }
|
||||||
|
|
||||||
// Enable the send button when there is text input into the new message body or there's
|
// Enable the send button when there is text input into the new message body or there's
|
||||||
// an attachment, disable otherwise
|
// an attachment, disable otherwise
|
||||||
@@ -163,11 +185,15 @@ class QkReplyViewModel(intent: Intent) : QkViewModel<QkReplyView, QkReplyState>(
|
|||||||
|
|
||||||
view.setDraft("")
|
view.setDraft("")
|
||||||
sendMessage.execute(SendMessage.Params(threadId, addresses, body, listOf())) {
|
sendMessage.execute(SendMessage.Params(threadId, addresses, body, listOf())) {
|
||||||
markRead.execute(threadId) {
|
|
||||||
view.finish()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
threadId
|
||||||
})
|
})
|
||||||
|
.doOnNext { threadId ->
|
||||||
|
markRead.execute(threadId) {
|
||||||
|
newState { it.copy(hasError = true) }
|
||||||
|
}
|
||||||
|
}
|
||||||
.autoDisposable(view.scope())
|
.autoDisposable(view.scope())
|
||||||
.subscribe()
|
.subscribe()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user