Created
March 25, 2019 17:13
-
-
Save ntngel1/0c275333f70bfbb655343dc751c8b5a4 to your computer and use it in GitHub Desktop.
ZHOPA
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override fun getAntropometricData(): Single<AnthropometricModel> = Single.create { emitter -> | |
val weightDataType = DataType.TYPE_WEIGHT | |
val heightDataType = DataType.TYPE_HEIGHT | |
val weightSubscription = Fitness.getRecordingClient(context, account) | |
.subscribe(weightDataType) | |
Tasks.await(weightSubscription) | |
val heightSubscription = Fitness.getRecordingClient(context, account) | |
.subscribe(heightDataType) | |
Tasks.await(heightSubscription) | |
val heightRequest = DataReadRequest.Builder() | |
.read(heightDataType) | |
.setTimeRange(1, Calendar.getInstance().timeInMillis, TimeUnit.MILLISECONDS) | |
.setLimit(1) | |
.build() | |
val weightRequest = DataReadRequest.Builder() | |
.read(weightDataType) | |
.setTimeRange(1, Calendar.getInstance().timeInMillis, TimeUnit.MILLISECONDS) | |
.setLimit(1) | |
.build() | |
val historyClient = Fitness.getHistoryClient(context, account) | |
val heightRequestTask = historyClient.readData(heightRequest) | |
val heightDataSets = Tasks.await(heightRequestTask).dataSets | |
val weightRequestTask = historyClient.readData(weightRequest) | |
val weightDataSets = Tasks.await(weightRequestTask).dataSets | |
val lastHeightDataSet = heightDataSets?.lastOrNull() | |
val lastWeightDataSet = weightDataSets?.lastOrNull() | |
val lastHeightDataPoint = lastHeightDataSet?.dataPoints?.lastOrNull() | |
val lastWeightDataPoint = lastWeightDataSet?.dataPoints?.lastOrNull() | |
val height = lastHeightDataPoint?.getValue(Field.FIELD_HEIGHT)?.asFloat() | |
val weight = lastWeightDataPoint?.getValue(Field.FIELD_WEIGHT)?.asFloat() | |
val person = getPeopleService(account).people() | |
.get("people/me") | |
.setPersonFields("genders") | |
.execute() | |
val genderString = person?.genders?.firstOrNull()?.value | |
val gender = when (genderString) { | |
"male" -> Gender.MALE | |
"female" -> Gender.FEMALE | |
"other" -> Gender.OTHER | |
else -> Gender.UNKNOWN | |
} | |
val result = AnthropometricModel(weight, height, gender) | |
emitter.onSuccess(result) | |
} | |
private fun getPeopleService(googleAccount: GoogleSignInAccount): PeopleService { | |
val account = Account(googleAccount.email, "com.google") | |
val credential = GoogleAccountCredential.usingOAuth2(context, listOf("https://www.googleapis.com/auth/user.birthday.read")) | |
credential.selectedAccount = account | |
val httpTransport = AndroidHttp.newCompatibleTransport() | |
val jsonFactory = GsonFactory.getDefaultInstance() | |
return PeopleService.Builder(httpTransport, jsonFactory, credential) // TODO Dependency injection | |
.setApplicationName("Google Sign In") // TODO Application name | |
.build() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment