From 1152c365368ac03ff09610c6c580e71f4e01890f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Frans-Lukas=20L=C3=B6venvald?= Date: Mon, 8 Jun 2020 16:15:40 +0200 Subject: [PATCH] #2119 filtering and deletion works --- .../core/history/viewmodel/Action.kt | 2 +- .../history/viewmodel/HistoryViewModel.kt | 25 ++++++++----------- .../core/history/viewmodel/State.kt | 21 ++++++++++------ .../effects/ShowDeleteHistoryDialog.kt | 4 +-- .../history/viewmodel/HistoryViewModelTest.kt | 4 +-- .../effects/ShowDeleteHistoryDialogTest.kt | 4 +-- 6 files changed, 32 insertions(+), 28 deletions(-) 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 a168e5403..d894cb015 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 @@ -23,7 +23,7 @@ import org.kiwix.kiwixmobile.core.history.adapter.HistoryListItem.HistoryItem sealed class Action { object ExitHistory : Action() object ExitActionModeMenu : Action() - object UserClickedDelete : Action() + object UserClickedConfirmDelete : Action() object UserClickedDeleteButton : Action() object UserClickedDeleteSelectedHistoryItems : 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 ec1a143ef..4d2b74d2a 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 @@ -15,7 +15,7 @@ import org.kiwix.kiwixmobile.core.history.viewmodel.Action.OnItemClick import org.kiwix.kiwixmobile.core.history.viewmodel.Action.OnItemLongClick import org.kiwix.kiwixmobile.core.history.viewmodel.Action.ToggleShowHistoryFromAllBooks import org.kiwix.kiwixmobile.core.history.viewmodel.Action.UpdateHistory -import org.kiwix.kiwixmobile.core.history.viewmodel.Action.UserClickedDelete +import org.kiwix.kiwixmobile.core.history.viewmodel.Action.UserClickedConfirmDelete 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.State.Results @@ -34,7 +34,7 @@ class HistoryViewModel @Inject constructor( private val sharedPreferenceUtil: SharedPreferenceUtil ) : ViewModel() { val state = MutableLiveData().apply { - value = Results(emptyList(), sharedPreferenceUtil.showHistoryAllBooks) + value = Results(emptyList(), sharedPreferenceUtil.showHistoryAllBooks, zimReaderContainer.id) } val effects = PublishProcessor.create>() val actions = PublishProcessor.create() @@ -57,7 +57,7 @@ class HistoryViewModel @Inject constructor( when (action) { ExitHistory -> finishHistoryActivity(state) ExitActionModeMenu -> deselectAllHistoryItems(state) - UserClickedDelete -> offerDeletionOfItems(state) + UserClickedConfirmDelete -> offerDeletionOfItems(state) UserClickedDeleteButton -> offerShowDeleteDialog(state) is OnItemClick -> handleItemClick(state, action) is OnItemLongClick -> handleItemLongClick(state, action) @@ -87,16 +87,9 @@ class HistoryViewModel @Inject constructor( private fun updateHistoryList( state: State, action: UpdateHistory - ): State { - return when (state) { - is Results -> state.copy(historyItems = action.history) - is SelectionResults -> { - val selectedItems = state.selectedItems - state.copy(historyItems = action.history.map { - if (selectedItems.contains(it)) it.copy(isSelected = true) else it - }) - } - } + ): State = when (state) { + is Results -> state.copy(historyItems = action.history) + is SelectionResults -> Results(action.history, state.showAll, zimReaderContainer.id) } private fun offerUpdateToShowAllToggle( @@ -156,7 +149,11 @@ class HistoryViewModel @Inject constructor( private fun deselectAllHistoryItems(state: State): State { return when (state) { is SelectionResults -> { - state.copy(historyItems = state.historyItems.map { it.copy(isSelected = false) }) + Results( + state.historyItems.map { it.copy(isSelected = false) }, + state.showAll, + state.searchTerm + ) } else -> state } 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 54ec21566..b1cf5115c 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 @@ -28,6 +28,7 @@ import java.util.Locale sealed class State( open val historyItems: List, open val showAll: Boolean, + open val currentZimId: String?, open val searchTerm: String = "" ) { @@ -35,7 +36,12 @@ sealed class State( val historyListItems: List by lazy { HeaderizableList(historyItems - .filter { it.historyTitle.contains(searchTerm, true) } + .filter { + it.historyTitle.contains( + searchTerm, + true + ) && (it.zimId == currentZimId || showAll) + } .sortedByDescending { dateFormatter.parse(it.dateString) }) .foldOverAddingHeaders( { historyItem -> DateItem(historyItem.dateString) }, @@ -49,10 +55,10 @@ sealed class State( isSelected = !isSelected } else it } - if (newList.isEmpty()) { - return Results(newList, showAll) + if (newList.none(HistoryItem::isSelected)) { + return Results(newList, showAll, currentZimId) } - return SelectionResults(newList, showAll, searchTerm) + return SelectionResults(newList, showAll, currentZimId, searchTerm) } // fun filterBasedOnSearchTerm(searchTerm: String): State = @@ -66,15 +72,16 @@ sealed class State( data class Results( override val historyItems: List, override val showAll: Boolean, + override val currentZimId: String?, override val searchTerm: String = "" - ) : State(historyItems, showAll, searchTerm) + ) : 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, searchTerm) { - + ) : State(historyItems, showAll, currentZimId, searchTerm) { val selectedItems: List = historyListItems.filterIsInstance().filter(HistoryItem::isSelected) } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/history/viewmodel/effects/ShowDeleteHistoryDialog.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/history/viewmodel/effects/ShowDeleteHistoryDialog.kt index 27be7ca8a..82a3e571c 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/history/viewmodel/effects/ShowDeleteHistoryDialog.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/history/viewmodel/effects/ShowDeleteHistoryDialog.kt @@ -23,7 +23,7 @@ import io.reactivex.processors.PublishProcessor import org.kiwix.kiwixmobile.core.base.SideEffect import org.kiwix.kiwixmobile.core.history.HistoryActivity import org.kiwix.kiwixmobile.core.history.viewmodel.Action -import org.kiwix.kiwixmobile.core.history.viewmodel.Action.UserClickedDelete +import org.kiwix.kiwixmobile.core.history.viewmodel.Action.UserClickedConfirmDelete import org.kiwix.kiwixmobile.core.utils.DialogShower import org.kiwix.kiwixmobile.core.utils.KiwixDialog.DeleteAllHistory import javax.inject.Inject @@ -35,7 +35,7 @@ data class ShowDeleteHistoryDialog( override fun invokeWith(activity: AppCompatActivity) { (activity as HistoryActivity).activityComponent.inject(this) dialogShower.show(DeleteAllHistory, { - actions.offer(UserClickedDelete) + actions.offer(UserClickedConfirmDelete) }) } } diff --git a/core/src/test/java/org/kiwix/kiwixmobile/core/history/viewmodel/HistoryViewModelTest.kt b/core/src/test/java/org/kiwix/kiwixmobile/core/history/viewmodel/HistoryViewModelTest.kt index eddebea4b..8b1efe33a 100644 --- a/core/src/test/java/org/kiwix/kiwixmobile/core/history/viewmodel/HistoryViewModelTest.kt +++ b/core/src/test/java/org/kiwix/kiwixmobile/core/history/viewmodel/HistoryViewModelTest.kt @@ -23,7 +23,7 @@ import org.kiwix.kiwixmobile.core.history.viewmodel.Action.Filter import org.kiwix.kiwixmobile.core.history.viewmodel.Action.OnItemClick import org.kiwix.kiwixmobile.core.history.viewmodel.Action.OnItemLongClick import org.kiwix.kiwixmobile.core.history.viewmodel.Action.ToggleShowHistoryFromAllBooks -import org.kiwix.kiwixmobile.core.history.viewmodel.Action.UserClickedDelete +import org.kiwix.kiwixmobile.core.history.viewmodel.Action.UserClickedConfirmDelete 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.State.Results @@ -458,7 +458,7 @@ internal class HistoryViewModelTest { @Test fun `DeleteHistoryItems calls DeleteSelectedOrAllHistoryItems side effect`() { actionResultsInEffects( - UserClickedDelete, + UserClickedConfirmDelete, DeleteHistoryItems(viewModel.state.value!!.historyItems, historyDao) ) } diff --git a/core/src/test/java/org/kiwix/kiwixmobile/core/history/viewmodel/effects/ShowDeleteHistoryDialogTest.kt b/core/src/test/java/org/kiwix/kiwixmobile/core/history/viewmodel/effects/ShowDeleteHistoryDialogTest.kt index 237e6492f..852d7d86e 100644 --- a/core/src/test/java/org/kiwix/kiwixmobile/core/history/viewmodel/effects/ShowDeleteHistoryDialogTest.kt +++ b/core/src/test/java/org/kiwix/kiwixmobile/core/history/viewmodel/effects/ShowDeleteHistoryDialogTest.kt @@ -8,7 +8,7 @@ import io.reactivex.processors.PublishProcessor import org.junit.jupiter.api.Test import org.kiwix.kiwixmobile.core.history.HistoryActivity import org.kiwix.kiwixmobile.core.history.viewmodel.Action -import org.kiwix.kiwixmobile.core.history.viewmodel.Action.UserClickedDelete +import org.kiwix.kiwixmobile.core.history.viewmodel.Action.UserClickedConfirmDelete import org.kiwix.kiwixmobile.core.utils.DialogShower import org.kiwix.kiwixmobile.core.utils.KiwixDialog.DeleteSelectedHistory @@ -30,6 +30,6 @@ internal class ShowDeleteHistoryDialogTest { dialogShower.show(DeleteSelectedHistory, capture(lambdaSlot)) } lambdaSlot.captured.invoke() - verify { actions.offer(UserClickedDelete) } + verify { actions.offer(UserClickedConfirmDelete) } } }