-
Notifications
You must be signed in to change notification settings - Fork 5
/
UseReduxExample.kt
296 lines (274 loc) · 8.68 KB
/
UseReduxExample.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package xyz.junerver.composehooks.example
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.layout.padding
import androidx.compose.material3.Divider
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import kotlin.random.Random
import kotlin.time.Duration.Companion.seconds
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import xyz.junerver.compose.hooks.Reducer
import xyz.junerver.compose.hooks.useGetState
import xyz.junerver.compose.hooks.useredux.createStore
import xyz.junerver.compose.hooks.useredux.useDispatch
import xyz.junerver.compose.hooks.useredux.useDispatchAsync
import xyz.junerver.compose.hooks.useredux.useSelector
import xyz.junerver.composehooks.MainActivity
import xyz.junerver.composehooks.net.NetApi
import xyz.junerver.composehooks.net.bean.UserInfo
import xyz.junerver.composehooks.ui.component.TButton
import xyz.junerver.composehooks.utils.NanoId
data class Todo(val name: String, val id: String)
sealed interface TodoAction
data class AddTodo(val todo: Todo) : TodoAction
data class DelTodo(val id: String) : TodoAction
val todoReducer: Reducer<List<Todo>, TodoAction> = { prevState: List<Todo>, action: TodoAction ->
when (action) {
is AddTodo -> buildList {
addAll(prevState)
add(action.todo)
}
is DelTodo -> prevState.filter { it.id != action.id }
}
}
val fetchReducer: Reducer<NetFetchResult<*>, NetFetchResult<*>> = { _, action ->
action
}
/**
* 通过使用[createStore]函数创建状态存储对象
*
* Create a state store object by using the [createStore] function
*/
val simpleStore = createStore(arrayOf(logMiddleware())) {
simpleReducer with SimpleData("default", 18)
todoReducer with emptyList()
}
val fetchStore = createStore {
arrayOf("fetch1", "fetch2").forEach {
named(it) {
fetchReducer with NetFetchResult.Idle
}
}
}
@Composable
fun UseReduxExample() {
/** store provide by root component,see at [MainActivity] */
Surface {
Column(
modifier = Modifier
.padding(20.dp)
) {
SimpleDataContainer()
Divider(
modifier = Modifier
.fillMaxWidth()
.padding(top = 20.dp, bottom = 20.dp)
)
TodosListContainer()
Divider(
modifier = Modifier
.fillMaxWidth()
.padding(top = 20.dp, bottom = 20.dp)
)
UseReduxFetch()
Divider(
modifier = Modifier
.fillMaxWidth()
.padding(top = 20.dp, bottom = 20.dp)
)
UseReduxFetch2()
}
}
}
@Composable
private fun TodosListContainer() {
Column {
Header()
TodoList()
}
}
@Composable
fun TodoList() {
/**
* 通过[useSelector]函数可以快速获取 store 中保存的对应的状态对象;
*
* The corresponding state object saved in the store can be quickly
* obtained through the [useSelector] function;
*/
val todos = useSelector<List<Todo>>()
Column {
todos.map {
TodoItem(item = it)
}
}
}
@Composable
private fun Header() {
/**
* 通过[useDispatch]可以快速获取对应Action的 dispatch 函数
*
* You can quickly obtain the dispatch function corresponding to the Action
* through [useDispatch]
*/
val dispatch = useDispatch<TodoAction>()
val (input, setInput) = useGetState("")
Row {
OutlinedTextField(
value = input,
onValueChange = setInput
)
TButton(text = "add") {
dispatch(AddTodo(Todo(input, NanoId.generate())))
setInput("")
}
}
}
@Composable
private fun TodoItem(item: Todo) {
val dispatch = useDispatch<TodoAction>()
Row(modifier = Modifier.fillMaxWidth()) {
Text(text = item.name)
TButton(text = "del") {
dispatch(DelTodo(item.id))
}
}
}
@Composable
private fun SimpleDataContainer() {
Column {
SubSimpleDataStateText()
SubSimpleDataStateText2()
Spacer(modifier = Modifier.height(10.dp))
SubSimpleDataDispatch()
}
}
@Composable
private fun SubSimpleDataStateText() {
/**
* 使用[useSelector]的另一个重载,你可以轻松的对状态进行变形,或者只取状态对象的部分属性作为你要关注的状态;
*
* Using another overload of [use Selector], you can easily transform the
* state, or only take some attributes of the state object as the state you
* want to focus on;
*/
val name = useSelector<SimpleData, String> { name }
Text(text = "User Name: $name")
}
@Composable
private fun SubSimpleDataStateText2() {
val age = useSelector<SimpleData, String> { "age : $age" }
Text(text = "User $age")
}
@Composable
private fun SubSimpleDataDispatch() {
val (input, setInput) = useGetState("")
val dispatch = useDispatch<SimpleAction>()
/**
* 使用[useDispatchAsync]你可以获得一个异步的dispatch函数,它允许你在当前组件的协程中执行异步操作,
* 异步函数的返回值是Action。
*
* Using [useDispatchAsync] you can obtain an asynchronous dispatch
* function, which allows you to perform asynchronous operations in the
* coroutine of the current component. The return value of the asynchronous
* function is Action.
*/
val asyncDispatch = useDispatchAsync<SimpleAction>()
Column {
OutlinedTextField(value = input, onValueChange = setInput)
Row {
val scope = rememberCoroutineScope()
TButton(text = "changeName") {
scope.launch {
// 异步任务
delay(1.seconds)
dispatch(SimpleAction.ChangeName(input))
}
}
TButton(text = "Async changeName") {
asyncDispatch {
delay(1.seconds)
SimpleAction.ChangeName(input)
}
}
TButton(text = "+1") {
dispatch(SimpleAction.AgeIncrease)
}
}
}
}
sealed interface NetFetchResult<out T> {
data class Success<T>(val data: T) : NetFetchResult<T>
data class Error(val msg: Throwable) : NetFetchResult<Nothing>
data object Idle : NetFetchResult<Nothing>
data object Loading : NetFetchResult<Nothing>
}
@Composable
fun UseReduxFetch() {
val fetchResult: NetFetchResult<String> = useSelector("fetch1")
val dispatchFetch = useFetch<String>("fetch1")
Column {
Text(text = "delay 2 seconds, throw error\nresult: $fetchResult")
TButton(text = "fetch") {
dispatchFetch {
delay(2.seconds)
error("fetch error")
}
}
}
}
@Composable
fun UseReduxFetch2() {
val fetchResult: NetFetchResult<UserInfo> = useSelector("fetch2")
val dispatchFetch = useFetch<UserInfo>("fetch2")
Column {
TButton(text = "fetch2") {
dispatchFetch {
if (Random.nextDouble() > 0.5) {
NetApi.SERVICE.userInfo("junerver")
} else {
error("custom err!")
}
}
}
when (fetchResult) {
is NetFetchResult.Error -> {
Text("err: ${fetchResult.msg}")
}
NetFetchResult.Idle -> {
Text(text = "idel")
}
NetFetchResult.Loading -> {
Text(text = "loading")
}
is NetFetchResult.Success -> {
Text(text = "succ: ${fetchResult.data}")
}
}
}
}
typealias ReduxFetch<T> = (block: suspend CoroutineScope.() -> T) -> Unit
@Composable
fun <T> useFetch(alias: String): ReduxFetch<T> {
val dispatchAsync =
useDispatchAsync<NetFetchResult<T>>(alias, onBefore = { it(NetFetchResult.Loading) })
return { block ->
dispatchAsync {
try {
NetFetchResult.Success(block())
} catch (t: Throwable) {
NetFetchResult.Error(t)
}
}
}
}