@@ -172,100 +172,54 @@ class MusicManager: ObservableObject {
172172 // MARK: - Update Methods
173173 @MainActor
174174 private func updateFromPlaybackState( _ state: PlaybackState ) {
175- // Check for playback state changes (playing/paused)
176- if state. isPlaying != self . isPlaying {
177- NSLog ( " Playback state changed: \( state. isPlaying ? " Playing " : " Paused " ) " )
178- withAnimation ( . smooth) {
179- self . isPlaying = state. isPlaying
180- self . updateIdleState ( state: state. isPlaying)
181- }
182-
183- if state. isPlaying && !state. title. isEmpty && !state. artist. isEmpty {
184- self . updateSneakPeek ( )
185- }
186- }
187-
188- // Check for changes in track metadata using last artwork change values
175+ // Determine if anything changed
189176 let titleChanged = state. title != self . lastArtworkTitle
190177 let artistChanged = state. artist != self . lastArtworkArtist
191178 let albumChanged = state. album != self . lastArtworkAlbum
192179 let bundleChanged = state. bundleIdentifier != self . lastArtworkBundleIdentifier
193-
194- // Check for artwork changes
195180 let artworkChanged = state. artwork != nil && state. artwork != self . artworkData
196181 let hasContentChange = titleChanged || artistChanged || albumChanged || artworkChanged || bundleChanged
197182
198- // Handle artwork and visual transitions for changed content
199183 if hasContentChange {
200- self . triggerFlipAnimation ( )
201-
202- if artworkChanged, let artwork = state. artwork {
203- self . updateArtwork ( artwork)
204- } else if state. artwork == nil {
205- // Try to use app icon if no artwork but track changed
206- if let appIconImage = AppIconAsNSImage ( for: state. bundleIdentifier) {
207- self . usingAppIconForArtwork = true
208- self . updateAlbumArt ( newAlbumArt: appIconImage)
209- }
210- }
184+ // Update last values
185+ self . lastArtworkTitle = state. title
186+ self . lastArtworkArtist = state. artist
187+ self . lastArtworkAlbum = state. album
188+ self . lastArtworkBundleIdentifier = state. bundleIdentifier
211189 self . artworkData = state. artwork
212190
213- if artworkChanged || state. artwork == nil {
214- // Update last artwork change values
215- self . lastArtworkTitle = state. title
216- self . lastArtworkArtist = state. artist
217- self . lastArtworkAlbum = state. album
218- self . lastArtworkBundleIdentifier = state. bundleIdentifier
191+ // Update all content together
192+ self . updatePlaybackContent (
193+ title: state. title,
194+ artist: state. artist,
195+ album: state. album,
196+ artworkData: state. artwork,
197+ bundleIdentifier: state. bundleIdentifier
198+ )
199+ }
200+
201+ // Playback state changes (play/pause)
202+ if state. isPlaying != self . isPlaying {
203+ withAnimation ( . smooth) {
204+ self . isPlaying = state. isPlaying
205+ self . updateIdleState ( state: state. isPlaying)
219206 }
220207
221- // Only update sneak peek if there's actual content and something changed
222- if !state. title. isEmpty && !state. artist. isEmpty && state. isPlaying {
208+ if state. isPlaying && !state. title. isEmpty && !state. artist. isEmpty {
223209 self . updateSneakPeek ( )
224210 }
225211 }
226212
227- let timeChanged = state. currentTime != self . elapsedTime
228- let durationChanged = state. duration != self . songDuration
229- let playbackRateChanged = state. playbackRate != self . playbackRate
230- let shuffleChanged = state. isShuffled != self . isShuffled
231- let repeatModeChanged = state. repeatMode != self . repeatMode
232-
233- if state. title != self . songTitle {
234- self . songTitle = state. title
235- }
236-
237- if state. artist != self . artistName {
238- self . artistName = state. artist
239- }
240-
241- if state. album != self . album {
242- self . album = state. album
243- }
244-
245- if timeChanged {
246- self . elapsedTime = state. currentTime
247- }
248-
249- if durationChanged {
250- self . songDuration = state. duration
251- }
252-
253- if playbackRateChanged {
254- self . playbackRate = state. playbackRate
255- }
256-
257- if shuffleChanged {
258- self . isShuffled = state. isShuffled
259- }
260-
261- if state. bundleIdentifier != self . bundleIdentifier {
262- self . bundleIdentifier = state. bundleIdentifier
263- }
264-
265- if repeatModeChanged {
266- self . repeatMode = state. repeatMode
267- }
268-
213+ // Other playback properties
214+ if state. currentTime != self . elapsedTime { self . elapsedTime = state. currentTime }
215+ if state. duration != self . songDuration { self . songDuration = state. duration }
216+ if state. playbackRate != self . playbackRate { self . playbackRate = state. playbackRate }
217+ if state. isShuffled != self . isShuffled { self . isShuffled = state. isShuffled }
218+ if state. repeatMode != self . repeatMode { self . repeatMode = state. repeatMode }
219+ if state. title != self . songTitle { self . songTitle = state. title }
220+ if state. artist != self . artistName { self . artistName = state. artist }
221+ if state. album != self . album { self . album = state. album }
222+ if state. bundleIdentifier != self . bundleIdentifier { self . bundleIdentifier = state. bundleIdentifier }
269223 self . timestampDate = state. lastUpdated
270224 }
271225
@@ -284,18 +238,30 @@ class MusicManager: ObservableObject {
284238 flipWorkItem = workItem
285239 DispatchQueue . main. async ( execute: workItem)
286240 }
287-
288- private func updateArtwork( _ artworkData: Data ) {
289- DispatchQueue . global ( qos: . userInitiated) . async { [ weak self] in
290- guard let self = self else { return }
291-
292- if let artworkImage = NSImage ( data: artworkData) {
293- DispatchQueue . main. async { [ weak self] in
294- self ? . usingAppIconForArtwork = false
295- self ? . updateAlbumArt ( newAlbumArt: artworkImage)
296- }
297- }
298- }
241+
242+ @MainActor
243+ private func updatePlaybackContent( title: String ,
244+ artist: String ,
245+ album: String ,
246+ artworkData: Data ? ,
247+ bundleIdentifier: String ? ) {
248+ var finalArtwork : NSImage = defaultImage
249+
250+ if let data = artworkData, let image = NSImage ( data: data) {
251+ finalArtwork = image
252+ self . usingAppIconForArtwork = false
253+ } else if let bundleID = bundleIdentifier,
254+ let appIcon = AppIconAsNSImage ( for: bundleID) {
255+ finalArtwork = appIcon
256+ self . usingAppIconForArtwork = true
257+ }
258+
259+ // Update properties on main actor
260+ self . songTitle = title
261+ self . artistName = artist
262+ self . album = album
263+ self . albumArt = finalArtwork
264+ self . triggerFlipAnimation ( )
299265 }
300266
301267 private func updateIdleState( state: Bool ) {
0 commit comments