diff --git a/.idea/compiler.xml b/.idea/compiler.xml index fb7f4a8..b589d56 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/.idea/deploymentTargetDropDown.xml b/.idea/deploymentTargetDropDown.xml new file mode 100644 index 0000000..0c0c338 --- /dev/null +++ b/.idea/deploymentTargetDropDown.xml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml index a9f4e52..0897082 100644 --- a/.idea/gradle.xml +++ b/.idea/gradle.xml @@ -1,17 +1,18 @@ + diff --git a/.idea/kotlinc.xml b/.idea/kotlinc.xml new file mode 100644 index 0000000..2b8a50f --- /dev/null +++ b/.idea/kotlinc.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/migrations.xml b/.idea/migrations.xml new file mode 100644 index 0000000..f8051a6 --- /dev/null +++ b/.idea/migrations.xml @@ -0,0 +1,10 @@ + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index 2a4d5b5..0f86676 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,5 @@ - - + diff --git a/app/src/main/java/edu/stanford/cardinalkit/presentation/MainActivity.kt b/app/src/main/java/edu/stanford/cardinalkit/presentation/MainActivity.kt index a85b0d8..06288c2 100644 --- a/app/src/main/java/edu/stanford/cardinalkit/presentation/MainActivity.kt +++ b/app/src/main/java/edu/stanford/cardinalkit/presentation/MainActivity.kt @@ -56,9 +56,9 @@ class MainActivity : AppCompatActivity() { val granted = healthViewModel .healthConnectManager .healthConnectClient - .permissionController - .getGrantedPermissions() - if (!granted.containsAll(permissions)) { + ?.permissionController + ?.getGrantedPermissions() + if (granted != null && !granted.containsAll(permissions)) { requestPermissions.launch(permissions) } else { healthViewModel.updatePermissionsStatus(true) 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 81a6092..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,13 +20,13 @@ 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) private set - private var permissionsGranted = mutableStateOf(false) + private var permissionsGranted = mutableStateOf(false) val permissions = setOf( HealthPermission.getReadPermission(StepsRecord::class), @@ -41,7 +41,7 @@ class HealthViewModel @Inject constructor( } fun getTotalStepsToday() = viewModelScope.launch { - if (permissionsGranted.value) { + if (permissionsGranted.value == true) { totalStepsToday.value = healthConnectManager.aggregateSteps( startTime = LocalDate.now().atStartOfDay(ZoneId.systemDefault()).toInstant(), endTime = Instant.now() @@ -56,7 +56,7 @@ class HealthViewModel @Inject constructor( val endOfWeek = Instant.now() val startOfWeek = endOfWeek.minus(7, ChronoUnit.DAYS) - if (permissionsGranted.value) { + if (permissionsGranted.value == true) { weeklyAverageWeight.value = healthConnectManager.getAverageWeight( startTime = startOfWeek, endTime = endOfWeek 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 230bdff..a14fe96 100644 --- a/app/src/main/java/edu/stanford/cardinalkit/services/HealthConnectManager.kt +++ b/app/src/main/java/edu/stanford/cardinalkit/services/HealthConnectManager.kt @@ -17,16 +17,18 @@ import javax.inject.Inject class HealthConnectManager @Inject constructor( private val context: Context ) { - val healthConnectClient by lazy { HealthConnectClient.getOrCreate(context) } + var healthConnectClient: HealthConnectClient? = null - var isAvailable = mutableStateOf(false) - private set + private var isAvailable = mutableStateOf(false) init { isAvailable.value = checkAvailabilityStatus() + if (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 } @@ -34,8 +36,8 @@ class HealthConnectManager @Inject constructor( /** * Determines if all requested permissions are granted. */ - suspend fun hasAllPermissions(permissions: Set): Boolean { - return healthConnectClient.permissionController.getGrantedPermissions().containsAll(permissions) + suspend fun hasAllPermissions(permissions: Set): Boolean? { + return healthConnectClient?.permissionController?.getGrantedPermissions()?.containsAll(permissions) } /** @@ -44,15 +46,15 @@ class HealthConnectManager @Inject constructor( suspend fun readStepsByTimeRange( startTime: Instant, endTime: Instant - ): List { + ): List? { val response = - healthConnectClient.readRecords( + healthConnectClient?.readRecords( ReadRecordsRequest( StepsRecord::class, timeRangeFilter = TimeRangeFilter.between(startTime, endTime) ) ) - return response.records + return response?.records } /** @@ -63,13 +65,13 @@ class HealthConnectManager @Inject constructor( endTime: Instant ): Long { val response = - healthConnectClient.aggregate( + healthConnectClient?.aggregate( AggregateRequest( metrics = setOf(StepsRecord.COUNT_TOTAL), timeRangeFilter = TimeRangeFilter.between(startTime, endTime) ) ) - return response[StepsRecord.COUNT_TOTAL] ?: 0 + return response?.get(StepsRecord.COUNT_TOTAL) ?: 0 } /** @@ -78,9 +80,9 @@ class HealthConnectManager @Inject constructor( suspend fun aggregateHeartRate( startTime: Instant, endTime: Instant - ): AggregationResult { + ): AggregationResult? { val response = - healthConnectClient.aggregate( + healthConnectClient?.aggregate( AggregateRequest( setOf(HeartRateRecord.BPM_MAX, HeartRateRecord.BPM_MIN), timeRangeFilter = TimeRangeFilter.between(startTime, endTime) @@ -94,26 +96,26 @@ class HealthConnectManager @Inject constructor( */ suspend fun getAverageWeight(startTime: Instant, endTime: Instant): Mass? { val response = - healthConnectClient.aggregate( + healthConnectClient?.aggregate( AggregateRequest( metrics = setOf(WeightRecord.WEIGHT_AVG), timeRangeFilter = TimeRangeFilter.between(startTime, endTime) ) ) - return response[WeightRecord.WEIGHT_AVG] + return response?.get(WeightRecord.WEIGHT_AVG) } /** * Get all weight records for a certain time period */ - suspend fun getWeightRecords(startTime: Instant, endTime: Instant): List { + suspend fun getWeightRecords(startTime: Instant, endTime: Instant): List? { val response = - healthConnectClient.readRecords( + healthConnectClient?.readRecords( ReadRecordsRequest( recordType = WeightRecord::class, timeRangeFilter = TimeRangeFilter.between(startTime, endTime) ) ) - return response.records + return response?.records } } diff --git a/build.gradle b/build.gradle index 2d9b4f2..70deb8c 100644 --- a/build.gradle +++ b/build.gradle @@ -10,7 +10,7 @@ buildscript { compose_version = "1.4.1" compose_bom_version = "2023.03.00" hilt_navigation_compose_version = "1.0.0" - firebase_bom_version = "30.1.0" + firebase_bom_version = "32.7.0" play_services_version = "1.6.2" play_services_auth_version = "20.3.0" accompanist_version = "0.24.10-beta"