Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support pagination in health connect #862

Merged
merged 3 commits into from
Jan 26, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1691,15 +1691,37 @@ 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
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.isNullOrEmpty()) {
hoffmatteo marked this conversation as resolved.
Show resolved Hide resolved
request = ReadRecordsRequest(
recordType = classType,
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 +1774,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