From c0069c1a4c25ebfbf0b592d88653aaf6ccdfb9ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Frans-Lukas=20L=C3=B6venvald?= Date: Tue, 9 Jun 2020 14:58:49 +0200 Subject: [PATCH] #2119 removed unnecessary states from history view model --- .../core/history/HistoryActivity.kt | 30 ++--- .../core/history/viewmodel/Action.kt | 1 - .../history/viewmodel/HistoryViewModel.kt | 119 +++++------------- .../core/history/viewmodel/State.kt | 34 ++--- .../viewmodel/effects/DeleteHistoryItems.kt | 9 +- .../core/utils/SharedPreferenceUtil.java | 10 -- 6 files changed, 57 insertions(+), 146 deletions(-) diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/history/HistoryActivity.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/history/HistoryActivity.kt index 06c64e580..979d6f949 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/history/HistoryActivity.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/history/HistoryActivity.kt @@ -37,8 +37,6 @@ import org.kiwix.kiwixmobile.core.history.viewmodel.Action.UserClickedDeleteSele import org.kiwix.kiwixmobile.core.history.viewmodel.Action.UserClickedShowAllToggle import org.kiwix.kiwixmobile.core.history.viewmodel.HistoryViewModel import org.kiwix.kiwixmobile.core.history.viewmodel.State -import org.kiwix.kiwixmobile.core.history.viewmodel.State.Results -import org.kiwix.kiwixmobile.core.history.viewmodel.State.SelectionResults import org.kiwix.kiwixmobile.core.utils.SimpleTextListener import javax.inject.Inject @@ -128,25 +126,19 @@ class HistoryActivity : OnItemClickListener, BaseActivity() { return super.onOptionsItemSelected(item) } - private fun render(state: State) = - when (state) { - is Results -> { - actionMode?.finish() - historyAdapter.items = state.getHistoryListItems() - history_switch.isChecked = state.showAll - history_switch.isEnabled = true - toggleNoHistoryText(state) - } - is SelectionResults -> { - if (state.historyItems.any(HistoryItem::isSelected) && actionMode == null) { - actionMode = startSupportActionMode(actionModeCallback) - } - historyAdapter.items = state.getHistoryListItems() - history_switch.isChecked = state.showAll - history_switch.isEnabled = false - toggleNoHistoryText(state) + private fun render(state: State) { + historyAdapter.items = state.getHistoryListItems() + if (!state.isInSelectionState) { + actionMode?.finish() + history_switch.isEnabled = true + } else { + if (actionMode == null) { + actionMode = startSupportActionMode(actionModeCallback) } + history_switch.isEnabled = false } + toggleNoHistoryText(state) + } private fun toggleNoHistoryText(state: State) { if (state.getHistoryListItems().isEmpty()) { diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/history/viewmodel/Action.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/history/viewmodel/Action.kt index d7bfb45a0..8ea5ade37 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/history/viewmodel/Action.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/history/viewmodel/Action.kt @@ -30,7 +30,6 @@ sealed class Action { data class OnItemClick(val historyItem: HistoryItem) : Action() data class OnItemLongClick(val historyItem: HistoryItem) : Action() data class UserClickedShowAllToggle(val isChecked: Boolean) : Action() - data class AllHistoryPreferenceChanged(val showAll: Boolean) : Action() data class Filter(val searchTerm: String) : Action() data class UpdateHistory(val history: List) : Action() } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/history/viewmodel/HistoryViewModel.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/history/viewmodel/HistoryViewModel.kt index 1c5908387..26ea5d459 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/history/viewmodel/HistoryViewModel.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/history/viewmodel/HistoryViewModel.kt @@ -1,13 +1,12 @@ package org.kiwix.kiwixmobile.core.history.viewmodel -import org.kiwix.kiwixmobile.core.history.viewmodel.effects.OpenHistoryItem import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import io.reactivex.disposables.CompositeDisposable import io.reactivex.processors.PublishProcessor +import io.reactivex.schedulers.Schedulers import org.kiwix.kiwixmobile.core.base.SideEffect import org.kiwix.kiwixmobile.core.dao.HistoryDao -import org.kiwix.kiwixmobile.core.history.viewmodel.Action.AllHistoryPreferenceChanged import org.kiwix.kiwixmobile.core.history.viewmodel.Action.ExitActionModeMenu import org.kiwix.kiwixmobile.core.history.viewmodel.Action.ExitHistory import org.kiwix.kiwixmobile.core.history.viewmodel.Action.Filter @@ -18,9 +17,8 @@ import org.kiwix.kiwixmobile.core.history.viewmodel.Action.UserClickedConfirmDel import org.kiwix.kiwixmobile.core.history.viewmodel.Action.UserClickedDeleteButton import org.kiwix.kiwixmobile.core.history.viewmodel.Action.UserClickedDeleteSelectedHistoryItems import org.kiwix.kiwixmobile.core.history.viewmodel.Action.UserClickedShowAllToggle -import org.kiwix.kiwixmobile.core.history.viewmodel.State.Results -import org.kiwix.kiwixmobile.core.history.viewmodel.State.SelectionResults import org.kiwix.kiwixmobile.core.history.viewmodel.effects.DeleteHistoryItems +import org.kiwix.kiwixmobile.core.history.viewmodel.effects.OpenHistoryItem import org.kiwix.kiwixmobile.core.history.viewmodel.effects.ShowDeleteHistoryDialog import org.kiwix.kiwixmobile.core.history.viewmodel.effects.UpdateAllHistoryPreference import org.kiwix.kiwixmobile.core.reader.ZimReaderContainer @@ -34,7 +32,7 @@ class HistoryViewModel @Inject constructor( private val sharedPreferenceUtil: SharedPreferenceUtil ) : ViewModel() { val state = MutableLiveData().apply { - value = Results(emptyList(), sharedPreferenceUtil.showHistoryAllBooks, zimReaderContainer.id) + value = State(emptyList(), sharedPreferenceUtil.showHistoryAllBooks, zimReaderContainer.id) } val effects = PublishProcessor.create>() val actions = PublishProcessor.create() @@ -42,60 +40,36 @@ class HistoryViewModel @Inject constructor( init { compositeDisposable.addAll( - historyDao.history().subscribe { actions.offer(UpdateHistory(it)) }, - sharedPreferenceUtil.showAllHistoryToggleSwitch.subscribe { - actions.offer(AllHistoryPreferenceChanged(it)) - }, - viewStateReducer() + viewStateReducer(), + historyDao.history().subscribeOn(Schedulers.io()) + .subscribe({ actions.offer(UpdateHistory(it)) }, Throwable::printStackTrace) ) } private fun viewStateReducer() = - actions.map { reduce(it, state.value!!) }.subscribe(state::postValue) + actions.map { reduce(it, state.value!!) } + .subscribe(state::postValue, Throwable::printStackTrace) - private fun reduce(action: Action, state: State): State = - when (action) { - ExitHistory -> finishHistoryActivity(state) - ExitActionModeMenu -> deselectAllHistoryItems(state) - UserClickedConfirmDelete -> offerDeletionOfItems(state) - UserClickedDeleteButton -> offerShowDeleteDialog(state) - UserClickedDeleteSelectedHistoryItems -> offerShowDeleteDialog(state) - is OnItemClick -> handleItemClick(state, action) - is OnItemLongClick -> handleItemLongClick(state, action) - is UserClickedShowAllToggle -> offerUpdateToShowAllToggle(action, state) - is Filter -> updateHistoryItemsBasedOnFilter(state, action) - is UpdateHistory -> updateHistoryList(state, action) - is AllHistoryPreferenceChanged -> changeShowHistoryToggle(state, action) - } + private fun reduce(action: Action, state: State): State = when (action) { + ExitHistory -> finishHistoryActivity(state) + ExitActionModeMenu -> deselectAllHistoryItems(state) + UserClickedConfirmDelete -> offerDeletionOfItems(state) + UserClickedDeleteButton -> offerShowDeleteDialog(state) + UserClickedDeleteSelectedHistoryItems -> offerShowDeleteDialog(state) + is UserClickedShowAllToggle -> offerUpdateToShowAllToggle(action, state) + is OnItemClick -> handleItemClick(state, action) + is OnItemLongClick -> handleItemLongClick(state, action) + is Filter -> updateHistoryItemsBasedOnFilter(state, action) + is UpdateHistory -> updateHistoryList(state, action) + } private fun updateHistoryItemsBasedOnFilter(state: State, action: Filter) = - when (state) { - is Results -> state.copy(searchTerm = action.searchTerm) - is SelectionResults -> state.copy(searchTerm = action.searchTerm) - } - - private fun changeShowHistoryToggle( - state: State, - action: AllHistoryPreferenceChanged - ): State { - return when (state) { - is SelectionResults -> state - is Results -> state.copy(showAll = action.showAll) - } - } + state.copy(searchTerm = action.searchTerm) private fun updateHistoryList( state: State, action: UpdateHistory - ): State = when (state) { - is Results -> state.copy(historyItems = action.history) - is SelectionResults -> Results( - action.history, - state.showAll, - zimReaderContainer.id, - state.searchTerm - ) - } + ): State = state.copy(historyItems = action.history) private fun offerUpdateToShowAllToggle( action: UserClickedShowAllToggle, @@ -107,33 +81,23 @@ class HistoryViewModel @Inject constructor( action.isChecked ) ) - return when (state) { - is Results -> state.copy(showAll = action.isChecked) - else -> state - } + return state.copy(showAll = action.isChecked) } private fun handleItemLongClick( state: State, action: OnItemLongClick - ): State { - return when (state) { - is Results -> state.toggleSelectionOfItem(action.historyItem) - else -> state - } - } + ): State = state.toggleSelectionOfItem(action.historyItem) private fun handleItemClick( state: State, action: OnItemClick ): State { - return when (state) { - is Results -> { - effects.offer(OpenHistoryItem(action.historyItem, zimReaderContainer)) - state - } - is SelectionResults -> state.toggleSelectionOfItem(action.historyItem) + if (state.isInSelectionState) { + effects.offer(OpenHistoryItem(action.historyItem, zimReaderContainer)) + return state } + return state.toggleSelectionOfItem(action.historyItem) } private fun offerShowDeleteDialog(state: State): State { @@ -142,31 +106,12 @@ class HistoryViewModel @Inject constructor( } private fun offerDeletionOfItems(state: State): State { - return when (state) { - is Results -> { - effects.offer(DeleteHistoryItems(state.historyItems, historyDao)) - state - } - is SelectionResults -> { - effects.offer(DeleteHistoryItems(state.selectedItems, historyDao)) - state - } - } + effects.offer(DeleteHistoryItems(state, historyDao)) + return state } - private fun deselectAllHistoryItems(state: State): State { - return when (state) { - is SelectionResults -> { - Results( - state.historyItems.map { it.copy(isSelected = false) }, - state.showAll, - state.currentZimId, - state.searchTerm - ) - } - else -> state - } - } + private fun deselectAllHistoryItems(state: State): State = + state.copy(historyItems = state.historyItems.map { it.copy(isSelected = false) }) private fun finishHistoryActivity(state: State): State { effects.offer(Finish) diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/history/viewmodel/State.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/history/viewmodel/State.kt index bffc42204..c884cf996 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/history/viewmodel/State.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/history/viewmodel/State.kt @@ -25,13 +25,13 @@ import org.kiwix.kiwixmobile.core.history.adapter.HistoryListItem.HistoryItem import java.text.SimpleDateFormat import java.util.Locale -sealed class State( - open val historyItems: List, - open val showAll: Boolean, - open val currentZimId: String?, - open val searchTerm: String = "" +data class State( + val historyItems: List, + val showAll: Boolean, + val currentZimId: String?, + val searchTerm: String = "" ) { - + val isInSelectionState = historyItems.any(HistoryItem::isSelected) private val dateFormatter = SimpleDateFormat("d MMM yyyy", Locale.getDefault()) fun getHistoryListItems(): List = @@ -54,26 +54,6 @@ sealed class State( isSelected = !isSelected } else it } - if (newList.none(HistoryItem::isSelected)) { - return Results(newList, showAll, currentZimId, searchTerm) - } - return SelectionResults(newList, showAll, currentZimId, searchTerm) - } - - data class Results( - override val historyItems: List, - override val showAll: Boolean, - override val currentZimId: String?, - override val searchTerm: String = "" - ) : State(historyItems, showAll, currentZimId, searchTerm) - - data class SelectionResults( - override val historyItems: List, - override val showAll: Boolean, - override val currentZimId: String?, - override val searchTerm: String - ) : State(historyItems, showAll, currentZimId, searchTerm) { - val selectedItems: List = - getHistoryListItems().filterIsInstance().filter(HistoryItem::isSelected) + return State(newList, showAll, currentZimId, searchTerm) } } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/history/viewmodel/effects/DeleteHistoryItems.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/history/viewmodel/effects/DeleteHistoryItems.kt index df5e3f5a2..701e6bfa7 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/history/viewmodel/effects/DeleteHistoryItems.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/history/viewmodel/effects/DeleteHistoryItems.kt @@ -22,12 +22,17 @@ import androidx.appcompat.app.AppCompatActivity import org.kiwix.kiwixmobile.core.base.SideEffect import org.kiwix.kiwixmobile.core.dao.HistoryDao import org.kiwix.kiwixmobile.core.history.adapter.HistoryListItem.HistoryItem +import org.kiwix.kiwixmobile.core.history.viewmodel.State data class DeleteHistoryItems( - private val itemsToDelete: List, + private val state: State, private val historyDao: HistoryDao ) : SideEffect { override fun invokeWith(activity: AppCompatActivity) { - historyDao.deleteHistory(itemsToDelete) + if (state.isInSelectionState) { + historyDao.deleteHistory(state.historyItems.filter(HistoryItem::isSelected)) + } else { + historyDao.deleteHistory(state.historyItems) + } } } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/SharedPreferenceUtil.java b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/SharedPreferenceUtil.java index f506a7c2d..539f7aab9 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/SharedPreferenceUtil.java +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/SharedPreferenceUtil.java @@ -61,11 +61,6 @@ public class SharedPreferenceUtil { private SharedPreferences sharedPreferences; private final PublishProcessor prefStorages = PublishProcessor.create(); - public PublishProcessor getShowAllHistoryToggleSwitch() { - return showAllHistoryToggleSwitch; - } - - private final PublishProcessor showAllHistoryToggleSwitch = PublishProcessor.create(); private final PublishProcessor nightModes = PublishProcessor.create(); @Inject @@ -158,10 +153,6 @@ public class SharedPreferenceUtil { return prefStorages.startWith(getPrefStorage()); } - public Flowable getShowAllHistoryToggleSwitches() { - return showAllHistoryToggleSwitch.startWith(getShowHistoryAllBooks()); - } - public void putPrefFullScreen(boolean fullScreen) { sharedPreferences.edit().putBoolean(PREF_FULLSCREEN, fullScreen).apply(); } @@ -186,7 +177,6 @@ public class SharedPreferenceUtil { sharedPreferences.edit() .putBoolean(PREF_SHOW_HISTORY_ALL_BOOKS, prefShowHistoryAllBooks) .apply(); - showAllHistoryToggleSwitch.offer(prefShowHistoryAllBooks); } public boolean getShowBookmarksCurrentBook() {