mirror of
https://github.com/FBukovina/Atoll.git
synced 2026-10-27 15:14:33 +00:00
Merge pull request #325 from AmriteshKhare/feature/auto-detect-pear-desktop
feat(media): auto-detect Pear Desktop for seamless YouTube Music sync
This commit is contained in:
commit
9ff3ba9748
1 changed files with 54 additions and 2 deletions
|
|
@ -71,6 +71,10 @@ class MusicManager: ObservableObject {
|
||||||
// Active controller
|
// Active controller
|
||||||
private var activeController: (any MediaControllerProtocol)?
|
private var activeController: (any MediaControllerProtocol)?
|
||||||
|
|
||||||
|
// Pear Desktop auto-detection
|
||||||
|
private static let pearDesktopBundleID = YouTubeMusicConfiguration.default.bundleIdentifier
|
||||||
|
private var isPearDesktopAutoSwitched: Bool = false
|
||||||
|
|
||||||
// Published properties for UI
|
// Published properties for UI
|
||||||
@Published var songTitle: String = "I'm Handsome"
|
@Published var songTitle: String = "I'm Handsome"
|
||||||
@Published var artistName: String = "Me"
|
@Published var artistName: String = "Me"
|
||||||
|
|
@ -150,10 +154,14 @@ class MusicManager: ObservableObject {
|
||||||
// Listen for changes to the default controller preference
|
// Listen for changes to the default controller preference
|
||||||
NotificationCenter.default.publisher(for: Notification.Name.mediaControllerChanged)
|
NotificationCenter.default.publisher(for: Notification.Name.mediaControllerChanged)
|
||||||
.sink { [weak self] _ in
|
.sink { [weak self] _ in
|
||||||
|
self?.isPearDesktopAutoSwitched = false
|
||||||
self?.setActiveControllerBasedOnPreference()
|
self?.setActiveControllerBasedOnPreference()
|
||||||
}
|
}
|
||||||
.store(in: &cancellables)
|
.store(in: &cancellables)
|
||||||
|
|
||||||
|
// Observe Pear Desktop launch/terminate for auto-detection
|
||||||
|
setupPearDesktopAutoDetection()
|
||||||
|
|
||||||
// Initialize deprecation check asynchronously
|
// Initialize deprecation check asynchronously
|
||||||
Task { @MainActor in
|
Task { @MainActor in
|
||||||
do {
|
do {
|
||||||
|
|
@ -164,11 +172,55 @@ class MusicManager: ObservableObject {
|
||||||
self.isNowPlayingDeprecated = false
|
self.isNowPlayingDeprecated = false
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize the active controller after deprecation check
|
// Check if Pear Desktop is already running at startup
|
||||||
self.setActiveControllerBasedOnPreference()
|
let pearDesktopRunning = NSWorkspace.shared.runningApplications.contains {
|
||||||
|
$0.bundleIdentifier == Self.pearDesktopBundleID
|
||||||
|
}
|
||||||
|
|
||||||
|
if pearDesktopRunning {
|
||||||
|
print("[MusicManager] Pear Desktop detected at startup, auto-switching to YouTubeMusicController")
|
||||||
|
self.isPearDesktopAutoSwitched = true
|
||||||
|
if let controller = self.createController(for: .youtubeMusic) {
|
||||||
|
self.setActiveController(controller)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Initialize the active controller after deprecation check
|
||||||
|
self.setActiveControllerBasedOnPreference()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MARK: - Pear Desktop Auto-Detection
|
||||||
|
private func setupPearDesktopAutoDetection() {
|
||||||
|
NSWorkspace.shared.notificationCenter.publisher(for: NSWorkspace.didLaunchApplicationNotification)
|
||||||
|
.sink { [weak self] notification in
|
||||||
|
guard let self = self,
|
||||||
|
let app = notification.userInfo?[NSWorkspace.applicationUserInfoKey] as? NSRunningApplication,
|
||||||
|
app.bundleIdentifier == Self.pearDesktopBundleID else { return }
|
||||||
|
|
||||||
|
print("[MusicManager] Pear Desktop launched, auto-switching to YouTubeMusicController")
|
||||||
|
self.isPearDesktopAutoSwitched = true
|
||||||
|
if let controller = self.createController(for: .youtubeMusic) {
|
||||||
|
self.setActiveController(controller)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.store(in: &cancellables)
|
||||||
|
|
||||||
|
NSWorkspace.shared.notificationCenter.publisher(for: NSWorkspace.didTerminateApplicationNotification)
|
||||||
|
.sink { [weak self] notification in
|
||||||
|
guard let self = self,
|
||||||
|
let app = notification.userInfo?[NSWorkspace.applicationUserInfoKey] as? NSRunningApplication,
|
||||||
|
app.bundleIdentifier == Self.pearDesktopBundleID else { return }
|
||||||
|
|
||||||
|
print("[MusicManager] Pear Desktop terminated, reverting to preferred controller")
|
||||||
|
if self.isPearDesktopAutoSwitched {
|
||||||
|
self.isPearDesktopAutoSwitched = false
|
||||||
|
self.setActiveControllerBasedOnPreference()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.store(in: &cancellables)
|
||||||
|
}
|
||||||
|
|
||||||
deinit {
|
deinit {
|
||||||
destroy()
|
destroy()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue