#2119 filtering and deletion works

This commit is contained in:
Frans-Lukas Lövenvald 2020-06-08 16:15:40 +02:00
parent 0b699fd89d
commit 1152c36536
6 changed files with 32 additions and 28 deletions

View File

@ -23,7 +23,7 @@ import org.kiwix.kiwixmobile.core.history.adapter.HistoryListItem.HistoryItem
sealed class Action { sealed class Action {
object ExitHistory : Action() object ExitHistory : Action()
object ExitActionModeMenu : Action() object ExitActionModeMenu : Action()
object UserClickedDelete : Action() object UserClickedConfirmDelete : Action()
object UserClickedDeleteButton : Action() object UserClickedDeleteButton : Action()
object UserClickedDeleteSelectedHistoryItems : Action() object UserClickedDeleteSelectedHistoryItems : Action()

View File

@ -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.OnItemLongClick
import org.kiwix.kiwixmobile.core.history.viewmodel.Action.ToggleShowHistoryFromAllBooks 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.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.UserClickedDeleteButton
import org.kiwix.kiwixmobile.core.history.viewmodel.Action.UserClickedDeleteSelectedHistoryItems import org.kiwix.kiwixmobile.core.history.viewmodel.Action.UserClickedDeleteSelectedHistoryItems
import org.kiwix.kiwixmobile.core.history.viewmodel.State.Results import org.kiwix.kiwixmobile.core.history.viewmodel.State.Results
@ -34,7 +34,7 @@ class HistoryViewModel @Inject constructor(
private val sharedPreferenceUtil: SharedPreferenceUtil private val sharedPreferenceUtil: SharedPreferenceUtil
) : ViewModel() { ) : ViewModel() {
val state = MutableLiveData<State>().apply { val state = MutableLiveData<State>().apply {
value = Results(emptyList(), sharedPreferenceUtil.showHistoryAllBooks) value = Results(emptyList(), sharedPreferenceUtil.showHistoryAllBooks, zimReaderContainer.id)
} }
val effects = PublishProcessor.create<SideEffect<*>>() val effects = PublishProcessor.create<SideEffect<*>>()
val actions = PublishProcessor.create<Action>() val actions = PublishProcessor.create<Action>()
@ -57,7 +57,7 @@ class HistoryViewModel @Inject constructor(
when (action) { when (action) {
ExitHistory -> finishHistoryActivity(state) ExitHistory -> finishHistoryActivity(state)
ExitActionModeMenu -> deselectAllHistoryItems(state) ExitActionModeMenu -> deselectAllHistoryItems(state)
UserClickedDelete -> offerDeletionOfItems(state) UserClickedConfirmDelete -> offerDeletionOfItems(state)
UserClickedDeleteButton -> offerShowDeleteDialog(state) UserClickedDeleteButton -> offerShowDeleteDialog(state)
is OnItemClick -> handleItemClick(state, action) is OnItemClick -> handleItemClick(state, action)
is OnItemLongClick -> handleItemLongClick(state, action) is OnItemLongClick -> handleItemLongClick(state, action)
@ -87,16 +87,9 @@ class HistoryViewModel @Inject constructor(
private fun updateHistoryList( private fun updateHistoryList(
state: State, state: State,
action: UpdateHistory action: UpdateHistory
): State { ): State = when (state) {
return when (state) {
is Results -> state.copy(historyItems = action.history) is Results -> state.copy(historyItems = action.history)
is SelectionResults -> { is SelectionResults -> Results(action.history, state.showAll, zimReaderContainer.id)
val selectedItems = state.selectedItems
state.copy(historyItems = action.history.map {
if (selectedItems.contains(it)) it.copy(isSelected = true) else it
})
}
}
} }
private fun offerUpdateToShowAllToggle( private fun offerUpdateToShowAllToggle(
@ -156,7 +149,11 @@ class HistoryViewModel @Inject constructor(
private fun deselectAllHistoryItems(state: State): State { private fun deselectAllHistoryItems(state: State): State {
return when (state) { return when (state) {
is SelectionResults -> { 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 else -> state
} }

View File

@ -28,6 +28,7 @@ import java.util.Locale
sealed class State( sealed class State(
open val historyItems: List<HistoryItem>, open val historyItems: List<HistoryItem>,
open val showAll: Boolean, open val showAll: Boolean,
open val currentZimId: String?,
open val searchTerm: String = "" open val searchTerm: String = ""
) { ) {
@ -35,7 +36,12 @@ sealed class State(
val historyListItems: List<HistoryListItem> by lazy { val historyListItems: List<HistoryListItem> by lazy {
HeaderizableList<HistoryListItem, HistoryItem, DateItem>(historyItems HeaderizableList<HistoryListItem, HistoryItem, DateItem>(historyItems
.filter { it.historyTitle.contains(searchTerm, true) } .filter {
it.historyTitle.contains(
searchTerm,
true
) && (it.zimId == currentZimId || showAll)
}
.sortedByDescending { dateFormatter.parse(it.dateString) }) .sortedByDescending { dateFormatter.parse(it.dateString) })
.foldOverAddingHeaders( .foldOverAddingHeaders(
{ historyItem -> DateItem(historyItem.dateString) }, { historyItem -> DateItem(historyItem.dateString) },
@ -49,10 +55,10 @@ sealed class State(
isSelected = !isSelected isSelected = !isSelected
} else it } else it
} }
if (newList.isEmpty()) { if (newList.none(HistoryItem::isSelected)) {
return Results(newList, showAll) return Results(newList, showAll, currentZimId)
} }
return SelectionResults(newList, showAll, searchTerm) return SelectionResults(newList, showAll, currentZimId, searchTerm)
} }
// fun filterBasedOnSearchTerm(searchTerm: String): State = // fun filterBasedOnSearchTerm(searchTerm: String): State =
@ -66,15 +72,16 @@ sealed class State(
data class Results( data class Results(
override val historyItems: List<HistoryItem>, override val historyItems: List<HistoryItem>,
override val showAll: Boolean, override val showAll: Boolean,
override val currentZimId: String?,
override val searchTerm: String = "" override val searchTerm: String = ""
) : State(historyItems, showAll, searchTerm) ) : State(historyItems, showAll, currentZimId, searchTerm)
data class SelectionResults( data class SelectionResults(
override val historyItems: List<HistoryItem>, override val historyItems: List<HistoryItem>,
override val showAll: Boolean, override val showAll: Boolean,
override val currentZimId: String?,
override val searchTerm: String override val searchTerm: String
) : State(historyItems, showAll, searchTerm) { ) : State(historyItems, showAll, currentZimId, searchTerm) {
val selectedItems: List<HistoryItem> = val selectedItems: List<HistoryItem> =
historyListItems.filterIsInstance<HistoryItem>().filter(HistoryItem::isSelected) historyListItems.filterIsInstance<HistoryItem>().filter(HistoryItem::isSelected)
} }

View File

@ -23,7 +23,7 @@ import io.reactivex.processors.PublishProcessor
import org.kiwix.kiwixmobile.core.base.SideEffect import org.kiwix.kiwixmobile.core.base.SideEffect
import org.kiwix.kiwixmobile.core.history.HistoryActivity import org.kiwix.kiwixmobile.core.history.HistoryActivity
import org.kiwix.kiwixmobile.core.history.viewmodel.Action 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.DialogShower
import org.kiwix.kiwixmobile.core.utils.KiwixDialog.DeleteAllHistory import org.kiwix.kiwixmobile.core.utils.KiwixDialog.DeleteAllHistory
import javax.inject.Inject import javax.inject.Inject
@ -35,7 +35,7 @@ data class ShowDeleteHistoryDialog(
override fun invokeWith(activity: AppCompatActivity) { override fun invokeWith(activity: AppCompatActivity) {
(activity as HistoryActivity).activityComponent.inject(this) (activity as HistoryActivity).activityComponent.inject(this)
dialogShower.show(DeleteAllHistory, { dialogShower.show(DeleteAllHistory, {
actions.offer(UserClickedDelete) actions.offer(UserClickedConfirmDelete)
}) })
} }
} }

View File

@ -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.OnItemClick
import org.kiwix.kiwixmobile.core.history.viewmodel.Action.OnItemLongClick 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.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.UserClickedDeleteButton
import org.kiwix.kiwixmobile.core.history.viewmodel.Action.UserClickedDeleteSelectedHistoryItems import org.kiwix.kiwixmobile.core.history.viewmodel.Action.UserClickedDeleteSelectedHistoryItems
import org.kiwix.kiwixmobile.core.history.viewmodel.State.Results import org.kiwix.kiwixmobile.core.history.viewmodel.State.Results
@ -458,7 +458,7 @@ internal class HistoryViewModelTest {
@Test @Test
fun `DeleteHistoryItems calls DeleteSelectedOrAllHistoryItems side effect`() { fun `DeleteHistoryItems calls DeleteSelectedOrAllHistoryItems side effect`() {
actionResultsInEffects( actionResultsInEffects(
UserClickedDelete, UserClickedConfirmDelete,
DeleteHistoryItems(viewModel.state.value!!.historyItems, historyDao) DeleteHistoryItems(viewModel.state.value!!.historyItems, historyDao)
) )
} }

View File

@ -8,7 +8,7 @@ import io.reactivex.processors.PublishProcessor
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
import org.kiwix.kiwixmobile.core.history.HistoryActivity import org.kiwix.kiwixmobile.core.history.HistoryActivity
import org.kiwix.kiwixmobile.core.history.viewmodel.Action 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.DialogShower
import org.kiwix.kiwixmobile.core.utils.KiwixDialog.DeleteSelectedHistory import org.kiwix.kiwixmobile.core.utils.KiwixDialog.DeleteSelectedHistory
@ -30,6 +30,6 @@ internal class ShowDeleteHistoryDialogTest {
dialogShower.show(DeleteSelectedHistory, capture(lambdaSlot)) dialogShower.show(DeleteSelectedHistory, capture(lambdaSlot))
} }
lambdaSlot.captured.invoke() lambdaSlot.captured.invoke()
verify { actions.offer(UserClickedDelete) } verify { actions.offer(UserClickedConfirmDelete) }
} }
} }