Skip to content

Commit

Permalink
Fix null values on home screen cards
Browse files Browse the repository at this point in the history
  • Loading branch information
vishnuravi committed Jan 15, 2024
1 parent 50b3555 commit 1beacf4
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import javax.inject.Inject
class HealthViewModel @Inject constructor(
var healthConnectManager: HealthConnectManager
) : ViewModel() {
var totalStepsToday = mutableStateOf<Long>(0)
var totalStepsToday = mutableStateOf<Long?>(null)
private set

var weeklyAverageWeight = mutableStateOf<Mass?>(null)
Expand All @@ -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()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ fun StepsCard(
viewModel: HealthViewModel = hiltViewModel()
) {
viewModel.getTotalStepsToday()
val totalStepsString = viewModel.totalStepsToday.value?.toString() ?: "-"

Card(
modifier = Modifier
.width(165.dp)
Expand All @@ -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
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -123,6 +116,6 @@ class HealthConnectManager @Inject constructor(
timeRangeFilter = TimeRangeFilter.between(startTime, endTime)
)
)
return response?.records
return response?.records
}
}

0 comments on commit 1beacf4

Please sign in to comment.