mirror of
https://github.com/quik-sms/quik.git
synced 2026-11-16 02:58:52 +01:00
Add ability to mark read, and expand/collapse qk reply
This commit is contained in:
@@ -19,9 +19,14 @@
|
||||
package feature.qkreply
|
||||
|
||||
import android.content.Intent
|
||||
import com.moez.QKSMS.R
|
||||
import com.uber.autodispose.android.lifecycle.scope
|
||||
import com.uber.autodispose.kotlin.autoDisposable
|
||||
import common.base.QkViewModel
|
||||
import injection.appComponent
|
||||
import interactor.MarkRead
|
||||
import io.reactivex.rxkotlin.plusAssign
|
||||
import io.reactivex.rxkotlin.withLatestFrom
|
||||
import model.Conversation
|
||||
import repository.MessageRepository
|
||||
import util.extensions.asObservable
|
||||
@@ -29,22 +34,55 @@ import javax.inject.Inject
|
||||
|
||||
class QkReplyViewModel(intent: Intent) : QkViewModel<QkReplyView, QkReplyState>(QkReplyState()) {
|
||||
|
||||
@Inject lateinit var markRead: MarkRead
|
||||
@Inject lateinit var messageRepo: MessageRepository
|
||||
|
||||
init {
|
||||
appComponent.inject(this)
|
||||
|
||||
val threadId = intent.getLongExtra("threadId", -1)
|
||||
|
||||
disposables += messageRepo.getConversationAsync(threadId)
|
||||
private val conversation by lazy {
|
||||
messageRepo.getConversationAsync(intent.getLongExtra("threadId", -1))
|
||||
.asObservable<Conversation>()
|
||||
.filter { it.isLoaded }
|
||||
.filter { it.isValid }
|
||||
.distinctUntilChanged()
|
||||
}
|
||||
|
||||
init {
|
||||
appComponent.inject(this)
|
||||
|
||||
disposables += markRead
|
||||
|
||||
disposables += conversation
|
||||
.doOnNext { conversation -> newState { it.copy(title = conversation.getTitle()) } }
|
||||
.take(1) // We only need to set the messages once
|
||||
.map { conversation -> Pair(conversation, messageRepo.getUnreadMessages(conversation.id)) }
|
||||
.doOnNext { data -> newState { it.copy(data = data) } }
|
||||
.subscribe()
|
||||
.subscribe { data -> newState { it.copy(data = data) } }
|
||||
}
|
||||
|
||||
override fun bindView(view: QkReplyView) {
|
||||
super.bindView(view)
|
||||
|
||||
// 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) }
|
||||
|
||||
// Show all messages
|
||||
view.menuItemIntent
|
||||
.filter { id -> id == R.id.expand }
|
||||
.withLatestFrom(conversation, { _, conversation -> conversation })
|
||||
.map { conversation -> Pair(conversation, messageRepo.getMessages(conversation.id)) }
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe { data -> newState { it.copy(expanded = true, data = data) } }
|
||||
|
||||
// Show unread messages only
|
||||
view.menuItemIntent
|
||||
.filter { id -> id == R.id.collapse }
|
||||
.withLatestFrom(conversation, { _, conversation -> conversation })
|
||||
.map { conversation -> Pair(conversation, messageRepo.getUnreadMessages(conversation.id)) }
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe { data -> newState { it.copy(expanded = false, data = data) } }
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user