Skip to content

Commit 6fad701

Browse files
authored
refactor: replace startActivityForResult with ActivityResultLauncher (#1150)
1 parent 447f972 commit 6fad701

File tree

2 files changed

+25
-20
lines changed

2 files changed

+25
-20
lines changed

google/build.gradle

+2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ android {
4242
dependencies {
4343
api "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
4444
api "com.google.android.gms:play-services-auth:19.2.0"
45+
api "androidx.activity:activity-ktx:1.3.1"
46+
api "androidx.fragment:fragment-ktx:1.3.1"
4547
implementation project(":parse")
4648
}
4749

google/src/main/java/com/parse/google/ParseGoogleUtils.kt

+23-20
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.parse.google
33
import android.app.Activity
44
import android.content.Context
55
import android.content.Intent
6+
import androidx.activity.result.ActivityResultLauncher
67
import com.google.android.gms.auth.api.signin.GoogleSignIn
78
import com.google.android.gms.auth.api.signin.GoogleSignInAccount
89
import com.google.android.gms.auth.api.signin.GoogleSignInClient
@@ -22,12 +23,12 @@ import com.parse.boltsinternal.TaskCompletionSource
2223
object ParseGoogleUtils {
2324

2425
private const val AUTH_TYPE = "google"
25-
private var clientId: String? = null
26+
private lateinit var clientId: String
2627

2728
private val lock = Any()
2829

2930
private var isInitialized = false
30-
private var googleSignInClient: GoogleSignInClient? = null
31+
private lateinit var googleSignInClient: GoogleSignInClient
3132

3233
/**
3334
* Just hope this doesn't clash I guess...
@@ -61,33 +62,35 @@ object ParseGoogleUtils {
6162
*
6263
* @param activity The activity which passes along the result via [onActivityResult]
6364
* @param callback The [LogInCallback] which is invoked on log in success or error
65+
* @param launcher The ActivityResultLauncher<Intent> from AndroidX for Example:
66+
*
67+
* val launcher: ActivityResultLauncher<Intent> = registerForActivityResult(
68+
* ActivityResultContracts.StartActivityForResult()) { result ->
69+
* ParseGoogleUtils.onActivityResult(result.resultCode, result.data!!)
70+
* }
6471
*/
6572
@JvmStatic
66-
fun logIn(activity: Activity, callback: LogInCallback) {
73+
fun logIn(activity: Activity, launcher: ActivityResultLauncher<Intent>, callback: LogInCallback) {
6774
checkInitialization()
6875
this.currentCallback = callback
6976
val googleSignInClient = buildGoogleSignInClient(activity)
7077
this.googleSignInClient = googleSignInClient
71-
activity.startActivityForResult(
72-
googleSignInClient.signInIntent,
73-
REQUEST_CODE_GOOGLE_SIGN_IN
74-
)
78+
launcher.launch(googleSignInClient.signInIntent)
7579
}
7680

7781
/**
7882
* The method that should be called from the Activity's or Fragment's onActivityResult method.
7983
*
80-
* @param requestCode The request code that's received by the Activity or Fragment.
8184
* @param resultCode The result code that's received by the Activity or Fragment.
8285
* @param data The result data that's received by the Activity or Fragment.
8386
* @return true if the result could be handled.
8487
*/
8588
@JvmStatic
86-
fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?): Boolean {
87-
if (requestCode != REQUEST_CODE_GOOGLE_SIGN_IN) {
89+
fun onActivityResult(resultCode: Int, data: Intent?): Boolean {
90+
if (resultCode != Activity.RESULT_OK) {
8891
return false
8992
}
90-
if (requestCode == REQUEST_CODE_GOOGLE_SIGN_IN) {
93+
if (resultCode == Activity.RESULT_OK) {
9194
if (data != null) {
9295
handleSignInResult(data)
9396
} else {
@@ -151,7 +154,7 @@ object ParseGoogleUtils {
151154
}
152155

153156
private fun onSignedIn(account: GoogleSignInAccount) {
154-
googleSignInClient?.signOut()?.addOnCompleteListener {}
157+
googleSignInClient.signOut().addOnCompleteListener {}
155158
val authData: Map<String, String> = getAuthData(account)
156159
ParseUser.logInWithInBackground(AUTH_TYPE, authData)
157160
.continueWith { task ->
@@ -224,34 +227,34 @@ object ParseGoogleUtils {
224227
}
225228
val tcs: TaskCompletionSource<T> = TaskCompletionSource<T>()
226229
task.continueWith<Void>(
227-
Continuation { task ->
228-
if (task.isCancelled && !reportCancellation) {
230+
Continuation { task2 ->
231+
if (task2.isCancelled && !reportCancellation) {
229232
tcs.setCancelled()
230233
return@Continuation null
231234
}
232235
Task.UI_THREAD_EXECUTOR.execute {
233236
try {
234-
var error = task.error
237+
var error = task2.error
235238
if (error != null && error !is ParseException) {
236239
error = ParseException(error)
237240
}
238241
if (callback is SaveCallback) {
239242
callback.done(error as? ParseException)
240243
} else if (callback is LogInCallback) {
241244
callback.done(
242-
task.result as? ParseUser, error as? ParseException
245+
task2.result as? ParseUser, error as? ParseException
243246
)
244247
}
245248
} finally {
246249
when {
247-
task.isCancelled -> {
250+
task2.isCancelled -> {
248251
tcs.setCancelled()
249252
}
250-
task.isFaulted -> {
251-
tcs.setError(task.error)
253+
task2.isFaulted -> {
254+
tcs.setError(task2.error)
252255
}
253256
else -> {
254-
tcs.setResult(task.result)
257+
tcs.setResult(task2.result)
255258
}
256259
}
257260
}

0 commit comments

Comments
 (0)