diff --git a/app/src/main/java/xyz/junerver/composehooks/example/request/Cache&SWR.kt b/app/src/main/java/xyz/junerver/composehooks/example/request/Cache&SWR.kt new file mode 100644 index 00000000..6bfef698 --- /dev/null +++ b/app/src/main/java/xyz/junerver/composehooks/example/request/Cache&SWR.kt @@ -0,0 +1,123 @@ +package xyz.junerver.composehooks.example.request + +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.Spacer +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.height +import androidx.compose.material3.HorizontalDivider +import androidx.compose.material3.Surface +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.ui.Modifier +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.unit.dp +import kotlin.time.Duration.Companion.seconds +import xyz.junerver.compose.hooks.optionsOf +import xyz.junerver.compose.hooks.useBoolean +import xyz.junerver.compose.hooks.userequest.RequestOptions +import xyz.junerver.compose.hooks.userequest.clearCache +import xyz.junerver.compose.hooks.userequest.useRequest +import xyz.junerver.composehooks.net.WebService +import xyz.junerver.composehooks.net.asRequestFn +import xyz.junerver.composehooks.ui.component.TButton +import xyz.junerver.kotlin.asBoolean + +/* + Description: + Author: Junerver + Date: 2024/8/1-9:12 + Email: junerver@gmail.com + Version: v1.0 +*/ + +@Composable +fun Cache() { + Surface { + Column { + TestSWR() + Spacer(modifier = Modifier.height(20.dp)) + TestStaleTime() + } + } +} + +@Composable +private fun TestSWR() { + val (isVisible, toggle) = useBoolean(true) + Column { + TButton(text = "show/hide") { + toggle() + } + if (isVisible) { + SWR() + } + HorizontalDivider(modifier = Modifier.fillMaxWidth()) + if (isVisible) { + SWR(true) + } + } +} + +/** + * 当配置了[RequestOptions.cacheKey]时,发起请求时会先查询是否存在缓存, + * 如果缓存存在则优先使用缓存返回,同时发起请求,在请求成功后更新状态 + * + * @param useCache + */ +@Composable +private fun SWR(useCache: Boolean = false) { + val (userInfo, userLoading) = useRequest( + requestFn = WebService::userInfo.asRequestFn(), + optionsOf { + defaultParams = arrayOf("junerver") + if (useCache) cacheKey = "userinfo-junerver" + } + ) + Column(modifier = Modifier.height(110.dp)) { + Text(text = "cache: $useCache", color = Color.Red) + Text(text = "Background loading: $userLoading") + if (userInfo.asBoolean()) { + Text(text = "$userInfo".substring(0..100)) + } + } +} + +@Composable +fun TestStaleTime() { + val (isVisible, toggle) = useBoolean(true) + val cacheKey = "userinfo-junerver-stale" + Column { + Row { + TButton(text = "show/hide") { + toggle() + } + TButton(text = "clearCache") { + // 通过调用top-level函数 `clearCache` 可以移除指定key的缓存,该函数可以接收多个key + clearCache(cacheKey) + } + } + if (isVisible) { + StaleTime(cacheKey) + } + } +} + +@Composable +private fun StaleTime(cacheKey: String) { + val (userInfo, userLoading) = useRequest( + requestFn = WebService::userInfo.asRequestFn(), + optionsOf { + defaultParams = arrayOf("junerver") + this.cacheKey = cacheKey + staleTime = 5.seconds + } + ) + Column(modifier = Modifier.height(110.dp)) { + Text(text = "statleTime: 5000ms", color = Color.Red) + Text(text = "Background loading: $userLoading") + if (userInfo.asBoolean()) { + Text(text = "$userInfo".substring(0..100)) + } + } +} diff --git a/app/src/main/java/xyz/junerver/composehooks/example/request/Ready.kt b/app/src/main/java/xyz/junerver/composehooks/example/request/Ready.kt index c30cbf77..ca6b4528 100644 --- a/app/src/main/java/xyz/junerver/composehooks/example/request/Ready.kt +++ b/app/src/main/java/xyz/junerver/composehooks/example/request/Ready.kt @@ -4,7 +4,7 @@ import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.height -import androidx.compose.material3.Divider +import androidx.compose.material3.HorizontalDivider import androidx.compose.material3.Surface import androidx.compose.material3.Text import androidx.compose.runtime.Composable @@ -54,7 +54,7 @@ fun Ready() { Text(text = "$userInfo".substring(0..100)) } Spacer(modifier = Modifier.height(30.dp)) - Divider(modifier = Modifier.fillMaxWidth()) + HorizontalDivider(modifier = Modifier.fillMaxWidth()) if (repoLoading) { Text(text = "repo loading ...") } else if (repoInfo.asBoolean()) { diff --git a/app/src/main/java/xyz/junerver/composehooks/route/routes.kt b/app/src/main/java/xyz/junerver/composehooks/route/routes.kt index a1d80a81..4dbe5e89 100644 --- a/app/src/main/java/xyz/junerver/composehooks/route/routes.kt +++ b/app/src/main/java/xyz/junerver/composehooks/route/routes.kt @@ -44,6 +44,7 @@ import xyz.junerver.composehooks.example.UseUpdateEffectExample import xyz.junerver.composehooks.example.UseUpdateExample import xyz.junerver.composehooks.example.UseVibrateExample import xyz.junerver.composehooks.example.request.AutoManual +import xyz.junerver.composehooks.example.request.Cache import xyz.junerver.composehooks.example.request.Cancel import xyz.junerver.composehooks.example.request.Debounce import xyz.junerver.composehooks.example.request.DepsRefresh @@ -123,7 +124,7 @@ val subRequestRoutes = mapOf Unit>( "depsRefresh" to { DepsRefresh() }, "debounce" to { Debounce() }, "throttle" to { Throttle() }, - "cache&swr" to { TODO() }, + "cache&swr" to { Cache() }, "errorRetry" to { ErrorRetry() } )