Skip to content

Commit 45c15f5

Browse files
authored
[Capacitor 3] complete migration to Capacitor 3 (#128)
* feature: migrate success/error to resolve/reject * feature: migrate android-plugin * feature: bump deployment target to 12.0 * feature: migrate callback
1 parent f5378b3 commit 45c15f5

File tree

6 files changed

+31
-39
lines changed

6 files changed

+31
-39
lines changed

android/build.gradle

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ buildscript {
44
google()
55
}
66
dependencies {
7-
classpath 'com.android.tools.build:gradle:3.3.2'
7+
classpath 'com.android.tools.build:gradle:4.2.1'
88
}
99
}
1010

1111
apply plugin: 'com.android.library'
1212

1313
android {
14-
compileSdkVersion 28
14+
compileSdkVersion 30
1515
defaultConfig {
1616
minSdkVersion 21
17-
targetSdkVersion 28
17+
targetSdkVersion 30
1818
versionCode 1
1919
versionName "1.0"
2020
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
@@ -40,7 +40,8 @@ repositories {
4040
dependencies {
4141
implementation fileTree(dir: 'libs', include: ['*.jar'])
4242
implementation project(':capacitor-android')
43-
testImplementation 'junit:junit:4.12'
43+
implementation 'androidx.appcompat:appcompat:1.2.0'
44+
testImplementation 'junit:junit:4.13'
4445
androidTestImplementation 'com.android.support.test:runner:1.0.2'
4546
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
4647
implementation 'com.google.android.gms:play-services-auth:18.+'

android/src/main/java/com/codetrixstudio/capacitor/GoogleAuth/GoogleAuth.java

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package com.codetrixstudio.capacitor.GoogleAuth;
22

33
import android.content.Intent;
4+
import androidx.activity.result.ActivityResult;
45

56
import com.codetrixstudio.capacitor.GoogleAuth.capacitorgoogleauth.R;
67
import com.getcapacitor.JSObject;
7-
import com.getcapacitor.NativePlugin;
88
import com.getcapacitor.Plugin;
99
import com.getcapacitor.PluginCall;
1010
import com.getcapacitor.PluginMethod;
11+
import com.getcapacitor.annotation.ActivityCallback;
12+
import com.getcapacitor.annotation.CapacitorPlugin;
1113
import com.google.android.gms.auth.api.signin.GoogleSignIn;
1214
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
1315
import com.google.android.gms.auth.api.signin.GoogleSignInClient;
@@ -19,9 +21,8 @@
1921
import org.json.JSONArray;
2022
import org.json.JSONException;
2123

22-
@NativePlugin(requestCodes = GoogleAuth.RC_SIGN_IN)
24+
@CapacitorPlugin()
2325
public class GoogleAuth extends Plugin {
24-
static final int RC_SIGN_IN = 1337;
2526
private GoogleSignInClient googleSignInClient;
2627

2728
@Override
@@ -62,23 +63,14 @@ public void load() {
6263
public void signIn(PluginCall call) {
6364
saveCall(call);
6465
Intent signInIntent = googleSignInClient.getSignInIntent();
65-
startActivityForResult(call, signInIntent, RC_SIGN_IN);
66+
startActivityForResult(call, signInIntent, "signInResult");
6667
}
6768

68-
@Override
69-
protected void handleOnActivityResult(int requestCode, int resultCode, Intent data) {
70-
super.handleOnActivityResult(requestCode, resultCode, data);
71-
72-
if (requestCode == RC_SIGN_IN) {
73-
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
74-
handleSignInResult(task);
75-
}
76-
}
69+
@ActivityCallback
70+
protected void signInResult(PluginCall call, ActivityResult result) {
71+
if (call == null) return;
7772

78-
private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
79-
PluginCall signInCall = getSavedCall();
80-
81-
if (signInCall == null) return;
73+
Task<GoogleSignInAccount> completedTask = GoogleSignIn.getSignedInAccountFromIntent(result.getData());
8274

8375
try {
8476
GoogleSignInAccount account = completedTask.getResult(ApiException.class);
@@ -90,29 +82,28 @@ private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
9082
user.put("serverAuthCode", account.getServerAuthCode());
9183
user.put("idToken", account.getIdToken());
9284
user.put("authentication", authentication);
93-
85+
9486
user.put("displayName", account.getDisplayName());
9587
user.put("email", account.getEmail());
9688
user.put("familyName", account.getFamilyName());
9789
user.put("givenName", account.getGivenName());
9890
user.put("id", account.getId());
9991
user.put("imageUrl", account.getPhotoUrl());
10092

101-
signInCall.success(user);
102-
93+
call.resolve(user);
10394
} catch (ApiException e) {
104-
signInCall.error("Something went wrong", e);
95+
call.reject("Something went wrong", e);
10596
}
10697
}
10798

10899
@PluginMethod()
109100
public void refresh(final PluginCall call) {
110-
call.error("I don't know how to refresh token on Android");
101+
call.reject("I don't know how to refresh token on Android");
111102
}
112103

113104
@PluginMethod()
114105
public void signOut(final PluginCall call) {
115106
googleSignInClient.signOut();
116-
call.success();
107+
call.resolve();
117108
}
118109
}

ios/Plugin.xcodeproj/project.pbxproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@
422422
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
423423
GCC_WARN_UNUSED_FUNCTION = YES;
424424
GCC_WARN_UNUSED_VARIABLE = YES;
425-
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
425+
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
426426
MTL_ENABLE_DEBUG_INFO = YES;
427427
ONLY_ACTIVE_ARCH = YES;
428428
SDKROOT = iphoneos;
@@ -476,7 +476,7 @@
476476
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
477477
GCC_WARN_UNUSED_FUNCTION = YES;
478478
GCC_WARN_UNUSED_VARIABLE = YES;
479-
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
479+
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
480480
MTL_ENABLE_DEBUG_INFO = NO;
481481
SDKROOT = iphoneos;
482482
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
@@ -499,7 +499,7 @@
499499
DYLIB_INSTALL_NAME_BASE = "@rpath";
500500
INFOPLIST_FILE = Plugin/Info.plist;
501501
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
502-
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
502+
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
503503
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks $(FRAMEWORK_SEARCH_PATHS)\n$(FRAMEWORK_SEARCH_PATHS)\n$(FRAMEWORK_SEARCH_PATHS)";
504504
ONLY_ACTIVE_ARCH = YES;
505505
PRODUCT_BUNDLE_IDENTIFIER = com.getcapacitor.Plugin;
@@ -524,7 +524,7 @@
524524
DYLIB_INSTALL_NAME_BASE = "@rpath";
525525
INFOPLIST_FILE = Plugin/Info.plist;
526526
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
527-
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
527+
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
528528
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks $(FRAMEWORK_SEARCH_PATHS)";
529529
ONLY_ACTIVE_ARCH = NO;
530530
PRODUCT_BUNDLE_IDENTIFIER = com.getcapacitor.Plugin;

ios/Plugin/Plugin.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,20 @@ public class GoogleAuth: CAPPlugin {
5050
func refresh(_ call: CAPPluginCall) {
5151
DispatchQueue.main.async {
5252
if self.googleSignIn.currentUser == nil {
53-
call.error("User not logged in.");
53+
call.reject("User not logged in.");
5454
return
5555
}
5656
self.googleSignIn.currentUser.authentication.getTokensWithHandler { (authentication, error) in
5757
guard let authentication = authentication else {
58-
call.error(error?.localizedDescription ?? "Something went wrong.");
58+
call.reject(error?.localizedDescription ?? "Something went wrong.");
5959
return;
6060
}
6161
let authenticationData: [String: Any] = [
6262
"accessToken": authentication.accessToken,
6363
"idToken": authentication.idToken,
6464
"refreshToken": authentication.refreshToken
6565
]
66-
call.success(authenticationData);
66+
call.resolve(authenticationData);
6767
}
6868
}
6969
}
@@ -73,7 +73,7 @@ public class GoogleAuth: CAPPlugin {
7373
DispatchQueue.main.async {
7474
self.googleSignIn.signOut();
7575
}
76-
call.success();
76+
call.resolve();
7777
}
7878

7979
@objc
@@ -106,14 +106,14 @@ public class GoogleAuth: CAPPlugin {
106106
if let imageUrl = user.profile.imageURL(withDimension: 100)?.absoluteString {
107107
userData["imageUrl"] = imageUrl;
108108
}
109-
signInCall?.success(userData);
109+
signInCall?.resolve(userData);
110110
}
111111
}
112112

113113
extension GoogleAuth: GIDSignInDelegate {
114114
public func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
115115
if let error = error {
116-
signInCall?.error(error.localizedDescription);
116+
signInCall?.reject(error.localizedDescription);
117117
return;
118118
}
119119
processCallback(user: user);

ios/Podfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
platform :ios, '11.0'
1+
platform :ios, '12.0'
22

33
target 'Plugin' do
44
# Comment the next line if you're not using Swift and don't want to use dynamic frameworks

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"experimentalDecorators": true,
66
"lib": [
77
"dom",
8-
"es2015"
8+
"es2017"
99
],
1010
"module": "ESNext",
1111
"moduleResolution": "node",

0 commit comments

Comments
 (0)