Package com.drake.net.utils

Types

Https
Link copied to clipboard
object Https
TipUtils
Link copied to clipboard
object TipUtils

Functions

debounce
Link copied to clipboard
fun EditText.debounce(timeoutMillis: Long = 800): Flow<String>
为EditText的输入框文本变化启用节流阀, 即超过指定时间后(默认800毫秒)的输入框文本变化事件TextWatcher.onTextChanged会被下游收集到
fastest
Link copied to clipboard
@JvmName(name = fastestTransform)
suspend fun <T, R> CoroutineScope.fastest(listDeferred: List<DeferredTransform<T, R>>?, group: Any? = null): R
该函数将选择listDeferred中的Deferred执行Deferred.await, 然后将返回最快的结果 执行过程中的异常将被忽略, 如果全部抛出异常则将抛出最后一个Deferred的异常
suspend fun <T> CoroutineScope.fastest(listDeferred: List<Deferred<T>>, group: Any? = null): T
该函数将选择listDeferred中的Deferred执行Deferred.await, 然后将返回最快的结果 执行过程中的异常将被忽略, 如果全部抛出异常则将抛出最后一个Deferred的异常
install
Link copied to clipboard
fun File.install()
安装APK
isValid
Link copied to clipboard
inline fun String?.isValid(block: (String) -> Unit)
launchIn
Link copied to clipboard
inline fun <T> Flow<T>.launchIn(owner: LifecycleOwner? = null, event: Lifecycle.Event = Lifecycle.Event.ON_DESTROY, dispatcher: CoroutineDispatcher = Dispatchers.Main, crossinline action: suspend CoroutineScope.(T) -> Unit): AndroidScope
Flow直接创建作用域
lazyField
Link copied to clipboard
fun <T, V> T.lazyField(block: T.(KProperty<*>) -> V): ReadWriteProperty<T, V>
延迟初始化 线程安全 等效于lazy, 但是可以获取委托字段属性
listen
Link copied to clipboard
inline fun <T> Flow<T>.listen(owner: LifecycleOwner? = null, event: Lifecycle.Event = Lifecycle.Event.ON_DESTROY, dispatcher: CoroutineDispatcher = Dispatchers.Main, crossinline action: suspend CoroutineScope.(T) -> Unit): AndroidScope
收集Flow结果并过滤重复结果
md5
Link copied to clipboard
fun File.md5(): String?
返回文件的MD5值
mediaType
Link copied to clipboard
fun File.mediaType(): MediaType?
返回文件的MediaType值, 如果不存在返回null
runMain
Link copied to clipboard
fun runMain(block: () -> Unit)
在主线程运行
scope
Link copied to clipboard
fun scope(dispatcher: CoroutineDispatcher = Dispatchers.Main, block: suspend CoroutineScope.() -> Unit): AndroidScope
异步作用域该作用域生命周期跟随整个应用, 注意内存泄漏
fun PageRefreshLayout.scope(dispatcher: CoroutineDispatcher = Dispatchers.Main, block: suspend CoroutineScope.() -> Unit): PageCoroutineScope
PageRefreshLayout的异步作用域
  1. 下拉刷新自动结束

  2. 上拉加载自动结束

  3. 捕获异常

  4. 打印异常日志

  5. 吐司部分异常com.drake.net.NetConfig.onStateError

  6. 判断添加还是覆盖数据

  7. 自动显示缺省页

布局被销毁或者界面关闭作用域被取消
fun StateLayout.scope(dispatcher: CoroutineDispatcher = Dispatchers.Main, block: suspend CoroutineScope.() -> Unit): NetCoroutineScope
自动处理缺省页的异步作用域 作用域开始执行时显示加载中缺省页 作用域正常结束时显示成功缺省页 作用域抛出异常时显示错误缺省页 并且自动吐司错误信息, 可配置 com.drake.net.NetConfig.onStateError 自动打印异常日志
inline fun <T> Flow<T>.scope(owner: LifecycleOwner? = null, event: Lifecycle.Event = Lifecycle.Event.ON_DESTROY, dispatcher: CoroutineDispatcher = Dispatchers.Main, crossinline action: suspend CoroutineScope.(T) -> Unit): AndroidScope
Flow直接创建作用域
scopeDialog
Link copied to clipboard
fun Fragment.scopeDialog(dialog: Dialog? = null, cancelable: Boolean = true, dispatcher: CoroutineDispatcher = Dispatchers.Main, block: suspend CoroutineScope.() -> Unit): NetCoroutineScope
fun FragmentActivity.scopeDialog(dialog: Dialog? = null, cancelable: Boolean = true, dispatcher: CoroutineDispatcher = Dispatchers.Main, block: suspend CoroutineScope.() -> Unit): NetCoroutineScope
作用域开始时自动显示加载对话框, 结束时自动关闭加载对话框 可以设置全局对话框 com.drake.net.NetConfig.dialogFactory
scopeLife
Link copied to clipboard
fun Fragment.scopeLife(lifeEvent: Lifecycle.Event = Lifecycle.Event.ON_STOP, dispatcher: CoroutineDispatcher = Dispatchers.Main, block: suspend CoroutineScope.() -> Unit): AndroidScope
fun LifecycleOwner.scopeLife(lifeEvent: Lifecycle.Event = Lifecycle.Event.ON_DESTROY, dispatcher: CoroutineDispatcher = Dispatchers.Main, block: suspend CoroutineScope.() -> Unit): AndroidScope
scopeNet
Link copied to clipboard
fun scopeNet(dispatcher: CoroutineDispatcher = Dispatchers.Main, block: suspend CoroutineScope.() -> Unit): NetCoroutineScope
该函数比scope多了以下功能
  • 在作用域内抛出异常时会被回调的NetConfig.onError函数中

  • 自动显示错误信息吐司, 可以通过指定NetConfig.onError来取消或者增加自己的处理

该作用域生命周期跟随整个应用, 注意内存泄漏
scopeNetLife
Link copied to clipboard
fun Fragment.scopeNetLife(lifeEvent: Lifecycle.Event = Lifecycle.Event.ON_STOP, dispatcher: CoroutineDispatcher = Dispatchers.Main, block: suspend CoroutineScope.() -> Unit): NetCoroutineScope
和上述函数功能相同, 只是接受者为FragmentFragment应当在Lifecycle.Event.ON_STOP时就取消作用域, 避免Fragment.onDestroyView导致引用空视图
fun LifecycleOwner.scopeNetLife(lifeEvent: Lifecycle.Event = Lifecycle.Event.ON_DESTROY, dispatcher: CoroutineDispatcher = Dispatchers.Main, block: suspend CoroutineScope.() -> Unit): NetCoroutineScope
该函数比scopeNet多了自动取消作用域功能该作用域生命周期跟随LifecycleOwner.
toRequestBody
Link copied to clipboard
fun File.toRequestBody(contentType: MediaType? = null): RequestBody
创建File的RequestBody
withDefault
Link copied to clipboard
suspend fun <T> withDefault(block: suspend CoroutineScope.() -> T): T
切换到默认调度器
withIO
Link copied to clipboard
suspend fun <T> withIO(block: suspend CoroutineScope.() -> T): T
切换到IO程调度器
withMain
Link copied to clipboard
suspend fun <T> withMain(block: suspend CoroutineScope.() -> T): T
切换到主线程调度器
withUnconfined
Link copied to clipboard
suspend fun <T> withUnconfined(block: suspend CoroutineScope.() -> T): T
切换到没有限制的调度器

Properties

isValid
Link copied to clipboard
val String?.isValid: String?