Refactor BasePresenter
parent
5293a8d209
commit
693f568b8e
@ -1,7 +1,44 @@
|
|||||||
package org.koitharu.kotatsu.ui.common
|
package org.koitharu.kotatsu.ui.common
|
||||||
|
|
||||||
|
import kotlinx.coroutines.*
|
||||||
import moxy.MvpPresenter
|
import moxy.MvpPresenter
|
||||||
import moxy.MvpView
|
import moxy.presenterScope
|
||||||
import org.koin.core.component.KoinComponent
|
import org.koin.core.component.KoinComponent
|
||||||
|
import org.koitharu.kotatsu.BuildConfig
|
||||||
|
import kotlin.coroutines.CoroutineContext
|
||||||
|
import kotlin.coroutines.EmptyCoroutineContext
|
||||||
|
|
||||||
abstract class BasePresenter<V : MvpView> : MvpPresenter<V>(), KoinComponent
|
abstract class BasePresenter<V : BaseMvpView> : MvpPresenter<V>(), KoinComponent {
|
||||||
|
|
||||||
|
protected fun launchJob(
|
||||||
|
context: CoroutineContext = EmptyCoroutineContext,
|
||||||
|
start: CoroutineStart = CoroutineStart.DEFAULT,
|
||||||
|
block: suspend CoroutineScope.() -> Unit
|
||||||
|
) {
|
||||||
|
presenterScope.launch(context + createErrorHandler(), start, block)
|
||||||
|
}
|
||||||
|
|
||||||
|
protected fun launchLoadingJob(
|
||||||
|
context: CoroutineContext = EmptyCoroutineContext,
|
||||||
|
start: CoroutineStart = CoroutineStart.DEFAULT,
|
||||||
|
block: suspend CoroutineScope.() -> Unit
|
||||||
|
) {
|
||||||
|
presenterScope.launch(context + createErrorHandler(), start) {
|
||||||
|
viewState.onLoadingStateChanged(isLoading = true)
|
||||||
|
try {
|
||||||
|
block()
|
||||||
|
} finally {
|
||||||
|
viewState.onLoadingStateChanged(isLoading = false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun createErrorHandler() = CoroutineExceptionHandler { _, throwable ->
|
||||||
|
if (BuildConfig.DEBUG) {
|
||||||
|
throwable.printStackTrace()
|
||||||
|
}
|
||||||
|
if (throwable !is CancellationException) {
|
||||||
|
viewState.onError(throwable)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue