Refactor mvp views

pull/1/head
Koitharu 6 years ago
parent 4c2b3a8093
commit 22a6d06e1f

@ -0,0 +1,14 @@
package org.koitharu.kotatsu.ui.common
import moxy.MvpView
import moxy.viewstate.strategy.alias.AddToEndSingle
import moxy.viewstate.strategy.alias.OneExecution
interface BaseMvpView : MvpView {
@OneExecution
fun onError(e: Throwable)
@AddToEndSingle
fun onLoadingStateChanged(isLoading: Boolean)
}

@ -7,18 +7,13 @@ import moxy.viewstate.strategy.alias.SingleState
import org.koitharu.kotatsu.core.model.FavouriteCategory import org.koitharu.kotatsu.core.model.FavouriteCategory
import org.koitharu.kotatsu.core.model.Manga import org.koitharu.kotatsu.core.model.Manga
import org.koitharu.kotatsu.core.model.MangaHistory import org.koitharu.kotatsu.core.model.MangaHistory
import org.koitharu.kotatsu.ui.common.BaseMvpView
interface MangaDetailsView : MvpView { interface MangaDetailsView : BaseMvpView {
@AddToEndSingle @AddToEndSingle
fun onMangaUpdated(manga: Manga) fun onMangaUpdated(manga: Manga)
@AddToEndSingle
fun onLoadingStateChanged(isLoading: Boolean)
@OneExecution
fun onError(e: Throwable)
@AddToEndSingle @AddToEndSingle
fun onHistoryChanged(history: MangaHistory?) fun onHistoryChanged(history: MangaHistory?)

@ -151,7 +151,7 @@ abstract class MangaListFragment<E> : BaseFragment(R.layout.fragment_list), Mang
} }
} }
override fun onError(e: Exception) { override fun onError(e: Throwable) {
if (recyclerView.hasItems) { if (recyclerView.hasItems) {
Snackbar.make(recyclerView, e.getDisplayMessage(resources), Snackbar.LENGTH_SHORT) Snackbar.make(recyclerView, e.getDisplayMessage(resources), Snackbar.LENGTH_SHORT)
.show() .show()
@ -167,7 +167,7 @@ abstract class MangaListFragment<E> : BaseFragment(R.layout.fragment_list), Mang
} }
} }
override fun onLoadingChanged(isLoading: Boolean) { override fun onLoadingStateChanged(isLoading: Boolean) {
val hasItems = recyclerView.hasItems val hasItems = recyclerView.hasItems
progressBar.isVisible = isLoading && !hasItems progressBar.isVisible = isLoading && !hasItems
swipeRefreshLayout.isRefreshing = isLoading && hasItems swipeRefreshLayout.isRefreshing = isLoading && hasItems

@ -1,13 +1,17 @@
package org.koitharu.kotatsu.ui.main.list package org.koitharu.kotatsu.ui.main.list
import moxy.MvpView import moxy.viewstate.strategy.AddToEndSingleTagStrategy
import moxy.viewstate.strategy.* import moxy.viewstate.strategy.AddToEndStrategy
import moxy.viewstate.strategy.StateStrategyType
import moxy.viewstate.strategy.alias.AddToEnd
import moxy.viewstate.strategy.alias.AddToEndSingle
import org.koitharu.kotatsu.core.model.Manga import org.koitharu.kotatsu.core.model.Manga
import org.koitharu.kotatsu.core.model.MangaFilter import org.koitharu.kotatsu.core.model.MangaFilter
import org.koitharu.kotatsu.core.model.MangaTag import org.koitharu.kotatsu.core.model.MangaTag
import org.koitharu.kotatsu.core.model.SortOrder import org.koitharu.kotatsu.core.model.SortOrder
import org.koitharu.kotatsu.ui.common.BaseMvpView
interface MangaListView<E> : MvpView { interface MangaListView<E> : BaseMvpView {
@StateStrategyType(AddToEndSingleTagStrategy::class, tag = "content") @StateStrategyType(AddToEndSingleTagStrategy::class, tag = "content")
fun onListChanged(list: List<Manga>) fun onListChanged(list: List<Manga>)
@ -15,15 +19,9 @@ interface MangaListView<E> : MvpView {
@StateStrategyType(AddToEndStrategy::class, tag = "content") @StateStrategyType(AddToEndStrategy::class, tag = "content")
fun onListAppended(list: List<Manga>) fun onListAppended(list: List<Manga>)
@StateStrategyType(AddToEndSingleStrategy::class) @AddToEndSingle
fun onLoadingChanged(isLoading: Boolean)
@StateStrategyType(OneExecutionStateStrategy::class)
fun onError(e: Exception)
@StateStrategyType(AddToEndSingleStrategy::class)
fun onInitFilter(sortOrders: List<SortOrder>, tags: List<MangaTag>, currentFilter: MangaFilter?) fun onInitFilter(sortOrders: List<SortOrder>, tags: List<MangaTag>, currentFilter: MangaFilter?)
@StateStrategyType(AddToEndStrategy::class) @AddToEnd
fun onItemRemoved(item: Manga) fun onItemRemoved(item: Manga)
} }

@ -1,5 +1,6 @@
package org.koitharu.kotatsu.ui.main.list.favourites package org.koitharu.kotatsu.ui.main.list.favourites
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
@ -22,7 +23,7 @@ class FavouritesListPresenter : BasePresenter<MangaListView<Unit>>() {
fun loadList(offset: Int) { fun loadList(offset: Int) {
presenterScope.launch { presenterScope.launch {
viewState.onLoadingChanged(true) viewState.onLoadingStateChanged(true)
try { try {
val list = withContext(Dispatchers.IO) { val list = withContext(Dispatchers.IO) {
repository.getAllManga(offset = offset) repository.getAllManga(offset = offset)
@ -32,13 +33,14 @@ class FavouritesListPresenter : BasePresenter<MangaListView<Unit>>() {
} else { } else {
viewState.onListAppended(list) viewState.onListAppended(list)
} }
} catch (e: Exception) { } catch (e: CancellationException) {
} catch (e: Throwable) {
if (BuildConfig.DEBUG) { if (BuildConfig.DEBUG) {
e.printStackTrace() e.printStackTrace()
} }
viewState.onError(e) viewState.onError(e)
} finally { } finally {
viewState.onLoadingChanged(false) viewState.onLoadingStateChanged(false)
} }
} }
} }

@ -58,7 +58,7 @@ class FavouriteCategoriesDialog() : BaseBottomSheet(R.layout.dialog_favorite_cat
presenter.removeFromCategory(manga ?: return, category.id) presenter.removeFromCategory(manga ?: return, category.id)
} }
override fun onError(e: Exception) { override fun onError(e: Throwable) {
Toast.makeText(context ?: return, e.getDisplayMessage(resources), Toast.LENGTH_SHORT).show() Toast.makeText(context ?: return, e.getDisplayMessage(resources), Toast.LENGTH_SHORT).show()
} }

@ -15,5 +15,5 @@ interface FavouriteCategoriesView : MvpView {
fun onCheckedCategoriesChanged(checkedIds: Set<Int>) fun onCheckedCategoriesChanged(checkedIds: Set<Int>)
@StateStrategyType(OneExecutionStateStrategy::class) @StateStrategyType(OneExecutionStateStrategy::class)
fun onError(e: Exception) fun onError(e: Throwable)
} }

@ -1,5 +1,6 @@
package org.koitharu.kotatsu.ui.main.list.history package org.koitharu.kotatsu.ui.main.list.history
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
@ -24,7 +25,7 @@ class HistoryListPresenter : BasePresenter<MangaListView<MangaHistory>>() {
fun loadList(offset: Int) { fun loadList(offset: Int) {
presenterScope.launch { presenterScope.launch {
viewState.onLoadingChanged(true) viewState.onLoadingStateChanged(true)
try { try {
val list = withContext(Dispatchers.IO) { val list = withContext(Dispatchers.IO) {
repository.getList(offset = offset) repository.getList(offset = offset)
@ -34,32 +35,34 @@ class HistoryListPresenter : BasePresenter<MangaListView<MangaHistory>>() {
} else { } else {
viewState.onListAppended(list) viewState.onListAppended(list)
} }
} catch (e: Exception) { } catch (_: CancellationException) {
} catch (e: Throwable) {
if (BuildConfig.DEBUG) { if (BuildConfig.DEBUG) {
e.printStackTrace() e.printStackTrace()
} }
viewState.onError(e) viewState.onError(e)
} finally { } finally {
viewState.onLoadingChanged(false) viewState.onLoadingStateChanged(false)
} }
} }
} }
fun clearHistory() { fun clearHistory() {
presenterScope.launch { presenterScope.launch {
viewState.onLoadingChanged(true) viewState.onLoadingStateChanged(true)
try { try {
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
repository.clear() repository.clear()
} }
viewState.onListChanged(emptyList()) viewState.onListChanged(emptyList())
} catch (e: Exception) { } catch (_: CancellationException) {
} catch (e: Throwable) {
if (BuildConfig.DEBUG) { if (BuildConfig.DEBUG) {
e.printStackTrace() e.printStackTrace()
} }
viewState.onError(e) viewState.onError(e)
} finally { } finally {
viewState.onLoadingChanged(false) viewState.onLoadingStateChanged(false)
} }
} }
} }
@ -71,7 +74,8 @@ class HistoryListPresenter : BasePresenter<MangaListView<MangaHistory>>() {
repository.delete(manga) repository.delete(manga)
} }
viewState.onItemRemoved(manga) viewState.onItemRemoved(manga)
} catch (e: Exception) { } catch (_: CancellationException) {
} catch (e: Throwable) {
if (BuildConfig.DEBUG) { if (BuildConfig.DEBUG) {
e.printStackTrace() e.printStackTrace()
} }

@ -35,20 +35,20 @@ class LocalListPresenter : BasePresenter<MangaListView<File>>() {
fun loadList() { fun loadList() {
presenterScope.launch { presenterScope.launch {
viewState.onLoadingChanged(true) viewState.onLoadingStateChanged(true)
try { try {
val list = withContext(Dispatchers.IO) { val list = withContext(Dispatchers.IO) {
repository.getList(0) repository.getList(0)
} }
viewState.onListChanged(list) viewState.onListChanged(list)
} catch (e: CancellationException) { } catch (e: CancellationException) {
} catch (e: Exception) { } catch (e: Throwable) {
if (BuildConfig.DEBUG) { if (BuildConfig.DEBUG) {
e.printStackTrace() e.printStackTrace()
} }
viewState.onError(e) viewState.onError(e)
} finally { } finally {
viewState.onLoadingChanged(false) viewState.onLoadingStateChanged(false)
} }
} }
} }
@ -73,7 +73,7 @@ class LocalListPresenter : BasePresenter<MangaListView<File>>() {
viewState.onListChanged(list) viewState.onListChanged(list)
} }
} catch (e: CancellationException) { } catch (e: CancellationException) {
} catch (e: Exception) { } catch (e: Throwable) {
if (BuildConfig.DEBUG) { if (BuildConfig.DEBUG) {
e.printStackTrace() e.printStackTrace()
} }
@ -95,7 +95,7 @@ class LocalListPresenter : BasePresenter<MangaListView<File>>() {
} }
viewState.onItemRemoved(manga) viewState.onItemRemoved(manga)
} catch (e: CancellationException) { } catch (e: CancellationException) {
} catch (e: Exception) { } catch (e: Throwable) {
if (BuildConfig.DEBUG) { if (BuildConfig.DEBUG) {
e.printStackTrace() e.printStackTrace()
} }

@ -1,5 +1,6 @@
package org.koitharu.kotatsu.ui.main.list.remote package org.koitharu.kotatsu.ui.main.list.remote
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
@ -20,7 +21,7 @@ class RemoteListPresenter : BasePresenter<MangaListView<Unit>>() {
fun loadList(source: MangaSource, offset: Int) { fun loadList(source: MangaSource, offset: Int) {
presenterScope.launch { presenterScope.launch {
viewState.onLoadingChanged(true) viewState.onLoadingStateChanged(true)
try { try {
val list = withContext(Dispatchers.IO) { val list = withContext(Dispatchers.IO) {
MangaProviderFactory.create(source).getList( MangaProviderFactory.create(source).getList(
@ -34,13 +35,14 @@ class RemoteListPresenter : BasePresenter<MangaListView<Unit>>() {
} else { } else {
viewState.onListAppended(list) viewState.onListAppended(list)
} }
} catch (e: Exception) { } catch (_: CancellationException) {
} catch (e: Throwable) {
if (BuildConfig.DEBUG) { if (BuildConfig.DEBUG) {
e.printStackTrace() e.printStackTrace()
} }
viewState.onError(e) viewState.onError(e)
} finally { } finally {
viewState.onLoadingChanged(false) viewState.onLoadingStateChanged(false)
} }
} }
if (!isFilterInitialized) { if (!isFilterInitialized) {

@ -35,7 +35,7 @@ abstract class BaseReaderFragment(@LayoutRes contentLayoutId: Int) : BaseFragmen
/** /**
* Handled by activity * Handled by activity
*/ */
override fun onError(e: Exception) = Unit override fun onError(e: Throwable) = Unit
/** /**
* Handled by activity * Handled by activity

@ -164,7 +164,7 @@ class ReaderActivity : BaseFullscreenActivity(), ReaderView, ChaptersDialog.OnCh
progressBar_bottom.isVisible = isLoading && hasPages progressBar_bottom.isVisible = isLoading && hasPages
} }
override fun onError(e: Exception) { override fun onError(e: Throwable) {
showDialog { showDialog {
setTitle(R.string.error_occurred) setTitle(R.string.error_occurred)
setMessage(e.message) setMessage(e.message)

@ -1,14 +1,14 @@
package org.koitharu.kotatsu.ui.reader package org.koitharu.kotatsu.ui.reader
import android.net.Uri import android.net.Uri
import moxy.MvpView
import moxy.viewstate.strategy.alias.AddToEndSingle import moxy.viewstate.strategy.alias.AddToEndSingle
import moxy.viewstate.strategy.alias.OneExecution import moxy.viewstate.strategy.alias.OneExecution
import org.koitharu.kotatsu.core.model.MangaChapter import org.koitharu.kotatsu.core.model.MangaChapter
import org.koitharu.kotatsu.core.model.MangaPage import org.koitharu.kotatsu.core.model.MangaPage
import org.koitharu.kotatsu.core.prefs.ReaderMode import org.koitharu.kotatsu.core.prefs.ReaderMode
import org.koitharu.kotatsu.ui.common.BaseMvpView
interface ReaderView : MvpView { interface ReaderView : BaseMvpView {
@AddToEndSingle @AddToEndSingle
fun onInitReader(mode: ReaderMode) fun onInitReader(mode: ReaderMode)
@ -19,12 +19,6 @@ interface ReaderView : MvpView {
@AddToEndSingle @AddToEndSingle
fun onPagesLoaded(chapterId: Long, pages: List<MangaPage>, action: ReaderAction) fun onPagesLoaded(chapterId: Long, pages: List<MangaPage>, action: ReaderAction)
@AddToEndSingle
fun onLoadingStateChanged(isLoading: Boolean)
@OneExecution
fun onError(e: Exception)
@OneExecution @OneExecution
fun onPageSaved(uri: Uri?) fun onPageSaved(uri: Uri?)
} }

@ -1,5 +1,6 @@
package org.koitharu.kotatsu.ui.search package org.koitharu.kotatsu.ui.search
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
@ -23,7 +24,7 @@ class SearchPresenter : BasePresenter<MangaListView<Unit>>() {
fun loadList(source: MangaSource, query: String, offset: Int) { fun loadList(source: MangaSource, query: String, offset: Int) {
presenterScope.launch { presenterScope.launch {
viewState.onLoadingChanged(true) viewState.onLoadingStateChanged(true)
try { try {
val list = withContext(Dispatchers.IO) { val list = withContext(Dispatchers.IO) {
MangaProviderFactory.create(source) MangaProviderFactory.create(source)
@ -34,13 +35,14 @@ class SearchPresenter : BasePresenter<MangaListView<Unit>>() {
} else { } else {
viewState.onListAppended(list) viewState.onListAppended(list)
} }
} catch (e: Exception) { } catch (_: CancellationException) {
} catch (e: Throwable) {
if (BuildConfig.DEBUG) { if (BuildConfig.DEBUG) {
e.printStackTrace() e.printStackTrace()
} }
viewState.onError(e) viewState.onError(e)
} finally { } finally {
viewState.onLoadingChanged(false) viewState.onLoadingStateChanged(false)
} }
} }
} }

@ -9,7 +9,7 @@ class IntPreferenceDelegate(private val key: String, private val defValue: Int)
ReadWriteProperty<SharedPreferences, Int> { ReadWriteProperty<SharedPreferences, Int> {
override fun getValue(thisRef: SharedPreferences, property: KProperty<*>): Int { override fun getValue(thisRef: SharedPreferences, property: KProperty<*>): Int {
return thisRef.getInt(key, defValue) ?: defValue return thisRef.getInt(key, defValue)
} }
override fun setValue(thisRef: SharedPreferences, property: KProperty<*>, value: Int) { override fun setValue(thisRef: SharedPreferences, property: KProperty<*>, value: Int) {

Loading…
Cancel
Save