Skip to content

Commit 270a541

Browse files
committed
Implement algorithm to merge playlists
1 parent c34549a commit 270a541

3 files changed

Lines changed: 57 additions & 7 deletions

File tree

app/src/main/java/org/schabi/newpipe/database/playlist/PlaylistLocalItem.java

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,62 @@
1111
public interface PlaylistLocalItem extends LocalItem {
1212
String getOrderingName();
1313

14+
long getDisplayIndex();
15+
1416
static List<PlaylistLocalItem> merge(
1517
final List<PlaylistMetadataEntry> localPlaylists,
1618
final List<PlaylistRemoteEntity> remotePlaylists) {
17-
// todo: merge algorithm
18-
final List<PlaylistLocalItem> items = new ArrayList<>(
19+
20+
// Merge localPlaylists and remotePlaylists by displayIndex.
21+
// If two items have the same displayIndex, sort them in CASE_INSENSITIVE_ORDER.
22+
// This algorithm is similar to the merge operation in merge sort.
23+
24+
final List<PlaylistLocalItem> result = new ArrayList<>(
1925
localPlaylists.size() + remotePlaylists.size());
20-
items.addAll(localPlaylists);
21-
items.addAll(remotePlaylists);
26+
final List<PlaylistLocalItem> itemsWithSameIndex = new ArrayList<>();
27+
int i = 0;
28+
int j = 0;
29+
while (i < localPlaylists.size()) {
30+
while (j < remotePlaylists.size()) {
31+
if (remotePlaylists.get(j).getDisplayIndex()
32+
<= localPlaylists.get(i).getDisplayIndex()) {
33+
addItem(result, remotePlaylists.get(j), itemsWithSameIndex);
34+
j++;
35+
} else {
36+
break;
37+
}
38+
}
39+
addItem(result, localPlaylists.get(i), itemsWithSameIndex);
40+
i++;
41+
}
42+
addItemsWithSameIndex(result, itemsWithSameIndex);
43+
44+
// If displayIndex does not match actual index, update displayIndex.
45+
// This may happen when a new list is created with default displayIndex = 0.
46+
// todo: update displayIndex
2247

23-
Collections.sort(items, Comparator.comparing(PlaylistLocalItem::getOrderingName,
24-
Comparator.nullsLast(String.CASE_INSENSITIVE_ORDER)));
48+
return result;
49+
}
50+
51+
static void addItem(final List<PlaylistLocalItem> result, final PlaylistLocalItem item,
52+
final List<PlaylistLocalItem> itemsWithSameIndex) {
53+
if (!itemsWithSameIndex.isEmpty()
54+
&& itemsWithSameIndex.get(0).getDisplayIndex() != item.getDisplayIndex()) {
55+
// The new item has a different displayIndex,
56+
// add previous items with same index to the result.
57+
addItemsWithSameIndex(result, itemsWithSameIndex);
58+
itemsWithSameIndex.clear();
59+
}
60+
itemsWithSameIndex.add(item);
61+
}
2562

26-
return items;
63+
static void addItemsWithSameIndex(final List<PlaylistLocalItem> result,
64+
final List<PlaylistLocalItem> itemsWithSameIndex) {
65+
if (itemsWithSameIndex.size() > 1) {
66+
Collections.sort(itemsWithSameIndex,
67+
Comparator.comparing(PlaylistLocalItem::getOrderingName,
68+
Comparator.nullsLast(String.CASE_INSENSITIVE_ORDER)));
69+
}
70+
result.addAll(itemsWithSameIndex);
2771
}
2872
}

app/src/main/java/org/schabi/newpipe/database/playlist/PlaylistMetadataEntry.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,9 @@ public LocalItemType getLocalItemType() {
3939
public String getOrderingName() {
4040
return name;
4141
}
42+
43+
@Override
44+
public long getDisplayIndex() {
45+
return displayIndex;
46+
}
4247
}

app/src/main/java/org/schabi/newpipe/database/playlist/model/PlaylistRemoteEntity.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ public void setUploader(final String uploader) {
153153
this.uploader = uploader;
154154
}
155155

156+
@Override
156157
public long getDisplayIndex() {
157158
return displayIndex;
158159
}

0 commit comments

Comments
 (0)