Skip to content

Commit

Permalink
merged code from issue cph-cachet#172
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasnilsson committed Sep 27, 2020
1 parent 82c7eb1 commit 7298840
Showing 1 changed file with 25 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -171,28 +171,33 @@ class HealthPlugin(val activity: Activity, val channel: MethodChannel) : MethodC

/// Start a new thread for doing a GoogleFit data lookup
thread {
val googleSignInAccount = GoogleSignIn.getAccountForExtension(activity.applicationContext, fitnessOptions)

val response = Fitness.getHistoryClient(activity.applicationContext, googleSignInAccount)
.readData(DataReadRequest.Builder()
.read(dataType)
.setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
.build())

/// Fetch all data points for the specified DataType
val dataPoints = Tasks.await<DataReadResponse>(response).getDataSet(dataType)

/// For each data point, extract the contents and send them to Flutter, along with date and unit.
val healthData = dataPoints.dataPoints.mapIndexed { _, dataPoint ->
return@mapIndexed hashMapOf(
"value" to getHealthDataValue(dataPoint, unit),
"date_from" to dataPoint.getStartTime(TimeUnit.MILLISECONDS),
"date_to" to dataPoint.getEndTime(TimeUnit.MILLISECONDS),
"unit" to unit.toString()
)
try {

val googleSignInAccount = GoogleSignIn.getAccountForExtension(activity.applicationContext, fitnessOptions)

val response = Fitness.getHistoryClient(activity.applicationContext, googleSignInAccount)
.readData(DataReadRequest.Builder()
.read(dataType)
.setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
.build())

/// Fetch all data points for the specified DataType
val dataPoints = Tasks.await<DataReadResponse>(response).getDataSet(dataType)

/// For each data point, extract the contents and send them to Flutter, along with date and unit.
val healthData = dataPoints.dataPoints.mapIndexed { _, dataPoint ->
return@mapIndexed hashMapOf(
"value" to getHealthDataValue(dataPoint, unit),
"date_from" to dataPoint.getStartTime(TimeUnit.MILLISECONDS),
"date_to" to dataPoint.getEndTime(TimeUnit.MILLISECONDS),
"unit" to unit.toString()
)

}
activity.runOnUiThread { result.success(healthData) }
} catch (e3: Exception) {
activity.runOnUiThread { result.success(null) }
}
activity.runOnUiThread { result.success(healthData) }
}
}

Expand Down

0 comments on commit 7298840

Please sign in to comment.