Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import android.os.Bundle
sealed class GoogleApiAlgebra
data class Connected(val bundle: Bundle?) : GoogleApiAlgebra()
data class Suspended(val suspended: Int) : GoogleApiAlgebra()
data class Failed(val result: ConnectionResult) : GoogleApiAlgebra()
suspend fun connect(client: GoogleApiClient): GoogleApiAlgebra =
suspendCancellableCoroutine { cont ->
val cc = object : GoogleApiClient.ConnectionCallbacks {
override fun onConnected(p0: Bundle?): Unit =
cont.resume(Connected(p0))
override fun onConnectionSuspended(p0: Int): Unit =
cont.resume(Suspended(p0))
}
val cfl = GoogleApiClient.OnConnectionFailedListener {
cont.resume(Failed(it))
}
cont.invokeOnCompletion {
client.unregisterConnectionCallbacks(cc)
client.unregisterConnectionFailedListener(cfl)
client.disconnect()
}
client.registerConnectionCallbacks(cc)
client.registerConnectionFailedListener(cfl)
client.connect()
}
fun Context.mkClient(): GoogleApiClient =
GoogleApiClient.Builder(ctx)
.addApi(Auth.GOOGLE_SIGN_IN_API,
GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestId()
.build()
)
.build()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment