diff --git a/presentation/src/main/java/feature/main/MainViewModel.kt b/presentation/src/main/java/feature/main/MainViewModel.kt index 66dd477e..d6a14152 100644 --- a/presentation/src/main/java/feature/main/MainViewModel.kt +++ b/presentation/src/main/java/feature/main/MainViewModel.kt @@ -65,9 +65,14 @@ class MainViewModel : QkViewModel(MainState()) { private val menuUnarchive by lazy { MenuItem(context.getString(R.string.menu_unarchive), 1) } private val menuDelete by lazy { MenuItem(context.getString(R.string.menu_delete), 2) } + private val conversations by lazy { messageRepo.getConversations() } + init { appComponent.inject(this) + // Now the conversations can be loaded + conversations + disposables += deleteConversation disposables += markAllSeen disposables += markArchived @@ -86,8 +91,6 @@ class MainViewModel : QkViewModel(MainState()) { .distinctUntilChanged() .subscribe { syncing -> newState { it.copy(syncing = syncing) } } - newState { it.copy(page = Inbox(data = messageRepo.getConversations())) } - // Migrate the preferences from 2.7.3 if necessary migratePreferences.execute(Unit) @@ -110,14 +113,14 @@ class MainViewModel : QkViewModel(MainState()) { super.bindView(view) view.queryChangedIntent - .skip(1) .debounce(200, TimeUnit.MILLISECONDS) .withLatestFrom(state, { query, state -> if (state.page is Inbox) { - val conversations = state.page.data - ?.map { conversations -> conversations.filter { conversationFilter.filter(it, query) } } + val filteredConversations = if (query.isEmpty()) conversations + else conversations + .map { conversations -> conversations.filter { conversationFilter.filter(it, query) } } - val page = state.page.copy(showClearButton = query.isNotEmpty(), data = conversations) + val page = state.page.copy(showClearButton = query.isNotEmpty(), data = filteredConversations) newState { it.copy(page = page) } } }) @@ -144,7 +147,7 @@ class MainViewModel : QkViewModel(MainState()) { .distinctUntilChanged() .doOnNext { when (it) { - DrawerItem.INBOX -> newState { it.copy(page = Inbox(data = messageRepo.getConversations())) } + DrawerItem.INBOX -> newState { it.copy(page = Inbox()) } DrawerItem.ARCHIVED -> newState { it.copy(page = Archived(messageRepo.getConversations(true))) } DrawerItem.SCHEDULED -> newState { it.copy(page = Scheduled()) } else -> {