Skip to content

Commit

Permalink
🧑‍💻: update example
Browse files Browse the repository at this point in the history
  • Loading branch information
junerver committed Nov 8, 2024
1 parent d2f1575 commit 7b700aa
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ package xyz.junerver.composehooks.example

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.foundation.layout.padding
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
Expand All @@ -15,6 +12,7 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import xyz.junerver.compose.hooks.*
import xyz.junerver.composehooks.example.request.DividerSpacer
import xyz.junerver.composehooks.ui.component.TButton

/*
Expand Down Expand Up @@ -60,7 +58,7 @@ fun UseContextExample() {
Text(
"Use `useContext` to easily pass states or events to child components, avoiding layer-by-layer transmission between components"
)
HorizontalDivider(modifier = Modifier.fillMaxWidth().padding(top = 20.dp, bottom = 20.dp))
DividerSpacer()
Text(text = "ChileOne:")
ChildOne()
Spacer(modifier = Modifier.height(20.dp))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
package xyz.junerver.composehooks.example

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import kotlin.time.Duration.Companion.seconds
import kotlinx.datetime.LocalDateTime
import kotlinx.datetime.TimeZone
import kotlinx.datetime.toInstant
import xyz.junerver.compose.hooks.useCountdown
import xyz.junerver.compose.hooks.useState
import xyz.junerver.composehooks.example.request.DividerSpacer

/*
Description:
Expand All @@ -32,7 +28,7 @@ fun UseCountdownExample() {
Surface {
Column {
SimpleOne()
HorizontalDivider(modifier = Modifier.fillMaxWidth().padding(top = 20.dp, bottom = 20.dp))
DividerSpacer()
SimpleTwo()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@ package xyz.junerver.composehooks.example

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
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 kotlin.random.Random
import xyz.junerver.compose.hooks.useEventPublish
import xyz.junerver.compose.hooks.useEventSubscribe
import xyz.junerver.compose.hooks.useGetState
import xyz.junerver.composehooks.example.request.DividerSpacer
import xyz.junerver.composehooks.ui.component.TButton

/*
Expand Down Expand Up @@ -58,6 +56,6 @@ fun SubComponent(index: Int) {
refresh()
}
}
HorizontalDivider(modifier = Modifier.fillMaxWidth())
DividerSpacer()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ package xyz.junerver.composehooks.example

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.foundation.layout.padding
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
Expand All @@ -24,6 +22,7 @@ import xyz.junerver.compose.hooks.tuple
import xyz.junerver.compose.hooks.useGetState
import xyz.junerver.compose.hooks.useLatestRef
import xyz.junerver.compose.hooks.useState
import xyz.junerver.composehooks.example.request.DividerSpacer
import xyz.junerver.composehooks.ui.component.TButton

/*
Expand All @@ -49,21 +48,17 @@ fun UseStateExample() {
Text(text = "this is a simple controlled component:")
OutlinedTextField(value = state, onValueChange = setState)
Text(text = "input:$state")
HorizontalDivider(modifier = Modifier.fillMaxWidth().padding(top = 20.dp))
DividerSpacer()
Text(
"Don't destructure `useState`, it will cause the following problems:",
modifier = Modifier.padding(top = 20.dp, bottom = 20.dp)
)
UseStateQuestionOne()
Spacer(modifier = Modifier.height(20.dp))
HorizontalDivider(
modifier = Modifier
.fillMaxWidth()
.padding(20.dp)
)
DividerSpacer()
Spacer(modifier = Modifier.height(20.dp))
UseStateQuestionTwo()
HorizontalDivider(modifier = Modifier.fillMaxWidth().padding(top = 20.dp, bottom = 20.dp))
DividerSpacer()
Text("Demonstrates how to avoid closure problems, please see the sample code")
HowToAvoidClosureProblems()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ 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.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
Expand Down Expand Up @@ -38,7 +36,7 @@ fun AutoManual() {
) {
Auto()
Spacer(modifier = Modifier.height(10.dp))
HorizontalDivider(modifier = Modifier.fillMaxWidth())
DividerSpacer()
Manual()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,11 @@ 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.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import kotlin.coroutines.coroutineContext
import kotlin.random.Random
import kotlin.time.Duration.Companion.seconds
Expand Down Expand Up @@ -56,8 +50,7 @@ fun Cache() {
Surface {
Column {
TestSWR()
Spacer(modifier = Modifier.height(20.dp))
HorizontalDivider(modifier = Modifier.fillMaxWidth())
DividerSpacer()
TestStaleTime()
}
}
Expand All @@ -73,7 +66,7 @@ private fun TestSWR() {
if (isVisible.value) {
SWR()
}
HorizontalDivider(modifier = Modifier.fillMaxWidth())
DividerSpacer()
if (isVisible.value) {
SWR(true)
}
Expand All @@ -98,7 +91,7 @@ private fun SWR(useCache: Boolean = false) {
)
val data by dataState
val loading by loadingState
Column(modifier = Modifier.height(210.dp)) {
Column {
Text(text = "cache: $useCache", color = Color.Red)
Text(text = "Background loading: $loading")
if (data.asBoolean()) {
Expand Down Expand Up @@ -143,7 +136,7 @@ private fun StaleTime(cacheKey: String) {
)
val data by dataState
val loading by loadingState
Column(modifier = Modifier.height(210.dp)) {
Column {
Text(text = "statleTime: 5s", color = Color.Red)
Text(text = "Background loading: $loading")
if (data.asBoolean()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@ package xyz.junerver.composehooks.example.request

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import xyz.junerver.compose.hooks.invoke
import xyz.junerver.compose.hooks.userequest.useRequest
import xyz.junerver.compose.hooks.utils.asBoolean
Expand Down Expand Up @@ -45,7 +42,7 @@ fun Cancel() {
cancel()
}
}
HorizontalDivider(modifier = Modifier.fillMaxWidth())
DividerSpacer()

if (loading) {
Text(text = "Loading ...")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package xyz.junerver.composehooks.example.request

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.HorizontalDivider
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
Expand Down Expand Up @@ -60,7 +58,7 @@ fun Ready() {
Text(text = "$userInfo".substring(0..100))
}
Spacer(modifier = Modifier.height(30.dp))
HorizontalDivider(modifier = Modifier.fillMaxWidth())
DividerSpacer()
if (repoLoading) {
Text(text = "repo loading ...")
} else if (repoInfo.asBoolean()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package xyz.junerver.composehooks.example.request

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.foundation.layout.padding
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
Expand All @@ -12,6 +12,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import kotlin.time.Duration.Companion.seconds
import xyz.junerver.compose.hooks.*
import xyz.junerver.compose.hooks.userequest.RequestOptions
import xyz.junerver.compose.hooks.userequest.useRequest
import xyz.junerver.compose.hooks.utils.asBoolean
import xyz.junerver.composehooks.net.NetApi
Expand All @@ -27,10 +28,7 @@ import xyz.junerver.composehooks.ui.component.TButton

@Composable
fun DividerSpacer() {
Column {
HorizontalDivider(modifier = Modifier.fillMaxWidth())
Spacer(modifier = Modifier.height(20.dp))
}
HorizontalDivider(modifier = Modifier.fillMaxWidth().padding(top = 20.dp, bottom = 20.dp))
}

enum class OptionFunc {
Expand Down Expand Up @@ -61,9 +59,9 @@ fun SubComponent(label: String, isUsed: Boolean = false, optionFunc: OptionFunc)
when (optionFunc) {
OptionFunc.LoadingDelay -> run {
/**
* 当你配置了[loadingDelay],如果请求在这个时间之内返回就不会引起loading的变化,
* 当你配置了[RequestOptions.loadingDelay],如果请求在这个时间之内返回就不会引起loading的变化,
* 这可以避免闪烁,适用于接口较为快速返回的场景。
* When you configure [loadingDelay], if the request response within this duration,
* When you configure [RequestOptions.loadingDelay], if the request response within this duration,
* it will not cause loading state changes.
* This can avoid flickering and is suitable for scenarios
* where the interface returns quickly.
Expand Down Expand Up @@ -97,7 +95,7 @@ fun SubComponent(label: String, isUsed: Boolean = false, optionFunc: OptionFunc)
val userInfo by userInfoState
val loading by loadingState
Column(modifier = Modifier.height(100.dp)) {
Text(text = "$label:$isUsed")
Text(text = "$label:$isUsed${if (isUsed) " $label wait 3seconds" else ""}")
if (loading) {
Text(text = "Loading ...")
} else if (userInfo.asBoolean()) {
Expand Down

0 comments on commit 7b700aa

Please sign in to comment.