Created
July 24, 2020 15:39
-
-
Save galex/8690cf881ae0f1d291df91b0f1fae3e8 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
package il.co.galex.alexpizzapp.utils | |
import android.app.Activity | |
import android.content.Context | |
import android.content.ContextWrapper | |
import androidx.activity.result.ActivityResultLauncher | |
import androidx.activity.result.ActivityResultRegistry | |
import androidx.activity.result.contract.ActivityResultContract | |
import androidx.compose.* | |
import androidx.ui.core.ContextAmbient | |
import com.google.android.gms.auth.api.signin.GoogleSignInAccount | |
import com.google.android.gms.auth.api.signin.GoogleSignInClient | |
import com.google.firebase.auth.GoogleAuthProvider | |
import com.google.firebase.auth.ktx.auth | |
import com.google.firebase.ktx.Firebase | |
import il.co.galex.alexpizzapp.feature.user.UserRepository | |
@OptIn(ExperimentalComposeApi::class) | |
@Composable | |
fun <I, O> ActivityResultRegistry.activityResultLauncher( | |
requestContract: ActivityResultContract<I, O>, | |
onResult: (O) -> Unit | |
): ActivityResultLauncher<I> { | |
val key = currentComposer.currentCompoundKeyHash.toString() | |
val launcher = remember(requestContract, onResult) { | |
register(key, requestContract, onResult) | |
} | |
onDispose { | |
launcher.unregister() | |
} | |
return launcher | |
} | |
fun Context.findActivity(): Activity? { | |
var context = this | |
while (context is ContextWrapper) { | |
if (context is Activity) return context | |
context = context.baseContext | |
} | |
return null | |
} | |
class GoogleSignInState(private val googleSignInClient: GoogleSignInClient, | |
val signInState: State<GoogleSignInAccount?>, | |
private val launcher: ActivityResultLauncher<GoogleSignInClient>) { | |
fun launchSignInRequest() = launcher.launch(googleSignInClient) | |
} | |
@Composable | |
fun ActivityResultRegistry.googleSignIn(googleSignInClient: GoogleSignInClient): GoogleSignInState { | |
val context = ContextAmbient.current | |
val activity = context.findActivity() | |
val signInState = mutableStateOf<GoogleSignInAccount?>( null ) | |
val launcher = activityResultLauncher(GoogleSignInContract()) { | |
signInState.value = it | |
} | |
return remember(launcher) { | |
GoogleSignInState(googleSignInClient, signInState ,launcher) | |
} | |
} | |
// How can I integrate into this composable the following code? | |
private fun firebaseAuthWithGoogle(account: GoogleSignInAccount) { | |
val credential = GoogleAuthProvider.getCredential(account.idToken, null) | |
Firebase.auth.signInWithCredential(credential).addOnCompleteListener { task -> | |
if (task.isSuccessful) { | |
// Sign in success, update UI with the signed-in user's information | |
val firebaseUser = Firebase.auth.currentUser | |
val repo = UserRepository() | |
account.toUser(firebaseUser!!.uid).let { | |
repo.add(it) { | |
// TODO something nice to show to the user when he is officially in the Firebase User table | |
} | |
} | |
} else { | |
// If sign in fails, display a message to the user. | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment