Skip to content

Commit

Permalink
support pagination in health connect
Browse files Browse the repository at this point in the history
  • Loading branch information
armanso committed Dec 20, 2023
1 parent 3aef0e2 commit fc152a6
Showing 1 changed file with 29 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1691,15 +1691,39 @@ class HealthPlugin(private var channel: MethodChannel? = null) :
val healthConnectData = mutableListOf<Map<String, Any?>>()
scope.launch {
MapToHCType[dataType]?.let { classType ->
val request = ReadRecordsRequest(
val records = mutableListOf<Record>()

// Set up the initial request to read health records with specified parameters
var request = ReadRecordsRequest(
recordType = classType,
// Define the maximum amount of data that HealthConnect can return in a single request
pageSize = 5000,
timeRangeFilter = TimeRangeFilter.between(startTime, endTime),
)
val response = healthConnectClient.readRecords(request)

var response = healthConnectClient.readRecords(request)
var pageToken = response.pageToken

// Add the records from the initial response to the records list
records.addAll(response.records)

// Continue making requests and fetching records while there is a page token
while (pageToken != null) {
request = ReadRecordsRequest(
recordType = classType,
pageSize = 5000,
timeRangeFilter = TimeRangeFilter.between(startTime, endTime),
pageToken = pageToken
)
response = healthConnectClient.readRecords(request)

pageToken = response.pageToken
records.addAll(response.records)
}

// Workout needs distance and total calories burned too
if (dataType == WORKOUT) {
for (rec in response.records) {
for (rec in records) {
val record = rec as ExerciseSessionRecord
val distanceRequest = healthConnectClient.readRecords(
ReadRecordsRequest(
Expand Down Expand Up @@ -1752,15 +1776,15 @@ class HealthPlugin(private var channel: MethodChannel? = null) :
}
// Filter sleep stages for requested stage
} else if (classType == SleepStageRecord::class) {
for (rec in response.records) {
for (rec in records) {
if (rec is SleepStageRecord) {
if (dataType == MapSleepStageToType[rec.stage]) {
healthConnectData.addAll(convertRecord(rec, dataType))
}
}
}
} else {
for (rec in response.records) {
for (rec in records) {
healthConnectData.addAll(convertRecord(rec, dataType))
}
}
Expand Down

0 comments on commit fc152a6

Please sign in to comment.