Skip to content

Instantly share code, notes, and snippets.

@dkhmelenko
Created February 4, 2019 14:35
Show Gist options
  • Save dkhmelenko/ca6bf45c999bc17c1da6a81233a4f9f4 to your computer and use it in GitHub Desktop.
Save dkhmelenko/ca6bf45c999bc17c1da6a81233a4f9f4 to your computer and use it in GitHub Desktop.
private val activityResultSubject = PublishSubject.create<ActivityResult>()
fun saveCredentialsRequest(credentialsToSave: Credential): Completable {
return Completable.create { emitter ->
Auth.CredentialsApi
.save(googleApiClient, credentialsToSave)
.setResultCallback { result ->
val status = result.status
if (status.isSuccess) {
emitter.onComplete()
} else if (status.hasResolution()) {
try {
val disposable = activityResultSubject
.filter { it.requestCode == REQUEST_CODE_STORE_CREDENTIALS }
.subscribe { handleSaveCredentialsResult(it.resultCode, emitter) }
emitter.setDisposable(disposable)
val activity = googleApiClient?.context as Activity
status.startResolutionForResult(activity, REQUEST_CODE_STORE_CREDENTIALS)
} catch (e: IntentSender.SendIntentException) {
emitter.onError(Exception("Save credential startResolutionForResult failed: $e"))
}
} else {
emitter.onError(Exception("Credentials cannot be saved for ${credentialsToSave.id}"))
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment