Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 50 additions & 38 deletions app/src/main/java/com/sa/commercex/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.animation.AnimatedContent
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.animation.slideInHorizontally
import androidx.compose.animation.slideOutHorizontally
import androidx.compose.animation.togetherWith
import androidx.compose.animation.core.tween
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
Expand All @@ -30,53 +37,49 @@ class MainActivity : ComponentActivity() {

setContent {
CommerceXTheme {
var showProductDetail by remember { mutableStateOf(false) }
var showCart by remember { mutableStateOf(false) }
var showSearch by remember { mutableStateOf(false) }
var showProfile by remember { mutableStateOf(false) }
var showLogin by remember { mutableStateOf(false) }
var currentScreen by remember { mutableStateOf(AppScreen.HOME) }

Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
when {
showLogin -> {
LoginScreenMockup(
onSignInClick = {
showLogin = false
showProfile = true
}
AnimatedContent(
targetState = currentScreen,
transitionSpec = {
(slideInHorizontally(
initialOffsetX = { it / 3 },
animationSpec = tween(260)
) + fadeIn(animationSpec = tween(260))).togetherWith(
slideOutHorizontally(
targetOffsetX = { -it / 3 },
animationSpec = tween(220)
) + fadeOut(animationSpec = tween(220))
)
}
showProfile -> {
ProfileScreenMockup(
onBackClick = { showProfile = false },
onLogoutClick = {
showProfile = false
showLogin = true
}
},
label = "main_screen_transition"
) { screen ->
when (screen) {
AppScreen.LOGIN -> LoginScreenMockup(
onSignInClick = { currentScreen = AppScreen.PROFILE }
)
}
showSearch -> {
SearchScreenDefaultMockup()
}
showCart -> {
CartScreenMockup(
onBackClick = { showCart = false }
AppScreen.PROFILE -> ProfileScreenMockup(
onBackClick = { currentScreen = AppScreen.HOME },
onLogoutClick = { currentScreen = AppScreen.LOGIN }
)
}
showProductDetail -> {
ProductDetailMockup(
onBackClick = { showProductDetail = false }
AppScreen.SEARCH -> SearchScreenDefaultMockup(
onBackClick = { currentScreen = AppScreen.HOME }
)
}
else -> {
HomeScreenMockup(
onProductClick = { showProductDetail = true },
onCartClick = { showCart = true },
onSearchClick = { showSearch = true },
onProfileClick = { showProfile = true }
AppScreen.CART -> CartScreenMockup(
onBackClick = { currentScreen = AppScreen.HOME }
)
AppScreen.PRODUCT_DETAIL -> ProductDetailMockup(
onBackClick = { currentScreen = AppScreen.HOME }
)
AppScreen.HOME -> HomeScreenMockup(
onProductClick = { currentScreen = AppScreen.PRODUCT_DETAIL },
onCartClick = { currentScreen = AppScreen.CART },
onSearchClick = { currentScreen = AppScreen.SEARCH },
onProfileClick = { currentScreen = AppScreen.PROFILE }
)
}
}
Expand All @@ -85,3 +88,12 @@ class MainActivity : ComponentActivity() {
}
}
}

private enum class AppScreen {
HOME,
SEARCH,
CART,
PRODUCT_DETAIL,
PROFILE,
LOGIN
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package com.sa.feature.cart.ui

import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.fadeOut
import androidx.compose.animation.shrinkVertically
import androidx.compose.animation.slideOutHorizontally
import androidx.compose.animation.core.tween
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.rememberScrollState
Expand Down Expand Up @@ -27,6 +32,7 @@ import com.sa.core.ui.component.OrderSummary
import com.sa.core.ui.component.PrimaryButton
import com.sa.core.ui.component.SimpleTopAppBar
import com.sa.core.ui.theme.*
import kotlinx.coroutines.delay

/**
* Cart Screen Mockup
Expand Down Expand Up @@ -64,12 +70,13 @@ fun CartScreenMockup(
onBackClick: () -> Unit = {}
) {
val cartItems = remember {
listOf(
CartMockItem("iPhone 9", 549.0, 1),
CartMockItem("MacBook Pro", 1749.0, 1),
CartMockItem("Brown Perfume", 40.0, 2)
mutableStateListOf(
CartMockItem(id = "1", title = "iPhone 9", price = 549.0, quantity = 1),
CartMockItem(id = "2", title = "MacBook Pro", price = 1749.0, quantity = 1),
CartMockItem(id = "3", title = "Brown Perfume", price = 40.0, quantity = 2)
)
}
val removingItemIds = remember { mutableStateListOf<String>() }

Box(
modifier = Modifier
Expand Down Expand Up @@ -103,14 +110,31 @@ fun CartScreenMockup(

Column(verticalArrangement = Arrangement.spacedBy(Spacing.md)) {
cartItems.forEach { item ->
CartItemCard(
imageUrl = "https://cdn.dummyjson.com/products/1/thumbnail.jpg",
title = item.title,
price = item.price,
quantity = item.quantity,
onQuantityChange = { /* Quantity change */ },
onDeleteClick = { /* Delete */ }
)
AnimatedVisibility(
visible = !removingItemIds.contains(item.id),
exit = slideOutHorizontally(
animationSpec = tween(240),
targetOffsetX = { -it }
) + fadeOut(animationSpec = tween(220)) + shrinkVertically(animationSpec = tween(220))
) {
CartItemCard(
imageUrl = "https://cdn.dummyjson.com/products/1/thumbnail.jpg",
title = item.title,
price = item.price,
quantity = item.quantity,
onQuantityChange = { newQty ->
val index = cartItems.indexOfFirst { it.id == item.id }
if (index >= 0) {
cartItems[index] = cartItems[index].copy(quantity = newQty)
}
},
onDeleteClick = {
if (!removingItemIds.contains(item.id)) {
removingItemIds.add(item.id)
}
}
)
}
}
}
}
Expand All @@ -122,6 +146,15 @@ fun CartScreenMockup(
.navigationBarsPadding()
)
}

LaunchedEffect(removingItemIds.size) {
if (removingItemIds.isNotEmpty()) {
val id = removingItemIds.first()
delay(240)
cartItems.removeAll { it.id == id }
removingItemIds.remove(id)
}
}
}

@Composable
Expand Down Expand Up @@ -211,8 +244,8 @@ private fun CartSummaryBar(
}

private data class CartMockItem(
val id: String,
val title: String,
val price: Double,
val quantity: Int
)

Loading
Loading