Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ final class YouTubeMusicController: MediaControllerProtocol {

nonisolated func isActive() -> Bool {
NSWorkspace.shared.runningApplications.contains {
$0.bundleIdentifier == configuration.bundleIdentifier
configuration.supports(bundleIdentifier: $0.bundleIdentifier)
}
}

Expand Down Expand Up @@ -179,7 +179,7 @@ final class YouTubeMusicController: MediaControllerProtocol {

private func handleAppLaunched(_ notification: Notification) async {
guard let app = notification.userInfo?[NSWorkspace.applicationUserInfoKey] as? NSRunningApplication,
app.bundleIdentifier == configuration.bundleIdentifier else {
configuration.supports(bundleIdentifier: app.bundleIdentifier) else {
return
}

Expand All @@ -189,7 +189,7 @@ final class YouTubeMusicController: MediaControllerProtocol {

private func handleAppTerminated(_ notification: Notification) async {
guard let app = notification.userInfo?[NSWorkspace.applicationUserInfoKey] as? NSRunningApplication,
app.bundleIdentifier == configuration.bundleIdentifier else {
configuration.supports(bundleIdentifier: app.bundleIdentifier) else {
return
}

Expand Down Expand Up @@ -452,6 +452,7 @@ final class YouTubeMusicController: MediaControllerProtocol {

private func updatePlaybackState(with response: PlaybackResponse) async {
var newState = playbackState
newState.bundleIdentifier = activeBundleIdentifier() ?? configuration.bundleIdentifier

newState.isPlaying = !response.isPaused

Expand Down Expand Up @@ -517,16 +518,24 @@ final class YouTubeMusicController: MediaControllerProtocol {

private func resetPlaybackState() {
playbackState = PlaybackState(
bundleIdentifier: configuration.bundleIdentifier,
bundleIdentifier: activeBundleIdentifier() ?? configuration.bundleIdentifier,
isPlaying: false
)
}

private func launchApp() {
guard let url = NSWorkspace.shared.urlForApplication(withBundleIdentifier: configuration.bundleIdentifier) else {
return
for bundleIdentifier in configuration.bundleIdentifiers {
if let url = NSWorkspace.shared.urlForApplication(withBundleIdentifier: bundleIdentifier) {
NSWorkspace.shared.open(url)
return
}
}
NSWorkspace.shared.open(url)
}

private func activeBundleIdentifier() -> String? {
NSWorkspace.shared.runningApplications.first {
configuration.supports(bundleIdentifier: $0.bundleIdentifier)
}?.bundleIdentifier
}

private func updateRepeatMode(_ mode: String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,25 @@ import Foundation
// MARK: - Configuration
struct YouTubeMusicConfiguration: Sendable {
let baseURL: String
let bundleIdentifier: String
let bundleIdentifiers: [String]
let reconnectDelay: ClosedRange<TimeInterval>
let updateInterval: TimeInterval

var bundleIdentifier: String {
bundleIdentifiers.first ?? "com.github.th-ch.youtube-music"
}

func supports(bundleIdentifier: String?) -> Bool {
guard let bundleIdentifier else { return false }
return bundleIdentifiers.contains(bundleIdentifier)
}

static let `default` = YouTubeMusicConfiguration(
baseURL: "http://localhost:26538",
bundleIdentifier: "com.github.th-ch.youtube-music",
bundleIdentifiers: [
"com.github.th-ch.youtube-music",
"com.sertacozercan.Kaset"
],
reconnectDelay: 1...60,
updateInterval: 2.0
)
Expand Down