Skip to content

Commit 888a526

Browse files
v1.1.4 released!
1 parent 15fd62c commit 888a526

File tree

7 files changed

+56
-58
lines changed

7 files changed

+56
-58
lines changed

app/src/main/java/com/funny/composedatasaver/ExampleActivity.kt

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,9 @@ import androidx.datastore.preferences.preferencesDataStore
1111
import com.funny.composedatasaver.ui.ExampleBean
1212
import com.funny.composedatasaver.ui.ExampleComposable
1313
import com.funny.composedatasaver.ui.theme.FunnyTheme
14-
import com.funny.data_saver.core.DataSaverPreferences
15-
import com.funny.data_saver.core.DataSaverPreferences.Companion.setContext
14+
import com.funny.data_saver.core.DataSaverConverter.registerTypeConverters
1615
import com.funny.data_saver.core.LocalDataSaver
17-
import com.funny.data_saver.core.registerTypeConverters
18-
import com.funny.data_saver_mmkv.DataSaverMMKV
19-
import com.funny.data_saver_mmkv.DataSaverMMKV.Companion.setKV
16+
import com.funny.data_saver_mmkv.DefaultDataSaverMMKV
2017
import com.tencent.mmkv.MMKV
2118
import kotlinx.serialization.ExperimentalSerializationApi
2219
import kotlinx.serialization.decodeFromString
@@ -31,23 +28,18 @@ class ExampleActivity : ComponentActivity() {
3128
super.onCreate(savedInstanceState)
3229

3330
// init preferences
34-
val dataSaverPreferences = DataSaverPreferences().apply {
35-
setContext(context = applicationContext)
36-
}
31+
// val dataSaverPreferences = DataSaverPreferences(applicationContext)
3732

3833
// If you want to use [MMKV](https://github.com/Tencent/MMKV) to save data
3934
MMKV.initialize(applicationContext)
40-
val dataSaverMMKV = DataSaverMMKV().apply {
41-
setKV(newKV = MMKV.defaultMMKV())
42-
}
35+
val dataSaverMMKV = DefaultDataSaverMMKV
36+
// you can use DefaultDataSaverMMKV like `DefaultDataSaverMMKV.readData(key, default)` and `DefaultDataSaverMMKV.saveData(key, value) anywhere`
4337

4438
// if you want to use [DataStorePreference](https://developer.android.google.cn/jetpack/androidx/releases/datastore) to save data
45-
// val dataSaverDataStorePreferences = DataSaverDataStorePreferences().apply {
46-
// setDataStorePreferences(applicationContext.dataStore)
47-
// }
39+
// val dataSaverDataStorePreferences = DataSaverDataStorePreferences(applicationContext.dataStore)
4840

4941
// cause we want to save custom bean, we provide a converter to convert it into String
50-
registerTypeConverters<ExampleBean>(
42+
registerTypeConverters<ExampleBean?>(
5143
save = { bean -> Json.encodeToString(bean) },
5244
restore = { str -> Json.decodeFromString(str) }
5345
)

app/src/main/java/com/funny/composedatasaver/ui/ExampleComposables.kt

Lines changed: 46 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ import androidx.compose.foundation.verticalScroll
88
import androidx.compose.material.*
99
import androidx.compose.runtime.*
1010
import androidx.compose.ui.Modifier
11-
import androidx.compose.ui.platform.LocalContext
1211
import androidx.compose.ui.text.font.FontWeight
13-
import androidx.compose.ui.tooling.preview.Preview
1412
import androidx.compose.ui.unit.dp
1513
import androidx.compose.ui.unit.sp
1614
import com.funny.composedatasaver.Constant
@@ -19,8 +17,6 @@ import com.funny.composedatasaver.Constant.KEY_BOOLEAN_EXAMPLE
1917
import com.funny.composedatasaver.Constant.KEY_STRING_EXAMPLE
2018
import com.funny.composedatasaver.ExampleParcelable
2119
import com.funny.data_saver.core.*
22-
import com.funny.data_saver_mmkv.DataSaverMMKV
23-
import com.tencent.mmkv.MMKV
2420
import kotlinx.serialization.ExperimentalSerializationApi
2521
import 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
180187
fun Heading(text: String) {
181188
Text(text, fontWeight = FontWeight.W600, fontSize = 18.sp)

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Top-level build file where you can add configuration options common to all sub-projects/modules.
22
buildscript {
33
ext.kotlin_version = '1.7.10'
4-
ext.version_name = "1.1.2"
4+
ext.version_name = "1.1.4"
55
ext.compose_version = "1.3.0"
66
repositories {
77
google()

data_saver_core/src/main/java/com/funny/data_saver/core/SavePolicy.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ open class SavePolicy {
1515

1616
/**
1717
* do data persistence when the Composable enters `onDispose`. NOTE: USE THIS MODE CAREFULLY, BECAUSE SOMETIME
18-
* `onDispose` will not be called
18+
* `onDispose` WILL NOT BE CALLED
1919
*
2020
* Composable `onDispose` 时做数据持久化,适合数据变动比较频繁、且此Composable会进入onDispose的情况。
2121
* **慎用此模式,因为有些情况下onDispose不会被回调**

data_saver_data_store_preferences/src/main/java/com/funny/data_saver_data_store/DataSaverDataStore.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ class DataSaverDataStorePreferences(private val dataStore: DataStore<Preferences
3636
// Reference:https://blog.csdn.net/qq_36707431/article/details/119447093
3737
private suspend fun <T> get(dataStore: DataStore<Preferences>, key: String, default: T): T {
3838
return when (default) {
39-
null -> default
4039
is Int -> {
4140
dataStore.data.map { setting ->
4241
setting[intPreferencesKey(key)] ?: default

data_saver_mmkv/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ dependencies {
7272
implementation "androidx.compose.ui:ui:$compose_version"
7373
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
7474

75-
implementation 'com.tencent:mmkv:1.2.12'
75+
implementation 'com.tencent:mmkv:1.2.14'
7676
implementation project(path: ":data_saver_core")
7777

7878
testImplementation 'junit:junit:4.13.2'

demo.apk

109 KB
Binary file not shown.

0 commit comments

Comments
 (0)