Skip to content

Commit 68ea99d

Browse files
committed
Made some small code improvements
1 parent 5d39558 commit 68ea99d

5 files changed

Lines changed: 25 additions & 25 deletions

File tree

app/src/main/java/org/schabi/newpipe/database/Migrations.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -206,19 +206,15 @@ public void migrate(@NonNull final SupportSQLiteDatabase database) {
206206
+ "INTEGER NOT NULL DEFAULT -1");
207207

208208
// Migrate the thumbnail_url to the thumbnail_stream_id
209-
database.execSQL("CREATE TEMPORARY TABLE temporary_table AS"
209+
database.execSQL("UPDATE playlists SET thumbnail_stream_id = ("
210+
+ " SELECT CASE WHEN COUNT(*) != 0 then stream_uid ELSE -1 END"
211+
+ " FROM ("
210212
+ " SELECT p.uid AS playlist_uid, s.uid AS stream_uid"
211213
+ " FROM playlists p"
212214
+ " LEFT JOIN playlist_stream_join ps ON p.uid = ps.playlist_id"
213215
+ " LEFT JOIN streams s ON s.uid = ps.stream_id"
214-
+ " WHERE s.thumbnail_url = p.thumbnail_url");
215-
216-
database.execSQL("UPDATE playlists SET thumbnail_stream_id = ("
217-
+ "SELECT CASE WHEN COUNT(*) != 0 then stream_uid ELSE -1 END "
218-
+ "FROM temporary_table "
219-
+ "WHERE playlist_uid = playlists.uid)");
220-
221-
database.execSQL("DROP TABLE temporary_table");
216+
+ " WHERE s.thumbnail_url = p.thumbnail_url) AS temporary_table"
217+
+ " WHERE playlist_uid = playlists.uid)");
222218

223219
// Remove the thumbnail_url field in the playlist table
224220
database.execSQL("CREATE TABLE IF NOT EXISTS `playlists_new`"

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.schabi.newpipe.database.playlist.PlaylistDuplicatesEntry;
1010
import org.schabi.newpipe.database.playlist.PlaylistMetadataEntry;
1111
import org.schabi.newpipe.database.playlist.PlaylistStreamEntry;
12+
import org.schabi.newpipe.database.playlist.model.PlaylistEntity;
1213
import org.schabi.newpipe.database.playlist.model.PlaylistStreamEntity;
1314

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

62-
@Query("SELECT CASE WHEN COUNT(*) != 0 then " + STREAM_ID + " ELSE -1 END"
63+
@Query("SELECT CASE WHEN COUNT(*) != 0 then " + STREAM_ID
64+
+ " ELSE " + PlaylistEntity.DEFAULT_THUMBNAIL_ID + " END"
6365
+ " FROM " + STREAM_TABLE
6466
+ " LEFT JOIN " + PLAYLIST_STREAM_JOIN_TABLE
6567
+ " ON " + STREAM_ID + " = " + JOIN_STREAM_ID
6668
+ " WHERE " + JOIN_PLAYLIST_ID + " = :playlistId "
6769
+ " LIMIT 1"
6870
)
69-
Flowable<Long> getAutomaticThumbnailUrl(long playlistId);
71+
Flowable<Long> getAutomaticThumbnailStreamId(long playlistId);
7072

7173
@RewriteQueriesToDropUnusedColumns
7274
@Transaction
@@ -91,14 +93,13 @@ default Flowable<List<PlaylistStreamEntity>> listByService(final int serviceId)
9193
@Transaction
9294
@Query("SELECT " + PLAYLIST_ID + ", " + PLAYLIST_NAME + ","
9395

94-
+ " CASE WHEN " + PLAYLIST_THUMBNAIL_STREAM_ID + " = -1"
95-
+ " THEN " + "'" + DEFAULT_THUMBNAIL + "'"
96+
+ " CASE WHEN " + PLAYLIST_THUMBNAIL_STREAM_ID + " = "
97+
+ PlaylistEntity.DEFAULT_THUMBNAIL_ID + " THEN " + "'" + DEFAULT_THUMBNAIL + "'"
9698
+ " ELSE (SELECT " + STREAM_THUMBNAIL_URL
9799
+ " FROM " + STREAM_TABLE
98100
+ " WHERE " + STREAM_TABLE + "." + STREAM_ID + " = " + PLAYLIST_THUMBNAIL_STREAM_ID
99101
+ " ) END AS " + PLAYLIST_THUMBNAIL_URL + ", "
100102

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

114-
+ " CASE WHEN " + PLAYLIST_THUMBNAIL_STREAM_ID + " = -1"
115-
+ " THEN " + "'" + DEFAULT_THUMBNAIL + "'"
115+
+ " CASE WHEN " + PLAYLIST_THUMBNAIL_STREAM_ID + " = "
116+
+ PlaylistEntity.DEFAULT_THUMBNAIL_ID + " THEN " + "'" + DEFAULT_THUMBNAIL + "'"
116117
+ " ELSE (SELECT " + STREAM_THUMBNAIL_URL
117118
+ " FROM " + STREAM_TABLE
118119
+ " WHERE " + STREAM_TABLE + "." + STREAM_ID + " = " + PLAYLIST_THUMBNAIL_STREAM_ID
119-
+ " ) END AS " + PLAYLIST_THUMBNAIL_URL
120+
+ " ) END AS " + PLAYLIST_THUMBNAIL_URL + ", "
120121

121-
+ ", COALESCE(COUNT(" + JOIN_PLAYLIST_ID + "), 0) AS " + PLAYLIST_STREAM_COUNT + ", "
122+
+ "COALESCE(COUNT(" + JOIN_PLAYLIST_ID + "), 0) AS " + PLAYLIST_STREAM_COUNT + ", "
122123
+ "COALESCE(SUM(" + STREAM_URL + " = :streamUrl), 0) AS "
123124
+ PLAYLIST_TIMES_STREAM_IS_CONTAINED
124125

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ public class PlaylistEntity {
1616

1717
public static final String DEFAULT_THUMBNAIL = "drawable://"
1818
+ R.drawable.placeholder_thumbnail_playlist;
19+
public static final long DEFAULT_THUMBNAIL_ID = -1;
20+
1921
public static final String PLAYLIST_TABLE = "playlists";
2022
public static final String PLAYLIST_ID = "uid";
2123
public static final String PLAYLIST_NAME = "name";

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.schabi.newpipe.database.LocalItem;
3636
import org.schabi.newpipe.database.history.model.StreamHistoryEntry;
3737
import org.schabi.newpipe.database.playlist.PlaylistStreamEntry;
38+
import org.schabi.newpipe.database.playlist.model.PlaylistEntity;
3839
import org.schabi.newpipe.database.stream.model.StreamEntity;
3940
import org.schabi.newpipe.databinding.DialogEditTextBinding;
4041
import org.schabi.newpipe.databinding.LocalPlaylistHeaderBinding;
@@ -70,8 +71,6 @@
7071
import io.reactivex.rxjava3.subjects.PublishSubject;
7172

7273
public class LocalPlaylistFragment extends BaseLocalListFragment<List<PlaylistStreamEntry>, Void> {
73-
public static final long DEFAULT_THUMBNAIL_ID = -1;
74-
public static final long NO_THUMBNAIL_ID = -2;
7574
// Save the list 10 seconds after the last change occurred
7675
private static final long SAVE_DEBOUNCE_MILLIS = 10000;
7776
private static final int MINIMUM_INITIAL_DRAG_VELOCITY = 12;
@@ -624,7 +623,7 @@ private void updateThumbnailUrl() {
624623
thumbnailStreamId = ((PlaylistStreamEntry) itemListAdapter.getItemsList().get(0))
625624
.getStreamEntity().getUid();
626625
} else {
627-
thumbnailStreamId = DEFAULT_THUMBNAIL_ID;
626+
thumbnailStreamId = PlaylistEntity.DEFAULT_THUMBNAIL_ID;
628627
}
629628

630629
changeThumbnailStreamId(thumbnailStreamId, false);

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import io.reactivex.rxjava3.schedulers.Schedulers;
2424

2525
public class LocalPlaylistManager {
26+
private static final long THUMBNAIL_ID_LEAVE_UNCHANGED = -2;
27+
2628
private final AppDatabase database;
2729
private final StreamDAO streamTable;
2830
private final PlaylistDAO playlistTable;
@@ -115,7 +117,7 @@ public Single<Integer> deletePlaylist(final long playlistId) {
115117
}
116118

117119
public Maybe<Integer> renamePlaylist(final long playlistId, final String name) {
118-
return modifyPlaylist(playlistId, name, LocalPlaylistFragment.NO_THUMBNAIL_ID, false);
120+
return modifyPlaylist(playlistId, name, THUMBNAIL_ID_LEAVE_UNCHANGED, false);
119121
}
120122

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

136138
public long getAutomaticPlaylistThumbnailStreamId(final long playlistId) {
137-
final long streamId = playlistStreamTable.getAutomaticThumbnailUrl(playlistId)
139+
final long streamId = playlistStreamTable.getAutomaticThumbnailStreamId(playlistId)
138140
.blockingFirst();
139141
if (streamId < 0) {
140-
return LocalPlaylistFragment.DEFAULT_THUMBNAIL_ID;
142+
return PlaylistEntity.DEFAULT_THUMBNAIL_ID;
141143
}
142144
return streamId;
143145
}
@@ -154,7 +156,7 @@ private Maybe<Integer> modifyPlaylist(final long playlistId,
154156
if (name != null) {
155157
playlist.setName(name);
156158
}
157-
if (thumbnailStreamId != LocalPlaylistFragment.NO_THUMBNAIL_ID) {
159+
if (thumbnailStreamId != THUMBNAIL_ID_LEAVE_UNCHANGED) {
158160
playlist.setThumbnailStreamId(thumbnailStreamId);
159161
playlist.setIsThumbnailPermanent(isPermanent);
160162
}

0 commit comments

Comments
 (0)