Skip to content

Commit ba8370b

Browse files
committed
Save changes to the database and bugfix
1 parent 813f551 commit ba8370b

7 files changed

Lines changed: 100 additions & 10 deletions

File tree

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ static List<PlaylistLocalItem> merge(
2424
final List<PlaylistLocalItem> result = new ArrayList<>(
2525
localPlaylists.size() + remotePlaylists.size());
2626
final List<PlaylistLocalItem> itemsWithSameIndex = new ArrayList<>();
27+
28+
// The data from database may not be in the displayIndex order
29+
Collections.sort(localPlaylists,
30+
Comparator.comparingLong(PlaylistMetadataEntry::getDisplayIndex));
31+
Collections.sort(remotePlaylists,
32+
Comparator.comparingLong(PlaylistRemoteEntity::getDisplayIndex));
2733
int i = 0;
2834
int j = 0;
2935
while (i < localPlaylists.size()) {
@@ -41,19 +47,15 @@ static List<PlaylistLocalItem> merge(
4147
}
4248
addItemsWithSameIndex(result, itemsWithSameIndex);
4349

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
47-
4850
return result;
4951
}
5052

5153
static void addItem(final List<PlaylistLocalItem> result, final PlaylistLocalItem item,
5254
final List<PlaylistLocalItem> itemsWithSameIndex) {
5355
if (!itemsWithSameIndex.isEmpty()
5456
&& 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+
// The new item has a different displayIndex, add previous items with same
58+
// index to the result.
5759
addItemsWithSameIndex(result, itemsWithSameIndex);
5860
itemsWithSameIndex.clear();
5961
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ public interface PlaylistRemoteDAO extends BasicDAO<PlaylistRemoteEntity> {
3131
+ " WHERE " + REMOTE_PLAYLIST_SERVICE_ID + " = :serviceId")
3232
Flowable<List<PlaylistRemoteEntity>> listByService(int serviceId);
3333

34+
@Query("SELECT * FROM " + REMOTE_PLAYLIST_TABLE + " WHERE "
35+
+ REMOTE_PLAYLIST_ID + " = :playlistId")
36+
Flowable<List<PlaylistRemoteEntity>> getPlaylist(long playlistId);
37+
3438
@Query("SELECT * FROM " + REMOTE_PLAYLIST_TABLE + " WHERE "
3539
+ REMOTE_PLAYLIST_URL + " = :url AND " + REMOTE_PLAYLIST_SERVICE_ID + " = :serviceId")
3640
Flowable<List<PlaylistRemoteEntity>> getPlaylist(long serviceId, String url);

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
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

@@ -28,11 +29,12 @@ public class PlaylistEntity {
2829
private String thumbnailUrl;
2930

3031
@ColumnInfo(name = PLAYLIST_DISPLAY_INDEX)
31-
private long displayIndex = 0;
32+
private long displayIndex;
3233

33-
public PlaylistEntity(final String name, final String thumbnailUrl) {
34+
public PlaylistEntity(final String name, final String thumbnailUrl, final long displayIndex) {
3435
this.name = name;
3536
this.thumbnailUrl = thumbnailUrl;
37+
this.displayIndex = displayIndex;
3638
}
3739

3840
public long getUid() {

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

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

144144
public boolean swapItems(final int fromAdapterPosition, final int toAdapterPosition) {
145+
// todo: reuse this code?
145146
final int actualFrom = adapterOffsetWithoutHeader(fromAdapterPosition);
146147
final int actualTo = adapterOffsetWithoutHeader(toAdapterPosition);
147148

app/src/main/java/org/schabi/newpipe/local/bookmark/BookmarkFragment.java

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,13 @@ public void onSubscribe(final Subscription s) {
199199

200200
@Override
201201
public void onNext(final List<PlaylistLocalItem> subscriptions) {
202+
203+
// If displayIndex does not match actual index, update displayIndex.
204+
// This may happen when a new list is created
205+
// or on the first run after database update
206+
// or displayIndex is not continuous for some reason.
207+
checkDisplayIndexUpdate(subscriptions);
208+
202209
handleResult(subscriptions);
203210
if (databaseSubscription != null) {
204211
databaseSubscription.request(1);
@@ -212,7 +219,8 @@ public void onError(final Throwable exception) {
212219
}
213220

214221
@Override
215-
public void onComplete() { }
222+
public void onComplete() {
223+
}
216224
};
217225
}
218226

@@ -316,5 +324,60 @@ private void changeLocalPlaylistName(final long id, final String name) {
316324
"Changing playlist name")));
317325
disposables.add(disposable);
318326
}
327+
328+
private void changeLocalPlaylistDisplayIndex(final long id, final long displayIndex) {
329+
330+
if (localPlaylistManager == null) {
331+
return;
332+
}
333+
334+
if (DEBUG) {
335+
Log.d(TAG, "Updating local playlist id=[" + id + "] "
336+
+ "with new display_index=[" + displayIndex + "]");
337+
}
338+
339+
final Disposable disposable =
340+
localPlaylistManager.changePlaylistDisplayIndex(id, displayIndex)
341+
.observeOn(AndroidSchedulers.mainThread())
342+
.subscribe(longs -> { /*Do nothing on success*/ }, throwable -> showError(
343+
new ErrorInfo(throwable,
344+
UserAction.REQUESTED_BOOKMARK,
345+
"Changing local playlist display_index")));
346+
disposables.add(disposable);
347+
}
348+
349+
private void changeRemotePlaylistDisplayIndex(final long id, final long displayIndex) {
350+
351+
if (remotePlaylistManager == null) {
352+
return;
353+
}
354+
355+
if (DEBUG) {
356+
Log.d(TAG, "Updating remote playlist id=[" + id + "] "
357+
+ "with new display_index=[" + displayIndex + "]");
358+
}
359+
360+
final Disposable disposable =
361+
remotePlaylistManager.changePlaylistDisplayIndex(id, displayIndex)
362+
.observeOn(AndroidSchedulers.mainThread())
363+
.subscribe(longs -> { /*Do nothing on success*/ }, throwable -> showError(
364+
new ErrorInfo(throwable,
365+
UserAction.REQUESTED_BOOKMARK,
366+
"Changing remote playlist display_index")));
367+
disposables.add(disposable);
368+
}
369+
370+
private void checkDisplayIndexUpdate(@NonNull final List<PlaylistLocalItem> result) {
371+
for (int i = 0; i < result.size(); i++) {
372+
final PlaylistLocalItem item = result.get(i);
373+
if (item.getDisplayIndex() != i) {
374+
if (item instanceof PlaylistMetadataEntry) {
375+
changeLocalPlaylistDisplayIndex(((PlaylistMetadataEntry) item).uid, i);
376+
} else if (item instanceof PlaylistRemoteEntity) {
377+
changeRemotePlaylistDisplayIndex(((PlaylistRemoteEntity) item).getUid(), i);
378+
}
379+
}
380+
}
381+
}
319382
}
320383

app/src/main/java/org/schabi/newpipe/local/playlist/LocalPlaylistManager.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,11 @@ public Maybe<List<Long>> createPlaylist(final String name, final List<StreamEnti
4040
return Maybe.empty();
4141
}
4242
final StreamEntity defaultStream = streams.get(0);
43+
44+
// Make sure the new playlist is always on the top of bookmark.
45+
// The index will be reassigned to non-negative number in BookmarkFragment.
4346
final PlaylistEntity newPlaylist =
44-
new PlaylistEntity(name, defaultStream.getThumbnailUrl());
47+
new PlaylistEntity(name, defaultStream.getThumbnailUrl(), -1);
4548

4649
return Maybe.fromCallable(() -> database.runInTransaction(() ->
4750
upsertStreams(playlistTable.insert(newPlaylist), streams, 0))

app/src/main/java/org/schabi/newpipe/local/playlist/RemotePlaylistManager.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.util.List;
99

1010
import io.reactivex.rxjava3.core.Flowable;
11+
import io.reactivex.rxjava3.core.Maybe;
1112
import io.reactivex.rxjava3.core.Single;
1213
import io.reactivex.rxjava3.schedulers.Schedulers;
1314

@@ -33,6 +34,20 @@ public Single<Integer> deletePlaylist(final long playlistId) {
3334
.subscribeOn(Schedulers.io());
3435
}
3536

37+
public Maybe<Integer> changePlaylistDisplayIndex(final long playlistId,
38+
final long displayIndex) {
39+
return playlistRemoteTable.getPlaylist(playlistId)
40+
.firstElement()
41+
.filter(playlistRemoteEntities -> !playlistRemoteEntities.isEmpty())
42+
.map(playlistRemoteEntities -> {
43+
final PlaylistRemoteEntity playlist = playlistRemoteEntities.get(0);
44+
if (displayIndex != -1) {
45+
playlist.setDisplayIndex(displayIndex);
46+
}
47+
return playlistRemoteTable.update(playlist);
48+
}).subscribeOn(Schedulers.io());
49+
}
50+
3651
public Single<Long> onBookmark(final PlaylistInfo playlistInfo) {
3752
return Single.fromCallable(() -> {
3853
final PlaylistRemoteEntity playlist = new PlaylistRemoteEntity(playlistInfo);

0 commit comments

Comments
 (0)