Skip to content

Commit

Permalink
Add back methodCheckIfSpotifyAppIsActive with a fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
fotiDim committed Oct 4, 2024
1 parent 74d013d commit b62c53b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions ios/Classes/SpotifySdkConstants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
38 changes: 28 additions & 10 deletions ios/Classes/SwiftSpotifySdkPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit b62c53b

Please sign in to comment.