@@ -6,7 +6,7 @@ import androidx.compose.foundation.layout.Spacer
66import androidx.compose.foundation.layout.fillMaxWidth
77import androidx.compose.foundation.layout.height
88import androidx.compose.foundation.layout.padding
9- import androidx.compose.material3.Divider
9+ import androidx.compose.material3.HorizontalDivider
1010import androidx.compose.material3.OutlinedTextField
1111import androidx.compose.material3.Surface
1212import androidx.compose.material3.Text
@@ -16,6 +16,10 @@ import androidx.compose.ui.Modifier
1616import androidx.compose.ui.unit.dp
1717import kotlin.random.Random
1818import kotlin.time.Duration.Companion.seconds
19+ import kotlinx.collections.immutable.PersistentList
20+ import kotlinx.collections.immutable.mutate
21+ import kotlinx.collections.immutable.persistentListOf
22+ import kotlinx.collections.immutable.plus
1923import kotlinx.coroutines.CoroutineScope
2024import kotlinx.coroutines.delay
2125import kotlinx.coroutines.launch
@@ -37,14 +41,13 @@ sealed interface TodoAction
3741data class AddTodo (val todo : Todo ) : TodoAction
3842data class DelTodo (val id : String ) : TodoAction
3943
40- val todoReducer: Reducer <List <Todo >, TodoAction > = { prevState: List <Todo >, action: TodoAction ->
44+ val todoReducer: Reducer <PersistentList <Todo >, TodoAction > = { prevState: PersistentList <Todo >, action: TodoAction ->
4145 when (action) {
42- is AddTodo -> buildList {
43- addAll(prevState)
44- add(action.todo)
45- }
46+ is AddTodo -> prevState + action.todo
4647
47- is DelTodo -> prevState.filter { it.id != action.id }
48+ is DelTodo -> prevState.mutate { mutator ->
49+ mutator.removeIf { it.id == action.id }
50+ }
4851 }
4952}
5053val fetchReducer: Reducer <NetFetchResult <* >, NetFetchResult <* >> = { _, action ->
@@ -58,7 +61,7 @@ val fetchReducer: Reducer<NetFetchResult<*>, NetFetchResult<*>> = { _, action ->
5861 */
5962val simpleStore = createStore(arrayOf(logMiddleware())) {
6063 simpleReducer with SimpleData (" default" , 18 )
61- todoReducer with emptyList ()
64+ todoReducer with persistentListOf ()
6265}
6366
6467val fetchStore = createStore {
@@ -78,19 +81,19 @@ fun UseReduxExample() {
7881 .padding(20 .dp)
7982 ) {
8083 SimpleDataContainer ()
81- Divider (
84+ HorizontalDivider (
8285 modifier = Modifier
8386 .fillMaxWidth()
8487 .padding(top = 20 .dp, bottom = 20 .dp)
8588 )
8689 TodosListContainer ()
87- Divider (
90+ HorizontalDivider (
8891 modifier = Modifier
8992 .fillMaxWidth()
9093 .padding(top = 20 .dp, bottom = 20 .dp)
9194 )
9295 UseReduxFetch ()
93- Divider (
96+ HorizontalDivider (
9497 modifier = Modifier
9598 .fillMaxWidth()
9699 .padding(top = 20 .dp, bottom = 20 .dp)
@@ -116,7 +119,7 @@ fun TodoList() {
116119 * The corresponding state object saved in the store can be quickly
117120 * obtained through the [useSelector] function;
118121 */
119- val todos = useSelector<List <Todo >>()
122+ val todos = useSelector<PersistentList <Todo >>()
120123 Column {
121124 todos.map {
122125 TodoItem (item = it)
0 commit comments