From 722f4a76427de09aab7f2110468fb9a400cfeed7 Mon Sep 17 00:00:00 2001 From: Frans-Lukas Date: Sun, 5 Apr 2020 13:15:08 +0200 Subject: [PATCH 1/7] #1972 initial conversion of HistoryPresenter to kotlin. --- .../core/history/HistoryPresenter.java | 133 ------------------ .../core/history/HistoryPresenter.kt | 100 +++++++++++++ 2 files changed, 100 insertions(+), 133 deletions(-) delete mode 100644 core/src/main/java/org/kiwix/kiwixmobile/core/history/HistoryPresenter.java create mode 100644 core/src/main/java/org/kiwix/kiwixmobile/core/history/HistoryPresenter.kt 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..b925882f3 --- /dev/null +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/history/HistoryPresenter.kt @@ -0,0 +1,100 @@ +/* + * 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 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) { + 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()) + } + }) + } + + 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()) + } + }) +} From 58ef0de3535f5250d9b00d878093b18d540accfd Mon Sep 17 00:00:00 2001 From: Frans-Lukas Date: Fri, 10 Apr 2020 14:59:08 +0200 Subject: [PATCH 2/7] #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) }) + } } From 6d82885f3cfa32a90f008d4c3e5bb5d4024fd8dd Mon Sep 17 00:00:00 2001 From: Frans-Lukas Date: Fri, 10 Apr 2020 15:20:07 +0200 Subject: [PATCH 3/7] #1972 codecov fix --- .../java/org/kiwix/kiwixmobile/core/history/HistoryPresenter.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 a376e44cc..60f0b6d63 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 @@ -44,7 +44,7 @@ internal class HistoryPresenter @Inject constructor( { histories: List -> view?.updateHistoryList(histories) }, { e: Throwable -> Log.e("HistoryPresenter", "Failed to load history.", e) } ) - disposable?.takeIf { !it.isDisposed }?.let { it.dispose() } + disposable?.takeIf { !it.isDisposed }?.dispose() disposable = d compositeDisposable.add(d) } From 8aed396a594b24c86fef8591d3feab3edcb688d1 Mon Sep 17 00:00:00 2001 From: Frans-Lukas Date: Fri, 10 Apr 2020 16:09:14 +0200 Subject: [PATCH 4/7] #1972 implemented some change requests --- .../core/history/HistoryPresenter.kt | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 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 60f0b6d63..d26b22d4f 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 @@ -40,25 +40,20 @@ internal class HistoryPresenter @Inject constructor( private var disposable: Disposable? = null override fun loadHistory(showHistoryCurrentBook: Boolean) { - val d = dataSource.getDateCategorizedHistory(showHistoryCurrentBook).subscribe( + val immutableDisposable = dataSource.getDateCategorizedHistory(showHistoryCurrentBook).subscribe( { histories: List -> view?.updateHistoryList(histories) }, { e: Throwable -> Log.e("HistoryPresenter", "Failed to load history.", e) } ) disposable?.takeIf { !it.isDisposed }?.dispose() - disposable = d - compositeDisposable.add(d) + compositeDisposable.add(immutableDisposable) } 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() + compositeDisposable.add(Observable.fromCallable{ historyList.filter { item -> + item is HistoryItem && + item.historyTitle.toLowerCase(Locale.getDefault()) + .contains(newText.toLowerCase(Locale.getDefault())) + }} .subscribeOn(computation) .observeOn(mainThread) .subscribe( From da211ddf26a7a6b013f38d960e466edde3b7e443 Mon Sep 17 00:00:00 2001 From: Frans-Lukas Date: Fri, 10 Apr 2020 16:36:57 +0200 Subject: [PATCH 5/7] #1972 more change request fixes --- .../core/history/HistoryPresenter.kt | 23 +++++++++---------- 1 file changed, 11 insertions(+), 12 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 d26b22d4f..21a387d98 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 @@ -20,7 +20,6 @@ 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 @@ -37,23 +36,23 @@ internal class HistoryPresenter @Inject constructor( @param:Computation private val computation: Scheduler ) : BasePresenter(), Presenter { - private var disposable: Disposable? = null - override fun loadHistory(showHistoryCurrentBook: Boolean) { - val immutableDisposable = dataSource.getDateCategorizedHistory(showHistoryCurrentBook).subscribe( + dataSource.getDateCategorizedHistory(showHistoryCurrentBook) + .subscribe( { histories: List -> view?.updateHistoryList(histories) }, { e: Throwable -> Log.e("HistoryPresenter", "Failed to load history.", e) } - ) - disposable?.takeIf { !it.isDisposed }?.dispose() - compositeDisposable.add(immutableDisposable) + ).let { + it.takeIf { !it.isDisposed }?.dispose() + compositeDisposable.add(it) + } } override fun filterHistory(historyList: List, newText: String) { - compositeDisposable.add(Observable.fromCallable{ historyList.filter { item -> - item is HistoryItem && - item.historyTitle.toLowerCase(Locale.getDefault()) - .contains(newText.toLowerCase(Locale.getDefault())) - }} + compositeDisposable.add(Observable.fromCallable { + historyList.filterIsInstance().filter { item -> + item.historyTitle.toLowerCase(Locale.getDefault()) + .contains(newText.toLowerCase(Locale.getDefault())) + } } .subscribeOn(computation) .observeOn(mainThread) .subscribe( From 6cd4816939ef4514e1e73dfd9cc1d97ae4ae069c Mon Sep 17 00:00:00 2001 From: Frans-Lukas Date: Fri, 10 Apr 2020 16:39:41 +0200 Subject: [PATCH 6/7] #1972 small change of error output text --- .../org/kiwix/kiwixmobile/core/history/HistoryPresenter.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 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 21a387d98..4139156df 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 @@ -57,13 +57,13 @@ internal class HistoryPresenter @Inject constructor( .observeOn(mainThread) .subscribe( { hList: List -> view?.notifyHistoryListFiltered(hList) }, - { e: Throwable -> Log.e("HistoryPresenter", "Failed to filter history", e) } + { 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) + Log.e("HistoryPresenter", "Failed to delete history.", e) }) } } From 545e4e9370e2dc95ce72d6a390110a4d6adb8c14 Mon Sep 17 00:00:00 2001 From: Frans-Lukas Date: Wed, 15 Apr 2020 11:43:20 +0200 Subject: [PATCH 7/7] #1972 Fixed change requests regarding disposable and string comparison. --- .../kiwixmobile/core/history/HistoryPresenter.kt | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 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 4139156df..54b67d8f6 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 @@ -20,6 +20,7 @@ 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 @@ -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.View import org.kiwix.kiwixmobile.core.history.HistoryListItem.HistoryItem -import java.util.Locale import javax.inject.Inject internal class HistoryPresenter @Inject constructor( @@ -35,24 +35,27 @@ 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) { + 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 { - it.takeIf { !it.isDisposed }?.dispose() compositeDisposable.add(it) + disposable = it } } override fun filterHistory(historyList: List, newText: String) { compositeDisposable.add(Observable.fromCallable { - historyList.filterIsInstance().filter { item -> - item.historyTitle.toLowerCase(Locale.getDefault()) - .contains(newText.toLowerCase(Locale.getDefault())) - } } + historyList + .filterIsInstance().filter { item -> + item.historyTitle.contains(newText, true) + } + } .subscribeOn(computation) .observeOn(mainThread) .subscribe(