Skip to content

Commit

Permalink
refactor: Code cleanup, optimization, and UI improvements
Browse files Browse the repository at this point in the history
Improved code readability, efficiency, and user experience:

- Renamed some functions for better clarity.
- Removed unnecessary printlns.
- Refactored code for a more concise and maintainable structure.
- Introduced a new color palette for non-dynamic colors.
- Enhanced snackbar behavior for better user feedback.
- Added a new check to determine the pickup state of items in the cart list.
- The project is now ready for release.
  • Loading branch information
Mihai-Cristian Condrea committed Nov 10, 2024
1 parent 726b497 commit b373dfb
Show file tree
Hide file tree
Showing 30 changed files with 356 additions and 254 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ android {
applicationId = "com.d4rk.cartcalculator"
minSdk = 23
targetSdk = 35
versionCode = 65
versionCode = 66
versionName = "1.1.1"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
resourceConfigurations += listOf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,21 @@ package com.d4rk.cartcalculator.ui.components.navigation
import android.content.Context
import android.view.SoundEffectConstants
import android.view.View
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.asPaddingValues
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.navigationBars
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.outlined.EventNote
import androidx.compose.material.icons.automirrored.outlined.HelpOutline
import androidx.compose.material.icons.filled.Menu
import androidx.compose.material.icons.outlined.AddShoppingCart
import androidx.compose.material.icons.outlined.Settings
import androidx.compose.material.icons.outlined.Share
import androidx.compose.material.icons.outlined.VolunteerActivism
import androidx.compose.material3.DrawerState
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.ExtendedFloatingActionButton
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.ModalDrawerSheet
import androidx.compose.material3.ModalNavigationDrawer
import androidx.compose.material3.NavigationDrawerItem
import androidx.compose.material3.NavigationDrawerItemDefaults
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
Expand All @@ -38,29 +26,23 @@ import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.d4rk.cartcalculator.R
import com.d4rk.cartcalculator.data.datastore.DataStore
import com.d4rk.cartcalculator.data.model.ui.navigation.NavigationDrawerItem
import com.d4rk.cartcalculator.ui.components.ads.AdBanner
import com.d4rk.cartcalculator.ui.components.animations.bounceClick
import com.d4rk.cartcalculator.ui.components.animations.hapticDrawerSwipe
import com.d4rk.cartcalculator.ui.screens.help.HelpActivity
import com.d4rk.cartcalculator.ui.screens.home.HomeScreen
import com.d4rk.cartcalculator.ui.screens.home.HomeViewModel
import com.d4rk.cartcalculator.ui.screens.support.SupportActivity
import com.d4rk.cartcalculator.ui.screens.main.MainScreenContent
import com.d4rk.cartcalculator.utils.IntentUtils
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun NavigationDrawer(
drawerState : DrawerState ,
view : View ,
context : Context ,
) {
val scope : CoroutineScope = rememberCoroutineScope()
val coroutineScope : CoroutineScope = rememberCoroutineScope()
val drawerItems : List<NavigationDrawerItem> = listOf(
NavigationDrawerItem(
title = R.string.settings ,
Expand All @@ -81,8 +63,6 @@ fun NavigationDrawer(

val selectedItemIndex : Int by rememberSaveable { mutableIntStateOf(value = - 1) }

val viewModel : HomeViewModel = viewModel()

ModalNavigationDrawer(modifier = Modifier.hapticDrawerSwipe(drawerState) ,
drawerState = drawerState ,
drawerContent = {
Expand Down Expand Up @@ -131,7 +111,7 @@ fun NavigationDrawer(
IntentUtils.shareApp(context)
}
}
scope.launch { drawerState.close() }
coroutineScope.launch { drawerState.close() }
} ,
icon = {
Icon(
Expand All @@ -153,78 +133,11 @@ fun NavigationDrawer(
}
} ,
content = {
Scaffold(topBar = {
TopAppBar(title = { Text(text = stringResource(id = R.string.app_name)) } ,
navigationIcon = {
IconButton(modifier = Modifier.bounceClick() ,
onClick = {
view.playSoundEffect(
SoundEffectConstants.CLICK
)
scope.launch {
drawerState.apply {
if (isClosed) open() else close()
}
}
}) {
Icon(
imageVector = Icons.Default.Menu ,
contentDescription = stringResource(id = R.string.navigation_drawer_open)
)
}
} ,
actions = {
IconButton(modifier = Modifier.bounceClick() ,
onClick = {
view.playSoundEffect(
SoundEffectConstants.CLICK
)
IntentUtils.openActivity(
context ,
SupportActivity::class.java
)
}) {
Icon(
Icons.Outlined.VolunteerActivism ,
contentDescription = stringResource(id = R.string.support_us)
)
}
})
} , floatingActionButton = {
ExtendedFloatingActionButton(modifier = Modifier.bounceClick() ,
text = {
Text(
text = stringResource(R.string.add_new_cart)
)
} ,
onClick = {
view.playSoundEffect(
SoundEffectConstants.CLICK
)
viewModel.openNewCartDialog()
} ,
icon = {
Icon(
Icons.Outlined.AddShoppingCart ,
contentDescription = null
)
})
} , bottomBar = {
val dataStore = DataStore.getInstance(context)
AdBanner(
dataStore = dataStore , modifier = Modifier.padding(
bottom = (WindowInsets.navigationBars.asPaddingValues()
.calculateBottomPadding() + 8.dp)
)
)
}) { paddingValues ->
Box(
modifier = Modifier.padding(paddingValues)
) {
HomeScreen(
context = context , view = view , viewModel = viewModel
)
}
}
MainScreenContent(
view = view ,
drawerState = drawerState ,
context = context ,
coroutineScope = coroutineScope
)
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ class CartActivity : AppCompatActivity() {
MobileAds.initialize(this@CartActivity)
cartId = intent.getIntExtra("cartId" , 0)
println("Shopping Cart Calculator -> CartActivity: onCreate - Received cartId: $cartId")
viewModel.loadSelectedCurrency(dataStore)
viewModel.loadCart(cartId)
viewModel.loadSelectedCurrency(dataStore = dataStore)
viewModel.loadCart(cartId = cartId)

setContent {
AppTheme {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,26 +131,57 @@ fun CartScreen(activity : CartActivity , cartId : Int) {
)
}
else {

val (checkedItems, uncheckedItems) = uiState.cartItems.partition { it.isChecked }
Box {
Column(
modifier = Modifier.fillMaxSize()
) {
LazyColumn(
modifier = Modifier.weight(1f)
) {
items(
items = uiState.cartItems ,
key = { item -> item.itemId }) { cartItem ->
CartItemComposable(viewModel = viewModel ,
cartItem = cartItem ,
onMinusClick = {
viewModel.decreaseQuantity(cartItem)
} ,
onPlusClick = {
viewModel.increaseQuantity(cartItem)
} ,
uiState = uiState ,
modifier = Modifier.animateItem())
if (checkedItems.isNotEmpty()) {
item {
Text(
text = stringResource(id = R.string.in_cart),
style = MaterialTheme.typography.titleMedium ,
modifier = Modifier.padding(start = 16.dp , top = 8.dp).animateItem()
)
}
items(
items = checkedItems ,
key = { item -> item.itemId }) { cartItem ->
CartItemComposable(
viewModel = viewModel ,
cartItem = cartItem ,
onMinusClick = { viewModel.decreaseQuantity(cartItem) } ,
onPlusClick = { viewModel.increaseQuantity(cartItem) } ,
uiState = uiState ,
modifier = Modifier.animateItem()
)
}
}

if (uncheckedItems.isNotEmpty()) {
item {
Text(
text = stringResource(id = R.string.items_to_pick_up),
style = MaterialTheme.typography.titleMedium ,
modifier = Modifier.padding(start = 16.dp , top = 8.dp).animateItem()
)
}
items(
items = uncheckedItems ,
key = { item -> item.itemId }) { cartItem ->
CartItemComposable(
viewModel = viewModel ,
cartItem = cartItem ,
onMinusClick = { viewModel.decreaseQuantity(cartItem) } ,
onPlusClick = { viewModel.increaseQuantity(cartItem) } ,
uiState = uiState ,
modifier = Modifier.animateItem()
)
}
}
}

Expand Down
Loading

0 comments on commit b373dfb

Please sign in to comment.