Skip to content

Commit

Permalink
Implement the ShoppingCart feature UI
Browse files Browse the repository at this point in the history
  • Loading branch information
etonotieno committed Sep 29, 2023
1 parent c420dac commit 569cda6
Show file tree
Hide file tree
Showing 4 changed files with 608 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
/*
* Copyright 2023 Eton Otieno
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.devbits.gocart.designsystem.component

import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.MoreVert
import androidx.compose.material.icons.outlined.DeleteOutline
import androidx.compose.material.icons.outlined.FavoriteBorder
import androidx.compose.material3.DropdownMenu
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import io.devbits.gocart.designsystem.model.Product
import io.devbits.gocart.designsystem.theme.GoCartTheme

@Composable
fun ShoppingCartCard(
product: Product,
onBookmark: () -> Unit,
onDelete: () -> Unit,
modifier: Modifier = Modifier,
) {
var quantity by remember { mutableIntStateOf(0) }
var expanded by remember { mutableStateOf(false) }

Row(
modifier = modifier
.clip(RoundedCornerShape(8.dp))
.fillMaxWidth()
.padding(16.dp),
horizontalArrangement = Arrangement.spacedBy(16.dp),
verticalAlignment = Alignment.CenterVertically,
) {
Image(
painter = painterResource(product.image),
contentDescription = null,
contentScale = ContentScale.Crop,
modifier = Modifier
.size(80.dp)
.clip(RoundedCornerShape(4.dp)),
)

Column(modifier = Modifier.weight(1f)) {
Text(
text = product.name,
style = MaterialTheme.typography.bodyLarge,
fontWeight = FontWeight.Bold,
)

val productQuantity = buildString {
if (product.unit.isNotBlank()) {
append(product.unit)
append(" ")
}

if (product.unitQuantity.isNotBlank()) {
val unitQuantity = if (product.unit.isBlank()) {
product.unitQuantity
} else {
"(${product.unitQuantity})"
}
append(unitQuantity)
}
}

Text(
text = productQuantity,
style = MaterialTheme.typography.bodyMedium,
)

Text(
text = "KSH ${product.price}",
style = MaterialTheme.typography.bodyMedium,
fontWeight = FontWeight.Bold,
)

Row(
horizontalArrangement = Arrangement.spacedBy(8.dp),
) {
Text(text = "Quantity")

QuantityControl(
onUpdate = { quantity = it },
showDelete = true,
)
}
}

Box(
modifier = Modifier
.wrapContentSize(Alignment.TopStart)
.align(Alignment.Top),
) {
Icon(
imageVector = Icons.Default.MoreVert,
contentDescription = null,
tint = MaterialTheme.colorScheme.primary,
modifier = Modifier.clickable { expanded = true },
)

DropdownMenu(
expanded = expanded,
onDismissRequest = { expanded = false },
) {
DropdownMenuItem(
text = { Text("Add to favorites") },
onClick = onBookmark,
leadingIcon = {
Icon(
imageVector = Icons.Outlined.FavoriteBorder,
contentDescription = null,
)
},
)
DropdownMenuItem(
text = { Text("Delete from list") },
onClick = onDelete,
leadingIcon = {
Icon(
imageVector = Icons.Outlined.DeleteOutline,
contentDescription = null,
)
},
)
}
}
}
}

@Preview(showBackground = true, backgroundColor = 0xFFFFFFFF)
@Composable
private fun ShoppingCartCardPreview() {
GoCartTheme {
ShoppingCartCard(
product = sampleProducts[0],
onBookmark = {},
onDelete = {},
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2023 Eton Otieno
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.devbits.gocart.cart.navigation

import androidx.compose.ui.Modifier
import androidx.navigation.NavController
import androidx.navigation.NavGraphBuilder
import androidx.navigation.NavOptions
import androidx.navigation.compose.composable
import io.devbits.gocart.cart.ui.ShoppingCartRoute

const val cartRoute = "cart"

fun NavController.navigateToCart(navOptions: NavOptions? = null) {
this.navigate(cartRoute, navOptions)
}

fun NavGraphBuilder.cartScreen(
onBack: () -> Unit,
) {
composable(route = cartRoute) {
ShoppingCartRoute(
onBack = onBack,
modifier = Modifier,
)
}
}
Loading

0 comments on commit 569cda6

Please sign in to comment.