Skip to content

Commit 3c48825

Browse files
committed
Debounced saver & bugfix & clean code
1 parent bfb56b4 commit 3c48825

11 files changed

Lines changed: 283 additions & 93 deletions

File tree

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ static List<PlaylistLocalItem> merge(
4545
addItem(result, localPlaylists.get(i), itemsWithSameIndex);
4646
i++;
4747
}
48+
while (j < remotePlaylists.size()) {
49+
addItem(result, remotePlaylists.get(j), itemsWithSameIndex);
50+
j++;
51+
}
4852
addItemsWithSameIndex(result, itemsWithSameIndex);
4953

5054
return result;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class PlaylistMetadataEntry implements PlaylistLocalItem {
1717
@ColumnInfo(name = PLAYLIST_THUMBNAIL_URL)
1818
public final String thumbnailUrl;
1919
@ColumnInfo(name = PLAYLIST_DISPLAY_INDEX)
20-
public final long displayIndex;
20+
public long displayIndex;
2121
@ColumnInfo(name = PLAYLIST_STREAM_COUNT)
2222
public final long streamCount;
2323

app/src/main/java/org/schabi/newpipe/database/playlist/dao/PlaylistDAO.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import androidx.room.Dao;
44
import androidx.room.Query;
5+
import androidx.room.Transaction;
56

67
import org.schabi.newpipe.database.BasicDAO;
78
import org.schabi.newpipe.database.playlist.model.PlaylistEntity;
@@ -36,4 +37,17 @@ default Flowable<List<PlaylistEntity>> listByService(final int serviceId) {
3637

3738
@Query("SELECT COUNT(*) FROM " + PLAYLIST_TABLE)
3839
Flowable<Long> getCount();
40+
41+
@Transaction
42+
default long upsertPlaylist(final PlaylistEntity playlist) {
43+
final long playlistId = playlist.getUid();
44+
45+
if (playlistId == -1) {
46+
// This situation is probably impossible.
47+
return insert(playlist);
48+
} else {
49+
update(playlist);
50+
return playlistId;
51+
}
52+
}
3953
}

app/src/main/java/org/schabi/newpipe/database/playlist/dao/PlaylistStreamDAO.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,19 @@ default Flowable<List<PlaylistStreamEntity>> listByService(final int serviceId)
8282
+ " FROM " + PLAYLIST_TABLE
8383
+ " LEFT JOIN " + PLAYLIST_STREAM_JOIN_TABLE
8484
+ " ON " + PLAYLIST_ID + " = " + JOIN_PLAYLIST_ID
85-
+ " GROUP BY " + JOIN_PLAYLIST_ID
85+
+ " GROUP BY " + PLAYLIST_ID
8686
+ " ORDER BY " + PLAYLIST_NAME + " COLLATE NOCASE ASC")
8787
Flowable<List<PlaylistMetadataEntry>> getPlaylistMetadata();
88+
89+
@Transaction
90+
@Query("SELECT " + PLAYLIST_ID + ", " + PLAYLIST_NAME + ", " + PLAYLIST_THUMBNAIL_URL + ", "
91+
+ PLAYLIST_DISPLAY_INDEX + ", "
92+
+ "COALESCE(COUNT(" + JOIN_PLAYLIST_ID + "), 0) AS " + PLAYLIST_STREAM_COUNT
93+
94+
+ " FROM " + PLAYLIST_TABLE
95+
+ " LEFT JOIN " + PLAYLIST_STREAM_JOIN_TABLE
96+
+ " ON " + PLAYLIST_ID + " = " + JOIN_PLAYLIST_ID
97+
+ " GROUP BY " + PLAYLIST_ID
98+
+ " ORDER BY " + PLAYLIST_DISPLAY_INDEX)
99+
Flowable<List<PlaylistMetadataEntry>> getDisplayIndexOrderedPlaylistMetadata();
88100
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22

33
import androidx.room.ColumnInfo;
44
import androidx.room.Entity;
5+
import androidx.room.Ignore;
56
import androidx.room.Index;
67
import androidx.room.PrimaryKey;
78

89
import static org.schabi.newpipe.database.playlist.model.PlaylistEntity.PLAYLIST_NAME;
910
import static org.schabi.newpipe.database.playlist.model.PlaylistEntity.PLAYLIST_TABLE;
1011

12+
import org.schabi.newpipe.database.playlist.PlaylistMetadataEntry;
13+
1114
@Entity(tableName = PLAYLIST_TABLE,
1215
indices = {@Index(value = {PLAYLIST_NAME})})
1316
public class PlaylistEntity {
@@ -36,6 +39,14 @@ public PlaylistEntity(final String name, final String thumbnailUrl, final long d
3639
this.displayIndex = displayIndex;
3740
}
3841

42+
@Ignore
43+
public PlaylistEntity(final PlaylistMetadataEntry item) {
44+
this.uid = item.uid;
45+
this.name = item.name;
46+
this.thumbnailUrl = item.thumbnailUrl;
47+
this.displayIndex = item.displayIndex;
48+
}
49+
3950
public long getUid() {
4051
return uid;
4152
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public class PlaylistRemoteEntity implements PlaylistLocalItem {
5454
private String uploader;
5555

5656
@ColumnInfo(name = REMOTE_PLAYLIST_DISPLAY_INDEX)
57-
private long displayIndex;
57+
private long displayIndex = -1; // Make sure the new item is on the top
5858

5959
@ColumnInfo(name = REMOTE_PLAYLIST_STREAM_COUNT)
6060
private Long streamCount;

app/src/main/java/org/schabi/newpipe/local/LocalItemListAdapter.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ public void removeItem(final LocalItem data) {
142142
}
143143

144144
public boolean swapItems(final int fromAdapterPosition, final int toAdapterPosition) {
145-
// todo: reuse this code?
146145
final int actualFrom = adapterOffsetWithoutHeader(fromAdapterPosition);
147146
final int actualTo = adapterOffsetWithoutHeader(toAdapterPosition);
148147

0 commit comments

Comments
 (0)