Refactor mvp views

This commit is contained in:
Koitharu
2020-03-02 20:12:52 +02:00
parent 4c2b3a8093
commit 22a6d06e1f
15 changed files with 65 additions and 54 deletions

View File

@@ -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)
}

View File

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

View File

@@ -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) {
Snackbar.make(recyclerView, e.getDisplayMessage(resources), Snackbar.LENGTH_SHORT)
.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
progressBar.isVisible = isLoading && !hasItems
swipeRefreshLayout.isRefreshing = isLoading && hasItems

View File

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

View File

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

View File

@@ -58,7 +58,7 @@ class FavouriteCategoriesDialog() : BaseBottomSheet(R.layout.dialog_favorite_cat
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()
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -9,7 +9,7 @@ class IntPreferenceDelegate(private val key: String, private val defValue: Int)
ReadWriteProperty<SharedPreferences, 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) {