From 2ead3c054ae0f41c216cbeb5a5e3fbee362c2694 Mon Sep 17 00:00:00 2001 From: Minecon724 Date: Thu, 8 Aug 2024 13:19:32 +0200 Subject: [PATCH] hide time card if no instances --- .../activity/dashboard/screen/Dashboard.kt | 156 ++++++++++-------- 1 file changed, 86 insertions(+), 70 deletions(-) diff --git a/app/src/main/java/eu/m724/vastapp/activity/dashboard/screen/Dashboard.kt b/app/src/main/java/eu/m724/vastapp/activity/dashboard/screen/Dashboard.kt index 9222c6e..20e47d1 100644 --- a/app/src/main/java/eu/m724/vastapp/activity/dashboard/screen/Dashboard.kt +++ b/app/src/main/java/eu/m724/vastapp/activity/dashboard/screen/Dashboard.kt @@ -72,85 +72,101 @@ fun DashboardScreen(dashboardViewModel: DashboardViewModel) { horizontalArrangement = Arrangement.Center ) { // balance card - Card( - modifier = Modifier - .padding(16.dp), - colors = CardDefaults.cardColors( - containerColor = balanceCardColor(user.credit) - ) - ) { - Row( - modifier = Modifier.padding(16.dp, 8.dp), - verticalAlignment = Alignment.CenterVertically - ) { - Icon( - modifier = Modifier.size(24.dp), - painter = painterResource(id = R.drawable.baseline_monetization_on_24), - contentDescription = stringResource(id = R.string.balance) - ) - Spacer( - modifier = Modifier.width(12.dp) - ) - Text( - text = "$%.2f".format(user.credit), - fontSize = 22.sp, - color = balanceColor(user.credit, user.balanceThreshold) - ) - } - } + BalanceCard(balance = user.credit, balanceWarning = user.balanceThreshold) // time card - Card( - modifier = Modifier - .padding(16.dp) - ) { - Row( - modifier = Modifier.padding(16.dp, 8.dp), - verticalAlignment = Alignment.CenterVertically - ) { - Icon( - modifier = Modifier.size(24.dp), - painter = painterResource(id = R.drawable.baseline_access_time_filled_24), - contentDescription = stringResource(id = R.string.time_left) - ) - Spacer( - modifier = Modifier.width(12.dp) - ) - Text( - text = formatTime(remainingTime), - fontSize = 22.sp - ) - } - } + if (rentedInstances.isNotEmpty()) + RemainingTimeCard(remainingTime = remainingTime) // instances - Card( - modifier = Modifier - .padding(16.dp) - ) { - Row( - modifier = Modifier.padding(16.dp, 8.dp), - verticalAlignment = Alignment.CenterVertically - ) { - Icon( - modifier = Modifier.size(24.dp), - painter = painterResource(id = R.drawable.server_solid), - contentDescription = stringResource(id = R.string.rented_instances) - ) - Spacer( - modifier = Modifier.width(12.dp) - ) - Text( - text = rentedInstances.size.toString(), - fontSize = 22.sp - ) - } - } + InstancesCard(rentedInstancesCount = rentedInstances.size) } } } } +@Composable +fun RemainingTimeCard(remainingTime: Int) { + Card( + modifier = Modifier + .padding(16.dp) + ) { + Row( + modifier = Modifier.padding(16.dp, 8.dp), + verticalAlignment = Alignment.CenterVertically + ) { + Icon( + modifier = Modifier.size(24.dp), + painter = painterResource(id = R.drawable.baseline_access_time_filled_24), + contentDescription = stringResource(id = R.string.time_left) + ) + Spacer( + modifier = Modifier.width(12.dp) + ) + Text( + text = formatTime(remainingTime), + fontSize = 22.sp + ) + } + } +} + +@Composable +fun BalanceCard(balance: Double, balanceWarning: Double) { + Card( + modifier = Modifier + .padding(16.dp), + colors = CardDefaults.cardColors( + containerColor = balanceCardColor(balance) + ) + ) { + Row( + modifier = Modifier.padding(16.dp, 8.dp), + verticalAlignment = Alignment.CenterVertically + ) { + Icon( + modifier = Modifier.size(24.dp), + painter = painterResource(id = R.drawable.baseline_monetization_on_24), + contentDescription = stringResource(id = R.string.balance) + ) + Spacer( + modifier = Modifier.width(12.dp) + ) + Text( + text = "$%.2f".format(balance), + fontSize = 22.sp, + color = balanceColor(balance, balanceWarning) + ) + } + } +} + +@Composable +fun InstancesCard(rentedInstancesCount: Int) { + Card( + modifier = Modifier + .padding(16.dp) + ) { + Row( + modifier = Modifier.padding(16.dp, 8.dp), + verticalAlignment = Alignment.CenterVertically + ) { + Icon( + modifier = Modifier.size(24.dp), + painter = painterResource(id = R.drawable.server_solid), + contentDescription = stringResource(id = R.string.rented_instances) + ) + Spacer( + modifier = Modifier.width(12.dp) + ) + Text( + text = rentedInstancesCount.toString(), + fontSize = 22.sp + ) + } + } +} + @Composable fun balanceCardColor(balance: Double): Color { return if (balance > 0) Color.Unspecified else MaterialTheme.colorScheme.errorContainer