Created
February 4, 2019 14:35
-
-
Save dkhmelenko/ca6bf45c999bc17c1da6a81233a4f9f4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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