-
Notifications
You must be signed in to change notification settings - Fork 4
/
ExampleComposables.kt
487 lines (441 loc) · 16.7 KB
/
ExampleComposables.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
package com.funny.data_saver.ui
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.AlertDialog
import androidx.compose.material.Button
import androidx.compose.material.MaterialTheme
import androidx.compose.material.OutlinedTextField
import androidx.compose.material.RadioButton
import androidx.compose.material.Switch
import androidx.compose.material.Text
import androidx.compose.material.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.ReadOnlyComposable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalInspectionMode
import androidx.compose.ui.semantics.heading
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.funny.data_saver.Constant
import com.funny.data_saver.Constant.KEY_BEAN_EXAMPLE
import com.funny.data_saver.Constant.KEY_BOOLEAN_EXAMPLE
import com.funny.data_saver.Constant.KEY_STRING_EXAMPLE
import com.funny.data_saver.core.DataSaverInMemory
import com.funny.data_saver.core.DataSaverInterface
import com.funny.data_saver.core.DataSaverMutableState
import com.funny.data_saver.core.LocalDataSaver
import com.funny.data_saver.core.SavePolicy
import com.funny.data_saver.core.getLocalDataSaverInterface
import com.funny.data_saver.core.rememberDataSaverState
import com.funny.data_saver.kmp.Log
import com.funny.data_saver.kmp.viewModel
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.Serializable
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import moe.tlaster.precompose.navigation.BackHandler
import kotlin.random.Random
/**
* Example usage of this library
*
* NOTE: You may see two languages in comments: English and Chinese,
* they HAVE THE SAME MEANING. you can choose one to read.
*
* -----------------------
* 此仓库的使用示例
* 注意:下面的注释包含两种语言:中、英文,意思一样的。
* 选一个读就好
*/
@Serializable
data class ExampleBean(var id: Int, val label: String)
val EmptyBean = ExampleBean(233, "FunnySaltyFish")
@ExperimentalSerializationApi
@Composable
fun ExampleComposable() {
// get dataSaver | 获取 DataSaverInterface
// you can use this to save data manually | 您可以使用此变量做手动保存
val dataSaverInterface = getLocalDataSaverInterface()
// support @Preview by additionally register the type converter
val isInspectMode = LocalInspectionMode.current
// you can set [savePolicy] to other types (see [SavePolicy] ) to prevent saving too frequently
// if you set it to SavePolicy.NEVER , you need to saveData by yourself
// eg: onClick = { dataSaverState.save() }
// .............................................................
// 你可以设置 [savePolicy]为其他类型(参见 [SavePolicy] ),以防止某些情况下过于频繁地保存
// 如果你设置为 SavePolicy.NEVER,则写入本地的操作需要自己做
// 例如: onClick = { dataSaverState.save() }
var stringExample by rememberDataSaverState(
KEY_STRING_EXAMPLE,
"FunnySaltyFish, tap to input",
savePolicy = SavePolicy.IMMEDIATELY,
async = true
)
var booleanExample by rememberDataSaverState(key = KEY_BOOLEAN_EXAMPLE, initialValue = false)
var beanExample by rememberDataSaverState(key = KEY_BEAN_EXAMPLE, initialValue = EmptyBean)
Column(
Modifier
.fillMaxSize()
.padding(24.dp)
.verticalScroll(rememberScrollState())
) {
Heading(text = "Simple Examples:")
Text(text = "This is an example of saving String") // 保存字符串的示例
OutlinedTextField(value = stringExample, onValueChange = {
stringExample = it
})
Text(text = "Saving Boolean") // 保存布尔值的示例
Switch(checked = booleanExample, onCheckedChange = {
booleanExample = it
})
Heading(text = "Saving Custom Data Bean") // 保存自定义类型的示例
Text(text = beanExample.toString())
Button(onClick = {
beanExample = beanExample.copy(id = beanExample.id + 1)
}) {
Text(text = "Add bean's id") // id自加
}
ParcelableExample()
ListExample()
CustomSealedClassExample()
NullableExample()
SenseExternalDataChangeExample()
SaveWhenDisposedExample()
CustomCoroutineScopeAndViewModelSample()
TimeConsumingJobExample()
}
}
@Composable
expect fun ParcelableExample()
@Composable
private fun ListExample() {
var listExample: List<ExampleBean> by rememberDataSaverState(
key = "key_list_example", initialValue = listOf(
EmptyBean.copy(label = "Name 1"),
EmptyBean.copy(label = "Name 2"),
EmptyBean.copy(label = "Name 3")
)
)
Heading(text = "List Example")
LazyColumn(Modifier.heightIn(0.dp, 400.dp)) {
items(listExample) { item ->
Text(modifier = Modifier.padding(8.dp), text = item.toString(), fontSize = 16.sp)
}
item {
Row {
Button(onClick = {
listExample += EmptyBean.copy(label = "Name ${listExample.size + 1}")
}) {
Text(text = "Add To List")
}
Spacer(modifier = Modifier.width(4.dp))
Button(onClick = {
if (listExample.isNotEmpty()) {
listExample = listExample.dropLast(1)
}
}) {
Text(text = "Remove From List")
}
}
}
}
}
@Composable
private fun SaveWhenDisposedExample() {
var showDialog by remember {
mutableStateOf(false)
}
if (showDialog) AlertDialog(
onDismissRequest = { showDialog = false },
title = { Text(text = "Sample") },
text = {
var stringExample2 by rememberDataSaverState(
key = Constant.KEY_STRING_EXAMPLE_2,
initialValue = "this one will be saved only when disposed",
savePolicy = SavePolicy.DISPOSED,
async = false
)
OutlinedTextField(value = stringExample2, onValueChange = {
stringExample2 = it
})
},
confirmButton = { TextButton(onClick = { showDialog = false }) { Text(text = "Close") } },
)
Heading(text = "Save-When-Disposed Example")
Button(onClick = { showDialog = true }) {
Text(text = "Click Me To Open Dialog")
}
}
@Composable
private fun ColumnScope.NullableExample() {
val nullableCustomBeanState: DataSaverMutableState<ExampleBean?> =
rememberDataSaverState(key = "nullable_bean", initialValue = null)
Heading(text = "Saving Custom Data Bean(nullable)") // 保存自定义类型的示例
Text(text = nullableCustomBeanState.value.toString())
Row(Modifier.fillMaxWidth()) {
Button(onClick = {
nullableCustomBeanState.value = ExampleBean(id = 100, label = "I'm not null")
}) {
Text(text = "Set As Not Null")
}
Spacer(modifier = Modifier.width(4.dp))
Button(onClick = {
nullableCustomBeanState.value = null
// nullableCustomBeanState.remove(replacement = EmptyBean)
}) {
Text(text = "Set As Null")
}
}
}
@OptIn(ExperimentalSerializationApi::class)
@Composable
private fun ColumnScope.SenseExternalDataChangeExample() {
val dataSaver = getSensorExternalDataSaver()
Heading(text = "Sense External Data Change Example")
CompositionLocalProvider(LocalDataSaver provides dataSaver) {
val key = "sense_external_data_change_example"
val stringExample by rememberDataSaverState(
key = key,
initialValue = "Hello World(1)",
senseExternalDataChange = true
)
val stringExample2 by rememberDataSaverState(
key = key,
initialValue = "Hello World(2)",
senseExternalDataChange = true
)
val dataSaverInterface = getLocalDataSaverInterface()
Text(text = "var1: $stringExample")
Text(text = "var2: $stringExample2")
Button(onClick = {
// here we change the local data instead of the state
dataSaverInterface.saveData(key, "Hello World ${Random.nextInt()}")
}) {
Text(text = "Click Me To Change Local Data")
}
Spacer(modifier = Modifier.height(4.dp))
val keyBean = "sense_external_data_change_example_bean"
val bean: ExampleBean? by rememberDataSaverState(
key = keyBean,
initialValue = null,
senseExternalDataChange = true
)
val saveBean = { b: ExampleBean? ->
dataSaverInterface.saveData(keyBean, Json.encodeToString(b))
}
Text(text = bean.toString())
Row {
// not null
Button(onClick = {
saveBean(ExampleBean(0, "not null"))
}) {
Text(text = "Not Null")
}
// id += 1
Spacer(modifier = Modifier.width(4.dp))
Button(onClick = {
bean?.copy(id = (bean?.id ?: 0) + 1)?.let(saveBean)
}) {
Text(text = "Add id by 1")
}
Spacer(modifier = Modifier.width(4.dp))
// set as null
Button(onClick = {
saveBean(null)
}) {
Text(text = "Set As Null")
}
}
Spacer(modifier = Modifier.height(4.dp))
val keyList = "sense_external_data_change_example_list"
val list by rememberDataSaverState(
key = keyList,
initialValue = listOf(ExampleBean(0, "initial")),
senseExternalDataChange = true
)
LazyColumn(Modifier.heightIn(0.dp, 200.dp)) {
items(list) { item ->
Text(modifier = Modifier.padding(8.dp), text = item.toString(), fontSize = 16.sp)
}
item {
Row {
// add
Button(onClick = {
val l = list + ExampleBean(list.size, "add")
dataSaverInterface.saveData(
keyList,
Json.encodeToString(l)
)
}) {
Text(text = "Add")
}
Spacer(modifier = Modifier.width(4.dp))
// remove
Button(onClick = {
if (list.isNotEmpty()) {
val l = list.dropLast(1)
dataSaverInterface.saveData(
keyList,
Json.encodeToString(l)
)
}
}) {
Text(text = "Remove")
}
}
}
}
}
}
@Composable
private fun CustomSealedClassExample() {
var themeType: ThemeType by rememberDataSaverState(
key = "key_theme_type",
initialValue = ThemeType.DynamicNative
)
Heading(text = "Saving custom Sealed Class") // 保存自定义类型的示例
Column(
Modifier
.background(MaterialTheme.colors.surface, RoundedCornerShape(16.dp))
.padding(8.dp)
) {
Text(
modifier = Modifier.semantics { heading() },
text = "主题/Theme",
fontSize = 32.sp,
fontWeight = FontWeight.ExtraBold
)
Spacer(modifier = Modifier.height(12.dp))
RadioTile(text = "默认", selected = themeType == ThemeType.StaticDefault) {
themeType = ThemeType.StaticDefault
}
RadioTile(text = "动态取色", selected = themeType == ThemeType.DynamicNative) {
themeType = ThemeType.DynamicNative
}
}
}
@Composable
private fun CustomCoroutineScopeAndViewModelSample() {
Heading(text = "Use custom CoroutineScope and ViewModel")
val vm: MainViewModel = viewModel()
Column(Modifier.fillMaxWidth(), horizontalAlignment = Alignment.CenterHorizontally) {
OutlinedTextField(value = vm.username, onValueChange = { vm.username = it }, label = {
Text(text = "Username")
})
OutlinedTextField(value = vm.password, onValueChange = { vm.password = it }, label = {
Text(text = "Password")
})
}
}
@Composable
private fun TimeConsumingJobExample() {
class TimeConsumingDataSaver : DataSaverInMemory() {
override suspend fun <T> saveDataAsync(key: String, data: T) {
Log.i("start to save data, it takes 5s...")
// mock time consuming, it might be HTTP request or complex data saving in real world
delay(5000)
super.saveDataAsync(key, data)
Log.i("finish saving data. key=$key, data=$data")
}
}
CompositionLocalProvider(LocalDataSaver provides TimeConsumingDataSaver()) {
Heading(text = "Time consuming example, wait until finished\n You cannot go back until it finished")
// here we pass a custom coroutineScope
val scope = remember {
CoroutineScope(Dispatchers.IO)
}
val state =
rememberDataSaverState(
key = "time_consuming_job",
initialValue = 0,
coroutineScope = scope
)
Button(onClick = {
scope.launch {
state.value = state.value + 1
}
}) {
Text(text = "Submit(curr value: ${state.value})")
}
var showDialog by remember { mutableStateOf(false) }
if (showDialog) {
AlertDialog(
onDismissRequest = { showDialog = false },
confirmButton = {
Button(onClick = { showDialog = false }) {
Text(text = "Close")
}
},
text = {
Text(text = "Current job is not finished, please wait until it is finished!")
}
)
}
LaunchedEffect(key1 = state.job) {
Log.d("ExampleComposable", "TimeConsumingJobExample job: ${state.job}")
}
BackHandler(state.job?.isCompleted == false) {
showDialog = true
Log.d("ExampleComposable", "TimeConsumingJobExample is not finished")
}
}
}
@Composable
fun PreviewExample() {
var checked by rememberDataSaverState(key = "preview_string", initialValue = false)
Switch(checked = checked, onCheckedChange = {
checked = it
})
}
@Composable
internal fun Heading(text: String) {
Spacer(modifier = Modifier.height(8.dp))
Text(text, fontWeight = FontWeight.W600, fontSize = 18.sp)
}
@Composable
private fun RadioTile(
text: String,
selected: Boolean,
onClick: () -> Unit,
) {
Row(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.SpaceBetween,
modifier = Modifier
.fillMaxWidth()
.padding(8.dp)
) {
Text(text = text, fontSize = 24.sp, fontWeight = FontWeight.W700)
RadioButton(selected = selected, onClick = onClick)
}
}
@Composable
@ReadOnlyComposable
internal expect fun getSensorExternalDataSaver(): DataSaverInterface