@@ -21,6 +21,7 @@ import androidx.compose.foundation.layout.Row
21
21
import androidx.compose.foundation.layout.Spacer
22
22
import androidx.compose.foundation.layout.WindowInsets
23
23
import androidx.compose.foundation.layout.WindowInsetsSides
24
+ import androidx.compose.foundation.layout.consumeWindowInsets
24
25
import androidx.compose.foundation.layout.fillMaxWidth
25
26
import androidx.compose.foundation.layout.only
26
27
import androidx.compose.foundation.layout.padding
@@ -38,7 +39,6 @@ import androidx.compose.material3.FilledIconButton
38
39
import androidx.compose.material3.HorizontalDivider
39
40
import androidx.compose.material3.Icon
40
41
import androidx.compose.material3.IconButton
41
- import androidx.compose.material3.IconButtonColors
42
42
import androidx.compose.material3.IconButtonDefaults
43
43
import androidx.compose.material3.MaterialTheme
44
44
import androidx.compose.material3.Scaffold
@@ -53,17 +53,21 @@ import androidx.compose.ui.graphics.Color
53
53
import androidx.compose.ui.platform.LocalContext
54
54
import androidx.compose.ui.res.stringResource
55
55
import androidx.compose.ui.text.style.TextAlign
56
+ import androidx.compose.ui.tooling.preview.Preview
56
57
import androidx.compose.ui.unit.dp
57
58
import androidx.lifecycle.viewmodel.compose.viewModel
59
+ import com.example.fruitties.android.MyApplicationTheme
58
60
import com.example.fruitties.android.R
59
61
import com.example.fruitties.android.di.App
60
62
import com.example.fruitties.model.CartItemDetails
63
+ import com.example.fruitties.model.Fruittie
64
+ import com.example.fruitties.viewmodel.CartUiState
61
65
import com.example.fruitties.viewmodel.CartViewModel
62
66
import com.example.fruitties.viewmodel.creationExtras
63
67
64
- @OptIn(ExperimentalMaterial3Api ::class )
65
68
@Composable
66
69
fun CartScreen (onNavBarBack : () -> Unit ) {
70
+
67
71
// Instantiate a ViewModel with a dependency on the AppContainer.
68
72
// To make ViewModel compatible with KMP, the ViewModel factory must
69
73
// create an instance without referencing the Android Application.
@@ -78,6 +82,22 @@ fun CartScreen(onNavBarBack: () -> Unit) {
78
82
79
83
val cartState by viewModel.cartUiState.collectAsState()
80
84
85
+ CartScreen (
86
+ onNavBarBack = onNavBarBack,
87
+ cartState = cartState,
88
+ increaseCountClick = viewModel::increaseCountClick,
89
+ decreaseCountClick = viewModel::decreaseCountClick,
90
+ )
91
+ }
92
+
93
+ @OptIn(ExperimentalMaterial3Api ::class )
94
+ @Composable
95
+ fun CartScreen (
96
+ onNavBarBack : () -> Unit ,
97
+ cartState : CartUiState ,
98
+ decreaseCountClick : (CartItemDetails ) -> Unit ,
99
+ increaseCountClick : (CartItemDetails ) -> Unit ,
100
+ ) {
81
101
Scaffold (
82
102
topBar = {
83
103
CenterAlignedTopAppBar (
@@ -109,14 +129,13 @@ fun CartScreen(onNavBarBack: () -> Unit) {
109
129
) { paddingValues ->
110
130
Column (
111
131
modifier = Modifier
112
- // Support edge-to-edge (required on Android 15)
113
- // https://developer.android.com/develop/ui/compose/layouts/insets#inset-size
114
132
.padding(paddingValues)
133
+ .consumeWindowInsets(paddingValues)
115
134
.padding(16 .dp),
116
135
) {
117
136
val cartItemCount = cartState.totalItemCount
118
137
Text (
119
- text = " Cart has $ cartItemCount items " ,
138
+ text = stringResource( R .string.cart_has_items, cartItemCount) ,
120
139
)
121
140
HorizontalDivider ()
122
141
LazyColumn (
@@ -125,8 +144,8 @@ fun CartScreen(onNavBarBack: () -> Unit) {
125
144
items(cartState.cartDetails) { cartItem ->
126
145
CartItem (
127
146
cartItem = cartItem,
128
- decreaseCountClick = viewModel:: decreaseCountClick,
129
- increaseCountClick = viewModel:: increaseCountClick,
147
+ decreaseCountClick = decreaseCountClick,
148
+ increaseCountClick = increaseCountClick,
130
149
)
131
150
}
132
151
item {
@@ -146,8 +165,10 @@ fun CartItem(
146
165
cartItem : CartItemDetails ,
147
166
increaseCountClick : (CartItemDetails ) -> Unit ,
148
167
decreaseCountClick : (CartItemDetails ) -> Unit ,
168
+ modifier : Modifier = Modifier ,
149
169
) {
150
170
Row (
171
+ modifier = modifier,
151
172
verticalAlignment = Alignment .CenterVertically ,
152
173
) {
153
174
Text (text = " ${cartItem.count} x" )
@@ -176,3 +197,62 @@ fun CartItem(
176
197
}
177
198
}
178
199
}
200
+
201
+ @Preview
202
+ @Composable
203
+ private fun CartScreenPreview () {
204
+ MyApplicationTheme {
205
+ CartScreen (
206
+ onNavBarBack = {},
207
+ cartState = CartUiState (
208
+ cartDetails = listOf (
209
+ CartItemDetails (
210
+ fruittie = Fruittie (
211
+ name = " Banana" ,
212
+ fullName = " Banana Banana" ,
213
+ calories = " 100" ,
214
+ ),
215
+ count = 4 ,
216
+ ),
217
+ CartItemDetails (
218
+ fruittie = Fruittie (
219
+ name = " Orange" ,
220
+ fullName = " Orange Orange" ,
221
+ calories = " 100" ,
222
+ ),
223
+ count = 1 ,
224
+ ),
225
+ CartItemDetails (
226
+ fruittie = Fruittie (
227
+ name = " Apple" ,
228
+ fullName = " Apple Apple" ,
229
+ calories = " 100" ,
230
+ ),
231
+ count = 100 ,
232
+ ),
233
+ )
234
+ ),
235
+ decreaseCountClick = {},
236
+ increaseCountClick = {},
237
+ )
238
+ }
239
+ }
240
+
241
+ @Preview
242
+ @Composable
243
+ private fun CartItemPreview () {
244
+ MyApplicationTheme {
245
+ CartItem (
246
+ cartItem = CartItemDetails (
247
+ fruittie = Fruittie (
248
+ name = " Banana" ,
249
+ fullName = " Banana Banana" ,
250
+ calories = " 100" ,
251
+ ),
252
+ count = 4 ,
253
+ ),
254
+ increaseCountClick = {},
255
+ decreaseCountClick = {}
256
+ )
257
+ }
258
+ }
0 commit comments