Skip to content

Commit

Permalink
Fix take medication button behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
Wottrich committed Oct 10, 2023
1 parent 83b6610 commit c8aee0a
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
)
}
}
}
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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(),
)
) {

}
}

0 comments on commit c8aee0a

Please sign in to comment.