#2119 added requested fixes

This commit is contained in:
Frans-Lukas Lövenvald 2020-06-10 14:34:18 +02:00
parent 6e9510e4d4
commit 5fb71c009e
4 changed files with 27 additions and 34 deletions

View File

@ -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.dao.entities.HistoryEntity_ import org.kiwix.kiwixmobile.core.dao.entities.HistoryEntity_
import org.kiwix.kiwixmobile.core.history.adapter.HistoryListItem.HistoryItem import org.kiwix.kiwixmobile.core.history.adapter.HistoryListItem.HistoryItem
import java.text.SimpleDateFormat
import java.util.Locale
import javax.inject.Inject import javax.inject.Inject
class HistoryDao @Inject constructor(val box: Box<HistoryEntity>) { class HistoryDao @Inject constructor(val box: Box<HistoryEntity>) {
private val dateFormatter = SimpleDateFormat("d MMM yyyy", Locale.getDefault())
fun history() = box.asFlowable() fun history() = box.asFlowable()
.map { it.map(::HistoryItem) } .map { mutableList ->
mutableList.map(::HistoryItem)
.sortedByDescending { historyItem -> dateFormatter.parse(historyItem.dateString) }
}
fun saveHistory(historyItem: HistoryItem) { fun saveHistory(historyItem: HistoryItem) {
box.store.callInTx { box.store.callInTx {

View File

@ -3,7 +3,8 @@ package org.kiwix.kiwixmobile.core.history
import android.os.Bundle import android.os.Bundle
import android.view.Menu import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import android.view.View import android.view.View.GONE
import android.view.View.VISIBLE
import android.widget.ImageView import android.widget.ImageView
import androidx.appcompat.view.ActionMode import androidx.appcompat.view.ActionMode
import androidx.appcompat.view.ActionMode.Callback import androidx.appcompat.view.ActionMode.Callback
@ -127,24 +128,15 @@ class HistoryActivity : OnItemClickListener, BaseActivity() {
} }
private fun render(state: State) { private fun render(state: State) {
historyAdapter.items = state.getHistoryListItems() historyAdapter.items = state.historyListItems
if (!state.isInSelectionState) { history_switch.isEnabled = !state.isInSelectionState
actionMode?.finish() no_history.visibility = if (state.historyListItems.isEmpty()) VISIBLE else GONE
history_switch.isEnabled = true if (state.isInSelectionState) {
} else {
if (actionMode == null) { if (actionMode == null) {
actionMode = startSupportActionMode(actionModeCallback) actionMode = startSupportActionMode(actionModeCallback)
} }
history_switch.isEnabled = false
}
toggleNoHistoryText(state)
}
private fun toggleNoHistoryText(state: State) {
if (state.getHistoryListItems().isEmpty()) {
no_history.visibility = View.VISIBLE
} else { } else {
no_history.visibility = View.GONE actionMode?.finish()
} }
} }

View File

@ -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
import org.kiwix.kiwixmobile.core.history.adapter.HistoryListItem.DateItem import org.kiwix.kiwixmobile.core.history.adapter.HistoryListItem.DateItem
import org.kiwix.kiwixmobile.core.history.adapter.HistoryListItem.HistoryItem import org.kiwix.kiwixmobile.core.history.adapter.HistoryListItem.HistoryItem
import java.text.SimpleDateFormat
import java.util.Locale
data class State( data class State(
val historyItems: List<HistoryItem>, val historyItems: List<HistoryItem>,
@ -32,21 +30,18 @@ data class State(
val searchTerm: String = "" val searchTerm: String = ""
) { ) {
val isInSelectionState = historyItems.any(HistoryItem::isSelected) 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 HeaderizableList<HistoryListItem, HistoryItem, DateItem>(historyItems
.filter { .filter {
it.historyTitle.contains( it.historyTitle.contains(
searchTerm, searchTerm,
true true
) && (it.zimId == currentZimId || showAll) ) && (it.zimId == currentZimId || showAll)
} }).foldOverAddingHeaders(
.sortedByDescending { dateFormatter.parse(it.dateString) }) { historyItem -> DateItem(historyItem.dateString) },
.foldOverAddingHeaders( { current, next -> current.dateString != next.dateString }
{ historyItem -> DateItem(historyItem.dateString) }, )
{ current, next -> current.dateString != next.dateString }
)
fun toggleSelectionOfItem(historyListItem: HistoryItem): State { fun toggleSelectionOfItem(historyListItem: HistoryItem): State {
val newList = historyItems.map { val newList = historyItems.map {

View File

@ -197,7 +197,7 @@ internal class HistoryViewModelTest {
) )
assertEquals( assertEquals(
State(listOf(item3, item1, item2), true, "id", "") State(listOf(item3, item1, item2), true, "id", "")
.getHistoryListItems(), .historyListItems,
listOf(date3, item3, date1, item1, date2, item2) listOf(date3, item3, date1, item1, date2, item2)
) )
} }
@ -223,9 +223,9 @@ internal class HistoryViewModelTest {
databaseResults = listOf(item2, item3, item1) databaseResults = listOf(item2, item3, item1)
) )
assertEquals( assertEquals(
State(listOf(item3, item1, item2), true, "id", "") listOf(date1, item1, item2, date3, item3),
.getHistoryListItems(), State(listOf(item1, item2, item3), true, "id", "")
listOf(date1, item1, item2, date3, item3) .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")) // resultsIn(Results(emptyList(), true, "id", "a"))
} }
@ -312,7 +312,7 @@ internal class HistoryViewModelTest {
databaseResults = listOf(item1, item2) databaseResults = listOf(item1, item2)
) )
viewModel.actions.offer(UserClickedShowAllToggle(true)) viewModel.actions.offer(UserClickedShowAllToggle(true))
assertEquals(listOf(date, item1, item2), viewModel.state.value?.getHistoryListItems()) assertEquals(listOf(date, item1, item2), viewModel.state.value?.historyListItems)
} }
@Test @Test
@ -325,7 +325,7 @@ internal class HistoryViewModelTest {
databaseResults = listOf(item1, item2) databaseResults = listOf(item1, item2)
) )
viewModel.actions.offer(UserClickedShowAllToggle(false)) viewModel.actions.offer(UserClickedShowAllToggle(false))
assertEquals(listOf(date, item1), viewModel.state.value?.getHistoryListItems()) assertEquals(listOf(date, item1), viewModel.state.value?.historyListItems)
} }
@Test @Test
@ -336,7 +336,7 @@ internal class HistoryViewModelTest {
searchTerm = "title_in_caps", searchTerm = "title_in_caps",
databaseResults = listOf(item1) databaseResults = listOf(item1)
) )
assertEquals(listOf(date, item1), viewModel.state.value?.getHistoryListItems()) assertEquals(listOf(date, item1), viewModel.state.value?.historyListItems)
} }
} }