From 68ea99d6e6cd932095ed61e2cadc0d6e661409a0 Mon Sep 17 00:00:00 2001 From: Jared Fantaye Date: Thu, 9 Feb 2023 23:17:36 +0100 Subject: [PATCH] Made some small code improvements --- .../schabi/newpipe/database/Migrations.java | 14 +++++--------- .../playlist/dao/PlaylistStreamDAO.java | 19 ++++++++++--------- .../playlist/model/PlaylistEntity.java | 2 ++ .../local/playlist/LocalPlaylistFragment.java | 5 ++--- .../local/playlist/LocalPlaylistManager.java | 10 ++++++---- 5 files changed, 25 insertions(+), 25 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/database/Migrations.java b/app/src/main/java/org/schabi/newpipe/database/Migrations.java index e995fdd6c..1886b87c2 100644 --- a/app/src/main/java/org/schabi/newpipe/database/Migrations.java +++ b/app/src/main/java/org/schabi/newpipe/database/Migrations.java @@ -206,19 +206,15 @@ public final class Migrations { + "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`" diff --git a/app/src/main/java/org/schabi/newpipe/database/playlist/dao/PlaylistStreamDAO.java b/app/src/main/java/org/schabi/newpipe/database/playlist/dao/PlaylistStreamDAO.java index 2915e7bd3..89b89b2fe 100644 --- a/app/src/main/java/org/schabi/newpipe/database/playlist/dao/PlaylistStreamDAO.java +++ b/app/src/main/java/org/schabi/newpipe/database/playlist/dao/PlaylistStreamDAO.java @@ -9,6 +9,7 @@ import org.schabi.newpipe.database.BasicDAO; 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; @@ -59,14 +60,15 @@ public interface PlaylistStreamDAO extends BasicDAO { + " WHERE " + JOIN_PLAYLIST_ID + " = :playlistId") Flowable 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 getAutomaticThumbnailUrl(long playlistId); + Flowable getAutomaticThumbnailStreamId(long playlistId); @RewriteQueriesToDropUnusedColumns @Transaction @@ -91,14 +93,13 @@ public interface PlaylistStreamDAO extends BasicDAO { @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 @@ -111,14 +112,14 @@ public interface PlaylistStreamDAO extends BasicDAO { @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 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 6c1d96d41..efb7278fd 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 @@ -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"; diff --git a/app/src/main/java/org/schabi/newpipe/local/playlist/LocalPlaylistFragment.java b/app/src/main/java/org/schabi/newpipe/local/playlist/LocalPlaylistFragment.java index a3648c3c2..3de630b96 100644 --- a/app/src/main/java/org/schabi/newpipe/local/playlist/LocalPlaylistFragment.java +++ b/app/src/main/java/org/schabi/newpipe/local/playlist/LocalPlaylistFragment.java @@ -35,6 +35,7 @@ import org.schabi.newpipe.R; 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; @@ -70,8 +71,6 @@ import io.reactivex.rxjava3.schedulers.Schedulers; import io.reactivex.rxjava3.subjects.PublishSubject; public class LocalPlaylistFragment extends BaseLocalListFragment, 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; @@ -624,7 +623,7 @@ public class LocalPlaylistFragment extends BaseLocalListFragment 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 changePlaylistThumbnail(final long playlistId, @@ -134,10 +136,10 @@ public class LocalPlaylistManager { } 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; } @@ -154,7 +156,7 @@ public class LocalPlaylistManager { if (name != null) { playlist.setName(name); } - if (thumbnailStreamId != LocalPlaylistFragment.NO_THUMBNAIL_ID) { + if (thumbnailStreamId != THUMBNAIL_ID_LEAVE_UNCHANGED) { playlist.setThumbnailStreamId(thumbnailStreamId); playlist.setIsThumbnailPermanent(isPermanent); }