diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/dao/HistoryDao.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/dao/HistoryDao.kt index 9ca034f47..7dcb07d00 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/dao/HistoryDao.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/dao/HistoryDao.kt @@ -22,12 +22,18 @@ import io.objectbox.kotlin.query import org.kiwix.kiwixmobile.core.dao.entities.HistoryEntity import org.kiwix.kiwixmobile.core.dao.entities.HistoryEntity_ import org.kiwix.kiwixmobile.core.history.adapter.HistoryListItem.HistoryItem +import java.text.SimpleDateFormat +import java.util.Locale import javax.inject.Inject class HistoryDao @Inject constructor(val box: Box) { + private val dateFormatter = SimpleDateFormat("d MMM yyyy", Locale.getDefault()) fun history() = box.asFlowable() - .map { it.map(::HistoryItem) } + .map { mutableList -> + mutableList.map(::HistoryItem) + .sortedByDescending { historyItem -> dateFormatter.parse(historyItem.dateString) } + } fun saveHistory(historyItem: HistoryItem) { box.store.callInTx { 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 979d6f949..1b84a66fe 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 @@ -3,7 +3,8 @@ package org.kiwix.kiwixmobile.core.history import android.os.Bundle import android.view.Menu import android.view.MenuItem -import android.view.View +import android.view.View.GONE +import android.view.View.VISIBLE import android.widget.ImageView import androidx.appcompat.view.ActionMode import androidx.appcompat.view.ActionMode.Callback @@ -127,24 +128,15 @@ class HistoryActivity : OnItemClickListener, BaseActivity() { } private fun render(state: State) { - historyAdapter.items = state.getHistoryListItems() - if (!state.isInSelectionState) { - actionMode?.finish() - history_switch.isEnabled = true - } else { + historyAdapter.items = state.historyListItems + history_switch.isEnabled = !state.isInSelectionState + no_history.visibility = if (state.historyListItems.isEmpty()) VISIBLE else GONE + if (state.isInSelectionState) { if (actionMode == null) { actionMode = startSupportActionMode(actionModeCallback) } - history_switch.isEnabled = false - } - toggleNoHistoryText(state) - } - - private fun toggleNoHistoryText(state: State) { - if (state.getHistoryListItems().isEmpty()) { - no_history.visibility = View.VISIBLE } else { - no_history.visibility = View.GONE + actionMode?.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 c884cf996..8db85b92a 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 @@ -22,8 +22,6 @@ import org.kiwix.kiwixmobile.core.extensions.HeaderizableList import org.kiwix.kiwixmobile.core.history.adapter.HistoryListItem import org.kiwix.kiwixmobile.core.history.adapter.HistoryListItem.DateItem import org.kiwix.kiwixmobile.core.history.adapter.HistoryListItem.HistoryItem -import java.text.SimpleDateFormat -import java.util.Locale data class State( val historyItems: List, @@ -32,21 +30,18 @@ data class State( val searchTerm: String = "" ) { val isInSelectionState = historyItems.any(HistoryItem::isSelected) - private val dateFormatter = SimpleDateFormat("d MMM yyyy", Locale.getDefault()) - fun getHistoryListItems(): List = + val historyListItems: List = HeaderizableList(historyItems .filter { it.historyTitle.contains( searchTerm, true ) && (it.zimId == currentZimId || showAll) - } - .sortedByDescending { dateFormatter.parse(it.dateString) }) - .foldOverAddingHeaders( - { historyItem -> DateItem(historyItem.dateString) }, - { current, next -> current.dateString != next.dateString } - ) + }).foldOverAddingHeaders( + { historyItem -> DateItem(historyItem.dateString) }, + { current, next -> current.dateString != next.dateString } + ) fun toggleSelectionOfItem(historyListItem: HistoryItem): State { val newList = historyItems.map { 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 368211548..001acf10a 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 @@ -197,7 +197,7 @@ internal class HistoryViewModelTest { ) assertEquals( State(listOf(item3, item1, item2), true, "id", "") - .getHistoryListItems(), + .historyListItems, listOf(date3, item3, date1, item1, date2, item2) ) } @@ -223,9 +223,9 @@ internal class HistoryViewModelTest { databaseResults = listOf(item2, item3, item1) ) assertEquals( - State(listOf(item3, item1, item2), true, "id", "") - .getHistoryListItems(), - listOf(date1, item1, item2, date3, item3) + listOf(date1, item1, item2, date3, item3), + State(listOf(item1, item2, item3), true, "id", "") + .historyListItems ) } @@ -298,7 +298,7 @@ internal class HistoryViewModelTest { ) ) ) - assertEquals(emptyList(), viewModel.state.value?.getHistoryListItems()) + assertEquals(emptyList(), viewModel.state.value?.historyListItems) // resultsIn(Results(emptyList(), true, "id", "a")) } @@ -312,7 +312,7 @@ internal class HistoryViewModelTest { databaseResults = listOf(item1, item2) ) viewModel.actions.offer(UserClickedShowAllToggle(true)) - assertEquals(listOf(date, item1, item2), viewModel.state.value?.getHistoryListItems()) + assertEquals(listOf(date, item1, item2), viewModel.state.value?.historyListItems) } @Test @@ -325,7 +325,7 @@ internal class HistoryViewModelTest { databaseResults = listOf(item1, item2) ) viewModel.actions.offer(UserClickedShowAllToggle(false)) - assertEquals(listOf(date, item1), viewModel.state.value?.getHistoryListItems()) + assertEquals(listOf(date, item1), viewModel.state.value?.historyListItems) } @Test @@ -336,7 +336,7 @@ internal class HistoryViewModelTest { searchTerm = "title_in_caps", databaseResults = listOf(item1) ) - assertEquals(listOf(date, item1), viewModel.state.value?.getHistoryListItems()) + assertEquals(listOf(date, item1), viewModel.state.value?.historyListItems) } }