Fix: Migrations

This commit is contained in:
Hariharan Mudaliar 2025-08-03 21:26:50 +05:30
commit be152c1531
41 changed files with 2373 additions and 1321 deletions

View file

@ -2,7 +2,7 @@
// SpotifyController.swift
// DynamicIsland
//
// Created by Alexander Greco on 2025-03-29.
// Created by Alexander on 2025-03-29.
//
import Foundation
@ -15,59 +15,81 @@ class SpotifyController: MediaControllerProtocol {
bundleIdentifier: "com.spotify.client"
)
var playbackStatePublisher: Published<PlaybackState>.Publisher { $playbackState }
var playbackStatePublisher: AnyPublisher<PlaybackState, Never> {
$playbackState.eraseToAnyPublisher()
}
var isWorking: Bool {
return true // Spotify controller always works
}
private var notificationTask: Task<Void, Never>?
//Constant for time between command and update
let commandUpdateDelay: Duration = .milliseconds(25)
init() {
setupPlaybackStateChangeObserver()
DispatchQueue.main.async { [weak self] in
if self?.isActive() == true {
self?.updatePlaybackInfo()
Task {
if isActive() {
await updatePlaybackInfo()
}
}
}
private func setupPlaybackStateChangeObserver() {
DistributedNotificationCenter.default().addObserver(
self,
selector: #selector(updatePlaybackInfo),
name: NSNotification.Name("com.spotify.client.PlaybackStateChanged"),
object: nil
)
notificationTask = Task { @Sendable [weak self] in
let notifications = DistributedNotificationCenter.default().notifications(
named: NSNotification.Name("com.spotify.client.PlaybackStateChanged")
)
for await _ in notifications {
await self?.updatePlaybackInfo()
}
}
}
deinit {
// Remove notification observer when controller is deallocated
DistributedNotificationCenter.default().removeObserver(
self,
name: NSNotification.Name("com.spotify.client.PlaybackStateChanged"),
object: nil
)
notificationTask?.cancel()
}
// MARK: - Protocol Implementation
func play() {
executeCommand("play")
func play() async {
await executeCommand("play")
}
func pause() {
executeCommand("pause")
func pause() async {
await executeCommand("pause")
}
func togglePlay() {
executeCommand("playpause")
func togglePlay() async {
await executeCommand("playpause")
}
func nextTrack() {
executeCommand("next track")
func nextTrack() async {
await executeCommand("next track")
}
func previousTrack() {
executeCommand("previous track")
func previousTrack() async {
await executeCommand("previous track")
}
func seek(to time: Double) {
executeCommand("set player position to \(time)")
updatePlaybackInfo()
func seek(to time: Double) async {
await executeCommand("set player position to \(time)")
try? await Task.sleep(for: commandUpdateDelay)
await updatePlaybackInfo()
}
func toggleShuffle() async {
await executeCommand("set shuffling to not shuffling")
try? await Task.sleep(for: commandUpdateDelay)
await updatePlaybackInfo()
}
func toggleRepeat() async {
await executeCommand("set repeating to not repeating")
try? await Task.sleep(for: commandUpdateDelay)
await updatePlaybackInfo()
}
func isActive() -> Bool {
@ -75,9 +97,8 @@ class SpotifyController: MediaControllerProtocol {
return runningApps.contains { $0.bundleIdentifier == playbackState.bundleIdentifier }
}
// MARK: - Private Methods
@objc func updatePlaybackInfo() {
guard let descriptor = fetchPlaybackInfo() else { return }
func updatePlaybackInfo() async {
guard let descriptor = try? await fetchPlaybackInfoAsync() else { return }
guard descriptor.numberOfItems >= 9 else { return }
let isPlaying = descriptor.atIndex(1)?.booleanValue ?? false
@ -100,42 +121,35 @@ class SpotifyController: MediaControllerProtocol {
duration: duration,
playbackRate: 1,
isShuffled: isShuffled,
isRepeating: isRepeating,
repeatMode: isRepeating ? .all : .off,
lastUpdated: Date()
)
DispatchQueue.main.async { [weak self] in
self?.playbackState = state
}
self.playbackState = state
// Load artwork asynchronously and update the state when complete
if !artworkURL.isEmpty, let url = URL(string: artworkURL) {
DispatchQueue.global(qos: .background).async { [weak self] in
do {
let artworkData = try Data(contentsOf: url)
DispatchQueue.main.async {
// Create a new state with the artwork data and update
var updatedState = state
updatedState.artwork = artworkData
self?.playbackState = updatedState
}
} catch {
print("Failed to load artwork: \(error)")
}
let currentState = state
do {
let (data, _) = try await URLSession.shared.data(from: url)
// Create a new state with the artwork data and update
var updatedState = currentState
updatedState.artwork = data
playbackState = updatedState
} catch {
print("Failed to load artwork: \(error)")
}
} else {
self.playbackState = state
}
}
private func executeCommand(_ command: String) {
// MARK: - Private Methods
private func executeCommand(_ command: String) async {
let script = "tell application \"Spotify\" to \(command)"
Task {
try? await AppleScriptHelper.executeVoid(script)
}
try? await AppleScriptHelper.executeVoid(script)
}
private func fetchPlaybackInfo() -> NSAppleEventDescriptor? {
private func fetchPlaybackInfoAsync() async throws -> NSAppleEventDescriptor? {
let script = """
tell application "Spotify"
set isRunning to true
@ -156,13 +170,6 @@ class SpotifyController: MediaControllerProtocol {
end tell
"""
var descriptor: NSAppleEventDescriptor? = nil
let semaphore = DispatchSemaphore(value: 0)
Task {
descriptor = try? await AppleScriptHelper.execute(script)
semaphore.signal()
}
semaphore.wait()
return descriptor
return try await AppleScriptHelper.execute(script)
}
}