From 3f434330da4ba67133c9f9e90b51b04421ffa441 Mon Sep 17 00:00:00 2001 From: Paulina Gallo Date: Tue, 26 Sep 2023 11:24:12 -0400 Subject: [PATCH 1/3] feature(ios): move functionality to return information with signIn function --- ios/Plugin/CapacitorGameConnect.swift | 19 ------------------- ios/Plugin/CapacitorGameConnectPlugin.swift | 20 +++++++++++++++++--- 2 files changed, 17 insertions(+), 22 deletions(-) diff --git a/ios/Plugin/CapacitorGameConnect.swift b/ios/Plugin/CapacitorGameConnect.swift index 3444648d..28b90b88 100644 --- a/ios/Plugin/CapacitorGameConnect.swift +++ b/ios/Plugin/CapacitorGameConnect.swift @@ -7,25 +7,6 @@ import Capacitor gameCenterViewController.dismiss(animated: true); } - @objc func signIn(_ call: CAPPluginCall, _ viewController: UIViewController) { - let localPlayer = GKLocalPlayer.local - - localPlayer.authenticateHandler = { gcAuthVC, error in - if localPlayer.isAuthenticated { - print("User is authenticated to Game Center!") - let result = [ - "player_name": localPlayer.displayName, - "player_id": localPlayer.gamePlayerID - ] - call.resolve(result) - } else if gcAuthVC != nil { - viewController.present(gcAuthVC!, animated: true) - } else { - call.reject("[GameServices] local player is not authenticated") - } - } - } - @objc func showLeaderboard(_ call: CAPPluginCall, _ viewController: UIViewController) { let leaderboardID = String(call.getString("leaderboardID") ?? "") // Property to get the leaderboard ID DispatchQueue.main.async { diff --git a/ios/Plugin/CapacitorGameConnectPlugin.swift b/ios/Plugin/CapacitorGameConnectPlugin.swift index f39536d4..534ac265 100644 --- a/ios/Plugin/CapacitorGameConnectPlugin.swift +++ b/ios/Plugin/CapacitorGameConnectPlugin.swift @@ -1,17 +1,31 @@ import Foundation import Capacitor +import GameKit /** * Please read the Capacitor iOS Plugin Development Guide * here: https://capacitorjs.com/docs/plugins/ios */ + @objc(CapacitorGameConnectPlugin) public class CapacitorGameConnectPlugin: CAPPlugin { private let implementation = CapacitorGameConnect() - @objc func signIn(_ call: CAPPluginCall) { - implementation.signIn(call, (self.bridge?.viewController)!) - call.resolve() + @objc func signIn(_ call: CAPPluginCall, _ viewController: UIViewController) { + let localPlayer = GKLocalPlayer.local + + localPlayer.authenticateHandler = { gcAuthVC, error in + if localPlayer.isAuthenticated { + call.resolve([ + "player_name": localPlayer.displayName, + "player_id": localPlayer.gamePlayerID, + ]) + } else if gcAuthVC != nil { + viewController.present(gcAuthVC!, animated: true) + } else { + call.reject("[GameServices] local player is not authenticated") + } + } } @objc func showLeaderboard(_ call: CAPPluginCall) { From 341d24743127c25f88b54540172a66d3618b79e8 Mon Sep 17 00:00:00 2001 From: Paulina Gallo Date: Thu, 28 Sep 2023 17:57:28 -0400 Subject: [PATCH 2/3] feat(android): enhance sign-in function with callbacks and fetch player --- .../CapacitorGameConnect.java | 93 +++++++++++-------- .../CapacitorGameConnectPlugin.java | 33 ++++++- .../PlayerResultCallback.java | 8 ++ .../capacitorgameconnect/SignInCallback.java | 6 ++ 4 files changed, 98 insertions(+), 42 deletions(-) create mode 100644 android/src/main/java/com/openforge/capacitorgameconnect/PlayerResultCallback.java create mode 100644 android/src/main/java/com/openforge/capacitorgameconnect/SignInCallback.java diff --git a/android/src/main/java/com/openforge/capacitorgameconnect/CapacitorGameConnect.java b/android/src/main/java/com/openforge/capacitorgameconnect/CapacitorGameConnect.java index 0b6052e0..9eb71c2e 100644 --- a/android/src/main/java/com/openforge/capacitorgameconnect/CapacitorGameConnect.java +++ b/android/src/main/java/com/openforge/capacitorgameconnect/CapacitorGameConnect.java @@ -22,31 +22,44 @@ public CapacitorGameConnect(AppCompatActivity activity) { * * Method to sign-in a user to Google Play Services * * @param call as PluginCall + * @param resultCallback as SignInCallback */ - public void signIn(PluginCall call) { + public void signIn(PluginCall call, final SignInCallback resultCallback) { Log.i(TAG, "SignIn method called"); GamesSignInClient gamesSignInClient = PlayGames.getGamesSignInClient(this.activity); gamesSignInClient - .isAuthenticated() - .addOnCompleteListener( - isAuthenticatedTask -> { - boolean isAuthenticated = (isAuthenticatedTask.isSuccessful() && isAuthenticatedTask.getResult().isAuthenticated()); + .isAuthenticated() + .addOnCompleteListener( + isAuthenticatedTask -> { + boolean isAuthenticated = (isAuthenticatedTask.isSuccessful() && isAuthenticatedTask.getResult().isAuthenticated()); - if (isAuthenticated) { - Log.i(TAG, "User is already authenticated"); - call.resolve(); - } else { - gamesSignInClient - .signIn() - .addOnCompleteListener( - data -> { - Log.i(TAG, "Sign-in completed successful"); - } - ); - } - } - ); + if (isAuthenticated) { + Log.i(TAG, "User is already authenticated"); + resultCallback.success(); + } else { + gamesSignInClient + .signIn() + .addOnCompleteListener( + data -> { + Log.i(TAG, "Sign-in completed successful"); + resultCallback.success(); + } + ).addOnFailureListener(e -> resultCallback.error(e.getMessage())); + } + } + ).addOnFailureListener(e -> resultCallback.error(e.getMessage())); + } + + /** + * * Method to fetch the logged in Player + * + * @param resultCallback as PlayerResultCallback + */ + public void fetchUserInformation(final PlayerResultCallback resultCallback) { + PlayGames.getPlayersClient(this.activity).getCurrentPlayer().addOnSuccessListener(player -> { + resultCallback.success(player); + }).addOnFailureListener(e -> resultCallback.error(e.getMessage())); } /** @@ -59,16 +72,16 @@ public void showLeaderboard(PluginCall call, ActivityResultLauncher star Log.i(TAG, "showLeaderboard has been called"); var leaderboardID = call.getString("leaderboardID"); PlayGames - .getLeaderboardsClient(this.activity) - .getLeaderboardIntent(leaderboardID) - .addOnSuccessListener( - new OnSuccessListener() { - @Override - public void onSuccess(Intent intent) { - startActivityIntent.launch(intent); - } - } - ); + .getLeaderboardsClient(this.activity) + .getLeaderboardIntent(leaderboardID) + .addOnSuccessListener( + new OnSuccessListener() { + @Override + public void onSuccess(Intent intent) { + startActivityIntent.launch(intent); + } + } + ); } /** @@ -91,16 +104,16 @@ public void submitScore(PluginCall call) { public void showAchievements(ActivityResultLauncher startActivityIntent) { Log.i(TAG, "showAchievements has been called"); PlayGames - .getAchievementsClient(this.activity) - .getAchievementsIntent() - .addOnSuccessListener( - new OnSuccessListener() { - @Override - public void onSuccess(Intent intent) { - startActivityIntent.launch(intent); - } - } - ); + .getAchievementsClient(this.activity) + .getAchievementsIntent() + .addOnSuccessListener( + new OnSuccessListener() { + @Override + public void onSuccess(Intent intent) { + startActivityIntent.launch(intent); + } + } + ); } /** @@ -123,4 +136,4 @@ public void incrementAchievementProgress(PluginCall call) { var pointsToIncrement = call.getInt("pointsToIncrement"); PlayGames.getAchievementsClient(this.activity).increment(achievementID, pointsToIncrement); } -} \ No newline at end of file +} diff --git a/android/src/main/java/com/openforge/capacitorgameconnect/CapacitorGameConnectPlugin.java b/android/src/main/java/com/openforge/capacitorgameconnect/CapacitorGameConnectPlugin.java index adbb1a7e..7edfb56a 100644 --- a/android/src/main/java/com/openforge/capacitorgameconnect/CapacitorGameConnectPlugin.java +++ b/android/src/main/java/com/openforge/capacitorgameconnect/CapacitorGameConnectPlugin.java @@ -1,15 +1,19 @@ package com.openforge.capacitorgameconnect; import android.content.Intent; + import androidx.activity.result.ActivityResult; import androidx.activity.result.ActivityResultCallback; import androidx.activity.result.ActivityResultLauncher; import androidx.activity.result.contract.ActivityResultContracts; + +import com.getcapacitor.JSObject; import com.getcapacitor.Plugin; import com.getcapacitor.PluginCall; import com.getcapacitor.PluginMethod; import com.getcapacitor.annotation.CapacitorPlugin; import com.google.android.gms.games.PlayGamesSdk; +import com.google.android.gms.games.Player; @CapacitorPlugin(name = "CapacitorGameConnect") public class CapacitorGameConnectPlugin extends Plugin { @@ -36,8 +40,33 @@ public void onActivityResult(ActivityResult result) { @PluginMethod public void signIn(PluginCall call) { - implementation.signIn(call); - call.resolve(); + implementation.signIn(call, new SignInCallback() { + @Override + public void success() { + implementation.fetchUserInformation(new PlayerResultCallback() { + @Override + public void success(Player player) { + String playerId = player.getPlayerId(); + String playerName = player.getDisplayName(); + + JSObject ret = new JSObject(); + ret.put("player_id", playerId); + ret.put("player_name", playerName); + call.resolve(ret); + } + + @Override + public void error(String message) { + call.reject(message); + } + }); + } + + @Override + public void error(String message) { + call.reject(message); + } + }); } @PluginMethod diff --git a/android/src/main/java/com/openforge/capacitorgameconnect/PlayerResultCallback.java b/android/src/main/java/com/openforge/capacitorgameconnect/PlayerResultCallback.java new file mode 100644 index 00000000..a48488ba --- /dev/null +++ b/android/src/main/java/com/openforge/capacitorgameconnect/PlayerResultCallback.java @@ -0,0 +1,8 @@ +package com.openforge.capacitorgameconnect; + +import com.google.android.gms.games.Player; + +public interface PlayerResultCallback { + void success(Player player); + void error(String message); +} diff --git a/android/src/main/java/com/openforge/capacitorgameconnect/SignInCallback.java b/android/src/main/java/com/openforge/capacitorgameconnect/SignInCallback.java new file mode 100644 index 00000000..8495bb7e --- /dev/null +++ b/android/src/main/java/com/openforge/capacitorgameconnect/SignInCallback.java @@ -0,0 +1,6 @@ +package com.openforge.capacitorgameconnect; + +public interface SignInCallback { + void success(); + void error(String message); +} From cf439064ffb313ea5268d95123154996854caab8 Mon Sep 17 00:00:00 2001 From: Paulina Gallo Date: Fri, 29 Sep 2023 18:14:59 -0400 Subject: [PATCH 3/3] fix(ios): revert and remove previous blank resolve --- ios/Plugin/CapacitorGameConnect.swift | 18 ++++++++++++++++++ ios/Plugin/CapacitorGameConnectPlugin.swift | 17 ++--------------- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/ios/Plugin/CapacitorGameConnect.swift b/ios/Plugin/CapacitorGameConnect.swift index 28b90b88..c1f9f3be 100644 --- a/ios/Plugin/CapacitorGameConnect.swift +++ b/ios/Plugin/CapacitorGameConnect.swift @@ -6,6 +6,24 @@ import Capacitor public func gameCenterViewControllerDidFinish(_ gameCenterViewController: GKGameCenterViewController) { gameCenterViewController.dismiss(animated: true); } + + @objc func signIn(_ call: CAPPluginCall, _ viewController: UIViewController) { + let localPlayer = GKLocalPlayer.local + + localPlayer.authenticateHandler = { gcAuthVC, error in + if localPlayer.isAuthenticated { + let result = [ + "player_name": localPlayer.displayName, + "player_id": localPlayer.gamePlayerID + ] + call.resolve(result) + } else if gcAuthVC != nil { + viewController.present(gcAuthVC!, animated: true) + } else { + call.reject("[GameServices] local player is not authenticated") + } + } + } @objc func showLeaderboard(_ call: CAPPluginCall, _ viewController: UIViewController) { let leaderboardID = String(call.getString("leaderboardID") ?? "") // Property to get the leaderboard ID diff --git a/ios/Plugin/CapacitorGameConnectPlugin.swift b/ios/Plugin/CapacitorGameConnectPlugin.swift index 534ac265..97ee49a9 100644 --- a/ios/Plugin/CapacitorGameConnectPlugin.swift +++ b/ios/Plugin/CapacitorGameConnectPlugin.swift @@ -11,21 +11,8 @@ import GameKit public class CapacitorGameConnectPlugin: CAPPlugin { private let implementation = CapacitorGameConnect() - @objc func signIn(_ call: CAPPluginCall, _ viewController: UIViewController) { - let localPlayer = GKLocalPlayer.local - - localPlayer.authenticateHandler = { gcAuthVC, error in - if localPlayer.isAuthenticated { - call.resolve([ - "player_name": localPlayer.displayName, - "player_id": localPlayer.gamePlayerID, - ]) - } else if gcAuthVC != nil { - viewController.present(gcAuthVC!, animated: true) - } else { - call.reject("[GameServices] local player is not authenticated") - } - } + @objc func signIn(_ call: CAPPluginCall) { + implementation.signIn(call, (self.bridge?.viewController)!) } @objc func showLeaderboard(_ call: CAPPluginCall) {