CoroutinePresenter changes
This commit is contained in:
@@ -1,41 +1,60 @@
|
||||
package exh.ui.base
|
||||
|
||||
import androidx.annotation.CallSuper
|
||||
import kotlinx.coroutines.CoroutineDispatcher
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.cancel
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.collect
|
||||
import kotlinx.coroutines.flow.launchIn
|
||||
import kotlinx.coroutines.flow.mapNotNull
|
||||
import kotlinx.coroutines.flow.onEach
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import nucleus.presenter.Presenter
|
||||
import kotlin.coroutines.CoroutineContext
|
||||
import kotlin.coroutines.EmptyCoroutineContext
|
||||
|
||||
@Suppress("DEPRECATION")
|
||||
open class CoroutinePresenter<V> : Presenter<V>() {
|
||||
val scope = CoroutineScope(Job() + Dispatchers.Main)
|
||||
|
||||
@Suppress("DEPRECATION", "unused")
|
||||
open class CoroutinePresenter<V>(
|
||||
scope: CoroutineScope = CoroutineScope(Job() + Dispatchers.Main)
|
||||
) : Presenter<V>(),
|
||||
CoroutineScope by scope
|
||||
{
|
||||
@Suppress("DeprecatedCallableAddReplaceWith")
|
||||
@Deprecated("Use launchInView")
|
||||
@Deprecated("Use launchInView, Flow.inView, Flow.mapView")
|
||||
override fun getView(): V? {
|
||||
return super.getView()
|
||||
}
|
||||
|
||||
fun launchInView(block: (CoroutineScope, V) -> Unit) = scope.launch(Dispatchers.Main) {
|
||||
fun launchInView(block: (CoroutineScope, V) -> Unit) = launch(Dispatchers.Main) {
|
||||
view?.let { block.invoke(this, it) }
|
||||
}
|
||||
|
||||
fun <F> Flow<F>.onEachView(block: (V, F) -> Unit) = onEach {
|
||||
view?.let { view -> block(view, it) }
|
||||
inline fun <F> Flow<F>.inView(crossinline block: (V, F) -> Unit) = onEach {
|
||||
withContext(Dispatchers.Main) {
|
||||
view?.let { view -> block(view, it) }
|
||||
}
|
||||
}
|
||||
|
||||
fun <F, P> Flow<F>.mapView(block: (V, F) -> P): Flow<P> = mapNotNull {
|
||||
view?.let { view -> block(view, it) }
|
||||
inline fun <F, P> Flow<F>.mapView(crossinline block: (V, F) -> P): Flow<P> {
|
||||
return mapNotNull {
|
||||
withContext(Dispatchers.Main) {
|
||||
view?.let { view -> block(view, it) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun Flow<*>.launchUnderContext(context: CoroutineContext = EmptyCoroutineContext) =
|
||||
launch(this + context) { this@launchInHere.collect() }
|
||||
|
||||
fun Flow<*>.launch() = launchIn(this)
|
||||
|
||||
@CallSuper
|
||||
override fun destroy() {
|
||||
super.destroy()
|
||||
scope.cancel()
|
||||
cancel()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,13 +41,13 @@ class MetadataViewPresenter(
|
||||
meta.value = it.raise(mainSource.metaClass)
|
||||
}
|
||||
}
|
||||
.launchIn(scope + Dispatchers.IO)
|
||||
.launchUnderContext(Dispatchers.IO)
|
||||
|
||||
meta
|
||||
.onEachView { view, metadata ->
|
||||
.inView { view, metadata ->
|
||||
view.onNextMangaInfo(metadata)
|
||||
}
|
||||
.launchIn(scope)
|
||||
.launch()
|
||||
}
|
||||
|
||||
private fun getMangaMetaObservable(): Flow<FlatMetadata?> {
|
||||
|
||||
@@ -21,7 +21,7 @@ class SmartSearchPresenter(private val source: CatalogueSource, private val conf
|
||||
override fun onCreate(savedState: Bundle?) {
|
||||
super.onCreate(savedState)
|
||||
|
||||
scope.launch(Dispatchers.IO) {
|
||||
launch(Dispatchers.IO) {
|
||||
val result = try {
|
||||
val resultManga = smartSearchEngine.smartSearch(source, config.origTitle)
|
||||
if (resultManga != null) {
|
||||
|
||||
Reference in New Issue
Block a user