mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-15 02:18:04 -04:00
Merge pull request #2006 from Frans-Lukas/1972-convert-history-presenter-to-kotlin
#1972 Convert HistoryPresenter to kotlin
This commit is contained in:
commit
918936eaca
@ -1,133 +0,0 @@
|
|||||||
/*
|
|
||||||
* Kiwix Android
|
|
||||||
* Copyright (c) 2019 Kiwix <android.kiwix.org>
|
|
||||||
* 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 <http://www.gnu.org/licenses/>.
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
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<HistoryContract.View>
|
|
||||||
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<List<HistoryListItem>>() {
|
|
||||||
@Override
|
|
||||||
public void onSubscribe(Disposable d) {
|
|
||||||
if (disposable != null && !disposable.isDisposed()) {
|
|
||||||
disposable.dispose();
|
|
||||||
}
|
|
||||||
disposable = d;
|
|
||||||
compositeDisposable.add(d);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onSuccess(List<HistoryListItem> histories) {
|
|
||||||
view.updateHistoryList(histories);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onError(Throwable e) {
|
|
||||||
Log.e("HistoryPresenter", e.toString());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void filterHistory(List<HistoryListItem> historyList, String newText) {
|
|
||||||
Observable.just(historyList)
|
|
||||||
.flatMapIterable(histories -> {
|
|
||||||
List<HistoryListItem> 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<List<HistoryListItem>>() {
|
|
||||||
@Override
|
|
||||||
public void onSubscribe(Disposable d) {
|
|
||||||
compositeDisposable.add(d);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onSuccess(List<HistoryListItem> historyList1) {
|
|
||||||
view.notifyHistoryListFiltered(historyList1);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onError(Throwable e) {
|
|
||||||
Log.e("HistoryPresenter", e.toString());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void deleteHistory(List<HistoryListItem> 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());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,72 @@
|
|||||||
|
/*
|
||||||
|
* Kiwix Android
|
||||||
|
* Copyright (c) 2019 Kiwix <android.kiwix.org>
|
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
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<View>(), Presenter {
|
||||||
|
private var disposable: Disposable? = null
|
||||||
|
|
||||||
|
override fun loadHistory(showHistoryCurrentBook: Boolean) {
|
||||||
|
disposable?.takeIf { !it.isDisposed }?.dispose()
|
||||||
|
dataSource.getDateCategorizedHistory(showHistoryCurrentBook)
|
||||||
|
.subscribe(
|
||||||
|
{ histories: List<HistoryListItem> -> view?.updateHistoryList(histories) },
|
||||||
|
{ e: Throwable -> Log.e("HistoryPresenter", "Failed to load history.", e) }
|
||||||
|
).let {
|
||||||
|
compositeDisposable.add(it)
|
||||||
|
disposable = it
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun filterHistory(historyList: List<HistoryListItem>, newText: String) {
|
||||||
|
compositeDisposable.add(Observable.fromCallable {
|
||||||
|
historyList
|
||||||
|
.filterIsInstance<HistoryItem>().filter { item ->
|
||||||
|
item.historyTitle.contains(newText, true)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.subscribeOn(computation)
|
||||||
|
.observeOn(mainThread)
|
||||||
|
.subscribe(
|
||||||
|
{ hList: List<HistoryListItem> -> view?.notifyHistoryListFiltered(hList) },
|
||||||
|
{ e: Throwable -> Log.e("HistoryPresenter", "Failed to filter history.", e) }
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun deleteHistory(deleteList: List<HistoryListItem>) {
|
||||||
|
dataSource.deleteHistory(deleteList).subscribe({}, { e: Throwable ->
|
||||||
|
Log.e("HistoryPresenter", "Failed to delete history.", e)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user