@@ -77,7 +77,7 @@ data class TodoItem(
77
77
// [START android_compose_components_swipeitem]
78
78
@Composable
79
79
fun SwipeItem (
80
- value : TodoItem ,
80
+ todoItem : TodoItem ,
81
81
startToEndAction : (TodoItem ) -> Unit ,
82
82
endToStartAction : (TodoItem ) -> Unit ,
83
83
modifier : Modifier = Modifier ,
@@ -87,12 +87,12 @@ fun SwipeItem(
87
87
confirmValueChange = {
88
88
when (it) {
89
89
SwipeToDismissBoxValue .StartToEnd -> {
90
- startToEndAction(value )
90
+ startToEndAction(todoItem )
91
91
// Do not dismiss this item.
92
92
false
93
93
}
94
94
SwipeToDismissBoxValue .EndToStart -> {
95
- endToStartAction(value )
95
+ endToStartAction(todoItem )
96
96
true
97
97
}
98
98
SwipeToDismissBoxValue .Settled -> {
@@ -104,8 +104,7 @@ fun SwipeItem(
104
104
105
105
SwipeToDismissBox (
106
106
state = swipeToDismissBoxState,
107
- modifier = modifier
108
- .fillMaxSize(),
107
+ modifier = modifier.fillMaxSize(),
109
108
backgroundContent = {
110
109
Row (
111
110
modifier = Modifier
@@ -128,23 +127,20 @@ fun SwipeItem(
128
127
) {
129
128
when (swipeToDismissBoxState.dismissDirection) {
130
129
SwipeToDismissBoxValue .StartToEnd -> {
131
- if (value.isItemDone) {
132
- Icon (
133
- imageVector = Icons .Default .CheckBox ,
134
- contentDescription = " Item done" ,
135
- tint = Color .White ,
136
- modifier = Modifier
137
- .padding(12 .dp)
138
- )
130
+ val icon = if (todoItem.isItemDone) {
131
+ Icons .Default .CheckBox
139
132
} else {
140
- Icon (
141
- imageVector = Icons .Default .CheckBoxOutlineBlank ,
142
- contentDescription = " Item not done" ,
143
- tint = Color .White ,
144
- modifier = Modifier
145
- .padding(12 .dp)
146
- )
133
+ Icons .Default .CheckBoxOutlineBlank
147
134
}
135
+
136
+ val contentDescription = if (todoItem.isItemDone) " Done" else " Not done"
137
+
138
+ Icon (
139
+ icon,
140
+ contentDescription,
141
+ Modifier .padding(12 .dp),
142
+ tint = Color .White
143
+ )
148
144
}
149
145
150
146
SwipeToDismissBoxValue .EndToStart -> {
@@ -153,8 +149,7 @@ fun SwipeItem(
153
149
imageVector = Icons .Default .Delete ,
154
150
contentDescription = " Remove item" ,
155
151
tint = Color .White ,
156
- modifier = Modifier
157
- .padding(12 .dp)
152
+ modifier = Modifier .padding(12 .dp)
158
153
)
159
154
}
160
155
@@ -163,7 +158,7 @@ fun SwipeItem(
163
158
}
164
159
}
165
160
) {
166
- content(value )
161
+ content(todoItem )
167
162
}
168
163
}
169
164
// [END android_compose_components_swipeitem]
@@ -187,7 +182,7 @@ private fun SwipeItemExample() {
187
182
key = { it.itemDescription }
188
183
) { todoItem ->
189
184
SwipeItem (
190
- value = todoItem,
185
+ todoItem = todoItem,
191
186
startToEndAction = {
192
187
todoItem.isItemDone = ! todoItem.isItemDone
193
188
},
@@ -208,23 +203,24 @@ private fun SwipeItemExample() {
208
203
// [START android_compose_components_swipecarditem]
209
204
@Composable
210
205
fun SwipeCardItem (
211
- value : TodoItem ,
206
+ todoItem : TodoItem ,
212
207
startToEndAction : (TodoItem ) -> Unit ,
213
208
endToStartAction : (TodoItem ) -> Unit ,
214
209
modifier : Modifier = Modifier ,
215
210
content : @Composable (TodoItem ) -> Unit
216
211
) {
212
+ // [START_EXCLUDE]
217
213
val swipeToDismissState = rememberSwipeToDismissBoxState(
218
214
positionalThreshold = { totalDistance -> totalDistance * 0.25f },
219
215
confirmValueChange = {
220
216
when (it) {
221
217
SwipeToDismissBoxValue .StartToEnd -> {
222
- startToEndAction(value )
218
+ startToEndAction(todoItem )
223
219
// Do not dismiss this item.
224
220
false
225
221
}
226
222
SwipeToDismissBoxValue .EndToStart -> {
227
- endToStartAction(value )
223
+ endToStartAction(todoItem )
228
224
true
229
225
}
230
226
SwipeToDismissBoxValue .Settled -> {
@@ -234,6 +230,7 @@ fun SwipeCardItem(
234
230
}
235
231
)
236
232
233
+ // [END_EXCLUDE]
237
234
SwipeToDismissBox (
238
235
modifier = Modifier ,
239
236
state = swipeToDismissState,
@@ -250,6 +247,7 @@ fun SwipeCardItem(
250
247
},
251
248
label = " swipeable card item background color"
252
249
)
250
+ // [START_EXCLUDE]
253
251
Row (
254
252
modifier = Modifier
255
253
.background(color)
@@ -259,23 +257,15 @@ fun SwipeCardItem(
259
257
) {
260
258
when (swipeToDismissState.dismissDirection) {
261
259
SwipeToDismissBoxValue .StartToEnd -> {
262
- if (value.isItemDone) {
263
- Icon (
264
- imageVector = Icons .Default .CheckBox ,
265
- contentDescription = " Item done" ,
266
- tint = Color .White ,
267
- modifier = Modifier
268
- .padding(12 .dp)
269
- )
260
+ val icon = if (todoItem.isItemDone) {
261
+ Icons .Default .CheckBox
270
262
} else {
271
- Icon (
272
- imageVector = Icons .Default .CheckBoxOutlineBlank ,
273
- contentDescription = " Item not done" ,
274
- tint = Color .White ,
275
- modifier = Modifier
276
- .padding(12 .dp)
277
- )
263
+ Icons .Default .CheckBoxOutlineBlank
278
264
}
265
+
266
+ val contentDescription = if (todoItem.isItemDone) " Done" else " Not done"
267
+
268
+ Icon (icon, contentDescription, Modifier .padding(12 .dp), tint = Color .White )
279
269
}
280
270
281
271
SwipeToDismissBoxValue .EndToStart -> {
@@ -284,8 +274,7 @@ fun SwipeCardItem(
284
274
imageVector = Icons .Default .Delete ,
285
275
contentDescription = " Remove item" ,
286
276
tint = Color .White ,
287
- modifier = Modifier
288
- .padding(12 .dp)
277
+ modifier = Modifier .padding(12 .dp)
289
278
)
290
279
}
291
280
@@ -294,8 +283,9 @@ fun SwipeCardItem(
294
283
}
295
284
}
296
285
) {
297
- content(value )
286
+ content(todoItem )
298
287
}
288
+ // [END_EXCLUDE]
299
289
}
300
290
// [END android_compose_components_swipecarditem]
301
291
@@ -318,7 +308,7 @@ private fun SwipeCardItemExample() {
318
308
key = { it.itemDescription }
319
309
) { todoItem ->
320
310
SwipeCardItem (
321
- value = todoItem,
311
+ todoItem = todoItem,
322
312
startToEndAction = {
323
313
todoItem.isItemDone = ! todoItem.isItemDone
324
314
},
0 commit comments