@@ -8,9 +8,7 @@ import androidx.compose.foundation.verticalScroll
88import androidx.compose.material.*
99import androidx.compose.runtime.*
1010import androidx.compose.ui.Modifier
11- import androidx.compose.ui.platform.LocalContext
1211import androidx.compose.ui.text.font.FontWeight
13- import androidx.compose.ui.tooling.preview.Preview
1412import androidx.compose.ui.unit.dp
1513import androidx.compose.ui.unit.sp
1614import com.funny.composedatasaver.Constant
@@ -19,8 +17,6 @@ import com.funny.composedatasaver.Constant.KEY_BOOLEAN_EXAMPLE
1917import com.funny.composedatasaver.Constant.KEY_STRING_EXAMPLE
2018import com.funny.composedatasaver.ExampleParcelable
2119import com.funny.data_saver.core.*
22- import com.funny.data_saver_mmkv.DataSaverMMKV
23- import com.tencent.mmkv.MMKV
2420import kotlinx.serialization.ExperimentalSerializationApi
2521import kotlinx.serialization.Serializable
2622
@@ -37,8 +33,9 @@ import kotlinx.serialization.Serializable
3733 */
3834
3935@Serializable
40- data class ExampleBean (var id : Int , val label : String )
41- val EmptyBean = ExampleBean (233 ," FunnySaltyFish" )
36+ data class ExampleBean (var id : Int , val label : String )
37+
38+ val EmptyBean = ExampleBean (233 , " FunnySaltyFish" )
4239
4340
4441@ExperimentalSerializationApi
@@ -55,15 +52,24 @@ fun ExampleComposable() {
5552 // 你可以设置 [savePolicy]为其他类型(参见 [SavePolicy] ),以防止某些情况下过于频繁地保存
5653 // 如果你设置为 SavePolicy.NEVER,则写入本地的操作需要自己做
5754 // 例如: onClick = { dataSaverState.save() }
58- var stringExample by rememberDataSaverState(KEY_STRING_EXAMPLE , " " , savePolicy = SavePolicy .IMMEDIATELY , async = true )
55+ var stringExample by rememberDataSaverState(
56+ KEY_STRING_EXAMPLE ,
57+ " " ,
58+ savePolicy = SavePolicy .IMMEDIATELY ,
59+ async = true
60+ )
5961
6062 var booleanExample by rememberDataSaverState(KEY_BOOLEAN_EXAMPLE , false )
6163
6264 var beanExample by rememberDataSaverState(KEY_BEAN_EXAMPLE , default = EmptyBean )
6365
64- var listExample by rememberDataSaverListState(key = " key_list_example" , default = listOf (
65- EmptyBean .copy(label = " Name 1" ), EmptyBean .copy(label = " Name 2" ),EmptyBean .copy(label = " Name 3" )
66- ))
66+ var listExample by rememberDataSaverListState(
67+ key = " key_list_example" , default = listOf (
68+ EmptyBean .copy(label = " Name 1" ),
69+ EmptyBean .copy(label = " Name 2" ),
70+ EmptyBean .copy(label = " Name 3" )
71+ )
72+ )
6773
6874 // Among our basic implementations, only MMKV supports `Parcelable` by default
6975 var parcelableExample by rememberDataSaverState(
@@ -90,34 +96,59 @@ fun ExampleComposable() {
9096
9197 Text (text = " This is an example of saving Parcelable" ) // 保存布尔值的示例
9298 Text (parcelableExample.toString())
93- Button (onClick = { parcelableExample = parcelableExample.copy(age = parcelableExample.age + 1 ) }) {
99+ Button (onClick = {
100+ parcelableExample = parcelableExample.copy(age = parcelableExample.age + 1 )
101+ }) {
94102 Text (text = " Add age by 1" )
95103 }
96104
97105 Text (text = " This is an example of saving custom Data Bean" ) // 保存自定义类型的示例
98106 Text (text = beanExample.toString())
99107 Button (onClick = {
100- beanExample = beanExample.copy(id = beanExample.id+ 1 )
108+ beanExample = beanExample.copy(id = beanExample.id + 1 )
101109 }) {
102110 Text (text = " Add bean's id" ) // id自加
103111 }
104112
113+ val nullableCustomBeanState: DataSaverMutableState <ExampleBean ?> = rememberDataSaverState(key = " nullable_bean" , initialValue = null )
114+ Text (text = " This is an example of saving custom Data Bean(nullable)" ) // 保存自定义类型的示例
115+ Text (text = nullableCustomBeanState.value.toString())
116+ Row (Modifier .fillMaxWidth()) {
117+ Button (onClick = {
118+ nullableCustomBeanState.value = ExampleBean (id = 100 , label = " I'm not null" )
119+ }) {
120+ Text (text = " Set As Not Null" )
121+ }
122+ Button (onClick = {
123+ nullableCustomBeanState.value = null
124+ // nullableCustomBeanState.remove(replacement = EmptyBean)
125+ }) {
126+ Text (text = " Set As Null" )
127+ }
128+ }
129+
130+
105131 Spacer (modifier = Modifier .height(16 .dp))
106132 Heading (text = " Save-When-Disposed Examples:" )
107133 SaveWhenDisposedExample ()
108134
109135 Spacer (modifier = Modifier .height(16 .dp))
110136 Heading (text = " List Example" )
111137 LazyColumn (Modifier .heightIn(0 .dp, 400 .dp)) {
112- items(listExample){ item ->
138+ items(listExample) { item ->
113139 Text (modifier = Modifier .padding(8 .dp), text = item.toString(), fontSize = 16 .sp)
114140 }
115141 item {
116142 Row {
117- Button (onClick = { listExample = listExample + EmptyBean .copy(label = " Name ${listExample.size + 1 } " ) }) {
143+ Button (onClick = {
144+ listExample =
145+ listExample + EmptyBean .copy(label = " Name ${listExample.size + 1 } " )
146+ }) {
118147 Text (text = " Add To List" )
119148 }
120- Button (onClick = { if (listExample.isNotEmpty()) listExample = listExample.dropLast(1 ) }) {
149+ Button (onClick = {
150+ if (listExample.isNotEmpty()) listExample = listExample.dropLast(1 )
151+ }) {
121152 Text (text = " Remove From List" )
122153 }
123154 }
@@ -152,30 +183,6 @@ private fun SaveWhenDisposedExample() {
152183 }
153184}
154185
155-
156- @OptIn(ExperimentalSerializationApi ::class )
157- @Preview
158- @Composable
159- fun PreViewExample () {
160- val context = LocalContext .current
161- SideEffect {
162- MMKV .initialize(context.applicationContext)
163- }
164-
165- val dataSaverMMKV = remember {
166- DataSaverMMKV ().apply {
167- DataSaverMMKV .setKV(newKV = MMKV .defaultMMKV())
168- }
169- }
170-
171- CompositionLocalProvider (LocalDataSaver provides dataSaverMMKV){
172- // or LocalDataSaver provides dataSaverMMKV
173- // or LocalDataSaver provides dataSaverDataStorePreferences
174- // or your Class instance
175- ExampleComposable ()
176- }
177- }
178-
179186@Composable
180187fun Heading (text : String ) {
181188 Text (text, fontWeight = FontWeight .W600 , fontSize = 18 .sp)
0 commit comments