Skip to content

Commit ef5402b

Browse files
revert the incorrect implementation of ListState
1 parent f4af9b9 commit ef5402b

2 files changed

Lines changed: 20 additions & 24 deletions

File tree

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.9.20'
4-
ext.version_name = "1.1.8"
4+
ext.version_name = "1.1.9"
55
ext.compose_compiler_version = "1.5.5"
66
ext.compose_bom_version = "2023.10.01"
77
repositories {

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

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package com.funny.data_saver.core
22

3-
import androidx.compose.runtime.*
4-
import androidx.compose.runtime.snapshots.SnapshotStateList
3+
import androidx.compose.runtime.Composable
4+
import androidx.compose.runtime.DisposableEffect
5+
import androidx.compose.runtime.LaunchedEffect
6+
import androidx.compose.runtime.MutableState
7+
import androidx.compose.runtime.mutableStateOf
8+
import androidx.compose.runtime.remember
59
import com.funny.data_saver.core.DataSaverConverter.findRestorer
610
import kotlinx.coroutines.CoroutineScope
711
import kotlinx.coroutines.Dispatchers
@@ -33,15 +37,15 @@ class DataSaverMutableListState<T>(
3337
private val savePolicy: SavePolicy = SavePolicy.IMMEDIATELY,
3438
private val async: Boolean = false,
3539
private val coroutineScope: CoroutineScope? = null
36-
) : MutableState<SnapshotStateList<T>> {
37-
private var stateList = initialValue.toMutableStateList()
40+
) : MutableState<List<T>> {
41+
private val listState = mutableStateOf(initialValue)
3842
private var job: Job? = null
3943
private val scope by lazy(LazyThreadSafetyMode.PUBLICATION) {
4044
coroutineScope ?: CoroutineScope(Dispatchers.IO)
4145
}
4246

43-
override var value: SnapshotStateList<T>
44-
get() = stateList
47+
override var value: List<T>
48+
get() = listState.value
4549
set(value) {
4650
doSetValue(value)
4751
}
@@ -62,11 +66,11 @@ class DataSaverMutableListState<T>(
6266
if (autoSave) SavePolicy.IMMEDIATELY else SavePolicy.NEVER
6367
)
6468

65-
operator fun setValue(thisObj: Any?, property: KProperty<*>, value: SnapshotStateList<T>) {
69+
operator fun setValue(thisObj: Any?, property: KProperty<*>, value: List<T>) {
6670
doSetValue(value)
6771
}
6872

69-
operator fun getValue(thisObj: Any?, property: KProperty<*>): SnapshotStateList<T> = stateList
73+
operator fun getValue(thisObj: Any?, property: KProperty<*>): List<T> = listState.value
7074

7175
fun saveData() {
7276
val value = value
@@ -86,35 +90,27 @@ class DataSaverMutableListState<T>(
8690
}
8791
}
8892

89-
fun valueChangedSinceInit() = stateList.deepEquals(initialValue)
93+
fun valueChangedSinceInit() = listState.value.deepEquals(initialValue.toList())
9094

9195
/**
9296
* remove the key and set the value to `replacement`
9397
* @param replacement List<T> new value of the state, `initialValue` by default
9498
*/
9599
fun remove(replacement: List<T> = initialValue) {
96100
dataSaverInterface.remove(key)
97-
clearAndAddAll(replacement)
101+
listState.value = replacement
98102
log("remove: key: $key, replace the value to $replacement")
99103
}
100104

101105
fun setValueWithoutSave(v: List<T>) {
102-
if (!v.deepEquals(stateList)) {
103-
clearAndAddAll(v)
104-
}
106+
if (!v.deepEquals(listState.value)) listState.value = v
105107
}
106108

107109
private fun doSetValue(value: List<T>) {
108-
val oldValue = this.stateList
109-
clearAndAddAll(value)
110-
if (savePolicy == SavePolicy.IMMEDIATELY && !oldValue.deepEquals(value)) {
110+
val oldValue = this.listState.value
111+
this.listState.value = value
112+
if (!oldValue.deepEquals(value) && savePolicy == SavePolicy.IMMEDIATELY)
111113
saveData()
112-
}
113-
}
114-
115-
private fun clearAndAddAll(list: List<T>) {
116-
stateList.clear()
117-
stateList.addAll(list)
118114
}
119115

120116
companion object {
@@ -129,7 +125,7 @@ class DataSaverMutableListState<T>(
129125
}
130126
}
131127

132-
override fun component1(): SnapshotStateList<T> = value
128+
override fun component1(): List<T> = value
133129

134130
override fun component2(): (List<T>) -> Unit = ::doSetValue
135131
}

0 commit comments

Comments
 (0)