#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.IO
import org.kiwix.kiwixmobile.core.di.qualifiers.MainThread import org.kiwix.kiwixmobile.core.di.qualifiers.MainThread
import org.kiwix.kiwixmobile.core.extensions.HeaderizableList 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
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
@ -68,9 +67,10 @@ class Repository @Inject internal constructor(
override fun booksOnDiskAsListItems(): Flowable<List<BooksOnDiskListItem>> = bookDao.books() override fun booksOnDiskAsListItems(): Flowable<List<BooksOnDiskListItem>> = bookDao.books()
.map { it.sortedBy { bookOnDisk -> bookOnDisk.book.language + bookOnDisk.book.title } } .map { it.sortedBy { bookOnDisk -> bookOnDisk.book.language + bookOnDisk.book.title } }
.map { .map {
(it as HeaderizableList).foldOverAddingHeaders( (HeaderizableList(it as List<BooksOnDiskListItem>)).foldOverAddingHeaders(
{ bookOnDisk -> LanguageItem(bookOnDisk.locale) }, { bookOnDisk -> LanguageItem((bookOnDisk as BookOnDisk).locale) },
{ current, next -> current.locale.displayName != next.locale.displayName }) { current, next ->
(current as BookOnDisk).locale.displayName != (next as BookOnDisk).locale.displayName })
} }
.map { it.toList() } .map { it.toList() }
@ -92,11 +92,11 @@ class Repository @Inject internal constructor(
showHistoryCurrentBook, showHistoryCurrentBook,
zimReaderContainer.zimCanonicalPath zimReaderContainer.zimCanonicalPath
) )
) ).map {
.map { (HeaderizableList<HistoryListItem>(it)).foldOverAddingHeaders(
(it as HeaderizableList).foldOverAddingHeaders( { historyItem -> DateItem((historyItem as HistoryItem).dateString) },
{ historyItem -> DateItem(historyItem.dateString) }, { current, next ->
{ current, next -> current.dateString != next.dateString }) (current as HistoryItem).dateString != (next as HistoryItem).dateString })
} }
.subscribeOn(io) .subscribeOn(io)
.observeOn(mainThread) .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 package org.kiwix.kiwixmobile.core.extensions
typealias HeaderizableList<T> = List<T> inline class HeaderizableList<T>(val list: List<T>) {
fun <HEADER : T> foldOverAddingHeaders(
fun <SUPERTYPE, ITEM : SUPERTYPE, HEADER : SUPERTYPE> HeaderizableList<ITEM>.foldOverAddingHeaders( headerConstructor: (T) -> HEADER,
headerConstructor: (ITEM) -> HEADER, criteriaToAddHeader: (T, T) -> Boolean
criteriaToAddHeader: (ITEM, ITEM) -> Boolean ): MutableList<T> =
): MutableList<SUPERTYPE> = list.foldIndexed(mutableListOf(), { index, acc, currentItem ->
foldIndexed(mutableListOf(), { index, acc, currentItem -> if (index == 0) {
if (index == 0) { acc.add(headerConstructor.invoke(currentItem))
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))
} }
} acc.add(currentItem)
acc 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.NoResults
import org.kiwix.kiwixmobile.core.history.viewmodel.State.Results 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.State.SelectionResults
import org.kiwix.kiwixmobile.core.utils.DialogShower
import org.kiwix.kiwixmobile.core.utils.SimpleTextListener import org.kiwix.kiwixmobile.core.utils.SimpleTextListener
import javax.inject.Inject import javax.inject.Inject
@ -54,7 +53,6 @@ class HistoryActivity : OnItemClickListener, BaseActivity() {
private val historyViewModel by lazy { viewModel<HistoryViewModel>(viewModelFactory) } private val historyViewModel by lazy { viewModel<HistoryViewModel>(viewModelFactory) }
private val compositeDisposable = CompositeDisposable() private val compositeDisposable = CompositeDisposable()
private val actionModeCallback: Callback = private val actionModeCallback: Callback =
object : Callback { object : Callback {
override fun onCreateActionMode(mode: ActionMode, menu: Menu): Boolean { 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
import org.kiwix.kiwixmobile.core.history.adapter.HistoryListItem.HistoryItem import org.kiwix.kiwixmobile.core.history.adapter.HistoryListItem.HistoryItem
import org.kiwix.kiwixmobile.core.utils.DialogShower
sealed class Action { sealed class Action {
object ExitHistory : 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.base.SideEffect
import org.kiwix.kiwixmobile.core.dao.HistoryDao import org.kiwix.kiwixmobile.core.dao.HistoryDao
import org.kiwix.kiwixmobile.core.extensions.HeaderizableList 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
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
@ -104,13 +103,13 @@ class HistoryViewModel @Inject constructor(
private fun searchResults( private fun searchResults(
searchString: String, searchString: String,
showAllToggle: Boolean, showAllToggle: Boolean,
historyList: HeaderizableList<HistoryListItem> historyList: List<HistoryListItem>
): List<HistoryListItem> = (historyList ): List<HistoryListItem> = HeaderizableList<HistoryListItem>(historyList
.filterIsInstance<HistoryItem>() .filterIsInstance<HistoryItem>()
.filter { h -> .filter { h ->
h.historyTitle.contains(searchString, true) && h.historyTitle.contains(searchString, true) &&
(h.zimName == zimReaderContainer.name || showAllToggle) (h.zimName == zimReaderContainer.name || showAllToggle)
} as HeaderizableList<HistoryListItem>).foldOverAddingHeaders( }).foldOverAddingHeaders(
{ historyItem -> DateItem((historyItem as HistoryItem).dateString) }, { historyItem -> DateItem((historyItem as HistoryItem).dateString) },
{ current, next -> (current as HistoryItem).dateString != (next 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.history.viewmodel.Action.DeleteHistoryItems
import org.kiwix.kiwixmobile.core.utils.DialogShower import org.kiwix.kiwixmobile.core.utils.DialogShower
import org.kiwix.kiwixmobile.core.utils.KiwixDialog import org.kiwix.kiwixmobile.core.utils.KiwixDialog
import org.kiwix.kiwixmobile.core.utils.KiwixDialog.DeleteSelectedHistory
import javax.inject.Inject import javax.inject.Inject
data class ShowDeleteHistoryDialog( data class ShowDeleteHistoryDialog(

View File

@ -1,16 +1,8 @@
package org.kiwix.kiwixmobile.core.history.viewmodel.effects package org.kiwix.kiwixmobile.core.history.viewmodel.effects
import android.app.Activity
import android.util.Log
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import io.reactivex.processors.BehaviorProcessor import io.reactivex.processors.BehaviorProcessor
import org.kiwix.kiwixmobile.core.Intents.internal
import org.kiwix.kiwixmobile.core.base.SideEffect 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 import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil
data class ToggleShowAllHistorySwitchAndSaveItsStateToPrefs( data class ToggleShowAllHistorySwitchAndSaveItsStateToPrefs(