Skip to content

Commit

Permalink
refactor: replace startActivityForResult with ActivityResultLauncher (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Shvet authored Jan 18, 2022
1 parent 447f972 commit 6fad701
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
2 changes: 2 additions & 0 deletions google/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ android {
dependencies {
api "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
api "com.google.android.gms:play-services-auth:19.2.0"
api "androidx.activity:activity-ktx:1.3.1"
api "androidx.fragment:fragment-ktx:1.3.1"
implementation project(":parse")
}

Expand Down
43 changes: 23 additions & 20 deletions google/src/main/java/com/parse/google/ParseGoogleUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.parse.google
import android.app.Activity
import android.content.Context
import android.content.Intent
import androidx.activity.result.ActivityResultLauncher
import com.google.android.gms.auth.api.signin.GoogleSignIn
import com.google.android.gms.auth.api.signin.GoogleSignInAccount
import com.google.android.gms.auth.api.signin.GoogleSignInClient
Expand All @@ -22,12 +23,12 @@ import com.parse.boltsinternal.TaskCompletionSource
object ParseGoogleUtils {

private const val AUTH_TYPE = "google"
private var clientId: String? = null
private lateinit var clientId: String

private val lock = Any()

private var isInitialized = false
private var googleSignInClient: GoogleSignInClient? = null
private lateinit var googleSignInClient: GoogleSignInClient

/**
* Just hope this doesn't clash I guess...
Expand Down Expand Up @@ -61,33 +62,35 @@ object ParseGoogleUtils {
*
* @param activity The activity which passes along the result via [onActivityResult]
* @param callback The [LogInCallback] which is invoked on log in success or error
* @param launcher The ActivityResultLauncher<Intent> from AndroidX for Example:
*
* val launcher: ActivityResultLauncher<Intent> = registerForActivityResult(
* ActivityResultContracts.StartActivityForResult()) { result ->
* ParseGoogleUtils.onActivityResult(result.resultCode, result.data!!)
* }
*/
@JvmStatic
fun logIn(activity: Activity, callback: LogInCallback) {
fun logIn(activity: Activity, launcher: ActivityResultLauncher<Intent>, callback: LogInCallback) {
checkInitialization()
this.currentCallback = callback
val googleSignInClient = buildGoogleSignInClient(activity)
this.googleSignInClient = googleSignInClient
activity.startActivityForResult(
googleSignInClient.signInIntent,
REQUEST_CODE_GOOGLE_SIGN_IN
)
launcher.launch(googleSignInClient.signInIntent)
}

/**
* The method that should be called from the Activity's or Fragment's onActivityResult method.
*
* @param requestCode The request code that's received by the Activity or Fragment.
* @param resultCode The result code that's received by the Activity or Fragment.
* @param data The result data that's received by the Activity or Fragment.
* @return true if the result could be handled.
*/
@JvmStatic
fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?): Boolean {
if (requestCode != REQUEST_CODE_GOOGLE_SIGN_IN) {
fun onActivityResult(resultCode: Int, data: Intent?): Boolean {
if (resultCode != Activity.RESULT_OK) {
return false
}
if (requestCode == REQUEST_CODE_GOOGLE_SIGN_IN) {
if (resultCode == Activity.RESULT_OK) {
if (data != null) {
handleSignInResult(data)
} else {
Expand Down Expand Up @@ -151,7 +154,7 @@ object ParseGoogleUtils {
}

private fun onSignedIn(account: GoogleSignInAccount) {
googleSignInClient?.signOut()?.addOnCompleteListener {}
googleSignInClient.signOut().addOnCompleteListener {}
val authData: Map<String, String> = getAuthData(account)
ParseUser.logInWithInBackground(AUTH_TYPE, authData)
.continueWith { task ->
Expand Down Expand Up @@ -224,34 +227,34 @@ object ParseGoogleUtils {
}
val tcs: TaskCompletionSource<T> = TaskCompletionSource<T>()
task.continueWith<Void>(
Continuation { task ->
if (task.isCancelled && !reportCancellation) {
Continuation { task2 ->
if (task2.isCancelled && !reportCancellation) {
tcs.setCancelled()
return@Continuation null
}
Task.UI_THREAD_EXECUTOR.execute {
try {
var error = task.error
var error = task2.error
if (error != null && error !is ParseException) {
error = ParseException(error)
}
if (callback is SaveCallback) {
callback.done(error as? ParseException)
} else if (callback is LogInCallback) {
callback.done(
task.result as? ParseUser, error as? ParseException
task2.result as? ParseUser, error as? ParseException
)
}
} finally {
when {
task.isCancelled -> {
task2.isCancelled -> {
tcs.setCancelled()
}
task.isFaulted -> {
tcs.setError(task.error)
task2.isFaulted -> {
tcs.setError(task2.error)
}
else -> {
tcs.setResult(task.result)
tcs.setResult(task2.result)
}
}
}
Expand Down

0 comments on commit 6fad701

Please sign in to comment.