From 1beacf4de6655e5b235aad428e6c59ee862fcdfb Mon Sep 17 00:00:00 2001 From: Vishnu Ravi Date: Mon, 15 Jan 2024 06:37:21 -0500 Subject: [PATCH] Fix null values on home screen cards --- .../presentation/health/HealthViewModel.kt | 8 +++----- .../presentation/home/components/StepsCard.kt | 4 +++- .../presentation/home/components/WeightCard.kt | 14 +++++++------- .../tasks/components/StepGoalProgress.kt | 5 +++-- .../cardinalkit/services/HealthConnectManager.kt | 13 +++---------- 5 files changed, 19 insertions(+), 25 deletions(-) diff --git a/app/src/main/java/edu/stanford/cardinalkit/presentation/health/HealthViewModel.kt b/app/src/main/java/edu/stanford/cardinalkit/presentation/health/HealthViewModel.kt index a899649..57b35d5 100644 --- a/app/src/main/java/edu/stanford/cardinalkit/presentation/health/HealthViewModel.kt +++ b/app/src/main/java/edu/stanford/cardinalkit/presentation/health/HealthViewModel.kt @@ -20,7 +20,7 @@ import javax.inject.Inject class HealthViewModel @Inject constructor( var healthConnectManager: HealthConnectManager ) : ViewModel() { - var totalStepsToday = mutableStateOf(0) + var totalStepsToday = mutableStateOf(null) private set var weeklyAverageWeight = mutableStateOf(null) @@ -35,10 +35,8 @@ class HealthViewModel @Inject constructor( init { viewModelScope.launch { - if (healthConnectManager.isAvailable.value) { - permissionsGranted.value = healthConnectManager.hasAllPermissions(permissions) - getTotalStepsToday() - } + permissionsGranted.value = healthConnectManager.hasAllPermissions(permissions) + getTotalStepsToday() } } diff --git a/app/src/main/java/edu/stanford/cardinalkit/presentation/home/components/StepsCard.kt b/app/src/main/java/edu/stanford/cardinalkit/presentation/home/components/StepsCard.kt index 011bd8e..33e2d9e 100644 --- a/app/src/main/java/edu/stanford/cardinalkit/presentation/home/components/StepsCard.kt +++ b/app/src/main/java/edu/stanford/cardinalkit/presentation/home/components/StepsCard.kt @@ -26,6 +26,8 @@ fun StepsCard( viewModel: HealthViewModel = hiltViewModel() ) { viewModel.getTotalStepsToday() + val totalStepsString = viewModel.totalStepsToday.value?.toString() ?: "-" + Card( modifier = Modifier .width(165.dp) @@ -48,7 +50,7 @@ fun StepsCard( color = MaterialTheme.colorScheme.onSecondary ) Text( - text = viewModel.totalStepsToday.value.toString(), + text = totalStepsString, fontSize = 40.sp, color = MaterialTheme.colorScheme.onSecondary ) diff --git a/app/src/main/java/edu/stanford/cardinalkit/presentation/home/components/WeightCard.kt b/app/src/main/java/edu/stanford/cardinalkit/presentation/home/components/WeightCard.kt index cd8099c..9661484 100644 --- a/app/src/main/java/edu/stanford/cardinalkit/presentation/home/components/WeightCard.kt +++ b/app/src/main/java/edu/stanford/cardinalkit/presentation/home/components/WeightCard.kt @@ -32,16 +32,16 @@ fun WeightCard( var avgWeightString = "-" val avgWeight = viewModel.weeklyAverageWeight.value - if (useMetricUnits) { - avgWeight?.inKilograms?.roundToInt().let { - avgWeightString = "$it kg" - } + val weightInSelectedUnit = if (useMetricUnits) { + avgWeight?.inKilograms?.roundToInt() } else { - avgWeight?.inPounds?.roundToInt().let { - avgWeightString = "$it lbs" - } + avgWeight?.inPounds?.roundToInt() } + avgWeightString = weightInSelectedUnit?.let { + "$it ${if (useMetricUnits) "kg" else "lbs"}" + } ?: "-" + Card( modifier = Modifier .width(165.dp) diff --git a/app/src/main/java/edu/stanford/cardinalkit/presentation/tasks/components/StepGoalProgress.kt b/app/src/main/java/edu/stanford/cardinalkit/presentation/tasks/components/StepGoalProgress.kt index c7c8d64..b767a8b 100644 --- a/app/src/main/java/edu/stanford/cardinalkit/presentation/tasks/components/StepGoalProgress.kt +++ b/app/src/main/java/edu/stanford/cardinalkit/presentation/tasks/components/StepGoalProgress.kt @@ -28,8 +28,9 @@ fun StepGoalProgress( // Fetch total step count and calculate metrics healthViewModel.getTotalStepsToday() val goal = task.context.integerGoal - val totalStepsToday = healthViewModel.totalStepsToday.value - var progress = healthViewModel.totalStepsToday.value.toFloat() / goal + val totalStepsToday = healthViewModel.totalStepsToday.value ?: 0 + + var progress = totalStepsToday.toFloat() / goal if (progress > 1.0F) { progress = 1.0F } diff --git a/app/src/main/java/edu/stanford/cardinalkit/services/HealthConnectManager.kt b/app/src/main/java/edu/stanford/cardinalkit/services/HealthConnectManager.kt index 508441a..a14fe96 100644 --- a/app/src/main/java/edu/stanford/cardinalkit/services/HealthConnectManager.kt +++ b/app/src/main/java/edu/stanford/cardinalkit/services/HealthConnectManager.kt @@ -19,23 +19,16 @@ class HealthConnectManager @Inject constructor( ) { var healthConnectClient: HealthConnectClient? = null - var isAvailable = mutableStateOf(false) - private set + private var isAvailable = mutableStateOf(false) init { isAvailable.value = checkAvailabilityStatus() if (isAvailable.value) { - initClient() - } - } - - private fun initClient() { - if (healthConnectClient == null && isAvailable.value) { healthConnectClient = HealthConnectClient.getOrCreate(context) } } - fun checkAvailabilityStatus(): Boolean { + private fun checkAvailabilityStatus(): Boolean { val availabilityStatus = HealthConnectClient.sdkStatus(context, "com.google.android.apps.healthdata") return availabilityStatus == HealthConnectClient.SDK_AVAILABLE } @@ -123,6 +116,6 @@ class HealthConnectManager @Inject constructor( timeRangeFilter = TimeRangeFilter.between(startTime, endTime) ) ) - return response?.records + return response?.records } }