|
@@ -52,19 +52,20 @@ fun FragmentActivity.scopeDialog(
|
|
|
dialog: Dialog? = null,
|
|
|
cancelable: Boolean = true,
|
|
|
dispatcher: CoroutineDispatcher = Dispatchers.Main,
|
|
|
- block: suspend CoroutineScope.() -> Unit
|
|
|
- ) = DialogCoroutineScope(this, dialog, cancelable, dispatcher).launch(block)
|
|
|
+ block: suspend CoroutineScope.() -> Unit) =
|
|
|
+ DialogCoroutineScope(this, dialog, cancelable, dispatcher).launch(block)
|
|
|
|
|
|
fun Fragment.scopeDialog(
|
|
|
dialog: Dialog? = null,
|
|
|
cancelable: Boolean = true,
|
|
|
dispatcher: CoroutineDispatcher = Dispatchers.Main,
|
|
|
- block: suspend CoroutineScope.() -> Unit
|
|
|
- ) = DialogCoroutineScope(requireActivity(), dialog, cancelable, dispatcher).launch(block)
|
|
|
+ block: suspend CoroutineScope.() -> Unit) =
|
|
|
+ DialogCoroutineScope(requireActivity(), dialog, cancelable, dispatcher).launch(block)
|
|
|
|
|
|
// </editor-fold>
|
|
|
|
|
|
|
|
|
+//<editor-fold desc="缺省页/分页">
|
|
|
/**
|
|
|
* 自动处理缺省页的异步作用域
|
|
|
* 作用域开始执行时显示加载中缺省页
|
|
@@ -76,8 +77,9 @@ fun Fragment.scopeDialog(
|
|
|
*
|
|
|
* 布局被销毁或者界面关闭作用域被取消
|
|
|
*/
|
|
|
-fun StateLayout.scope(dispatcher: CoroutineDispatcher = Dispatchers.Main,
|
|
|
- block: suspend CoroutineScope.() -> Unit): NetCoroutineScope {
|
|
|
+fun StateLayout.scope(
|
|
|
+ dispatcher: CoroutineDispatcher = Dispatchers.Main,
|
|
|
+ block: suspend CoroutineScope.() -> Unit): NetCoroutineScope {
|
|
|
val scope = StateCoroutineScope(this, dispatcher)
|
|
|
scope.launch(block)
|
|
|
return scope
|
|
@@ -96,13 +98,16 @@ fun StateLayout.scope(dispatcher: CoroutineDispatcher = Dispatchers.Main,
|
|
|
*
|
|
|
* 布局被销毁或者界面关闭作用域被取消
|
|
|
*/
|
|
|
-fun PageRefreshLayout.scope(dispatcher: CoroutineDispatcher = Dispatchers.Main,
|
|
|
- block: suspend CoroutineScope.() -> Unit): PageCoroutineScope {
|
|
|
+fun PageRefreshLayout.scope(
|
|
|
+ dispatcher: CoroutineDispatcher = Dispatchers.Main,
|
|
|
+ block: suspend CoroutineScope.() -> Unit): PageCoroutineScope {
|
|
|
val scope = PageCoroutineScope(this, dispatcher)
|
|
|
scope.launch(block)
|
|
|
return scope
|
|
|
}
|
|
|
+//</editor-fold>
|
|
|
|
|
|
+//<editor-fold desc="异步任务">
|
|
|
/**
|
|
|
* 异步作用域
|
|
|
*
|
|
@@ -113,18 +118,16 @@ fun scope(dispatcher: CoroutineDispatcher = Dispatchers.Main,
|
|
|
return AndroidScope(dispatcher = dispatcher).launch(block)
|
|
|
}
|
|
|
|
|
|
-fun LifecycleOwner.scopeLife(
|
|
|
- lifeEvent: Lifecycle.Event = Lifecycle.Event.ON_DESTROY,
|
|
|
- dispatcher: CoroutineDispatcher = Dispatchers.Main,
|
|
|
- block: suspend CoroutineScope.() -> Unit
|
|
|
- ) = AndroidScope(this, lifeEvent, dispatcher).launch(block)
|
|
|
+fun LifecycleOwner.scopeLife(lifeEvent: Lifecycle.Event = Lifecycle.Event.ON_DESTROY,
|
|
|
+ dispatcher: CoroutineDispatcher = Dispatchers.Main,
|
|
|
+ block: suspend CoroutineScope.() -> Unit) = AndroidScope(this, lifeEvent, dispatcher).launch(block)
|
|
|
|
|
|
-fun Fragment.scopeLife(
|
|
|
- lifeEvent: Lifecycle.Event = Lifecycle.Event.ON_STOP,
|
|
|
- dispatcher: CoroutineDispatcher = Dispatchers.Main,
|
|
|
- block: suspend CoroutineScope.() -> Unit
|
|
|
- ) = AndroidScope(this, lifeEvent, dispatcher).launch(block)
|
|
|
+fun Fragment.scopeLife(lifeEvent: Lifecycle.Event = Lifecycle.Event.ON_STOP,
|
|
|
+ dispatcher: CoroutineDispatcher = Dispatchers.Main,
|
|
|
+ block: suspend CoroutineScope.() -> Unit) = AndroidScope(this, lifeEvent, dispatcher).launch(block)
|
|
|
+//</editor-fold>
|
|
|
|
|
|
+//<editor-fold desc="网络">
|
|
|
/**
|
|
|
* 网络请求的异步作用域
|
|
|
* 自动显示错误信息吐司
|
|
@@ -135,35 +138,37 @@ fun scopeNet(dispatcher: CoroutineDispatcher = Dispatchers.Main,
|
|
|
block: suspend CoroutineScope.() -> Unit) = NetCoroutineScope(dispatcher = dispatcher).launch(block)
|
|
|
|
|
|
|
|
|
-fun LifecycleOwner.scopeNetLife(
|
|
|
- lifeEvent: Lifecycle.Event = Lifecycle.Event.ON_DESTROY,
|
|
|
- dispatcher: CoroutineDispatcher = Dispatchers.Main,
|
|
|
- block: suspend CoroutineScope.() -> Unit
|
|
|
- ) = NetCoroutineScope(this, lifeEvent, dispatcher).launch(block)
|
|
|
+fun LifecycleOwner.scopeNetLife(lifeEvent: Lifecycle.Event = Lifecycle.Event.ON_DESTROY,
|
|
|
+ dispatcher: CoroutineDispatcher = Dispatchers.Main,
|
|
|
+ block: suspend CoroutineScope.() -> Unit) = NetCoroutineScope(this, lifeEvent, dispatcher).launch(block)
|
|
|
|
|
|
/**
|
|
|
* Fragment应当在[Lifecycle.Event.ON_STOP]时就取消作用域, 避免[Fragment.onDestroyView]导致引用空视图
|
|
|
*/
|
|
|
-fun Fragment.scopeNetLife(
|
|
|
- lifeEvent: Lifecycle.Event = Lifecycle.Event.ON_STOP,
|
|
|
- dispatcher: CoroutineDispatcher = Dispatchers.Main,
|
|
|
- block: suspend CoroutineScope.() -> Unit
|
|
|
- ) = NetCoroutineScope(this, lifeEvent, dispatcher).launch(block)
|
|
|
+fun Fragment.scopeNetLife(lifeEvent: Lifecycle.Event = Lifecycle.Event.ON_STOP,
|
|
|
+ dispatcher: CoroutineDispatcher = Dispatchers.Main,
|
|
|
+ block: suspend CoroutineScope.() -> Unit) = NetCoroutineScope(this, lifeEvent, dispatcher).launch(block)
|
|
|
+//</editor-fold>
|
|
|
|
|
|
|
|
|
+/**
|
|
|
+ * Flow直接创建作用域
|
|
|
+ * @param owner 跟随的生命周期组件
|
|
|
+ * @param event 销毁时机
|
|
|
+ * @param dispatcher 指定调度器
|
|
|
+ */
|
|
|
@OptIn(InternalCoroutinesApi::class)
|
|
|
-inline fun <T> Flow<T>.scope(
|
|
|
- owner: LifecycleOwner? = null,
|
|
|
- event: Lifecycle.Event = Lifecycle.Event.ON_DESTROY,
|
|
|
- dispatcher: CoroutineDispatcher = Dispatchers.Main,
|
|
|
- crossinline action: suspend (value: T) -> Unit
|
|
|
- ): AndroidScope = AndroidScope(owner, event, dispatcher).launch {
|
|
|
+inline fun <T> Flow<T>.scope(owner: LifecycleOwner? = null,
|
|
|
+ event: Lifecycle.Event = Lifecycle.Event.ON_DESTROY,
|
|
|
+ dispatcher: CoroutineDispatcher = Dispatchers.Main,
|
|
|
+ crossinline action: suspend (value: T) -> Unit): AndroidScope = AndroidScope(owner, event, dispatcher).launch {
|
|
|
this@scope.collect(object : FlowCollector<T> {
|
|
|
override suspend fun emit(value: T) = action(value)
|
|
|
})
|
|
|
}
|
|
|
|
|
|
|
|
|
+//<editor-fold desc="并发返回最快">
|
|
|
/**
|
|
|
* 该函数将选择[deferredArray]中的Deferred执行[Deferred.await], 然后将返回最快的结果
|
|
|
* 执行过程中的异常将被忽略, 如果全部抛出异常则将抛出最后一个Deferred的异常
|
|
@@ -264,6 +269,7 @@ suspend fun <T, R> CoroutineScope.fastest(deferredList: List<DeferredTransform<T
|
|
|
}
|
|
|
return chan.receive()
|
|
|
}
|
|
|
+//</editor-fold>
|
|
|
|
|
|
|
|
|
|