#1990 inlined headerizable list class and updated foldOverAddingHeaders usage

This commit is contained in:
Frans-Lukas 2020-05-27 09:48:17 +02:00
parent c4e39d1bc2
commit f016f78af7
8 changed files with 51 additions and 43 deletions

View File

@ -31,7 +31,6 @@ import org.kiwix.kiwixmobile.core.dao.NewRecentSearchDao
import org.kiwix.kiwixmobile.core.di.qualifiers.IO
import org.kiwix.kiwixmobile.core.di.qualifiers.MainThread
import org.kiwix.kiwixmobile.core.extensions.HeaderizableList
import org.kiwix.kiwixmobile.core.extensions.foldOverAddingHeaders
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
@ -68,9 +67,10 @@ class Repository @Inject internal constructor(
override fun booksOnDiskAsListItems(): Flowable<List<BooksOnDiskListItem>> = bookDao.books()
.map { it.sortedBy { bookOnDisk -> bookOnDisk.book.language + bookOnDisk.book.title } }
.map {
(it as HeaderizableList).foldOverAddingHeaders(
{ bookOnDisk -> LanguageItem(bookOnDisk.locale) },
{ current, next -> current.locale.displayName != next.locale.displayName })
(HeaderizableList(it as List<BooksOnDiskListItem>)).foldOverAddingHeaders(
{ bookOnDisk -> LanguageItem((bookOnDisk as BookOnDisk).locale) },
{ current, next ->
(current as BookOnDisk).locale.displayName != (next as BookOnDisk).locale.displayName })
}
.map { it.toList() }
@ -92,11 +92,11 @@ class Repository @Inject internal constructor(
showHistoryCurrentBook,
zimReaderContainer.zimCanonicalPath
)
)
.map {
(it as HeaderizableList).foldOverAddingHeaders(
{ historyItem -> DateItem(historyItem.dateString) },
{ current, next -> current.dateString != next.dateString })
).map {
(HeaderizableList<HistoryListItem>(it)).foldOverAddingHeaders(
{ historyItem -> DateItem((historyItem as HistoryItem).dateString) },
{ current, next ->
(current as HistoryItem).dateString != (next as HistoryItem).dateString })
}
.subscribeOn(io)
.observeOn(mainThread)

View File

@ -0,0 +1,21 @@
package org.kiwix.kiwixmobile.core.extensions
inline class HeaderizableList<T>(val list: List<T>) {
fun <HEADER : T> foldOverAddingHeaders(
headerConstructor: (T) -> HEADER,
criteriaToAddHeader: (T, T) -> Boolean
): MutableList<T> =
list.foldIndexed(mutableListOf(), { index, acc, currentItem ->
if (index == 0) {
acc.add(headerConstructor.invoke(currentItem))
}
acc.add(currentItem)
if (index < list.size - 1) {
val nextItem = list.get(index + 1)
if (criteriaToAddHeader.invoke(currentItem, nextItem)) {
acc.add(headerConstructor.invoke(nextItem))
}
}
acc
})
}

View File

@ -1,21 +1,21 @@
package org.kiwix.kiwixmobile.core.extensions
typealias HeaderizableList<T> = List<T>
fun <SUPERTYPE, ITEM : SUPERTYPE, HEADER : SUPERTYPE> HeaderizableList<ITEM>.foldOverAddingHeaders(
headerConstructor: (ITEM) -> HEADER,
criteriaToAddHeader: (ITEM, ITEM) -> Boolean
): MutableList<SUPERTYPE> =
foldIndexed(mutableListOf(), { index, acc, currentItem ->
if (index == 0) {
acc.add(headerConstructor.invoke(currentItem))
}
acc.add(currentItem)
if (index < size - 1) {
val nextItem = get(index + 1)
if (criteriaToAddHeader.invoke(currentItem, nextItem)) {
acc.add(headerConstructor.invoke(nextItem))
inline class HeaderizableList<T>(val list: List<T>) {
fun <HEADER : T> foldOverAddingHeaders(
headerConstructor: (T) -> HEADER,
criteriaToAddHeader: (T, T) -> Boolean
): MutableList<T> =
list.foldIndexed(mutableListOf(), { index, acc, currentItem ->
if (index == 0) {
acc.add(headerConstructor.invoke(currentItem))
}
}
acc
})
acc.add(currentItem)
if (index < list.size - 1) {
val nextItem = list.get(index + 1)
if (criteriaToAddHeader.invoke(currentItem, nextItem)) {
acc.add(headerConstructor.invoke(nextItem))
}
}
acc
})
}

View File

@ -41,7 +41,6 @@ import org.kiwix.kiwixmobile.core.history.viewmodel.State
import org.kiwix.kiwixmobile.core.history.viewmodel.State.NoResults
import org.kiwix.kiwixmobile.core.history.viewmodel.State.Results
import org.kiwix.kiwixmobile.core.history.viewmodel.State.SelectionResults
import org.kiwix.kiwixmobile.core.utils.DialogShower
import org.kiwix.kiwixmobile.core.utils.SimpleTextListener
import javax.inject.Inject
@ -54,7 +53,6 @@ class HistoryActivity : OnItemClickListener, BaseActivity() {
private val historyViewModel by lazy { viewModel<HistoryViewModel>(viewModelFactory) }
private val compositeDisposable = CompositeDisposable()
private val actionModeCallback: Callback =
object : Callback {
override fun onCreateActionMode(mode: ActionMode, menu: Menu): Boolean {

View File

@ -20,7 +20,6 @@ package org.kiwix.kiwixmobile.core.history.viewmodel
import org.kiwix.kiwixmobile.core.history.adapter.HistoryListItem
import org.kiwix.kiwixmobile.core.history.adapter.HistoryListItem.HistoryItem
import org.kiwix.kiwixmobile.core.utils.DialogShower
sealed class Action {
object ExitHistory : Action()

View File

@ -14,7 +14,6 @@ import io.reactivex.processors.PublishProcessor
import org.kiwix.kiwixmobile.core.base.SideEffect
import org.kiwix.kiwixmobile.core.dao.HistoryDao
import org.kiwix.kiwixmobile.core.extensions.HeaderizableList
import org.kiwix.kiwixmobile.core.extensions.foldOverAddingHeaders
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
@ -104,13 +103,13 @@ class HistoryViewModel @Inject constructor(
private fun searchResults(
searchString: String,
showAllToggle: Boolean,
historyList: HeaderizableList<HistoryListItem>
): List<HistoryListItem> = (historyList
historyList: List<HistoryListItem>
): List<HistoryListItem> = HeaderizableList<HistoryListItem>(historyList
.filterIsInstance<HistoryItem>()
.filter { h ->
h.historyTitle.contains(searchString, true) &&
(h.zimName == zimReaderContainer.name || showAllToggle)
} as HeaderizableList<HistoryListItem>).foldOverAddingHeaders(
}).foldOverAddingHeaders(
{ historyItem -> DateItem((historyItem as HistoryItem).dateString) },
{ current, next -> (current as HistoryItem).dateString != (next as HistoryItem).dateString }
)

View File

@ -26,7 +26,6 @@ import org.kiwix.kiwixmobile.core.history.viewmodel.Action
import org.kiwix.kiwixmobile.core.history.viewmodel.Action.DeleteHistoryItems
import org.kiwix.kiwixmobile.core.utils.DialogShower
import org.kiwix.kiwixmobile.core.utils.KiwixDialog
import org.kiwix.kiwixmobile.core.utils.KiwixDialog.DeleteSelectedHistory
import javax.inject.Inject
data class ShowDeleteHistoryDialog(

View File

@ -1,16 +1,8 @@
package org.kiwix.kiwixmobile.core.history.viewmodel.effects
import android.app.Activity
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import io.reactivex.processors.BehaviorProcessor
import org.kiwix.kiwixmobile.core.Intents.internal
import org.kiwix.kiwixmobile.core.base.SideEffect
import org.kiwix.kiwixmobile.core.history.adapter.HistoryListItem.HistoryItem
import org.kiwix.kiwixmobile.core.main.CoreMainActivity
import org.kiwix.kiwixmobile.core.reader.ZimReaderContainer
import org.kiwix.kiwixmobile.core.utils.EXTRA_CHOSE_X_FILE
import org.kiwix.kiwixmobile.core.utils.EXTRA_CHOSE_X_URL
import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil
data class ToggleShowAllHistorySwitchAndSaveItsStateToPrefs(