From b62c53b3a09018bca5de1c1a7a8a771c6fd7a7e6 Mon Sep 17 00:00:00 2001 From: Foti Dim Date: Fri, 4 Oct 2024 15:28:22 +0200 Subject: [PATCH] Add back methodCheckIfSpotifyAppIsActive with a fallback --- README.md | 2 +- ios/Classes/SpotifySdkConstants.swift | 1 + ios/Classes/SwiftSpotifySdkPlugin.swift | 38 ++++++++++++++++++------- 3 files changed, 30 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index d87e20d9..771b5609 100644 --- a/README.md +++ b/README.md @@ -166,7 +166,7 @@ Token Swap is for now "web only". While the iOS SDK also supports the "token swa | connectToSpotifyRemote | Connects the App to Spotify | ✔ | ✔ | ✔ | | getAccessToken | Gets the Access Token that you can use to work with the [Web Api](https://developer.spotify.com/documentation/web-api/) | ✔ | ✔ | ✔ | | disconnect | Disconnects the app connection | ✔ | ✔ | ✔ | -| isSpotifyAppActive | Checks if the Spotify app is active. The Spotify app will be considered active if music is playing. | ❌ | ✔ | 🚧 | +| isSpotifyAppActive | Checks if the Spotify app is active. The Spotify app will be considered active if music is playing. | ✔ | ✔ | 🚧 | | subscribeConnectionStatus | Subscribes to the current player state. | ✔ | ✔ | 🚧 | #### Player Api diff --git a/ios/Classes/SpotifySdkConstants.swift b/ios/Classes/SpotifySdkConstants.swift index ec5550cd..1b524f38 100644 --- a/ios/Classes/SpotifySdkConstants.swift +++ b/ios/Classes/SpotifySdkConstants.swift @@ -6,6 +6,7 @@ public class SpotifySdkConstants public static let methodConnectToSpotify = "connectToSpotify" public static let methodGetAccessToken = "getAccessToken" public static let methodDisconnectFromSpotify = "disconnectFromSpotify" + public static let methodCheckIfSpotifyAppIsActive = "isSpotifyAppActive" //player api public static let methodQueueTrack = "queueTrack" diff --git a/ios/Classes/SwiftSpotifySdkPlugin.swift b/ios/Classes/SwiftSpotifySdkPlugin.swift index 7580f0be..7d652169 100644 --- a/ios/Classes/SwiftSpotifySdkPlugin.swift +++ b/ios/Classes/SwiftSpotifySdkPlugin.swift @@ -125,17 +125,10 @@ public class SwiftSpotifySdkPlugin: NSObject, FlutterPlugin { return } - appRemote.playerAPI?.getPlayerState({ (playerState, error) in - guard error == nil else { - result(FlutterError(code: "PlayerAPI Error", message: error?.localizedDescription, details: nil)) - return - } - guard let playerState = playerState as? SPTAppRemotePlayerState else { - result(FlutterError(code: "PlayerAPI Error", message: "PlayerState is empty", details: nil)) - return - } + getPlayerState(appRemote: appRemote, result: result) { playerState, _ in + let isActive = !playerState.isPaused && playerState.playbackSpeed > 0 result(State.playerStateDictionary(playerState).json) - }) + } case SpotifySdkConstants.methodDisconnectFromSpotify: appRemote?.disconnect() // appRemote?.connectionParameters.accessToken = nil @@ -306,6 +299,16 @@ public class SwiftSpotifySdkPlugin: NSObject, FlutterPlugin { return } appRemote.playerAPI?.setRepeatMode(repeatMode, callback: defaultPlayAPICallback) + case SpotifySdkConstants.methodCheckIfSpotifyAppIsActive: + guard let appRemote = appRemote else { + result(FlutterError(code: "Connection Error", message: "AppRemote is null", details: nil)) + return + } + + getPlayerState(appRemote: appRemote, result: result) { playerState, _ in + let isActive = !playerState.isPaused && playerState.playbackSpeed > 0 + result(isActive) + } case SpotifySdkConstants.getLibraryState: guard let appRemote = appRemote else { result(FlutterError(code: "Connection Error", message: "AppRemote is null", details: nil)) @@ -370,6 +373,21 @@ public class SwiftSpotifySdkPlugin: NSObject, FlutterPlugin { } } } + + private func getPlayerState(appRemote: SPTAppRemote, result: @escaping FlutterResult, completion: @escaping (SPTAppRemotePlayerState, Error?) -> Void) { + appRemote.playerAPI?.getPlayerState({ (playerState, error) in + guard error == nil else { + result(FlutterError(code: "PlayerAPI Error", message: error?.localizedDescription, details: nil)) + return + } + guard let playerState = playerState as? SPTAppRemotePlayerState else { + result(FlutterError(code: "PlayerAPI Error", message: "PlayerState is empty", details: nil)) + return + } + + completion(playerState, nil) + }) + } } extension SwiftSpotifySdkPlugin {