From c8aee0a633ca8e56f4fa9812dbd612eced6e5c2f Mon Sep 17 00:00:00 2001 From: Wottrich Date: Tue, 10 Oct 2023 08:43:03 -0300 Subject: [PATCH 1/2] Fix take medication button behaviour --- .../doseapp/feature/home/HomeScreen.kt | 74 +---------- .../doseapp/feature/home/MedicationCard.kt | 121 ++++++++++++++++++ 2 files changed, 128 insertions(+), 67 deletions(-) create mode 100644 app/src/main/java/com/waseefakhtar/doseapp/feature/home/MedicationCard.kt diff --git a/app/src/main/java/com/waseefakhtar/doseapp/feature/home/HomeScreen.kt b/app/src/main/java/com/waseefakhtar/doseapp/feature/home/HomeScreen.kt index 759b0de..137b6d0 100644 --- a/app/src/main/java/com/waseefakhtar/doseapp/feature/home/HomeScreen.kt +++ b/app/src/main/java/com/waseefakhtar/doseapp/feature/home/HomeScreen.kt @@ -273,7 +273,13 @@ fun DailyMedications(navController: NavController, analyticsHelper: AnalyticsHel ) } is MedicationListItem.MedicationItem -> { - MedicationCard(it.medication, viewModel, analyticsHelper) + MedicationCard( + it.medication, + onTakeButtonClicked = { medication -> + analyticsHelper.logEvent(AnalyticsEvents.TAKE_MEDICATION_CLICKED) + viewModel.takeMedication(medication) + } + ) } } } @@ -287,72 +293,6 @@ sealed class MedicationListItem { data class HeaderItem(val headerText: String) : MedicationListItem() } -@Composable -fun MedicationCard(medication: Medication, viewModel: HomeViewModel, analyticsHelper: AnalyticsHelper) { - - Card( - modifier = Modifier - .fillMaxWidth() - .padding(vertical = 8.dp), - shape = RoundedCornerShape(30.dp), - colors = cardColors( - containerColor = MaterialTheme.colorScheme.surfaceVariant, - ) - ) { - - Row( - verticalAlignment = Alignment.CenterVertically - ) { - - Column( - modifier = Modifier - .padding(16.dp), - horizontalAlignment = Alignment.Start - ) { - Text( - text = medication.name, - fontWeight = FontWeight.Bold, - style = MaterialTheme.typography.titleLarge - ) - Text( - text = medication.timesOfDay.joinToString(", ") - ) - Spacer(modifier = Modifier.height(16.dp)) - Text( - text = getTimeRemaining(medication), - fontWeight = FontWeight.Bold - ) - } - - Column( - modifier = Modifier - .fillMaxSize() - .padding(16.dp), - horizontalAlignment = Alignment.End - ) { - - Button( - onClick = { - analyticsHelper.logEvent(AnalyticsEvents.TAKE_MEDICATION_CLICKED) - viewModel.takeMedication(medication) - }, - enabled = !medication.medicationTaken - ) { - if (medication.medicationTaken) { - Text( - text = "Taken" - ) - } else { - Text( - text = "Take now" - ) - } - } - } - } - } -} - @OptIn(ExperimentalPermissionsApi::class) @Composable fun PermissionDialog(analyticsHelper: AnalyticsHelper, askNotificationPermission: Boolean) { diff --git a/app/src/main/java/com/waseefakhtar/doseapp/feature/home/MedicationCard.kt b/app/src/main/java/com/waseefakhtar/doseapp/feature/home/MedicationCard.kt new file mode 100644 index 0000000..4c31eaf --- /dev/null +++ b/app/src/main/java/com/waseefakhtar/doseapp/feature/home/MedicationCard.kt @@ -0,0 +1,121 @@ +package com.waseefakhtar.doseapp.feature.home + +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.Spacer +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.height +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.shape.RoundedCornerShape +import androidx.compose.material3.Button +import androidx.compose.material3.Card +import androidx.compose.material3.CardDefaults +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.text.font.FontWeight +import androidx.compose.ui.tooling.preview.Preview +import androidx.compose.ui.unit.dp +import com.waseefakhtar.doseapp.domain.model.Medication +import com.waseefakhtar.doseapp.util.getTimeRemaining +import java.util.Date + +@Composable +fun MedicationCard( + medication: Medication, + onTakeButtonClicked: (Medication) -> Unit +) { + + Card( + modifier = Modifier + .fillMaxWidth() + .padding(vertical = 8.dp), + shape = RoundedCornerShape(30.dp), + colors = CardDefaults.cardColors( + containerColor = MaterialTheme.colorScheme.surfaceVariant, + ) + ) { + + Row( + verticalAlignment = Alignment.CenterVertically + ) { + + Column( + modifier = Modifier + .weight(2f) + .padding(16.dp), + horizontalAlignment = Alignment.Start + ) { + Text( + text = medication.name, + fontWeight = FontWeight.Bold, + style = MaterialTheme.typography.titleLarge + ) + Text( + text = medication.timesOfDay.joinToString(", ") + ) + Spacer(modifier = Modifier.height(16.dp)) + Text( + text = getTimeRemaining(medication), + fontWeight = FontWeight.Bold + ) + } + + Button( + modifier = Modifier.weight(1f).padding(end = 16.dp), + onClick = { onTakeButtonClicked(medication) }, + enabled = !medication.medicationTaken + ) { + if (medication.medicationTaken) { + Text( + text = "Taken" + ) + } else { + Text( + text = "Take now" + ) + } + } + } + } +} + +@Preview +@Composable +private fun MedicationCardTakeNowPreview() { + MedicationCard( + Medication( + id = 123L, + name = "A big big name for a little medication I needs to take", + dosage = 1, + recurrence = "2", + endDate = Date(), + timesOfDay = listOf(), + medicationTaken = false, + date = Date(), + ) + ) { + + } +} + +@Preview +@Composable +private fun MedicationCardTakenPreview() { + MedicationCard( + Medication( + id = 123L, + name = "A big big name for a little medication I needs to take", + dosage = 1, + recurrence = "2", + endDate = Date(), + timesOfDay = listOf(), + medicationTaken = true, + date = Date(), + ) + ) { + + } +} \ No newline at end of file From a35d6bc4326d41d37a3e403d8c7a8ffa06431b6f Mon Sep 17 00:00:00 2001 From: Waseef Akhtar Date: Wed, 11 Oct 2023 18:42:29 +0200 Subject: [PATCH 2/2] Fix ktlint --- .../java/com/waseefakhtar/doseapp/feature/home/HomeScreen.kt | 1 - .../com/waseefakhtar/doseapp/feature/home/MedicationCard.kt | 4 +--- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/app/src/main/java/com/waseefakhtar/doseapp/feature/home/HomeScreen.kt b/app/src/main/java/com/waseefakhtar/doseapp/feature/home/HomeScreen.kt index 137b6d0..4132cf4 100644 --- a/app/src/main/java/com/waseefakhtar/doseapp/feature/home/HomeScreen.kt +++ b/app/src/main/java/com/waseefakhtar/doseapp/feature/home/HomeScreen.kt @@ -48,7 +48,6 @@ import com.waseefakhtar.doseapp.domain.model.Medication import com.waseefakhtar.doseapp.feature.addmedication.navigation.AddMedicationDestination import com.waseefakhtar.doseapp.feature.home.viewmodel.HomeState import com.waseefakhtar.doseapp.feature.home.viewmodel.HomeViewModel -import com.waseefakhtar.doseapp.util.getTimeRemaining import java.util.Calendar @Composable diff --git a/app/src/main/java/com/waseefakhtar/doseapp/feature/home/MedicationCard.kt b/app/src/main/java/com/waseefakhtar/doseapp/feature/home/MedicationCard.kt index 4c31eaf..8ad0057 100644 --- a/app/src/main/java/com/waseefakhtar/doseapp/feature/home/MedicationCard.kt +++ b/app/src/main/java/com/waseefakhtar/doseapp/feature/home/MedicationCard.kt @@ -97,7 +97,6 @@ private fun MedicationCardTakeNowPreview() { date = Date(), ) ) { - } } @@ -116,6 +115,5 @@ private fun MedicationCardTakenPreview() { date = Date(), ) ) { - } -} \ No newline at end of file +}