-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathMain.kt
More file actions
75 lines (66 loc) · 3.01 KB
/
Copy pathMain.kt
File metadata and controls
75 lines (66 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import com.handstandsam.shoppingapp.cart.InMemoryShoppingCartDao
import com.handstandsam.shoppingapp.cart.ShoppingCart
import com.handstandsam.shoppingapp.models.ItemWithQuantity
import com.handstandsam.shoppingapp.models.totalItemCount
import com.handstandsam.shoppingapp.multiplatform.ui.ButtonType
import com.handstandsam.shoppingapp.multiplatform.ui.CartViewModel
import com.handstandsam.shoppingapp.multiplatform.ui.CategoryDetailViewModel
import com.handstandsam.shoppingapp.multiplatform.ui.HomeViewModel
import com.handstandsam.shoppingapp.multiplatform.ui.ItemDetailViewModel
import com.handstandsam.shoppingapp.multiplatform.ui.NavRoute
import com.handstandsam.shoppingapp.multiplatform.ui.ShoppingAppButton
import com.handstandsam.shoppingapp.multiplatform.ui.WrappedPreformattedText
import com.handstandsam.shoppingapp.multiplatform.ui.navController
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.launch
import org.jetbrains.compose.web.dom.Button
import org.jetbrains.compose.web.dom.Div
import org.jetbrains.compose.web.dom.Text
import org.jetbrains.compose.web.renderComposable
fun main() {
val scope = CoroutineScope(Dispatchers.Unconfined)
val shoppingCart = ShoppingCart(InMemoryShoppingCartDao())
val homeViewModel = HomeViewModel()
val categoryDetailViewModel = CategoryDetailViewModel()
val cartViewModel = CartViewModel(shoppingCart = shoppingCart)
val itemDetailViewModel = ItemDetailViewModel(shoppingCart = shoppingCart)
@Composable
fun ShoppingApp() {
val itemsInCartFlow: Flow<List<ItemWithQuantity>> = shoppingCart.itemsInCart
val itemsInCartCount: List<ItemWithQuantity> by itemsInCartFlow.collectAsState(listOf())
val collectedNavRoute: NavRoute by navController.collectAsState()
Div {
Text("${itemsInCartCount.totalItemCount()} Item(s) in Cart")
ShoppingAppButton(label = "View Cart", buttonType = ButtonType.PRIMARY) {
scope.launch {
navController.tryEmit(NavRoute.CartScreen(shoppingCart.latestItemsInCart()))
}
}
WrappedPreformattedText(itemsInCartCount.toString())
}
when (val navRoute = collectedNavRoute) {
NavRoute.HomeScreen -> {
homeViewModel.HomeScreen()
}
is NavRoute.CategoryDetailScreen -> {
categoryDetailViewModel.CategoryDetailScreen(navRoute.category)
}
is NavRoute.ItemDetailScreen -> {
itemDetailViewModel.ItemDetailScreen(navRoute.item)
}
is NavRoute.CartScreen -> {
cartViewModel.CartScreen(navRoute.itemsWithQuantity)
}
}
}
/** Entry Point */
renderComposable(rootElementId = "root") {
ShoppingApp()
}
Backstack.init()
}