Skip to content

Commit 047af3e

Browse files
Merge pull request #260 from android/jv/js_cart_state_flow
[Jetsnack] Cart screen ViewModel and StateFlow
2 parents 4d21fa4 + 1cb1501 commit 047af3e

File tree

13 files changed

+273
-70
lines changed

13 files changed

+273
-70
lines changed

Jetsnack/app/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ dependencies {
8080

8181
implementation Libs.Kotlin.stdlib
8282

83+
implementation Libs.Coroutines.core
84+
8385
implementation Libs.AndroidX.coreKtx
8486

8587
implementation Libs.AndroidX.Compose.runtime

Jetsnack/app/src/main/java/com/example/jetsnack/model/Snack.kt

Lines changed: 57 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ data class Snack(
2323
val id: Long,
2424
val name: String,
2525
val imageUrl: String,
26+
val price: Long,
2627
val tagline: String = "",
2728
val tags: Set<String> = emptySet()
2829
)
@@ -36,162 +37,190 @@ val snacks = listOf(
3637
id = 1L,
3738
name = "Cupcake",
3839
tagline = "A tag line",
39-
imageUrl = "https://source.unsplash.com/pGM4sjt_BdQ"
40+
imageUrl = "https://source.unsplash.com/pGM4sjt_BdQ",
41+
price = 299
4042
),
4143
Snack(
4244
id = 2L,
4345
name = "Donut",
4446
tagline = "A tag line",
45-
imageUrl = "https://source.unsplash.com/Yc5sL-ejk6U"
47+
imageUrl = "https://source.unsplash.com/Yc5sL-ejk6U",
48+
price = 299
4649
),
4750
Snack(
4851
id = 3L,
4952
name = "Eclair",
5053
tagline = "A tag line",
51-
imageUrl = "https://source.unsplash.com/-LojFX9NfPY"
54+
imageUrl = "https://source.unsplash.com/-LojFX9NfPY",
55+
price = 299
5256
),
5357
Snack(
5458
id = 4L,
5559
name = "Froyo",
5660
tagline = "A tag line",
57-
imageUrl = "https://source.unsplash.com/3U2V5WqK1PQ"
61+
imageUrl = "https://source.unsplash.com/3U2V5WqK1PQ",
62+
price = 299
5863
),
5964
Snack(
6065
id = 5L,
6166
name = "Gingerbread",
6267
tagline = "A tag line",
63-
imageUrl = "https://source.unsplash.com/Y4YR9OjdIMk"
68+
imageUrl = "https://source.unsplash.com/Y4YR9OjdIMk",
69+
price = 499
6470
),
6571
Snack(
6672
id = 6L,
6773
name = "Honeycomb",
6874
tagline = "A tag line",
69-
imageUrl = "https://source.unsplash.com/bELvIg_KZGU"
75+
imageUrl = "https://source.unsplash.com/bELvIg_KZGU",
76+
price = 299
7077
),
7178
Snack(
7279
id = 7L,
7380
name = "Ice Cream Sandwich",
7481
tagline = "A tag line",
75-
imageUrl = "https://source.unsplash.com/AqorcpZIKnU"
82+
imageUrl = "https://source.unsplash.com/AqorcpZIKnU",
83+
price = 1299
7684
),
7785
Snack(
7886
id = 8L,
7987
name = "Jellybean",
8088
tagline = "A tag line",
81-
imageUrl = "https://source.unsplash.com/0u_vbeOkMpk"
89+
imageUrl = "https://source.unsplash.com/0u_vbeOkMpk",
90+
price = 299
8291
),
8392
Snack(
8493
id = 9L,
8594
name = "KitKat",
8695
tagline = "A tag line",
87-
imageUrl = "https://source.unsplash.com/yb16pT5F_jE"
96+
imageUrl = "https://source.unsplash.com/yb16pT5F_jE",
97+
price = 549
8898
),
8999
Snack(
90100
id = 10L,
91101
name = "Lollipop",
92102
tagline = "A tag line",
93-
imageUrl = "https://source.unsplash.com/AHF_ZktTL6Q"
103+
imageUrl = "https://source.unsplash.com/AHF_ZktTL6Q",
104+
price = 299
94105
),
95106
Snack(
96107
id = 11L,
97108
name = "Marshmallow",
98109
tagline = "A tag line",
99-
imageUrl = "https://source.unsplash.com/rqFm0IgMVYY"
110+
imageUrl = "https://source.unsplash.com/rqFm0IgMVYY",
111+
price = 299
100112
),
101113
Snack(
102114
id = 12L,
103115
name = "Nougat",
104116
tagline = "A tag line",
105-
imageUrl = "https://source.unsplash.com/qRE_OpbVPR8"
117+
imageUrl = "https://source.unsplash.com/qRE_OpbVPR8",
118+
price = 299
106119
),
107120
Snack(
108121
id = 13L,
109122
name = "Oreo",
110123
tagline = "A tag line",
111-
imageUrl = "https://source.unsplash.com/33fWPnyN6tU"
124+
imageUrl = "https://source.unsplash.com/33fWPnyN6tU",
125+
price = 299
112126
),
113127
Snack(
114128
id = 14L,
115129
name = "Pie",
116130
tagline = "A tag line",
117-
imageUrl = "https://source.unsplash.com/aX_ljOOyWJY"
131+
imageUrl = "https://source.unsplash.com/aX_ljOOyWJY",
132+
price = 299
118133
),
119134
Snack(
120135
id = 15L,
121136
name = "Chips",
122-
imageUrl = "https://source.unsplash.com/UsSdMZ78Q3E"
137+
imageUrl = "https://source.unsplash.com/UsSdMZ78Q3E",
138+
price = 299
123139
),
124140
Snack(
125141
id = 16L,
126142
name = "Pretzels",
127-
imageUrl = "https://source.unsplash.com/7meCnGCJ5Ms"
143+
imageUrl = "https://source.unsplash.com/7meCnGCJ5Ms",
144+
price = 299
128145
),
129146
Snack(
130147
id = 17L,
131148
name = "Smoothies",
132-
imageUrl = "https://source.unsplash.com/m741tj4Cz7M"
149+
imageUrl = "https://source.unsplash.com/m741tj4Cz7M",
150+
price = 299
133151
),
134152
Snack(
135153
id = 18L,
136154
name = "Popcorn",
137-
imageUrl = "https://source.unsplash.com/iuwMdNq0-s4"
155+
imageUrl = "https://source.unsplash.com/iuwMdNq0-s4",
156+
price = 299
138157
),
139158
Snack(
140159
id = 19L,
141160
name = "Almonds",
142-
imageUrl = "https://source.unsplash.com/qgWWQU1SzqM"
161+
imageUrl = "https://source.unsplash.com/qgWWQU1SzqM",
162+
price = 299
143163
),
144164
Snack(
145165
id = 20L,
146166
name = "Cheese",
147-
imageUrl = "https://source.unsplash.com/9MzCd76xLGk"
167+
imageUrl = "https://source.unsplash.com/9MzCd76xLGk",
168+
price = 299
148169
),
149170
Snack(
150171
id = 21L,
151172
name = "Apples",
152173
tagline = "A tag line",
153-
imageUrl = "https://source.unsplash.com/1d9xXWMtQzQ"
174+
imageUrl = "https://source.unsplash.com/1d9xXWMtQzQ",
175+
price = 299
154176
),
155177
Snack(
156178
id = 22L,
157179
name = "Apple sauce",
158180
tagline = "A tag line",
159-
imageUrl = "https://source.unsplash.com/wZxpOw84QTU"
181+
imageUrl = "https://source.unsplash.com/wZxpOw84QTU",
182+
price = 299
160183
),
161184
Snack(
162185
id = 23L,
163186
name = "Apple chips",
164187
tagline = "A tag line",
165-
imageUrl = "https://source.unsplash.com/okzeRxm_GPo"
188+
imageUrl = "https://source.unsplash.com/okzeRxm_GPo",
189+
price = 299
166190
),
167191
Snack(
168192
id = 24L,
169193
name = "Apple juice",
170194
tagline = "A tag line",
171-
imageUrl = "https://source.unsplash.com/l7imGdupuhU"
195+
imageUrl = "https://source.unsplash.com/l7imGdupuhU",
196+
price = 299
172197
),
173198
Snack(
174199
id = 25L,
175200
name = "Apple pie",
176201
tagline = "A tag line",
177-
imageUrl = "https://source.unsplash.com/bkXzABDt08Q"
202+
imageUrl = "https://source.unsplash.com/bkXzABDt08Q",
203+
price = 299
178204
),
179205
Snack(
180206
id = 26L,
181207
name = "Grapes",
182208
tagline = "A tag line",
183-
imageUrl = "https://source.unsplash.com/y2MeW00BdBo"
209+
imageUrl = "https://source.unsplash.com/y2MeW00BdBo",
210+
price = 299
184211
),
185212
Snack(
186213
id = 27L,
187214
name = "Kiwi",
188215
tagline = "A tag line",
189-
imageUrl = "https://source.unsplash.com/1oMGgHn-M8k"
216+
imageUrl = "https://source.unsplash.com/1oMGgHn-M8k",
217+
price = 299
190218
),
191219
Snack(
192220
id = 28L,
193221
name = "Mango",
194222
tagline = "A tag line",
195-
imageUrl = "https://source.unsplash.com/TIGDsyy0TK4"
223+
imageUrl = "https://source.unsplash.com/TIGDsyy0TK4",
224+
price = 299
196225
)
197226
)

Jetsnack/app/src/main/java/com/example/jetsnack/model/SnackCollection.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,14 @@ private val related = listOf(
9595
popular
9696
)
9797

98-
private val cart = snacks.subList(4, 7)
98+
private val cart = listOf(
99+
OrderLine(snacks[4], 2),
100+
OrderLine(snacks[6], 3),
101+
OrderLine(snacks[8], 1)
102+
)
103+
104+
@Immutable
105+
data class OrderLine(
106+
val snack: Snack,
107+
val count: Int
108+
)

Jetsnack/app/src/main/java/com/example/jetsnack/ui/components/QuantitySelector.kt

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.example.jetsnack.ui.components
1818

19+
import androidx.compose.animation.Crossfade
1920
import androidx.compose.foundation.Text
2021
import androidx.compose.foundation.layout.ChainStyle
2122
import androidx.compose.foundation.layout.ConstraintLayout
@@ -66,16 +67,20 @@ fun QuantitySelector(
6667
}
6768
)
6869
ProvideEmphasis(emphasis = AmbientEmphasisLevels.current.high) {
69-
Text(
70-
text = "$count",
71-
style = MaterialTheme.typography.subtitle2,
72-
fontSize = 18.sp,
73-
color = JetsnackTheme.colors.textPrimary,
74-
textAlign = TextAlign.Center,
75-
modifier = Modifier.preferredWidthIn(min = 24.dp).constrainAs(quantity) {
76-
baseline.linkTo(qty.baseline)
77-
}
78-
)
70+
Crossfade(
71+
current = count,
72+
modifier = Modifier
73+
.constrainAs(quantity) { baseline.linkTo(qty.baseline) }
74+
) {
75+
Text(
76+
text = "$it",
77+
style = MaterialTheme.typography.subtitle2,
78+
fontSize = 18.sp,
79+
color = JetsnackTheme.colors.textPrimary,
80+
textAlign = TextAlign.Center,
81+
modifier = Modifier.preferredWidthIn(min = 24.dp)
82+
)
83+
}
7984
}
8085
JetsnackGradientTintedIconButton(
8186
asset = Icons.Outlined.AddCircleOutline,

Jetsnack/app/src/main/java/com/example/jetsnack/ui/home/Home.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ import androidx.ui.tooling.preview.Preview
6969
import com.example.jetsnack.R
7070
import com.example.jetsnack.ui.components.JetsnackScaffold
7171
import com.example.jetsnack.ui.components.JetsnackSurface
72+
import com.example.jetsnack.ui.home.cart.Cart
7273
import com.example.jetsnack.ui.home.search.Search
7374
import com.example.jetsnack.ui.theme.JetsnackTheme
7475
import com.example.jetsnack.ui.utils.navigationBarsPadding

0 commit comments

Comments
 (0)