From ba8370bcfd980089ef5984b6ca55e4b072a69fee Mon Sep 17 00:00:00 2001 From: GGAutomaton <32899400+GGAutomaton@users.noreply.github.com> Date: Thu, 14 Apr 2022 12:13:42 +0800 Subject: [PATCH] Save changes to the database and bugfix --- .../database/playlist/PlaylistLocalItem.java | 14 ++-- .../playlist/dao/PlaylistRemoteDAO.java | 4 ++ .../playlist/model/PlaylistEntity.java | 6 +- .../newpipe/local/LocalItemListAdapter.java | 1 + .../local/bookmark/BookmarkFragment.java | 65 ++++++++++++++++++- .../local/playlist/LocalPlaylistManager.java | 5 +- .../local/playlist/RemotePlaylistManager.java | 15 +++++ 7 files changed, 100 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/database/playlist/PlaylistLocalItem.java b/app/src/main/java/org/schabi/newpipe/database/playlist/PlaylistLocalItem.java index ae81ce3f5..5bf50cd97 100644 --- a/app/src/main/java/org/schabi/newpipe/database/playlist/PlaylistLocalItem.java +++ b/app/src/main/java/org/schabi/newpipe/database/playlist/PlaylistLocalItem.java @@ -24,6 +24,12 @@ public interface PlaylistLocalItem extends LocalItem { final List result = new ArrayList<>( localPlaylists.size() + remotePlaylists.size()); final List itemsWithSameIndex = new ArrayList<>(); + + // The data from database may not be in the displayIndex order + Collections.sort(localPlaylists, + Comparator.comparingLong(PlaylistMetadataEntry::getDisplayIndex)); + Collections.sort(remotePlaylists, + Comparator.comparingLong(PlaylistRemoteEntity::getDisplayIndex)); int i = 0; int j = 0; while (i < localPlaylists.size()) { @@ -41,10 +47,6 @@ public interface PlaylistLocalItem extends LocalItem { } addItemsWithSameIndex(result, itemsWithSameIndex); - // If displayIndex does not match actual index, update displayIndex. - // This may happen when a new list is created with default displayIndex = 0. - // todo: update displayIndex - return result; } @@ -52,8 +54,8 @@ public interface PlaylistLocalItem extends LocalItem { final List itemsWithSameIndex) { if (!itemsWithSameIndex.isEmpty() && itemsWithSameIndex.get(0).getDisplayIndex() != item.getDisplayIndex()) { - // The new item has a different displayIndex, - // add previous items with same index to the result. + // The new item has a different displayIndex, add previous items with same + // index to the result. addItemsWithSameIndex(result, itemsWithSameIndex); itemsWithSameIndex.clear(); } diff --git a/app/src/main/java/org/schabi/newpipe/database/playlist/dao/PlaylistRemoteDAO.java b/app/src/main/java/org/schabi/newpipe/database/playlist/dao/PlaylistRemoteDAO.java index 6bb849428..ade857464 100644 --- a/app/src/main/java/org/schabi/newpipe/database/playlist/dao/PlaylistRemoteDAO.java +++ b/app/src/main/java/org/schabi/newpipe/database/playlist/dao/PlaylistRemoteDAO.java @@ -31,6 +31,10 @@ public interface PlaylistRemoteDAO extends BasicDAO { + " WHERE " + REMOTE_PLAYLIST_SERVICE_ID + " = :serviceId") Flowable> listByService(int serviceId); + @Query("SELECT * FROM " + REMOTE_PLAYLIST_TABLE + " WHERE " + + REMOTE_PLAYLIST_ID + " = :playlistId") + Flowable> getPlaylist(long playlistId); + @Query("SELECT * FROM " + REMOTE_PLAYLIST_TABLE + " WHERE " + REMOTE_PLAYLIST_URL + " = :url AND " + REMOTE_PLAYLIST_SERVICE_ID + " = :serviceId") Flowable> getPlaylist(long serviceId, String url); diff --git a/app/src/main/java/org/schabi/newpipe/database/playlist/model/PlaylistEntity.java b/app/src/main/java/org/schabi/newpipe/database/playlist/model/PlaylistEntity.java index c1ae0a2b3..82f697ab3 100644 --- a/app/src/main/java/org/schabi/newpipe/database/playlist/model/PlaylistEntity.java +++ b/app/src/main/java/org/schabi/newpipe/database/playlist/model/PlaylistEntity.java @@ -2,6 +2,7 @@ package org.schabi.newpipe.database.playlist.model; import androidx.room.ColumnInfo; import androidx.room.Entity; +import androidx.room.Ignore; import androidx.room.Index; import androidx.room.PrimaryKey; @@ -28,11 +29,12 @@ public class PlaylistEntity { private String thumbnailUrl; @ColumnInfo(name = PLAYLIST_DISPLAY_INDEX) - private long displayIndex = 0; + private long displayIndex; - public PlaylistEntity(final String name, final String thumbnailUrl) { + public PlaylistEntity(final String name, final String thumbnailUrl, final long displayIndex) { this.name = name; this.thumbnailUrl = thumbnailUrl; + this.displayIndex = displayIndex; } public long getUid() { diff --git a/app/src/main/java/org/schabi/newpipe/local/LocalItemListAdapter.java b/app/src/main/java/org/schabi/newpipe/local/LocalItemListAdapter.java index 05e2fdac0..e2bfd5977 100644 --- a/app/src/main/java/org/schabi/newpipe/local/LocalItemListAdapter.java +++ b/app/src/main/java/org/schabi/newpipe/local/LocalItemListAdapter.java @@ -142,6 +142,7 @@ public class LocalItemListAdapter extends RecyclerView.Adapter subscriptions) { + + // If displayIndex does not match actual index, update displayIndex. + // This may happen when a new list is created + // or on the first run after database update + // or displayIndex is not continuous for some reason. + checkDisplayIndexUpdate(subscriptions); + handleResult(subscriptions); if (databaseSubscription != null) { databaseSubscription.request(1); @@ -212,7 +219,8 @@ public final class BookmarkFragment extends BaseLocalListFragment { /*Do nothing on success*/ }, throwable -> showError( + new ErrorInfo(throwable, + UserAction.REQUESTED_BOOKMARK, + "Changing local playlist display_index"))); + disposables.add(disposable); + } + + private void changeRemotePlaylistDisplayIndex(final long id, final long displayIndex) { + + if (remotePlaylistManager == null) { + return; + } + + if (DEBUG) { + Log.d(TAG, "Updating remote playlist id=[" + id + "] " + + "with new display_index=[" + displayIndex + "]"); + } + + final Disposable disposable = + remotePlaylistManager.changePlaylistDisplayIndex(id, displayIndex) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(longs -> { /*Do nothing on success*/ }, throwable -> showError( + new ErrorInfo(throwable, + UserAction.REQUESTED_BOOKMARK, + "Changing remote playlist display_index"))); + disposables.add(disposable); + } + + private void checkDisplayIndexUpdate(@NonNull final List result) { + for (int i = 0; i < result.size(); i++) { + final PlaylistLocalItem item = result.get(i); + if (item.getDisplayIndex() != i) { + if (item instanceof PlaylistMetadataEntry) { + changeLocalPlaylistDisplayIndex(((PlaylistMetadataEntry) item).uid, i); + } else if (item instanceof PlaylistRemoteEntity) { + changeRemotePlaylistDisplayIndex(((PlaylistRemoteEntity) item).getUid(), i); + } + } + } + } } diff --git a/app/src/main/java/org/schabi/newpipe/local/playlist/LocalPlaylistManager.java b/app/src/main/java/org/schabi/newpipe/local/playlist/LocalPlaylistManager.java index aabda1bf0..47817f9e4 100644 --- a/app/src/main/java/org/schabi/newpipe/local/playlist/LocalPlaylistManager.java +++ b/app/src/main/java/org/schabi/newpipe/local/playlist/LocalPlaylistManager.java @@ -40,8 +40,11 @@ public class LocalPlaylistManager { return Maybe.empty(); } final StreamEntity defaultStream = streams.get(0); + + // Make sure the new playlist is always on the top of bookmark. + // The index will be reassigned to non-negative number in BookmarkFragment. final PlaylistEntity newPlaylist = - new PlaylistEntity(name, defaultStream.getThumbnailUrl()); + new PlaylistEntity(name, defaultStream.getThumbnailUrl(), -1); return Maybe.fromCallable(() -> database.runInTransaction(() -> upsertStreams(playlistTable.insert(newPlaylist), streams, 0)) diff --git a/app/src/main/java/org/schabi/newpipe/local/playlist/RemotePlaylistManager.java b/app/src/main/java/org/schabi/newpipe/local/playlist/RemotePlaylistManager.java index 5221139e3..b49f149d6 100644 --- a/app/src/main/java/org/schabi/newpipe/local/playlist/RemotePlaylistManager.java +++ b/app/src/main/java/org/schabi/newpipe/local/playlist/RemotePlaylistManager.java @@ -8,6 +8,7 @@ import org.schabi.newpipe.extractor.playlist.PlaylistInfo; import java.util.List; import io.reactivex.rxjava3.core.Flowable; +import io.reactivex.rxjava3.core.Maybe; import io.reactivex.rxjava3.core.Single; import io.reactivex.rxjava3.schedulers.Schedulers; @@ -33,6 +34,20 @@ public class RemotePlaylistManager { .subscribeOn(Schedulers.io()); } + public Maybe changePlaylistDisplayIndex(final long playlistId, + final long displayIndex) { + return playlistRemoteTable.getPlaylist(playlistId) + .firstElement() + .filter(playlistRemoteEntities -> !playlistRemoteEntities.isEmpty()) + .map(playlistRemoteEntities -> { + final PlaylistRemoteEntity playlist = playlistRemoteEntities.get(0); + if (displayIndex != -1) { + playlist.setDisplayIndex(displayIndex); + } + return playlistRemoteTable.update(playlist); + }).subscribeOn(Schedulers.io()); + } + public Single onBookmark(final PlaylistInfo playlistInfo) { return Single.fromCallable(() -> { final PlaylistRemoteEntity playlist = new PlaylistRemoteEntity(playlistInfo);