Skip to content

Commit 24f15cf

Browse files
committed
Don't clear history each time individual items are added or removed.
This will prevent the playback history from vanishing unexpectedly in case a preset can't be loaded. Also fixed a few typos in the Doxygen comments and clarified how the history is changed when changing the playlist.
1 parent 9b8f331 commit 24f15cf

File tree

3 files changed

+126
-9
lines changed

3 files changed

+126
-9
lines changed

src/playlist/Playlist.cpp

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,22 @@ bool Playlist::AddItem(const std::string& filename, uint32_t index, bool allowDu
6565
}
6666
}
6767

68-
m_presetHistory.clear();
6968
if (index >= m_items.size())
7069
{
7170
m_items.emplace_back(filename);
7271
}
7372
else
7473
{
74+
// Increment indices of items equal or grater than the newly added index.
75+
for (auto& historyItem : m_presetHistory)
76+
{
77+
if (historyItem >= index)
78+
{
79+
historyItem++;
80+
}
81+
}
82+
83+
7584
m_items.emplace(m_items.cbegin() + index, filename);
7685
}
7786

@@ -83,7 +92,6 @@ auto Playlist::AddPath(const std::string& path, uint32_t index, bool recursive,
8392
{
8493
uint32_t presetsAdded{0};
8594

86-
m_presetHistory.clear();
8795
if (recursive)
8896
{
8997
try
@@ -129,6 +137,15 @@ auto Playlist::AddPath(const std::string& path, uint32_t index, bool recursive,
129137
}
130138
}
131139

140+
// Increment indices of items in playback history equal or grater than the newly added index.
141+
for (auto& historyItem : m_presetHistory)
142+
{
143+
if (historyItem >= index)
144+
{
145+
historyItem += presetsAdded;
146+
}
147+
}
148+
132149
return presetsAdded;
133150
}
134151

@@ -140,7 +157,23 @@ auto Playlist::RemoveItem(uint32_t index) -> bool
140157
return false;
141158
}
142159

143-
m_presetHistory.clear();
160+
// Remove item from history and decrement indices of items after the removed index.
161+
for (auto it = begin(m_presetHistory); it != end(m_presetHistory);)
162+
{
163+
if (*it == index)
164+
{
165+
it = m_presetHistory.erase(it);
166+
continue;
167+
}
168+
169+
if (*it > index)
170+
{
171+
(*it)--;
172+
}
173+
174+
++it;
175+
}
176+
144177
m_items.erase(m_items.cbegin() + index);
145178

146179
return true;
@@ -333,6 +366,15 @@ void Playlist::RemoveLastHistoryEntry()
333366
}
334367

335368

369+
auto Playlist::HistoryItems() const -> std::vector<uint32_t>
370+
{
371+
std::vector<uint32_t> items;
372+
std::copy(begin(m_presetHistory), end(m_presetHistory), std::back_inserter(items));
373+
374+
return items;
375+
}
376+
377+
336378
auto Playlist::Filter() -> class Filter&
337379
{
338380
return m_filter;

src/playlist/Playlist.hpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class Playlist
3737
{
3838
public:
3939
/**
40-
* Short-hand constant which can be used in AddItem() to add new presets at the end of the playlist.
40+
* Shorthand constant which can be used in AddItem() to add new presets at the end of the playlist.
4141
*/
4242
static constexpr auto InsertAtEnd = std::numeric_limits<uint32_t>::max();
4343

@@ -89,21 +89,23 @@ class Playlist
8989
virtual bool Empty() const;
9090

9191
/**
92-
* @brief Clears the current playlist.
92+
* @brief Clears the current playlist and playback history.
9393
*/
9494
virtual void Clear();
9595

9696
/**
9797
* @brief Returns the playlist items.
9898
* @return A vector with items in the current playlist.
9999
*/
100-
virtual const std::vector<Item>& Items() const;
100+
virtual auto Items() const -> const std::vector<Item>&;
101101

102102
/**
103103
* @brief Adds a preset file to the playlist.
104104
*
105105
* Use Playlist::InsertAtEnd as index to always insert an item at the end of the playlist.
106106
*
107+
* The playback history will be kept, and indices are updated accordingly.
108+
*
107109
* @param filename The file path and name to add.
108110
* @param index The index to insert the preset at. If larger than the playlist size, it's added
109111
* to the end of the playlist.
@@ -117,7 +119,9 @@ class Playlist
117119
* @brief Adds presets (recursively) from the given path.
118120
*
119121
* The function will scan the given path (and possible subdirs) for files with a .milk extension
120-
* and and the to the playlist, starting at the given index.
122+
* and add them to the playlist, starting at the given index.
123+
*
124+
* The playback history will be kept, and indices are updated accordingly.
121125
*
122126
* The order of the added files is unspecified. Use the Sort() method to sort the playlist or
123127
* the newly added range.
@@ -135,7 +139,8 @@ class Playlist
135139
bool allowDuplicates) -> uint32_t;
136140

137141
/**
138-
* @brief Removed a playlist item at the given playlist index.
142+
* @brief Removes a playlist item at the given playlist index.
143+
* The playback history will be kept, and indices are updated accordingly.
139144
* @param index The index to remove.
140145
* @return True if an item was removed, false if the index was out of bounds and no item was
141146
* removed..
@@ -159,6 +164,8 @@ class Playlist
159164
*
160165
* Sorting is case-sensitive.
161166
*
167+
* The playback history is cleared when calling this function.
168+
*
162169
* @param startIndex The index to start sorting at. If the index is larger than the last
163170
* item index, the playlist will remain unchanged.
164171
* @param count The number of items to sort. If the value exceeds the playlist length, only
@@ -226,6 +233,12 @@ class Playlist
226233
*/
227234
virtual void RemoveLastHistoryEntry();
228235

236+
/**
237+
* @brief Returns a vector with the playlist indices of the current playback history.
238+
* @return A vector of indices with the last played presets.
239+
*/
240+
virtual auto HistoryItems() const -> std::vector<uint32_t>;
241+
229242
/**
230243
* @brief Returns the current playlist filter list.
231244
* @return The filter list for the current playlist.

tests/playlist/PlaylistTest.cpp

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,38 @@ TEST(projectMPlaylistPlaylist, AddItemNoDuplicates)
153153
}
154154

155155

156+
TEST(projectMPlaylistPlaylist, AddItemWithHistory)
157+
{
158+
Playlist playlist;
159+
EXPECT_TRUE(playlist.AddItem("/some/file", 0, false));
160+
EXPECT_TRUE(playlist.AddItem("/some/other/file", Playlist::InsertAtEnd, false));
161+
EXPECT_TRUE(playlist.AddItem("/and/another/file", Playlist::InsertAtEnd, false));
162+
163+
ASSERT_EQ(playlist.Size(), 3);
164+
165+
playlist.SetPresetIndex(1);
166+
playlist.SetPresetIndex(2);
167+
playlist.SetPresetIndex(0);
168+
169+
auto historyItemsBefore = playlist.HistoryItems();
170+
ASSERT_EQ(historyItemsBefore.size(), 3);
171+
EXPECT_EQ(historyItemsBefore.at(0), 0); // Playback started with index 0
172+
EXPECT_EQ(historyItemsBefore.at(1), 1);
173+
EXPECT_EQ(historyItemsBefore.at(2), 2);
174+
175+
EXPECT_TRUE(playlist.AddItem("/yet/another/file", 1, false));
176+
177+
ASSERT_EQ(playlist.Size(), 4);
178+
179+
auto historyItemsAfter = playlist.HistoryItems();
180+
ASSERT_EQ(historyItemsAfter.size(), 3);
181+
EXPECT_EQ(historyItemsAfter.at(0), 0);
182+
EXPECT_EQ(historyItemsAfter.at(1), 2);
183+
EXPECT_EQ(historyItemsAfter.at(2), 3);
184+
}
185+
186+
187+
156188
TEST(projectMPlaylistPlaylist, AddPathRecursively)
157189
{
158190
Playlist playlist;
@@ -218,7 +250,7 @@ TEST(projectMPlaylistPlaylist, AddPathNonRecursively)
218250
}
219251

220252

221-
TEST(projectMPlaylistPlaylist, AddPathnonRecursivelyNoDuplicates)
253+
TEST(projectMPlaylistPlaylist, AddPathNonRecursivelyNoDuplicates)
222254
{
223255
Playlist playlist;
224256

@@ -289,6 +321,36 @@ TEST(projectMPlaylistPlaylist, RemoveItemFromMiddle)
289321
}
290322

291323

324+
TEST(projectMPlaylistPlaylist, RemoveItemWithHistory)
325+
{
326+
Playlist playlist;
327+
EXPECT_TRUE(playlist.AddItem("/some/file", Playlist::InsertAtEnd, false));
328+
EXPECT_TRUE(playlist.AddItem("/some/other/file", Playlist::InsertAtEnd, false));
329+
EXPECT_TRUE(playlist.AddItem("/yet/another/file", Playlist::InsertAtEnd, false));
330+
331+
ASSERT_EQ(playlist.Size(), 3);
332+
333+
playlist.SetPresetIndex(1);
334+
playlist.SetPresetIndex(2);
335+
playlist.SetPresetIndex(0);
336+
337+
auto historyItemsBefore = playlist.HistoryItems();
338+
ASSERT_EQ(historyItemsBefore.size(), 3);
339+
EXPECT_EQ(historyItemsBefore.at(0), 0); // Playback started with index 0
340+
EXPECT_EQ(historyItemsBefore.at(1), 1);
341+
EXPECT_EQ(historyItemsBefore.at(2), 2);
342+
343+
EXPECT_TRUE(playlist.RemoveItem(1));
344+
345+
ASSERT_EQ(playlist.Size(), 2);
346+
347+
auto historyItemsAfter = playlist.HistoryItems();
348+
ASSERT_EQ(historyItemsAfter.size(), 2);
349+
EXPECT_EQ(historyItemsAfter.at(0), 0);
350+
EXPECT_EQ(historyItemsAfter.at(1), 1);
351+
}
352+
353+
292354
TEST(projectMPlaylistPlaylist, RemoveItemIndexOutOfBounds)
293355
{
294356
Playlist playlist;

0 commit comments

Comments
 (0)