mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-17 03:16:27 -04:00
#2119 removed unnecessary states from history view model
This commit is contained in:
parent
18d5f9a787
commit
c0069c1a4c
@ -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()) {
|
||||
|
@ -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<HistoryItem>) : Action()
|
||||
}
|
||||
|
@ -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<State>().apply {
|
||||
value = Results(emptyList(), sharedPreferenceUtil.showHistoryAllBooks, zimReaderContainer.id)
|
||||
value = State(emptyList(), sharedPreferenceUtil.showHistoryAllBooks, zimReaderContainer.id)
|
||||
}
|
||||
val effects = PublishProcessor.create<SideEffect<*>>()
|
||||
val actions = PublishProcessor.create<Action>()
|
||||
@ -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)
|
||||
|
@ -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<HistoryItem>,
|
||||
open val showAll: Boolean,
|
||||
open val currentZimId: String?,
|
||||
open val searchTerm: String = ""
|
||||
data class State(
|
||||
val historyItems: List<HistoryItem>,
|
||||
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<HistoryListItem> =
|
||||
@ -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<HistoryItem>,
|
||||
override val showAll: Boolean,
|
||||
override val currentZimId: String?,
|
||||
override val searchTerm: String = ""
|
||||
) : 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, currentZimId, searchTerm) {
|
||||
val selectedItems: List<HistoryItem> =
|
||||
getHistoryListItems().filterIsInstance<HistoryItem>().filter(HistoryItem::isSelected)
|
||||
return State(newList, showAll, currentZimId, searchTerm)
|
||||
}
|
||||
}
|
||||
|
@ -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<HistoryItem>,
|
||||
private val state: State,
|
||||
private val historyDao: HistoryDao
|
||||
) : SideEffect<Unit> {
|
||||
override fun invokeWith(activity: AppCompatActivity) {
|
||||
historyDao.deleteHistory(itemsToDelete)
|
||||
if (state.isInSelectionState) {
|
||||
historyDao.deleteHistory(state.historyItems.filter(HistoryItem::isSelected))
|
||||
} else {
|
||||
historyDao.deleteHistory(state.historyItems)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -61,11 +61,6 @@ public class SharedPreferenceUtil {
|
||||
private SharedPreferences sharedPreferences;
|
||||
private final PublishProcessor<String> prefStorages = PublishProcessor.create();
|
||||
|
||||
public PublishProcessor<Boolean> getShowAllHistoryToggleSwitch() {
|
||||
return showAllHistoryToggleSwitch;
|
||||
}
|
||||
|
||||
private final PublishProcessor<Boolean> showAllHistoryToggleSwitch = PublishProcessor.create();
|
||||
private final PublishProcessor<NightModeConfig.Mode> nightModes = PublishProcessor.create();
|
||||
|
||||
@Inject
|
||||
@ -158,10 +153,6 @@ public class SharedPreferenceUtil {
|
||||
return prefStorages.startWith(getPrefStorage());
|
||||
}
|
||||
|
||||
public Flowable<Boolean> 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() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user