Make Google login a suspend fun which waits for auth completion

This commit is contained in:
MM20 2021-09-28 15:42:34 +02:00
parent 893e644b0f
commit f4c7f54df1
No known key found for this signature in database
GPG Key ID: 0B61A8F2DEAFA389
3 changed files with 39 additions and 20 deletions

View File

@ -153,7 +153,9 @@ class PreferencesSearchFragment : PreferenceFragmentCompat() {
}
private fun googleLogin() {
GoogleApiHelper.getInstance(requireContext()).login(requireActivity())
lifecycleScope.launch {
GoogleApiHelper.getInstance(requireContext()).login(requireActivity())
}
}
private fun setSettingsScreen(fragment: Fragment) {

View File

@ -47,7 +47,9 @@ class PreferencesServicesFragment : PreferenceFragmentCompat() {
setTitle(R.string.preference_google_signin)
setSummary(R.string.preference_google_signin_summary)
setOnPreferenceClickListener {
googleApiHelper.login(requireActivity())
lifecycleScope.launch {
googleApiHelper.login(requireActivity())
}
true
}
}

View File

@ -4,6 +4,7 @@ import android.app.Activity
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.util.Log
import androidx.browser.customtabs.*
import androidx.core.content.edit
import com.google.api.client.auth.oauth2.Credential
@ -17,6 +18,7 @@ import com.google.api.services.drive.Drive
import com.google.api.services.oauth2.Oauth2
import de.mm20.launcher2.crashreporter.CrashReporter
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlinx.coroutines.withContext
import java.io.IOException
@ -119,29 +121,41 @@ class GoogleApiHelper private constructor(private val context: Context) {
.build()
}
fun login(activity: Activity) {
private var callback: (() -> Unit)? = null
suspend fun login(activity: Activity) {
val authFlow = getAuthFlow() ?: return
val url = authFlow
.newAuthorizationUrl()
.setRedirectUri(getRedirectUri())
.toString()
val themeColor = 0xFF4285f4.toInt()
suspendCancellableCoroutine<Unit> {
val url = authFlow
.newAuthorizationUrl()
.setRedirectUri(getRedirectUri())
.toString()
val themeColor = 0xFF4285f4.toInt()
val customTabsIntent = CustomTabsIntent
.Builder()
.setDefaultColorSchemeParams(
CustomTabColorSchemeParams.Builder()
.setToolbarColor(themeColor)
.setNavigationBarColor(themeColor)
.build()
)
.build()
val customTabsIntent = CustomTabsIntent
.Builder()
.setDefaultColorSchemeParams(
CustomTabColorSchemeParams.Builder()
.setToolbarColor(themeColor)
.setNavigationBarColor(themeColor)
.build()
)
.build()
callingActivity = activity.javaClass
callingActivity = activity.javaClass
callback = {
it.resumeWith(Result.success(Unit))
}
it.invokeOnCancellation {
callback = null
Log.d("MM20", "Google Signin has been canceled")
}
customTabsIntent.intent.flags = Intent.FLAG_ACTIVITY_NO_HISTORY
customTabsIntent.launchUrl(activity, Uri.parse(url))
customTabsIntent.intent.flags = Intent.FLAG_ACTIVITY_NO_HISTORY
customTabsIntent.launchUrl(activity, Uri.parse(url))
}
}
suspend fun finishAuthFlow(activity: Activity, code: String) {
@ -168,6 +182,7 @@ class GoogleApiHelper private constructor(private val context: Context) {
callingActivity = null
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP)
activity.startActivity(intent)
callback?.invoke()
}
private suspend fun loadAccountName(): String? {