mirror of
https://github.com/quik-sms/quik.git
synced 2026-11-17 11:38:51 +01:00
Clear conversation query when changing pages or conversation selected
This commit is contained in:
@@ -2,6 +2,7 @@ package com.moez.QKSMS.presentation.common.base
|
||||
|
||||
import android.arch.lifecycle.ViewModel
|
||||
import android.support.annotation.CallSuper
|
||||
import io.reactivex.android.schedulers.AndroidSchedulers
|
||||
import io.reactivex.disposables.CompositeDisposable
|
||||
import io.reactivex.rxkotlin.plusAssign
|
||||
import io.reactivex.subjects.BehaviorSubject
|
||||
@@ -17,7 +18,9 @@ abstract class QkViewModel<in View : QkView<State>, State>(initialState: State)
|
||||
open fun bindView(view: View) {
|
||||
intents.clear()
|
||||
|
||||
intents += state.subscribe { view.render(it) }
|
||||
intents += state
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe { view.render(it) }
|
||||
}
|
||||
|
||||
protected fun newState(reducer: (State) -> State) {
|
||||
|
||||
@@ -160,8 +160,12 @@ class MainActivity : QkActivity<MainViewModel>(), MainView {
|
||||
}
|
||||
|
||||
override fun render(state: MainState) {
|
||||
toolbarSearch.isEnabled = state.page is Inbox
|
||||
toolbarSearch.textSize = if (state.page is Inbox) 16f else 20f
|
||||
|
||||
when (state.page) {
|
||||
is Inbox -> {
|
||||
if (toolbarSearch.text.toString() != state.page.query.toString()) toolbarSearch.setText(state.page.query)
|
||||
if (recyclerView.adapter != conversationsAdapter) recyclerView.adapter = conversationsAdapter
|
||||
if (conversationsAdapter.flowable != state.page.data) conversationsAdapter.flowable = state.page.data
|
||||
itemTouchHelper.attachToRecyclerView(recyclerView)
|
||||
@@ -169,6 +173,7 @@ class MainActivity : QkActivity<MainViewModel>(), MainView {
|
||||
}
|
||||
|
||||
is Archived -> {
|
||||
toolbarSearch.setText(R.string.title_archived)
|
||||
if (recyclerView.adapter != conversationsAdapter) recyclerView.adapter = conversationsAdapter
|
||||
if (conversationsAdapter.flowable != state.page.data) conversationsAdapter.flowable = state.page.data
|
||||
itemTouchHelper.attachToRecyclerView(null)
|
||||
@@ -176,12 +181,14 @@ class MainActivity : QkActivity<MainViewModel>(), MainView {
|
||||
}
|
||||
|
||||
is Scheduled -> {
|
||||
toolbarSearch.setText(R.string.title_scheduled)
|
||||
recyclerView.adapter = null
|
||||
itemTouchHelper.attachToRecyclerView(null)
|
||||
menuItemAdapter.data = ArrayList()
|
||||
}
|
||||
|
||||
is Blocked -> {
|
||||
toolbarSearch.setText(R.string.title_blocked)
|
||||
recyclerView.adapter = null
|
||||
itemTouchHelper.attachToRecyclerView(null)
|
||||
menuItemAdapter.data = ArrayList()
|
||||
|
||||
@@ -5,14 +5,15 @@ import com.moez.QKSMS.data.model.MenuItem
|
||||
import io.reactivex.Flowable
|
||||
|
||||
data class MainState(
|
||||
val page: MainPage = Inbox(null),
|
||||
val page: MainPage = Inbox(),
|
||||
val drawerOpen: Boolean = false
|
||||
)
|
||||
|
||||
sealed class MainPage
|
||||
|
||||
data class Inbox(
|
||||
val data: Flowable<List<InboxItem>>?,
|
||||
val query: CharSequence = "",
|
||||
val data: Flowable<List<InboxItem>>? = null,
|
||||
val menu: List<MenuItem> = ArrayList(),
|
||||
val showArchivedSnackbar: Boolean = false) : MainPage()
|
||||
|
||||
|
||||
@@ -10,9 +10,7 @@ import com.moez.QKSMS.data.repository.MessageRepository
|
||||
import com.moez.QKSMS.domain.interactor.*
|
||||
import com.moez.QKSMS.presentation.Navigator
|
||||
import com.moez.QKSMS.presentation.common.base.QkViewModel
|
||||
import io.reactivex.BackpressureStrategy
|
||||
import io.reactivex.Observable
|
||||
import io.reactivex.android.schedulers.AndroidSchedulers
|
||||
import io.reactivex.rxkotlin.plusAssign
|
||||
import io.reactivex.rxkotlin.withLatestFrom
|
||||
import java.util.concurrent.TimeUnit
|
||||
@@ -45,7 +43,7 @@ class MainViewModel : QkViewModel<MainView, MainState>(MainState()) {
|
||||
disposables += deleteConversation
|
||||
disposables += partialSync
|
||||
|
||||
newState { it.copy(page = Inbox(messageRepo.getConversations())) }
|
||||
newState { it.copy(page = Inbox(data = messageRepo.getConversations())) }
|
||||
|
||||
if (Telephony.Sms.getDefaultSmsPackage(context) != context.packageName) {
|
||||
partialSync.execute(Unit)
|
||||
@@ -59,16 +57,17 @@ class MainViewModel : QkViewModel<MainView, MainState>(MainState()) {
|
||||
|
||||
intents += view.queryChangedIntent
|
||||
.skip(1)
|
||||
.toFlowable(BackpressureStrategy.LATEST)
|
||||
.debounce(200, TimeUnit.MILLISECONDS)
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe { query ->
|
||||
val filteredConversations = conversations.map { list ->
|
||||
list.filter { conversationFilter.filter(it.conversation, query) }
|
||||
.withLatestFrom(state, { query, state ->
|
||||
if (state.page is Inbox) {
|
||||
val conversations = conversations.map { list ->
|
||||
list.filter { conversationFilter.filter(it.conversation, query) }
|
||||
}
|
||||
val page = state.page.copy(query = query, data = conversations)
|
||||
newState { it.copy(page = page) }
|
||||
}
|
||||
|
||||
newState { it.copy(page = Inbox(filteredConversations)) }
|
||||
}
|
||||
})
|
||||
.subscribe()
|
||||
|
||||
intents += view.composeIntent
|
||||
.subscribe { navigator.showCompose() }
|
||||
@@ -82,7 +81,7 @@ class MainViewModel : QkViewModel<MainView, MainState>(MainState()) {
|
||||
.distinctUntilChanged()
|
||||
.doOnNext {
|
||||
when (it) {
|
||||
DrawerItem.INBOX -> newState { it.copy(page = Inbox(conversations)) }
|
||||
DrawerItem.INBOX -> newState { it.copy(page = Inbox(query = "", data = conversations)) }
|
||||
DrawerItem.ARCHIVED -> newState { it.copy(page = Archived(messageRepo.getConversations(true))) }
|
||||
DrawerItem.SCHEDULED -> newState { it.copy(page = Scheduled()) }
|
||||
DrawerItem.BLOCKED -> newState { it.copy(page = Blocked()) }
|
||||
@@ -91,7 +90,14 @@ class MainViewModel : QkViewModel<MainView, MainState>(MainState()) {
|
||||
.subscribe()
|
||||
|
||||
intents += view.conversationClickIntent
|
||||
.subscribe { threadId -> navigator.showConversation(threadId) }
|
||||
.doOnNext { threadId -> navigator.showConversation(threadId) }
|
||||
.withLatestFrom(state, { _, state ->
|
||||
if (state.page is Inbox && state.page.query != "") {
|
||||
val page = state.page.copy(query = "", data = conversations)
|
||||
newState { it.copy(page = page) }
|
||||
}
|
||||
})
|
||||
.subscribe()
|
||||
|
||||
intents += view.conversationLongClickIntent
|
||||
.withLatestFrom(state, { l, mainState ->
|
||||
|
||||
Reference in New Issue
Block a user