Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed a bug that caused erroneous updates of the playlist thumbnails #9755

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Made some small code improvements
  • Loading branch information
Jared234 committed Feb 9, 2023
commit 68ea99d6e6cd932095ed61e2cadc0d6e661409a0
14 changes: 5 additions & 9 deletions app/src/main/java/org/schabi/newpipe/database/Migrations.java
Original file line number Diff line number Diff line change
Expand Up @@ -206,19 +206,15 @@ public void migrate(@NonNull final SupportSQLiteDatabase database) {
+ "INTEGER NOT NULL DEFAULT -1");

// Migrate the thumbnail_url to the thumbnail_stream_id
database.execSQL("CREATE TEMPORARY TABLE temporary_table AS"
database.execSQL("UPDATE playlists SET thumbnail_stream_id = ("
+ " SELECT CASE WHEN COUNT(*) != 0 then stream_uid ELSE -1 END"
+ " FROM ("
+ " SELECT p.uid AS playlist_uid, s.uid AS stream_uid"
+ " FROM playlists p"
+ " LEFT JOIN playlist_stream_join ps ON p.uid = ps.playlist_id"
+ " LEFT JOIN streams s ON s.uid = ps.stream_id"
+ " WHERE s.thumbnail_url = p.thumbnail_url");

database.execSQL("UPDATE playlists SET thumbnail_stream_id = ("
+ "SELECT CASE WHEN COUNT(*) != 0 then stream_uid ELSE -1 END "
+ "FROM temporary_table "
+ "WHERE playlist_uid = playlists.uid)");

database.execSQL("DROP TABLE temporary_table");
+ " WHERE s.thumbnail_url = p.thumbnail_url) AS temporary_table"
+ " WHERE playlist_uid = playlists.uid)");

// Remove the thumbnail_url field in the playlist table
database.execSQL("CREATE TABLE IF NOT EXISTS `playlists_new`"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.schabi.newpipe.database.playlist.PlaylistDuplicatesEntry;
import org.schabi.newpipe.database.playlist.PlaylistMetadataEntry;
import org.schabi.newpipe.database.playlist.PlaylistStreamEntry;
import org.schabi.newpipe.database.playlist.model.PlaylistEntity;
import org.schabi.newpipe.database.playlist.model.PlaylistStreamEntity;

import java.util.List;
Expand Down Expand Up @@ -59,14 +60,15 @@ default Flowable<List<PlaylistStreamEntity>> listByService(final int serviceId)
+ " WHERE " + JOIN_PLAYLIST_ID + " = :playlistId")
Flowable<Integer> getMaximumIndexOf(long playlistId);

@Query("SELECT CASE WHEN COUNT(*) != 0 then " + STREAM_ID + " ELSE -1 END"
@Query("SELECT CASE WHEN COUNT(*) != 0 then " + STREAM_ID
+ " ELSE " + PlaylistEntity.DEFAULT_THUMBNAIL_ID + " END"
+ " FROM " + STREAM_TABLE
+ " LEFT JOIN " + PLAYLIST_STREAM_JOIN_TABLE
+ " ON " + STREAM_ID + " = " + JOIN_STREAM_ID
+ " WHERE " + JOIN_PLAYLIST_ID + " = :playlistId "
+ " LIMIT 1"
)
Flowable<Long> getAutomaticThumbnailUrl(long playlistId);
Flowable<Long> getAutomaticThumbnailStreamId(long playlistId);

@RewriteQueriesToDropUnusedColumns
@Transaction
Expand All @@ -91,14 +93,13 @@ default Flowable<List<PlaylistStreamEntity>> listByService(final int serviceId)
@Transaction
@Query("SELECT " + PLAYLIST_ID + ", " + PLAYLIST_NAME + ","

+ " CASE WHEN " + PLAYLIST_THUMBNAIL_STREAM_ID + " = -1"
+ " THEN " + "'" + DEFAULT_THUMBNAIL + "'"
+ " CASE WHEN " + PLAYLIST_THUMBNAIL_STREAM_ID + " = "
+ PlaylistEntity.DEFAULT_THUMBNAIL_ID + " THEN " + "'" + DEFAULT_THUMBNAIL + "'"
+ " ELSE (SELECT " + STREAM_THUMBNAIL_URL
+ " FROM " + STREAM_TABLE
+ " WHERE " + STREAM_TABLE + "." + STREAM_ID + " = " + PLAYLIST_THUMBNAIL_STREAM_ID
+ " ) END AS " + PLAYLIST_THUMBNAIL_URL + ", "

+ PLAYLIST_NAME + ", "
+ "COALESCE(COUNT(" + JOIN_PLAYLIST_ID + "), 0) AS " + PLAYLIST_STREAM_COUNT
+ " FROM " + PLAYLIST_TABLE
+ " LEFT JOIN " + PLAYLIST_STREAM_JOIN_TABLE
Expand All @@ -111,14 +112,14 @@ default Flowable<List<PlaylistStreamEntity>> listByService(final int serviceId)
@Query("SELECT " + PLAYLIST_TABLE + "." + PLAYLIST_ID + ", "
+ PLAYLIST_NAME + ", "

+ " CASE WHEN " + PLAYLIST_THUMBNAIL_STREAM_ID + " = -1"
+ " THEN " + "'" + DEFAULT_THUMBNAIL + "'"
+ " CASE WHEN " + PLAYLIST_THUMBNAIL_STREAM_ID + " = "
+ PlaylistEntity.DEFAULT_THUMBNAIL_ID + " THEN " + "'" + DEFAULT_THUMBNAIL + "'"
+ " ELSE (SELECT " + STREAM_THUMBNAIL_URL
+ " FROM " + STREAM_TABLE
+ " WHERE " + STREAM_TABLE + "." + STREAM_ID + " = " + PLAYLIST_THUMBNAIL_STREAM_ID
+ " ) END AS " + PLAYLIST_THUMBNAIL_URL
+ " ) END AS " + PLAYLIST_THUMBNAIL_URL + ", "

+ ", COALESCE(COUNT(" + JOIN_PLAYLIST_ID + "), 0) AS " + PLAYLIST_STREAM_COUNT + ", "
+ "COALESCE(COUNT(" + JOIN_PLAYLIST_ID + "), 0) AS " + PLAYLIST_STREAM_COUNT + ", "
+ "COALESCE(SUM(" + STREAM_URL + " = :streamUrl), 0) AS "
+ PLAYLIST_TIMES_STREAM_IS_CONTAINED

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public class PlaylistEntity {

public static final String DEFAULT_THUMBNAIL = "drawable://"
+ R.drawable.placeholder_thumbnail_playlist;
public static final long DEFAULT_THUMBNAIL_ID = -1;

public static final String PLAYLIST_TABLE = "playlists";
public static final String PLAYLIST_ID = "uid";
public static final String PLAYLIST_NAME = "name";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.schabi.newpipe.database.LocalItem;
import org.schabi.newpipe.database.history.model.StreamHistoryEntry;
import org.schabi.newpipe.database.playlist.PlaylistStreamEntry;
import org.schabi.newpipe.database.playlist.model.PlaylistEntity;
import org.schabi.newpipe.database.stream.model.StreamEntity;
import org.schabi.newpipe.databinding.DialogEditTextBinding;
import org.schabi.newpipe.databinding.LocalPlaylistHeaderBinding;
Expand Down Expand Up @@ -70,8 +71,6 @@
import io.reactivex.rxjava3.subjects.PublishSubject;

public class LocalPlaylistFragment extends BaseLocalListFragment<List<PlaylistStreamEntry>, Void> {
public static final long DEFAULT_THUMBNAIL_ID = -1;
public static final long NO_THUMBNAIL_ID = -2;
// Save the list 10 seconds after the last change occurred
private static final long SAVE_DEBOUNCE_MILLIS = 10000;
private static final int MINIMUM_INITIAL_DRAG_VELOCITY = 12;
Expand Down Expand Up @@ -624,7 +623,7 @@ private void updateThumbnailUrl() {
thumbnailStreamId = ((PlaylistStreamEntry) itemListAdapter.getItemsList().get(0))
.getStreamEntity().getUid();
} else {
thumbnailStreamId = DEFAULT_THUMBNAIL_ID;
thumbnailStreamId = PlaylistEntity.DEFAULT_THUMBNAIL_ID;
}

changeThumbnailStreamId(thumbnailStreamId, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import io.reactivex.rxjava3.schedulers.Schedulers;

public class LocalPlaylistManager {
private static final long THUMBNAIL_ID_LEAVE_UNCHANGED = -2;

private final AppDatabase database;
private final StreamDAO streamTable;
private final PlaylistDAO playlistTable;
Expand Down Expand Up @@ -115,7 +117,7 @@ public Single<Integer> deletePlaylist(final long playlistId) {
}

public Maybe<Integer> renamePlaylist(final long playlistId, final String name) {
return modifyPlaylist(playlistId, name, LocalPlaylistFragment.NO_THUMBNAIL_ID, false);
return modifyPlaylist(playlistId, name, THUMBNAIL_ID_LEAVE_UNCHANGED, false);
}

public Maybe<Integer> changePlaylistThumbnail(final long playlistId,
Expand All @@ -134,10 +136,10 @@ public boolean getIsPlaylistThumbnailPermanent(final long playlistId) {
}

public long getAutomaticPlaylistThumbnailStreamId(final long playlistId) {
final long streamId = playlistStreamTable.getAutomaticThumbnailUrl(playlistId)
final long streamId = playlistStreamTable.getAutomaticThumbnailStreamId(playlistId)
.blockingFirst();
if (streamId < 0) {
return LocalPlaylistFragment.DEFAULT_THUMBNAIL_ID;
return PlaylistEntity.DEFAULT_THUMBNAIL_ID;
}
return streamId;
}
Expand All @@ -154,7 +156,7 @@ private Maybe<Integer> modifyPlaylist(final long playlistId,
if (name != null) {
playlist.setName(name);
}
if (thumbnailStreamId != LocalPlaylistFragment.NO_THUMBNAIL_ID) {
if (thumbnailStreamId != THUMBNAIL_ID_LEAVE_UNCHANGED) {
playlist.setThumbnailStreamId(thumbnailStreamId);
playlist.setIsThumbnailPermanent(isPermanent);
}
Expand Down