From c606d3a5e0f2c68050304fcc920ef2e3c0999543 Mon Sep 17 00:00:00 2001 From: Hritik Wadhwa Date: Tue, 31 Mar 2020 03:33:29 +0530 Subject: [PATCH 1/2] Fixed #1985 Converted MainPresenter to Kotlin --- .../core/di/modules/DialogModule.kt | 2 +- .../kiwixmobile/core/main/MainPresenter.java | 153 ------------------ .../kiwixmobile/core/main/MainPresenter.kt | 142 ++++++++++++++++ 3 files changed, 143 insertions(+), 154 deletions(-) delete mode 100644 core/src/main/java/org/kiwix/kiwixmobile/core/main/MainPresenter.java create mode 100644 core/src/main/java/org/kiwix/kiwixmobile/core/main/MainPresenter.kt diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/di/modules/DialogModule.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/di/modules/DialogModule.kt index d9ad67405..c60954060 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/di/modules/DialogModule.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/di/modules/DialogModule.kt @@ -34,5 +34,5 @@ abstract class DialogModule { abstract fun bindDialogShower(alertDialogShower: AlertDialogShower): DialogShower @Binds - abstract fun bindMainPresenter(mainPresenter: MainPresenter): MainContract.Presenter + internal abstract fun bindMainPresenter(mainPresenter: MainPresenter): MainContract.Presenter } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/main/MainPresenter.java b/core/src/main/java/org/kiwix/kiwixmobile/core/main/MainPresenter.java deleted file mode 100644 index d80fdc393..000000000 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/main/MainPresenter.java +++ /dev/null @@ -1,153 +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.main; - -import android.util.Log; -import io.reactivex.CompletableObserver; -import io.reactivex.SingleObserver; -import io.reactivex.disposables.Disposable; -import java.util.List; -import javax.inject.Inject; -import org.kiwix.kiwixmobile.core.base.BasePresenter; -import org.kiwix.kiwixmobile.core.bookmark.BookmarkItem; -import org.kiwix.kiwixmobile.core.data.DataSource; -import org.kiwix.kiwixmobile.core.di.ActivityScope; -import org.kiwix.kiwixmobile.core.history.HistoryListItem; -import org.kiwix.kiwixmobile.core.zim_manager.fileselect_view.adapter.BooksOnDiskListItem; - -/** - * Presenter for {@link MainActivity}. - */ - -@ActivityScope -public class MainPresenter extends BasePresenter - implements MainContract.Presenter { - - private static final String TAG = "MainPresenter"; - private final DataSource dataSource; - - @Inject public MainPresenter(DataSource dataSource) { - this.dataSource = dataSource; - } - - @Override - public void loadBooks() { - dataSource.getLanguageCategorizedBooks() - .subscribe(new SingleObserver>() { - @Override - public void onSubscribe(Disposable d) { - compositeDisposable.add(d); - } - - @Override - public void onSuccess(List books) { - view.addBooks(books); - } - - @Override - public void onError(Throwable e) { - Log.e(TAG, "Unable to load books", e); - } - }); - } - - @Override - public void saveBooks(List book) { - dataSource.saveBooks(book) - .subscribe(new CompletableObserver() { - @Override - public void onSubscribe(Disposable d) { - - } - - @Override - public void onComplete() { - loadBooks(); - } - - @Override - public void onError(Throwable e) { - Log.e(TAG, "Unable to save books", e); - } - }); - } - - @Override - public void saveHistory(HistoryListItem.HistoryItem history) { - dataSource.saveHistory(history) - .subscribe(new CompletableObserver() { - @Override - public void onSubscribe(Disposable d) { - - } - - @Override - public void onComplete() { - - } - - @Override - public void onError(Throwable e) { - Log.e(TAG, "Unable to save history", e); - } - }); - } - - @Override - public void saveBookmark(BookmarkItem bookmark) { - dataSource.saveBookmark(bookmark) - .subscribe(new CompletableObserver() { - @Override - public void onSubscribe(Disposable d) { - - } - - @Override - public void onComplete() { - - } - - @Override - public void onError(Throwable e) { - Log.e(TAG, "Unable to save bookmark", e); - } - }); - } - - @Override - public void deleteBookmark(String bookmarkUrl) { - dataSource.deleteBookmark(bookmarkUrl) - .subscribe(new CompletableObserver() { - @Override - public void onSubscribe(Disposable d) { - - } - - @Override - public void onComplete() { - - } - - @Override - public void onError(Throwable e) { - Log.e(TAG, "Unable to delete bookmark", e); - } - }); - } -} diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/main/MainPresenter.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/main/MainPresenter.kt new file mode 100644 index 000000000..61ce1b415 --- /dev/null +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/main/MainPresenter.kt @@ -0,0 +1,142 @@ +/* + * 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.main + +import android.util.Log +import io.reactivex.CompletableObserver +import io.reactivex.SingleObserver +import io.reactivex.disposables.Disposable +import org.kiwix.kiwixmobile.core.base.BasePresenter +import org.kiwix.kiwixmobile.core.bookmark.BookmarkItem +import org.kiwix.kiwixmobile.core.data.DataSource +import org.kiwix.kiwixmobile.core.di.ActivityScope +import org.kiwix.kiwixmobile.core.history.HistoryListItem.HistoryItem +import org.kiwix.kiwixmobile.core.main.MainContract.Presenter +import org.kiwix.kiwixmobile.core.main.MainContract.View +import org.kiwix.kiwixmobile.core.zim_manager.fileselect_view.adapter.BooksOnDiskListItem +import org.kiwix.kiwixmobile.core.zim_manager.fileselect_view.adapter.BooksOnDiskListItem.BookOnDisk +import javax.inject.Inject + +@ActivityScope +internal class MainPresenter @Inject constructor(private val dataSource: DataSource) : + BasePresenter(), Presenter { + override fun loadBooks() { + dataSource.languageCategorizedBooks + .subscribe(object : SingleObserver?> { + override fun onSubscribe(d: Disposable) { + compositeDisposable.add(d) + } + + override fun onSuccess(books: List) { + view!!.addBooks(books) + } + + override fun onError(e: Throwable) { + Log.e( + TAG, + "Unable to load books", + e + ) + } + }) + } + + override fun saveBooks(book: List) { + dataSource.saveBooks(book) + .subscribe(object : CompletableObserver { + override fun onSubscribe(d: Disposable) { + // TODO + } + + override fun onComplete() { + loadBooks() + } + + override fun onError(e: Throwable) { + Log.e( + TAG, + "Unable to save books", + e + ) + } + }) + } + + override fun saveHistory(history: HistoryItem) { + dataSource.saveHistory(history) + .subscribe(object : CompletableObserver { + override fun onSubscribe(d: Disposable) { + // TODO + } + + override fun onComplete() { + // TODO + } + + override fun onError(e: Throwable) { + Log.e(TAG, "Unable to save history", e) + } + }) + } + + override fun saveBookmark(bookmark: BookmarkItem) { + dataSource.saveBookmark(bookmark) + .subscribe(object : CompletableObserver { + override fun onSubscribe(d: Disposable) { + // TODO + } + + override fun onComplete() { + // TODO + } + + override fun onError(e: Throwable) { + Log.e( + TAG, + "Unable to save bookmark", + e + ) + } + }) + } + + override fun deleteBookmark(bookmarkUrl: String) { + dataSource.deleteBookmark(bookmarkUrl) + .subscribe(object : CompletableObserver { + override fun onSubscribe(d: Disposable) { + // TODO + } + + override fun onComplete() { + // TODO + } + + override fun onError(e: Throwable) { + Log.e( + TAG, + "Unable to delete bookmark", + e + ) + } + }) + } + + companion object { + private const val TAG = "MainPresenter" + } +} From 21e075f9abf8f4ceda171f9fa2b901f47de855bd Mon Sep 17 00:00:00 2001 From: Hritik Wadhwa Date: Fri, 3 Apr 2020 02:45:06 +0530 Subject: [PATCH 2/2] Refactor MainPresenter --- .../kiwixmobile/core/main/MainPresenter.kt | 100 ++---------------- 1 file changed, 10 insertions(+), 90 deletions(-) diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/main/MainPresenter.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/main/MainPresenter.kt index 61ce1b415..a87f02de1 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/main/MainPresenter.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/main/MainPresenter.kt @@ -18,9 +18,6 @@ package org.kiwix.kiwixmobile.core.main import android.util.Log -import io.reactivex.CompletableObserver -import io.reactivex.SingleObserver -import io.reactivex.disposables.Disposable import org.kiwix.kiwixmobile.core.base.BasePresenter import org.kiwix.kiwixmobile.core.bookmark.BookmarkItem import org.kiwix.kiwixmobile.core.data.DataSource @@ -28,115 +25,38 @@ import org.kiwix.kiwixmobile.core.di.ActivityScope import org.kiwix.kiwixmobile.core.history.HistoryListItem.HistoryItem import org.kiwix.kiwixmobile.core.main.MainContract.Presenter import org.kiwix.kiwixmobile.core.main.MainContract.View -import org.kiwix.kiwixmobile.core.zim_manager.fileselect_view.adapter.BooksOnDiskListItem import org.kiwix.kiwixmobile.core.zim_manager.fileselect_view.adapter.BooksOnDiskListItem.BookOnDisk import javax.inject.Inject +private const val TAG = "MainPresenter" + @ActivityScope internal class MainPresenter @Inject constructor(private val dataSource: DataSource) : BasePresenter(), Presenter { override fun loadBooks() { - dataSource.languageCategorizedBooks - .subscribe(object : SingleObserver?> { - override fun onSubscribe(d: Disposable) { - compositeDisposable.add(d) - } - - override fun onSuccess(books: List) { - view!!.addBooks(books) - } - - override fun onError(e: Throwable) { - Log.e( - TAG, - "Unable to load books", - e - ) - } - }) + compositeDisposable.add( + dataSource.languageCategorizedBooks.subscribe( + view!!::addBooks + ) { e -> Log.e(TAG, "Unable to load books", e) }) } override fun saveBooks(book: List) { dataSource.saveBooks(book) - .subscribe(object : CompletableObserver { - override fun onSubscribe(d: Disposable) { - // TODO - } - - override fun onComplete() { - loadBooks() - } - - override fun onError(e: Throwable) { - Log.e( - TAG, - "Unable to save books", - e - ) - } - }) + .subscribe(::loadBooks) { e -> Log.e(TAG, "Unable to save books", e) } } override fun saveHistory(history: HistoryItem) { dataSource.saveHistory(history) - .subscribe(object : CompletableObserver { - override fun onSubscribe(d: Disposable) { - // TODO - } - - override fun onComplete() { - // TODO - } - - override fun onError(e: Throwable) { - Log.e(TAG, "Unable to save history", e) - } - }) + .subscribe({}, { e -> Log.e(TAG, "Unable to save history", e) }) } override fun saveBookmark(bookmark: BookmarkItem) { dataSource.saveBookmark(bookmark) - .subscribe(object : CompletableObserver { - override fun onSubscribe(d: Disposable) { - // TODO - } - - override fun onComplete() { - // TODO - } - - override fun onError(e: Throwable) { - Log.e( - TAG, - "Unable to save bookmark", - e - ) - } - }) + .subscribe({}, { e -> Log.e(TAG, "Unable to save bookmark", e) }) } override fun deleteBookmark(bookmarkUrl: String) { dataSource.deleteBookmark(bookmarkUrl) - .subscribe(object : CompletableObserver { - override fun onSubscribe(d: Disposable) { - // TODO - } - - override fun onComplete() { - // TODO - } - - override fun onError(e: Throwable) { - Log.e( - TAG, - "Unable to delete bookmark", - e - ) - } - }) - } - - companion object { - private const val TAG = "MainPresenter" + .subscribe({}, { e -> Log.e(TAG, "Unable to delete bookmark", e) }) } }