#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 {
object ExitHistory : Action()
object ExitActionModeMenu : Action()
object UserClickedDelete : Action()
object UserClickedConfirmDelete : Action()
object UserClickedDeleteButton : 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.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<State>().apply {
value = Results(emptyList(), sharedPreferenceUtil.showHistoryAllBooks)
value = Results(emptyList(), sharedPreferenceUtil.showHistoryAllBooks, zimReaderContainer.id)
}
val effects = PublishProcessor.create<SideEffect<*>>()
val actions = PublishProcessor.create<Action>()
@ -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) {
): State = 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
})
}
}
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
}

View File

@ -28,6 +28,7 @@ import java.util.Locale
sealed class State(
open val historyItems: List<HistoryItem>,
open val showAll: Boolean,
open val currentZimId: String?,
open val searchTerm: String = ""
) {
@ -35,7 +36,12 @@ sealed class State(
val historyListItems: List<HistoryListItem> by lazy {
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) })
.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<HistoryItem>,
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<HistoryItem>,
override val showAll: Boolean,
override val currentZimId: String?,
override val searchTerm: String
) : State(historyItems, showAll, searchTerm) {
) : State(historyItems, showAll, currentZimId, searchTerm) {
val selectedItems: List<HistoryItem> =
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.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)
})
}
}

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.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)
)
}

View File

@ -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) }
}
}