Skip to content

Commit

Permalink
Improve management of player conflicts
Browse files Browse the repository at this point in the history
Closes #36
  • Loading branch information
jonathangarelick committed May 13, 2024
1 parent 762fd90 commit f724781
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
2 changes: 1 addition & 1 deletion SoundSeer/Models/MusicApplication.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ enum MusicApplication {
static let app: SBMusicApplication? = SBApplicationManager.musicApp()

static func getPlayerState() -> PlayerState? {
guard let app = app else { return nil }
guard let app = app, Utils.isAppRunning(bundleID) else { return nil }
return PlayerState(.music, app)
}
}
4 changes: 2 additions & 2 deletions SoundSeer/Models/SpotifyApplication.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
enum SpotifyApplication {
static let bundleID = "com.spotify.client"
static let app: SBSpotifyApplication? = SBApplicationManager.spotifyApp()

static func getPlayerState() -> PlayerState? {
guard let app = app else { return nil }
guard let app = app, Utils.isAppRunning(bundleID) else { return nil }
return PlayerState(.spotify, app)
}
}
20 changes: 13 additions & 7 deletions SoundSeer/SoundSeerModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,7 @@ class SoundSeerModel {
return nil
}

if Utils.isAppRunning(MusicApplication.bundleID) {
playerState = MusicApplication.getPlayerState()
} else if Utils.isAppRunning(SpotifyApplication.bundleID) {
playerState = SpotifyApplication.getPlayerState()
} else {
Logger.model.debug("Neither app is running")
}
playerState = getPlayerState()

DistributedNotificationCenter.default().addObserver(
forName: Notification.Name("com.apple.Music.playerInfo"), object: nil, queue: nil) { [weak self] in
Expand All @@ -33,4 +27,16 @@ class SoundSeerModel {
deinit {
DistributedNotificationCenter.default().removeObserver(self)
}

func getPlayerState() -> PlayerState? {
let musicState = MusicApplication.getPlayerState()
let spotifyState = SpotifyApplication.getPlayerState()

if (spotifyState?.playbackState == .playing && (musicState == nil || musicState?.playbackState != .playing))
|| (spotifyState?.playbackState != .playing && musicState == nil) {
return spotifyState
}

return musicState
}
}

0 comments on commit f724781

Please sign in to comment.