Skip to content

Commit

Permalink
Fix health connect availability check
Browse files Browse the repository at this point in the history
  • Loading branch information
vishnuravi committed Jan 15, 2024
1 parent d759d84 commit 50b3555
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/deploymentTargetDropDown.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/kotlinc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/migrations.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package edu.stanford.cardinalkit.data.repositories

import android.util.Log
import com.google.android.gms.auth.api.identity.BeginSignInRequest
import com.google.android.gms.auth.api.identity.SignInClient
import com.google.android.gms.auth.api.signin.GoogleSignInClient
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class HealthViewModel @Inject constructor(
var weeklyAverageWeight = mutableStateOf<Mass?>(null)
private set

private var permissionsGranted = mutableStateOf<Boolean>(false)
private var permissionsGranted = mutableStateOf<Boolean?>(false)

val permissions = setOf(
HealthPermission.getReadPermission(StepsRecord::class),
Expand All @@ -35,13 +35,15 @@ class HealthViewModel @Inject constructor(

init {
viewModelScope.launch {
permissionsGranted.value = healthConnectManager.hasAllPermissions(permissions)
getTotalStepsToday()
if (healthConnectManager.isAvailable.value) {
permissionsGranted.value = healthConnectManager.hasAllPermissions(permissions)
getTotalStepsToday()
}
}
}

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()
Expand All @@ -56,7 +58,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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,22 @@ 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

init {
isAvailable.value = checkAvailabilityStatus()
if (isAvailable.value) {
initClient()
}
}

private fun initClient() {
if (healthConnectClient == null && isAvailable.value) {
healthConnectClient = HealthConnectClient.getOrCreate(context)
}
}

fun checkAvailabilityStatus(): Boolean {
Expand All @@ -34,8 +43,8 @@ class HealthConnectManager @Inject constructor(
/**
* Determines if all requested permissions are granted.
*/
suspend fun hasAllPermissions(permissions: Set<String>): Boolean {
return healthConnectClient.permissionController.getGrantedPermissions().containsAll(permissions)
suspend fun hasAllPermissions(permissions: Set<String>): Boolean? {
return healthConnectClient?.permissionController?.getGrantedPermissions()?.containsAll(permissions)
}

/**
Expand All @@ -44,15 +53,15 @@ class HealthConnectManager @Inject constructor(
suspend fun readStepsByTimeRange(
startTime: Instant,
endTime: Instant
): List<StepsRecord> {
): List<StepsRecord>? {
val response =
healthConnectClient.readRecords(
healthConnectClient?.readRecords(
ReadRecordsRequest(
StepsRecord::class,
timeRangeFilter = TimeRangeFilter.between(startTime, endTime)
)
)
return response.records
return response?.records
}

/**
Expand All @@ -63,13 +72,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
}

/**
Expand All @@ -78,9 +87,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)
Expand All @@ -94,26 +103,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<WeightRecord> {
suspend fun getWeightRecords(startTime: Instant, endTime: Instant): List<WeightRecord>? {
val response =
healthConnectClient.readRecords(
healthConnectClient?.readRecords(
ReadRecordsRequest(
recordType = WeightRecord::class,
timeRangeFilter = TimeRangeFilter.between(startTime, endTime)
)
)
return response.records
return response?.records
}
}
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 50b3555

Please sign in to comment.