Skip to content

Commit

Permalink
manage media players better
Browse files Browse the repository at this point in the history
  • Loading branch information
pranavkarthik10 committed Nov 13, 2024
1 parent e020802 commit dbd5609
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 14 deletions.
4 changes: 2 additions & 2 deletions PodPals.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@
CODE_SIGN_ENTITLEMENTS = PodPals/PodPals.entitlements;
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 1;
CURRENT_PROJECT_VERSION = 2;
DEVELOPMENT_ASSET_PATHS = "\"PodPals/Preview Content\"";
DEVELOPMENT_TEAM = ZC5P42MMZ2;
ENABLE_HARDENED_RUNTIME = YES;
Expand Down Expand Up @@ -365,7 +365,7 @@
CODE_SIGN_ENTITLEMENTS = PodPals/PodPals.entitlements;
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 1;
CURRENT_PROJECT_VERSION = 2;
DEVELOPMENT_ASSET_PATHS = "\"PodPals/Preview Content\"";
DEVELOPMENT_TEAM = ZC5P42MMZ2;
ENABLE_HARDENED_RUNTIME = YES;
Expand Down
47 changes: 36 additions & 11 deletions PodPals/ConnectionCalibrationView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,51 @@ enum GestureAction: String, CaseIterable {
case volumeDown = "Volume Down"

func execute() {
let availability = MediaPlayerChecker.checkAvailability()

switch self {
case .playPause:
SpotifyController.shared.playPause()
MusicController.shared.playPause()
if availability == .spotifyOnly || availability == .both {
SpotifyController.shared.playPause()
}
if availability == .appleMusicOnly || availability == .both {
MusicController.shared.playPause()
}

case .nextTrack:
SpotifyController.shared.nextTrack()
MusicController.shared.nextTrack()
if availability == .spotifyOnly || availability == .both {
SpotifyController.shared.nextTrack()
}
if availability == .appleMusicOnly || availability == .both {
MusicController.shared.nextTrack()
}

case .previousTrack:
SpotifyController.shared.previousTrack()
MusicController.shared.previousTrack()
if availability == .spotifyOnly || availability == .both {
SpotifyController.shared.previousTrack()
}
if availability == .appleMusicOnly || availability == .both {
MusicController.shared.previousTrack()
}

case .volumeUp:
SpotifyController.shared.adjustVolume(up: true)
MusicController.shared.adjustVolume(up: true)
if availability == .spotifyOnly || availability == .both {
SpotifyController.shared.adjustVolume(up: true)
}
if availability == .appleMusicOnly || availability == .both {
MusicController.shared.adjustVolume(up: true)
}

case .volumeDown:
SpotifyController.shared.adjustVolume(up: false)
MusicController.shared.adjustVolume(up: false)
if availability == .spotifyOnly || availability == .both {
SpotifyController.shared.adjustVolume(up: false)
}
if availability == .appleMusicOnly || availability == .both {
MusicController.shared.adjustVolume(up: false)
}
}
}
}

class SpotifyController {
static let shared = SpotifyController()

Expand Down
79 changes: 78 additions & 1 deletion PodPals/PodPalsApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@ struct PodPalsApp: App {

init () {
UserDefaults.registerDefaults()

let availability = MediaPlayerChecker.checkAvailability()
if availability == .neither {
DispatchQueue.main.async {
let alert = NSAlert()
alert.messageText = "No Media Player Available"
alert.informativeText = "PodPals requires either Spotify or Apple Music to be installed and running. Please launch one of these applications to use PodPals."
alert.alertStyle = .critical
alert.addButton(withTitle: "Quit")

alert.runModal()
NSApplication.shared.terminate(nil)
}
}
}

var body: some Scene {
Expand Down Expand Up @@ -68,6 +82,13 @@ class AppState: ObservableObject {
}
}

@Published var mediaPlayerAvailability: MediaPlayerAvailability {
willSet {
UserDefaults.standard.set(newValue == .both || newValue == .spotifyOnly, forKey: "spotifyAvailable")
UserDefaults.standard.set(newValue == .both || newValue == .appleMusicOnly, forKey: "appleMusicAvailable")
}
}

@AppStorage("AppState.calibration") private var calibration: Data = .init()

private var accessCheckTimer = Timer()
Expand All @@ -82,7 +103,7 @@ class AppState: ObservableObject {
self.rightFlick = UserDefaults.standard.string(forKey: "rightFlick") ?? "Next Track"
self.nod = UserDefaults.standard.string(forKey: "nod") ?? "Play/Pause"
self.trackingEnabled = UserDefaults.standard.bool(forKey: "trackingEnabled")

self.mediaPlayerAvailability = MediaPlayerChecker.checkAvailability()
// Set up motion detector
headphoneMotionDetector.onUpdate = { [self] in
quaternion = self.headphoneMotionDetector.correctedQuaternion
Expand All @@ -102,6 +123,7 @@ class AppState: ObservableObject {
}
}
}

}

extension UserDefaults {
Expand Down Expand Up @@ -138,3 +160,58 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}

}

enum MediaPlayerAvailability {
case spotifyOnly
case appleMusicOnly
case both
case neither
}

class MediaPlayerChecker {
static func checkAvailability() -> MediaPlayerAvailability {
let spotifyAvailable = checkSpotify()
let appleMusicAvailable = checkAppleMusic()

switch (spotifyAvailable, appleMusicAvailable) {
case (true, true):
return .both
case (true, false):
return .spotifyOnly
case (false, true):
return .appleMusicOnly
case (false, false):
return .neither
}
}

private static func checkSpotify() -> Bool {
let script = """
tell application "System Events"
return exists application process "Spotify"
end tell
"""

if let scriptObject = NSAppleScript(source: script) {
var error: NSDictionary?
let result = scriptObject.executeAndReturnError(&error)
return result.booleanValue
}
return false
}

private static func checkAppleMusic() -> Bool {
let script = """
tell application "System Events"
return exists application process "Music"
end tell
"""

if let scriptObject = NSAppleScript(source: script) {
var error: NSDictionary?
let result = scriptObject.executeAndReturnError(&error)
return result.booleanValue
}
return false
}
}

0 comments on commit dbd5609

Please sign in to comment.