资源清理管理
This commit is contained in:
78
app/src/main/java/com/aiosman/ravenow/utils/DebounceUtils.kt
Normal file
78
app/src/main/java/com/aiosman/ravenow/utils/DebounceUtils.kt
Normal file
@@ -0,0 +1,78 @@
|
||||
package com.aiosman.ravenow.utils
|
||||
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.launch
|
||||
import java.util.concurrent.atomic.AtomicBoolean
|
||||
|
||||
/**
|
||||
* 防抖工具类
|
||||
* 用于防止用户快速重复点击
|
||||
*/
|
||||
object DebounceUtils {
|
||||
|
||||
/**
|
||||
* 防抖点击处理
|
||||
* @param scope 协程作用域
|
||||
* @param delayMillis 防抖延迟时间(毫秒),默认500ms
|
||||
* @param action 要执行的操作
|
||||
*/
|
||||
fun debounceClick(
|
||||
scope: CoroutineScope,
|
||||
delayMillis: Long = 500L,
|
||||
action: () -> Unit
|
||||
) {
|
||||
scope.launch {
|
||||
delay(delayMillis)
|
||||
action()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 带状态检查的防抖点击处理
|
||||
* @param scope 协程作用域
|
||||
* @param delayMillis 防抖延迟时间(毫秒),默认500ms
|
||||
* @param isProcessing 是否正在处理中的状态
|
||||
* @param action 要执行的操作
|
||||
*/
|
||||
fun debounceClickWithState(
|
||||
scope: CoroutineScope,
|
||||
delayMillis: Long = 500L,
|
||||
isProcessing: AtomicBoolean,
|
||||
action: () -> Unit
|
||||
) {
|
||||
if (isProcessing.get()) {
|
||||
return
|
||||
}
|
||||
|
||||
isProcessing.set(true)
|
||||
scope.launch {
|
||||
delay(delayMillis)
|
||||
try {
|
||||
action()
|
||||
} finally {
|
||||
isProcessing.set(false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 简单的防抖点击处理(无协程)
|
||||
* @param lastClickTime 上次点击时间
|
||||
* @param delayMillis 防抖延迟时间(毫秒),默认500ms
|
||||
* @param action 要执行的操作
|
||||
* @return 是否执行了操作
|
||||
*/
|
||||
fun simpleDebounceClick(
|
||||
lastClickTime: Long,
|
||||
delayMillis: Long = 500L,
|
||||
action: () -> Unit
|
||||
): Boolean {
|
||||
val currentTime = System.currentTimeMillis()
|
||||
if (currentTime - lastClickTime < delayMillis) {
|
||||
return false
|
||||
}
|
||||
action()
|
||||
return true
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user