Add Duration-based delay extension helpers

This commit is contained in:
2dust committed 2026-08-16 15:32:35 +08:00
1 parent 780d194213
commit 4b3c8177db
9 files changed
+32 -15

No files matched your search

@@ -31,7 +31,7 @@ import com.v2ray.ang.util.LogUtil
import com.v2ray.ang.util.Utils
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import com.v2ray.ang.extension.delay
import kotlinx.coroutines.launch
import libv2ray.CoreCallbackHandler
import libv2ray.CoreController
@@ -7,6 +7,7 @@ import com.v2ray.ang.enums.EConfigType
import java.io.Serializable
import java.net.URI
import java.util.Locale
import kotlin.time.Duration.Companion.milliseconds
const val THRESHOLD = 1000L
const val DIVISOR = 1024.0
@@ -76,3 +77,19 @@ fun EConfigType.isGroupType(): Boolean {
fun EConfigType.isComplexType(): Boolean {
return this == EConfigType.CUSTOM || this == EConfigType.POLICYGROUP || this == EConfigType.PROXYCHAIN
}
/**
* Shorthand for delay with Int milliseconds using Duration to avoid legacy Long overload warning.
*/
suspend fun delay(millis: Int) {
kotlinx.coroutines.delay(millis.toLong().milliseconds)
}
/**
* Shorthand for delay with Long milliseconds using Duration to avoid legacy Long overload warning.
*/
suspend fun delay(millis: Long) {
kotlinx.coroutines.delay(millis.milliseconds)
}
@@ -21,7 +21,7 @@ import com.v2ray.ang.util.LogUtil
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import com.v2ray.ang.extension.delay
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import kotlin.math.min
@@ -8,7 +8,7 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.delay
import com.v2ray.ang.extension.delay
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import okhttp3.Call
@@ -10,7 +10,7 @@ import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import com.v2ray.ang.extension.delay
import kotlinx.coroutines.launch
/**
@@ -26,7 +26,7 @@ import androidx.compose.ui.unit.dp
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.compose.LocalLifecycleOwner
import androidx.lifecycle.compose.currentStateAsState
import kotlinx.coroutines.delay
import com.v2ray.ang.extension.delay
import kotlinx.coroutines.flow.collectLatest
data class ScrollbarConfig(
@@ -33,7 +33,7 @@ import androidx.lifecycle.compose.LocalLifecycleOwner
import androidx.lifecycle.repeatOnLifecycle
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.delay
import com.v2ray.ang.extension.delay
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.launch
@@ -88,7 +88,7 @@ class AppSnackbarController(
if (currentShowTime != 0L) {
val elapsed = System.currentTimeMillis() - currentShowTime
if (elapsed < SnackbarThrottleMs) {
delay((SnackbarThrottleMs - elapsed).milliseconds)
delay((SnackbarThrottleMs - elapsed))
}
}
@@ -30,7 +30,7 @@ import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.v2ray.ang.dto.entities.ProfileItem
import com.v2ray.ang.ui.compose.LocalDarkTheme
import com.v2ray.ang.ui.compose.QRCodeDialog
import kotlinx.coroutines.delay
import com.v2ray.ang.extension.delay
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.launch
@@ -130,7 +130,7 @@ fun MainScreen(
lazyListStates[target.groupId] != null
}
if (ready) return@repeat
delay(16L)
delay(16)
}
if (latestDoubleColumnDisplay) {
@@ -149,7 +149,7 @@ fun MainScreen(
}
}
} finally {
delay(32L)
delay(32)
locateInProgress = false
mainViewModel.onAction(MainAction.LocateHandled(target))
}
@@ -24,7 +24,7 @@ import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.currentCoroutineContext
import kotlinx.coroutines.delay
import com.v2ray.ang.extension.delay
import kotlinx.coroutines.ensureActive
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
@@ -222,7 +222,7 @@ class MainViewModel(
viewModelScope.launch(preloadDispatcher) {
try {
initialPageReady.await()
delay(32L)
delay(32)
dataSource.initAssets()
dataSource.syncSubscriptions()
} catch (cancelled: CancellationException) {
@@ -364,7 +364,7 @@ class MainViewModel(
preloadJob = viewModelScope.launch(preloadDispatcher) {
preloadOrder.forEach { groupId ->
ensureActive()
delay(32L)
delay(32)
val servers = loadGroup(groupId, forceRefresh)
updateGroupUi(groupId, servers)
}
@@ -617,7 +617,7 @@ class MainViewModel(
}
order.forEachIndexed { index, groupId ->
ensureActive()
if (index > 0) delay(32L)
if (index > 0) delay(32)
updateGroupUi(groupId, loadGroup(groupId, forceRefresh = true))
}
}
@@ -628,7 +628,7 @@ class MainViewModel(
keywordFilter = keyword
filterJob?.cancel()
filterJob = viewModelScope.launch(defaultDispatcher) {
delay(300L)
delay(300)
val snapshot = cacheMutex.withLock { groupDataCache.toMap() }
ensureActive()
snapshot.forEach { (groupId, servers) ->