Skip to content

Commit

Permalink
🧑‍💻: update example, add cache&swr related examples
Browse files Browse the repository at this point in the history
  • Loading branch information
junerver committed Aug 1, 2024
1 parent b923171 commit e3af241
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -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: [email protected]
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))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()) {
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/xyz/junerver/composehooks/route/routes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -123,7 +124,7 @@ val subRequestRoutes = mapOf<String, @Composable () -> Unit>(
"depsRefresh" to { DepsRefresh() },
"debounce" to { Debounce() },
"throttle" to { Throttle() },
"cache&swr" to { TODO() },
"cache&swr" to { Cache() },
"errorRetry" to { ErrorRetry() }
)

Expand Down

0 comments on commit e3af241

Please sign in to comment.