mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-16 10:56:50 -04:00
#2119 added requested fixes
This commit is contained in:
parent
6e9510e4d4
commit
5fb71c009e
@ -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<HistoryEntity>) {
|
||||
|
||||
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 {
|
||||
|
@ -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()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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<HistoryItem>,
|
||||
@ -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<HistoryListItem> =
|
||||
val historyListItems: List<HistoryListItem> =
|
||||
HeaderizableList<HistoryListItem, HistoryItem, DateItem>(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 {
|
||||
|
@ -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<HistoryListItem>(), viewModel.state.value?.getHistoryListItems())
|
||||
assertEquals(emptyList<HistoryListItem>(), 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)
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user