Skip to content

Commit c6ce494

Browse files
committed
refactor(player): update playlist handling and library refresh notifications
Signed-off-by: samzong <samzong.lu@gmail.com>
1 parent 299b522 commit c6ce494

File tree

3 files changed

+29
-11
lines changed

3 files changed

+29
-11
lines changed

MacMusicPlayer/AppDelegate.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
155155
if oldMenu == nil {
156156
NotificationCenter.default.addObserver(self, selector: #selector(updateMenuItems), name: NSNotification.Name("TrackChanged"), object: nil)
157157
NotificationCenter.default.addObserver(self, selector: #selector(updateMenuItems), name: NSNotification.Name("PlaybackStateChanged"), object: nil)
158-
NotificationCenter.default.addObserver(self, selector: #selector(updateMenuItems), name: NSNotification.Name("RefreshMusicLibrary"), object: nil)
158+
NotificationCenter.default.addObserver(self, selector: #selector(updateMenuItems), name: NSNotification.Name("PlaylistUpdated"), object: nil)
159159
}
160160

161161
statusItem?.menu = menu
@@ -441,9 +441,10 @@ class AppDelegate: NSObject, NSApplicationDelegate {
441441
}
442442

443443
@objc func refreshCurrentLibrary() {
444-
guard libraryManager.currentLibrary != nil else { return }
444+
guard let currentLibrary = libraryManager.currentLibrary else { return }
445445

446-
NotificationCenter.default.post(name: NSNotification.Name("RefreshMusicLibrary"), object: nil)
446+
// Simply reload the library, same as startup
447+
playerManager.loadLibrary(currentLibrary)
447448

448449
if let button = statusItem?.button,
449450
let refreshingIcon = makeStatusBarImage(symbolName: "arrow.clockwise", accessibilityDescription: "Refreshing") {

MacMusicPlayer/Managers/PlayerManager.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,13 +209,19 @@ class PlayerManager: NSObject, ObservableObject {
209209
// Legacy playlist for backward compatibility
210210
self.playlist = sortedTracks
211211

212-
// If there are songs, set the first one as current track
212+
// Set first track as current if available
213213
if !sortedTracks.isEmpty {
214214
self.currentIndex = 0
215215
self.currentTrack = sortedTracks[0]
216-
// Set up queue with all tracks starting at index 0
216+
self.playlistStore.setCurrentIndex(0)
217217
self.queueController.setQueue(sortedTracks, startingAt: 0)
218+
} else {
219+
self.currentTrack = nil
220+
self.currentIndex = 0
218221
}
222+
223+
// Notify that playlist has been updated
224+
NotificationCenter.default.post(name: NSNotification.Name("PlaylistUpdated"), object: nil)
219225
}
220226
}
221227

@@ -340,7 +346,7 @@ class PlayerManager: NSObject, ObservableObject {
340346

341347

342348
@objc func refreshMusicLibrary() {
343-
// Check if there is a current library
349+
// Simply reload the current library, same as startup
344350
if let library = (NSApplication.shared.delegate as? AppDelegate)?.libraryManager.currentLibrary {
345351
loadLibrary(library)
346352
} else {

MacMusicPlayer/Views/SimpleSongPickerWindow.swift

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//
77

88
import Cocoa
9+
import Combine
910

1011
class SimpleSongPickerWindow: NSPanel {
1112
private weak var playerManager: PlayerManager?
@@ -17,6 +18,7 @@ class SimpleSongPickerWindow: NSPanel {
1718
private var allTracks: [Track] = []
1819
private var filteredTracks: [Track] = []
1920
private var filterWorkItem: DispatchWorkItem?
21+
private var playlistCancellable: AnyCancellable?
2022

2123
private let windowWidth: CGFloat = 600
2224
private let windowHeight: CGFloat = 400
@@ -36,24 +38,33 @@ class SimpleSongPickerWindow: NSPanel {
3638
setupViews()
3739
loadTracks()
3840

39-
// Listen for music library refresh notification
41+
// Listen for playlist updates
4042
NotificationCenter.default.addObserver(
4143
self,
42-
selector: #selector(handleRefreshMusicLibrary),
43-
name: NSNotification.Name("RefreshMusicLibrary"),
44+
selector: #selector(handlePlaylistUpdated),
45+
name: NSNotification.Name("PlaylistUpdated"),
4446
object: nil
4547
)
48+
49+
if let playerManager = self.playerManager {
50+
playlistCancellable = playerManager.$playlist
51+
.receive(on: DispatchQueue.main)
52+
.sink { [weak self] _ in
53+
self?.loadTracks()
54+
}
55+
}
4656
}
4757

4858
override var canBecomeKey: Bool { true }
4959

5060
deinit {
5161
filterWorkItem?.cancel()
62+
playlistCancellable?.cancel()
5263
NotificationCenter.default.removeObserver(self)
5364
}
5465

55-
@objc private func handleRefreshMusicLibrary() {
56-
// Reload tracks when music library is refreshed
66+
@objc private func handlePlaylistUpdated() {
67+
// Reload tracks when playlist is updated
5768
loadTracks()
5869
}
5970

0 commit comments

Comments
 (0)