#1972 Fixed change requests regarding disposable and string comparison.

This commit is contained in:
Frans-Lukas 2020-04-15 11:43:20 +02:00
parent 6cd4816939
commit 545e4e9370

View File

@ -20,6 +20,7 @@ package org.kiwix.kiwixmobile.core.history
import android.util.Log import android.util.Log
import io.reactivex.Observable import io.reactivex.Observable
import io.reactivex.Scheduler import io.reactivex.Scheduler
import io.reactivex.disposables.Disposable
import org.kiwix.kiwixmobile.core.base.BasePresenter import org.kiwix.kiwixmobile.core.base.BasePresenter
import org.kiwix.kiwixmobile.core.data.DataSource import org.kiwix.kiwixmobile.core.data.DataSource
import org.kiwix.kiwixmobile.core.di.qualifiers.Computation import org.kiwix.kiwixmobile.core.di.qualifiers.Computation
@ -27,7 +28,6 @@ import org.kiwix.kiwixmobile.core.di.qualifiers.MainThread
import org.kiwix.kiwixmobile.core.history.HistoryContract.Presenter import org.kiwix.kiwixmobile.core.history.HistoryContract.Presenter
import org.kiwix.kiwixmobile.core.history.HistoryContract.View import org.kiwix.kiwixmobile.core.history.HistoryContract.View
import org.kiwix.kiwixmobile.core.history.HistoryListItem.HistoryItem import org.kiwix.kiwixmobile.core.history.HistoryListItem.HistoryItem
import java.util.Locale
import javax.inject.Inject import javax.inject.Inject
internal class HistoryPresenter @Inject constructor( internal class HistoryPresenter @Inject constructor(
@ -35,24 +35,27 @@ internal class HistoryPresenter @Inject constructor(
@param:MainThread private val mainThread: Scheduler, @param:MainThread private val mainThread: Scheduler,
@param:Computation private val computation: Scheduler @param:Computation private val computation: Scheduler
) : BasePresenter<View>(), Presenter { ) : BasePresenter<View>(), Presenter {
private var disposable: Disposable? = null
override fun loadHistory(showHistoryCurrentBook: Boolean) { override fun loadHistory(showHistoryCurrentBook: Boolean) {
disposable?.takeIf { !it.isDisposed }?.dispose()
dataSource.getDateCategorizedHistory(showHistoryCurrentBook) dataSource.getDateCategorizedHistory(showHistoryCurrentBook)
.subscribe( .subscribe(
{ histories: List<HistoryListItem> -> view?.updateHistoryList(histories) }, { histories: List<HistoryListItem> -> view?.updateHistoryList(histories) },
{ e: Throwable -> Log.e("HistoryPresenter", "Failed to load history.", e) } { e: Throwable -> Log.e("HistoryPresenter", "Failed to load history.", e) }
).let { ).let {
it.takeIf { !it.isDisposed }?.dispose()
compositeDisposable.add(it) compositeDisposable.add(it)
disposable = it
} }
} }
override fun filterHistory(historyList: List<HistoryListItem>, newText: String) { override fun filterHistory(historyList: List<HistoryListItem>, newText: String) {
compositeDisposable.add(Observable.fromCallable { compositeDisposable.add(Observable.fromCallable {
historyList.filterIsInstance<HistoryItem>().filter { item -> historyList
item.historyTitle.toLowerCase(Locale.getDefault()) .filterIsInstance<HistoryItem>().filter { item ->
.contains(newText.toLowerCase(Locale.getDefault())) item.historyTitle.contains(newText, true)
} } }
}
.subscribeOn(computation) .subscribeOn(computation)
.observeOn(mainThread) .observeOn(mainThread)
.subscribe( .subscribe(