mirror of
https://github.com/gkd-kit/gkd.git
synced 2024-11-16 11:42:22 +08:00
This commit is contained in:
parent
d4f1509b85
commit
e86b323906
|
@ -9,6 +9,7 @@ import androidx.compose.runtime.getValue
|
||||||
import kotlinx.coroutines.Job
|
import kotlinx.coroutines.Job
|
||||||
import kotlinx.coroutines.flow.MutableStateFlow
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
import kotlinx.coroutines.yield
|
import kotlinx.coroutines.yield
|
||||||
|
import li.songe.gkd.util.throttle
|
||||||
import kotlin.coroutines.coroutineContext
|
import kotlin.coroutines.coroutineContext
|
||||||
import kotlin.coroutines.resume
|
import kotlin.coroutines.resume
|
||||||
import kotlin.coroutines.suspendCoroutine
|
import kotlin.coroutines.suspendCoroutine
|
||||||
|
@ -39,13 +40,13 @@ fun buildDialogOptions(
|
||||||
},
|
},
|
||||||
onDismissRequest = onDismissRequest,
|
onDismissRequest = onDismissRequest,
|
||||||
confirmButton = {
|
confirmButton = {
|
||||||
TextButton(onClick = confirmAction) {
|
TextButton(onClick = throttle(fn = confirmAction)) {
|
||||||
Text(text = confirmText)
|
Text(text = confirmText)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
dismissButton = if (dismissText != null && dismissAction != null) {
|
dismissButton = if (dismissText != null && dismissAction != null) {
|
||||||
{
|
{
|
||||||
TextButton(onClick = dismissAction) {
|
TextButton(onClick = throttle(fn = dismissAction)) {
|
||||||
Text(text = dismissText)
|
Text(text = dismissText)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
package li.songe.gkd.util
|
package li.songe.gkd.util
|
||||||
|
|
||||||
import li.songe.gkd.data.Value
|
|
||||||
import java.text.SimpleDateFormat
|
import java.text.SimpleDateFormat
|
||||||
import java.util.Locale
|
import java.util.Locale
|
||||||
import java.util.concurrent.TimeUnit
|
import java.util.concurrent.TimeUnit
|
||||||
|
@ -37,34 +36,39 @@ fun Long.format(formatStr: String): String {
|
||||||
return df.format(this)
|
return df.format(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
private val defaultThrottleTimer by lazy {
|
data class ThrottleTimer(
|
||||||
Value(0L)
|
private val interval: Long = 1000L,
|
||||||
|
private var value: Long = 0L
|
||||||
|
) {
|
||||||
|
fun expired(): Boolean {
|
||||||
|
val t = System.currentTimeMillis()
|
||||||
|
if (t - value > interval) {
|
||||||
|
value = t
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
private const val defaultThrottleInterval = 1000L
|
|
||||||
|
private val defaultThrottleTimer by lazy { ThrottleTimer() }
|
||||||
|
|
||||||
fun throttle(
|
fun throttle(
|
||||||
interval: Long = defaultThrottleInterval,
|
timer: ThrottleTimer = defaultThrottleTimer,
|
||||||
timer: Value<Long> = defaultThrottleTimer,
|
|
||||||
fn: (() -> Unit),
|
fn: (() -> Unit),
|
||||||
): (() -> Unit) {
|
): (() -> Unit) {
|
||||||
return {
|
return {
|
||||||
val t = System.currentTimeMillis()
|
if (timer.expired()) {
|
||||||
if (t - timer.value > interval) {
|
|
||||||
timer.value = t
|
|
||||||
fn.invoke()
|
fn.invoke()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun <T> throttle(
|
fun <T> throttle(
|
||||||
interval: Long = defaultThrottleInterval,
|
timer: ThrottleTimer = defaultThrottleTimer,
|
||||||
timer: Value<Long> = defaultThrottleTimer,
|
|
||||||
fn: ((T) -> Unit),
|
fn: ((T) -> Unit),
|
||||||
): ((T) -> Unit) {
|
): ((T) -> Unit) {
|
||||||
return {
|
return {
|
||||||
val t = System.currentTimeMillis()
|
if (timer.expired()) {
|
||||||
if (t - timer.value > interval) {
|
|
||||||
timer.value = t
|
|
||||||
fn.invoke(it)
|
fn.invoke(it)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user