Skip to content

Commit

Permalink
History list resizing
Browse files Browse the repository at this point in the history
  • Loading branch information
kartik-venugopal committed May 9, 2024
1 parent 1810d99 commit c9370e3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Core/History/HistoryDelegateProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ protocol HistoryDelegateProtocol {
// Plays a given item.
func playItem(_ item: HistoryItem)

func resizeList(_ listSize: Int)
func resizeRecentItemsList(to newListSize: Int)

func clearAllHistory()

Expand Down
25 changes: 22 additions & 3 deletions Core/PlayQueue/Delegate/PlayQueueDelegate+History.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ extension PlayQueueDelegate {
markNewEvent(forItem: existingHistoryItem)

} else {

recentItems[trackKey] = TrackHistoryItem(track: track, lastEventTime: Date())
maintainListSize()
}
}

Expand Down Expand Up @@ -116,7 +118,9 @@ extension PlayQueueDelegate {
markNewEvent(forItem: existingHistoryItem)

} else {

recentItems[folderKey] = FolderHistoryItem(folder: folder, lastEventTime: Date())
maintainListSize()
}
}

Expand All @@ -143,7 +147,9 @@ extension PlayQueueDelegate {
markNewEvent(forItem: existingHistoryItem)

} else {

recentItems[playlistFileKey] = PlaylistFileHistoryItem(playlistFile: playlistFile, lastEventTime: Date())
maintainListSize()
}
}

Expand Down Expand Up @@ -172,7 +178,9 @@ extension PlayQueueDelegate {
markNewEvent(forItem: existingHistoryItem)

} else {

recentItems[groupKey] = GroupHistoryItem(groupName: group.name, groupType: group.type, lastEventTime: Date())
maintainListSize()
}
}

Expand All @@ -186,7 +194,9 @@ extension PlayQueueDelegate {
markNewEvent(forItem: existingHistoryItem)

} else {

recentItems[playlistKey] = PlaylistHistoryItem(playlistName: playlist.name, lastEventTime: Date())
maintainListSize()
}
}

Expand Down Expand Up @@ -269,10 +279,19 @@ extension PlayQueueDelegate {
// recentlyPlayedItems.remove(item)
}

func resizeList(_ listSize: Int) {
func resizeRecentItemsList(to newListSize: Int) {

guard recentItems.count > newListSize else {return}

// recentlyPlayedItems.resize(recentlyPlayedListSize)
// messenger.publish(.history_updated)
recentItems.removeFirst(recentItems.count - newListSize)
messenger.publish(.History.updated)
}

private func maintainListSize() {

if let maxListSize = preferences.historyPreferences.recentItemsListSize.value {
resizeRecentItemsList(to: maxListSize)
}
}

func clearAllHistory() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ class HistoryPreferencesViewController: NSViewController, PreferencesViewProtoco
func save() throws {

let historyPrefs = preferences.historyPreferences

let listSizeBeforeChange = historyPrefs.recentItemsListSize.value ?? Int.max
historyPrefs.recentItemsListSize.value = btnLimitRecentItemsListSize.isOn ? recentItemsListSizeMenu.selectedTag() : nil
let listSizeAfterChange = historyPrefs.recentItemsListSize.value ?? Int.max

if listSizeAfterChange < listSizeBeforeChange {

// Need to trim "Recent Items" list
historyDelegate.resizeRecentItemsList(to: listSizeAfterChange)
}
}
}

0 comments on commit c9370e3

Please sign in to comment.