@@ -3,6 +3,7 @@ package com.parse.google
3
3
import android.app.Activity
4
4
import android.content.Context
5
5
import android.content.Intent
6
+ import androidx.activity.result.ActivityResultLauncher
6
7
import com.google.android.gms.auth.api.signin.GoogleSignIn
7
8
import com.google.android.gms.auth.api.signin.GoogleSignInAccount
8
9
import com.google.android.gms.auth.api.signin.GoogleSignInClient
@@ -22,12 +23,12 @@ import com.parse.boltsinternal.TaskCompletionSource
22
23
object ParseGoogleUtils {
23
24
24
25
private const val AUTH_TYPE = " google"
25
- private var clientId: String? = null
26
+ private lateinit var clientId: String
26
27
27
28
private val lock = Any ()
28
29
29
30
private var isInitialized = false
30
- private var googleSignInClient: GoogleSignInClient ? = null
31
+ private lateinit var googleSignInClient: GoogleSignInClient
31
32
32
33
/* *
33
34
* Just hope this doesn't clash I guess...
@@ -61,33 +62,35 @@ object ParseGoogleUtils {
61
62
*
62
63
* @param activity The activity which passes along the result via [onActivityResult]
63
64
* @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
+ * }
64
71
*/
65
72
@JvmStatic
66
- fun logIn (activity : Activity , callback : LogInCallback ) {
73
+ fun logIn (activity : Activity , launcher : ActivityResultLauncher < Intent >, callback : LogInCallback ) {
67
74
checkInitialization()
68
75
this .currentCallback = callback
69
76
val googleSignInClient = buildGoogleSignInClient(activity)
70
77
this .googleSignInClient = googleSignInClient
71
- activity.startActivityForResult(
72
- googleSignInClient.signInIntent,
73
- REQUEST_CODE_GOOGLE_SIGN_IN
74
- )
78
+ launcher.launch(googleSignInClient.signInIntent)
75
79
}
76
80
77
81
/* *
78
82
* The method that should be called from the Activity's or Fragment's onActivityResult method.
79
83
*
80
- * @param requestCode The request code that's received by the Activity or Fragment.
81
84
* @param resultCode The result code that's received by the Activity or Fragment.
82
85
* @param data The result data that's received by the Activity or Fragment.
83
86
* @return true if the result could be handled.
84
87
*/
85
88
@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 ) {
88
91
return false
89
92
}
90
- if (requestCode == REQUEST_CODE_GOOGLE_SIGN_IN ) {
93
+ if (resultCode == Activity . RESULT_OK ) {
91
94
if (data != null ) {
92
95
handleSignInResult(data)
93
96
} else {
@@ -151,7 +154,7 @@ object ParseGoogleUtils {
151
154
}
152
155
153
156
private fun onSignedIn (account : GoogleSignInAccount ) {
154
- googleSignInClient? .signOut()? .addOnCompleteListener {}
157
+ googleSignInClient.signOut().addOnCompleteListener {}
155
158
val authData: Map <String , String > = getAuthData(account)
156
159
ParseUser .logInWithInBackground(AUTH_TYPE , authData)
157
160
.continueWith { task ->
@@ -224,34 +227,34 @@ object ParseGoogleUtils {
224
227
}
225
228
val tcs: TaskCompletionSource <T > = TaskCompletionSource <T >()
226
229
task.continueWith<Void >(
227
- Continuation { task ->
228
- if (task .isCancelled && ! reportCancellation) {
230
+ Continuation { task2 ->
231
+ if (task2 .isCancelled && ! reportCancellation) {
229
232
tcs.setCancelled()
230
233
return @Continuation null
231
234
}
232
235
Task .UI_THREAD_EXECUTOR .execute {
233
236
try {
234
- var error = task .error
237
+ var error = task2 .error
235
238
if (error != null && error !is ParseException ) {
236
239
error = ParseException (error)
237
240
}
238
241
if (callback is SaveCallback ) {
239
242
callback.done(error as ? ParseException )
240
243
} else if (callback is LogInCallback ) {
241
244
callback.done(
242
- task .result as ? ParseUser , error as ? ParseException
245
+ task2 .result as ? ParseUser , error as ? ParseException
243
246
)
244
247
}
245
248
} finally {
246
249
when {
247
- task .isCancelled -> {
250
+ task2 .isCancelled -> {
248
251
tcs.setCancelled()
249
252
}
250
- task .isFaulted -> {
251
- tcs.setError(task .error)
253
+ task2 .isFaulted -> {
254
+ tcs.setError(task2 .error)
252
255
}
253
256
else -> {
254
- tcs.setResult(task .result)
257
+ tcs.setResult(task2 .result)
255
258
}
256
259
}
257
260
}
0 commit comments