diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/history/HistoryPresenter.java b/core/src/main/java/org/kiwix/kiwixmobile/core/history/HistoryPresenter.java deleted file mode 100644 index fe66e6dd1..000000000 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/history/HistoryPresenter.java +++ /dev/null @@ -1,133 +0,0 @@ -/* - * Kiwix Android - * Copyright (c) 2019 Kiwix - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - * - */ - -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 java.util.ArrayList; -import java.util.List; -import javax.inject.Inject; -import org.kiwix.kiwixmobile.core.base.BasePresenter; -import org.kiwix.kiwixmobile.core.data.DataSource; -import org.kiwix.kiwixmobile.core.di.qualifiers.Computation; -import org.kiwix.kiwixmobile.core.di.qualifiers.MainThread; - -class HistoryPresenter extends BasePresenter - implements HistoryContract.Presenter { - - private final DataSource dataSource; - private final Scheduler mainThread; - private final Scheduler computation; - private Disposable disposable; - - @Inject HistoryPresenter(DataSource dataSource, @MainThread Scheduler mainThread, - @Computation Scheduler computation) { - this.dataSource = dataSource; - this.mainThread = mainThread; - this.computation = computation; - } - - @Override - public void loadHistory(boolean showHistoryCurrentBook) { - dataSource.getDateCategorizedHistory(showHistoryCurrentBook) - .subscribe(new SingleObserver>() { - @Override - public void onSubscribe(Disposable d) { - if (disposable != null && !disposable.isDisposed()) { - disposable.dispose(); - } - disposable = d; - compositeDisposable.add(d); - } - - @Override - public void onSuccess(List histories) { - view.updateHistoryList(histories); - } - - @Override - public void onError(Throwable e) { - Log.e("HistoryPresenter", e.toString()); - } - }); - } - - @Override - public void filterHistory(List historyList, String newText) { - Observable.just(historyList) - .flatMapIterable(histories -> { - List filteredHistories = new ArrayList<>(); - for (HistoryListItem historyListItem : histories) { - if (historyListItem instanceof HistoryListItem.HistoryItem) { - final HistoryListItem.HistoryItem historyItem = - (HistoryListItem.HistoryItem) historyListItem; - if (historyItem.getHistoryTitle().toLowerCase() - .contains(newText.toLowerCase())) { - filteredHistories.add(historyItem); - } - } - } - return filteredHistories; - }) - .toList() - .subscribeOn(computation) - .observeOn(mainThread) - .subscribe(new SingleObserver>() { - @Override - public void onSubscribe(Disposable d) { - compositeDisposable.add(d); - } - - @Override - public void onSuccess(List historyList1) { - view.notifyHistoryListFiltered(historyList1); - } - - @Override - public void onError(Throwable e) { - Log.e("HistoryPresenter", e.toString()); - } - }); - } - - @Override - public void deleteHistory(List deleteList) { - dataSource.deleteHistory(deleteList) - .subscribe(new CompletableObserver() { - @Override - public void onSubscribe(Disposable d) { - - } - - @Override - public void onComplete() { - - } - - @Override - public void onError(Throwable e) { - Log.e("HistoryPresenter", e.toString()); - } - }); - } -} 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 new file mode 100644 index 000000000..54b67d8f6 --- /dev/null +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/history/HistoryPresenter.kt @@ -0,0 +1,72 @@ +/* + * Kiwix Android + * Copyright (c) 2019 Kiwix + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ +package org.kiwix.kiwixmobile.core.history + +import android.util.Log +import io.reactivex.Observable +import io.reactivex.Scheduler +import io.reactivex.disposables.Disposable +import org.kiwix.kiwixmobile.core.base.BasePresenter +import org.kiwix.kiwixmobile.core.data.DataSource +import org.kiwix.kiwixmobile.core.di.qualifiers.Computation +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 javax.inject.Inject + +internal class HistoryPresenter @Inject constructor( + private val dataSource: DataSource, + @param:MainThread private val mainThread: Scheduler, + @param:Computation private val computation: Scheduler +) : BasePresenter(), Presenter { + private var disposable: Disposable? = null + + override fun loadHistory(showHistoryCurrentBook: Boolean) { + disposable?.takeIf { !it.isDisposed }?.dispose() + dataSource.getDateCategorizedHistory(showHistoryCurrentBook) + .subscribe( + { histories: List -> view?.updateHistoryList(histories) }, + { e: Throwable -> Log.e("HistoryPresenter", "Failed to load history.", e) } + ).let { + compositeDisposable.add(it) + disposable = it + } + } + + override fun filterHistory(historyList: List, newText: String) { + compositeDisposable.add(Observable.fromCallable { + historyList + .filterIsInstance().filter { item -> + item.historyTitle.contains(newText, true) + } + } + .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) + }) + } +}