From 58ef0de3535f5250d9b00d878093b18d540accfd Mon Sep 17 00:00:00 2001 From: Frans-Lukas Date: Fri, 10 Apr 2020 14:59:08 +0200 Subject: [PATCH] #1972 switched to use lambda method calls --- .../core/history/HistoryPresenter.kt | 85 +++++++------------ 1 file changed, 30 insertions(+), 55 deletions(-) diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/history/HistoryPresenter.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/history/HistoryPresenter.kt index b925882f3..a376e44cc 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/history/HistoryPresenter.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/history/HistoryPresenter.kt @@ -18,10 +18,8 @@ package org.kiwix.kiwixmobile.core.history import android.util.Log -import io.reactivex.CompletableObserver import io.reactivex.Observable import io.reactivex.Scheduler -import io.reactivex.SingleObserver import io.reactivex.disposables.Disposable import org.kiwix.kiwixmobile.core.base.BasePresenter import org.kiwix.kiwixmobile.core.data.DataSource @@ -30,6 +28,7 @@ import org.kiwix.kiwixmobile.core.di.qualifiers.MainThread import org.kiwix.kiwixmobile.core.history.HistoryContract.Presenter import org.kiwix.kiwixmobile.core.history.HistoryContract.View import org.kiwix.kiwixmobile.core.history.HistoryListItem.HistoryItem +import java.util.Locale import javax.inject.Inject internal class HistoryPresenter @Inject constructor( @@ -37,64 +36,40 @@ internal class HistoryPresenter @Inject constructor( @param:MainThread private val mainThread: Scheduler, @param:Computation private val computation: Scheduler ) : BasePresenter(), Presenter { + private var disposable: Disposable? = null override fun loadHistory(showHistoryCurrentBook: Boolean) { - dataSource.getDateCategorizedHistory(showHistoryCurrentBook) - .subscribe(object : SingleObserver> { - override fun onSubscribe(d: Disposable) { - if (disposable != null && !disposable!!.isDisposed) { - disposable!!.dispose() - } - disposable = d - compositeDisposable.add(d) - } - - override fun onSuccess(histories: List) { - view?.updateHistoryList(histories) - } - - override fun onError(e: Throwable) { - Log.e("HistoryPresenter", e.toString()) - } - }) + val d = dataSource.getDateCategorizedHistory(showHistoryCurrentBook).subscribe( + { histories: List -> view?.updateHistoryList(histories) }, + { e: Throwable -> Log.e("HistoryPresenter", "Failed to load history.", e) } + ) + disposable?.takeIf { !it.isDisposed }?.let { it.dispose() } + disposable = d + compositeDisposable.add(d) } - override fun filterHistory( - historyList: List, - newText: String - ) = Observable.just(historyList) - .flatMapIterable { flatHistoryList: List -> - flatHistoryList.filter { item -> - item is HistoryItem - && item.historyTitle.toLowerCase() - .contains(newText.toLowerCase()) - } - } - .toList() - .subscribeOn(computation) - .observeOn(mainThread) - .subscribe(object : SingleObserver> { - override fun onSubscribe(d: Disposable) { - compositeDisposable.add(d) - } - - override fun onError(e: Throwable) { - Log.e("HistoryPresenter", e.toString()) - } - - override fun onSuccess(historyList: List) { - view?.notifyHistoryListFiltered(historyList) - } - }) - - override fun deleteHistory(deleteList: List) = - dataSource.deleteHistory(deleteList) - .subscribe(object : CompletableObserver { - override fun onSubscribe(d: Disposable) {} - override fun onComplete() {} - override fun onError(e: Throwable) { - Log.e("HistoryPresenter", e.toString()) + override fun filterHistory(historyList: List, newText: String) { + compositeDisposable.add(Observable.just(historyList) + .flatMapIterable { flatHistoryList: List -> + flatHistoryList.filter { item -> + item is HistoryItem && + item.historyTitle.toLowerCase(Locale.getDefault()) + .contains(newText.toLowerCase(Locale.getDefault())) } + } + .toList() + .subscribeOn(computation) + .observeOn(mainThread) + .subscribe( + { hList: List -> view?.notifyHistoryListFiltered(hList) }, + { e: Throwable -> Log.e("HistoryPresenter", "Failed to filter history", e) } + )) + } + + override fun deleteHistory(deleteList: List) { + dataSource.deleteHistory(deleteList).subscribe({}, { e: Throwable -> + Log.e("HistoryPresenter", "Failed to delete history", e) }) + } }