Skip to content

Commit 1cb1501

Browse files
[Jetsnack] Fix review comments
1 parent 34ff388 commit 1cb1501

File tree

7 files changed

+36
-10
lines changed

7 files changed

+36
-10
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/SnackCollection.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ private val cart = listOf(
101101
OrderLine(snacks[8], 1)
102102
)
103103

104+
@Immutable
104105
data class OrderLine(
105106
val snack: Snack,
106107
val count: Int

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

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,13 @@ private fun CartContent(
128128
onSnackClick: (Long) -> Unit,
129129
modifier: Modifier = Modifier
130130
) {
131-
val snackCountFormattedString = ContextAmbient.current.resources
132-
.getQuantityString(R.plurals.cart_order_count, orderLines.size, orderLines.size)
131+
val resources = ContextAmbient.current.resources
132+
val snackCountFormattedString = remember(orderLines.size, resources) {
133+
resources.getQuantityString(
134+
R.plurals.cart_order_count,
135+
orderLines.size, orderLines.size
136+
)
137+
}
133138
LazyColumn(modifier) {
134139
item {
135140
Spacer(Modifier.statusBarsHeight(additional = 56.dp))
@@ -220,7 +225,7 @@ fun CartItem(
220225
end.linkTo(parent.end)
221226
}.padding(top = 12.dp)
222227
) {
223-
Icon(asset = Icons.Filled.Close, tint = JetsnackTheme.colors.brand)
228+
Icon(asset = Icons.Filled.Close, tint = JetsnackTheme.colors.iconSecondary)
224229
}
225230
Text(
226231
text = snack.tagline,
@@ -275,7 +280,6 @@ fun CartItem(
275280
}
276281
}
277282

278-
// TODO: Hoist state instead of using hard-coded total price
279283
@Composable
280284
fun SummaryItem(
281285
subtotal: Long,
@@ -373,14 +377,28 @@ private fun CheckoutBar(modifier: Modifier = Modifier) {
373377
@Composable
374378
fun CartPreview() {
375379
JetsnackTheme {
376-
Cart(onSnackClick = { })
380+
Cart(
381+
orderLines = SnackRepo.getCart(),
382+
removeSnack = {},
383+
increaseItemCount = {},
384+
decreaseItemCount = {},
385+
inspiredByCart = SnackRepo.getInspiredByCart(),
386+
onSnackClick = {}
387+
)
377388
}
378389
}
379390

380391
@Preview("Cart • Dark Theme")
381392
@Composable
382393
fun CartDarkPreview() {
383394
JetsnackTheme(darkTheme = true) {
384-
Cart(onSnackClick = { })
395+
Cart(
396+
orderLines = SnackRepo.getCart(),
397+
removeSnack = {},
398+
increaseItemCount = {},
399+
decreaseItemCount = {},
400+
inspiredByCart = SnackRepo.getInspiredByCart(),
401+
onSnackClick = { }
402+
)
385403
}
386404
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ import com.example.jetsnack.model.SnackRepo
2222
import kotlinx.coroutines.flow.MutableStateFlow
2323
import kotlinx.coroutines.flow.StateFlow
2424

25+
/**
26+
* Holds the contents of the cart and allows changes to it.
27+
*
28+
* TODO: Move data to Repository so it can be displayed and changed consistently throughout the app.
29+
*/
2530
class CartViewModel : ViewModel() {
2631
private val _orderLines: MutableStateFlow<List<OrderLine>> = MutableStateFlow(SnackRepo.getCart())
2732
val orderLines: StateFlow<List<OrderLine>> get() = _orderLines

Jetsnack/app/src/main/res/values/strings.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
<?xml version="1.0" encoding="utf-8"?><!--
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
23
Copyright 2020 The Android Open Source Project
34
45
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except

Jetsnack/build.gradle

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ subprojects {
5656
freeCompilerArgs += '-Xallow-jvm-ir-dependencies'
5757
// Opt-in to experimental compose APIs
5858
freeCompilerArgs += '-Xopt-in=kotlin.RequiresOptIn'
59-
60-
// Enable experimental coroutines APIs
59+
// Enable experimental coroutines APIs, including collectAsState()
6160
freeCompilerArgs += '-Xopt-in=kotlinx.coroutines.ExperimentalCoroutinesApi'
6261
}
6362
}

Jetsnack/buildSrc/src/main/java/com/example/jetsnack/buildsrc/Dependencies.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ object Libs {
3737
}
3838

3939
object Coroutines {
40-
private const val version = "1.3.9"
40+
private const val version = "1.4.0"
4141
const val core = "org.jetbrains.kotlinx:kotlinx-coroutines-core:$version"
4242
const val android = "org.jetbrains.kotlinx:kotlinx-coroutines-android:$version"
4343
const val test = "org.jetbrains.kotlinx:kotlinx-coroutines-test:$version"

0 commit comments

Comments
 (0)