Skip to content

Instantly share code, notes, and snippets.

@fbencosme
Last active June 8, 2016 12:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fbencosme/00c29479003d9ea10f69918eaf7875ee to your computer and use it in GitHub Desktop.
Save fbencosme/00c29479003d9ea10f69918eaf7875ee to your computer and use it in GitHub Desktop.
fun googleSignInOptionsObservable() : Observable<GoogleSignInOptions> =
  Observable.just(
  GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
   .requestIdToken(activity.getString(R.string.default_web_client_id))
   .requestEmail()
   .build())

fun googleApiClientObservable(gso : GoogleSignInOptions) : Observable<GoogleApiClient> =
  observable<GoogleApiClient> { subscriber ->
     var googleClient : GoogleApiClient? = null

     googleClient =
      GoogleApiClient.Builder(activity)
       .addConnectionCallbacks(object : GoogleApiClient.ConnectionCallbacks {
           override fun onConnected(var1 : Bundle?) {
               subscriber.onNext(googleClient)
               subscriber.onCompleted()
           }

           override fun onConnectionSuspended(var1 : Int) {
               subscriber.onError(Exception("onConnectionSuspended"))
               subscriber.onCompleted()
           }
       })
       .enableAutoManage(activity, { subscriber.onError(Exception(it.errorMessage)) })
       .addApi(GmsAuth.GOOGLE_SIGN_IN_API, gso)
       .build()
 }

fun continueWithGoogleRegister() = google.setOnClickListener {
    subscription!! +=
     googleSignInOptionsObservable()
      .flatMap { googleApiClientObservable(it) }
      .subscribeOn(Schedulers.newThread())
      .observeOn(AndroidSchedulers.mainThread())
      .map { GmsAuth.GoogleSignInApi.getSignInIntent(it) }
      .subscribe (
       { startActivityForResult(it, ReqCodeGoogleSignIn) },
       { context.toastLong(R.string.common_google_play_services_api_unavailable_text) })
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment