Skip to content

Commit 6809756

Browse files
committed
Fixed the bug by replacing the thumbnail_url with the thumbnail_stream_id
1 parent fceec71 commit 6809756

11 files changed

Lines changed: 885 additions & 69 deletions

File tree

app/schemas/org.schabi.newpipe.database.AppDatabase/7.json

Lines changed: 737 additions & 0 deletions
Large diffs are not rendered by default.

app/src/androidTest/java/org/schabi/newpipe/database/DatabaseMigrationTest.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,13 @@ class DatabaseMigrationTest {
101101
Migrations.MIGRATION_5_6
102102
)
103103

104+
testHelper.runMigrationsAndValidate(
105+
AppDatabase.DATABASE_NAME,
106+
Migrations.DB_VER_7,
107+
true,
108+
Migrations.MIGRATION_6_7
109+
)
110+
104111
val migratedDatabaseV3 = getMigratedDatabase()
105112
val listFromDB = migratedDatabaseV3.streamDAO().all.blockingFirst()
106113

app/src/main/java/org/schabi/newpipe/NewPipeDatabase.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import static org.schabi.newpipe.database.Migrations.MIGRATION_3_4;
77
import static org.schabi.newpipe.database.Migrations.MIGRATION_4_5;
88
import static org.schabi.newpipe.database.Migrations.MIGRATION_5_6;
9+
import static org.schabi.newpipe.database.Migrations.MIGRATION_6_7;
910

1011
import android.content.Context;
1112
import android.database.Cursor;
@@ -26,7 +27,7 @@ private static AppDatabase getDatabase(final Context context) {
2627
return Room
2728
.databaseBuilder(context.getApplicationContext(), AppDatabase.class, DATABASE_NAME)
2829
.addMigrations(MIGRATION_1_2, MIGRATION_2_3, MIGRATION_3_4, MIGRATION_4_5,
29-
MIGRATION_5_6)
30+
MIGRATION_5_6, MIGRATION_6_7)
3031
.build();
3132
}
3233

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package org.schabi.newpipe.database;
22

3-
import static org.schabi.newpipe.database.Migrations.DB_VER_6;
3+
import static org.schabi.newpipe.database.Migrations.DB_VER_7;
44

55
import androidx.room.Database;
66
import androidx.room.RoomDatabase;
@@ -38,7 +38,7 @@
3838
FeedEntity.class, FeedGroupEntity.class, FeedGroupSubscriptionEntity.class,
3939
FeedLastUpdatedEntity.class
4040
},
41-
version = DB_VER_6
41+
version = DB_VER_7
4242
)
4343
public abstract class AppDatabase extends RoomDatabase {
4444
public static final String DATABASE_NAME = "newpipe.db";

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public final class Migrations {
2424
public static final int DB_VER_4 = 4;
2525
public static final int DB_VER_5 = 5;
2626
public static final int DB_VER_6 = 6;
27+
public static final int DB_VER_7 = 7;
2728

2829
private static final String TAG = Migrations.class.getName();
2930
public static final boolean DEBUG = MainActivity.DEBUG;
@@ -197,6 +198,47 @@ public void migrate(@NonNull final SupportSQLiteDatabase database) {
197198
}
198199
};
199200

201+
public static final Migration MIGRATION_6_7 = new Migration(DB_VER_6, DB_VER_7) {
202+
@Override
203+
public void migrate(@NonNull final SupportSQLiteDatabase database) {
204+
// Create a new column thumbnail_stream_id
205+
database.execSQL("ALTER TABLE `playlists` ADD COLUMN `thumbnail_stream_id` "
206+
+ "INTEGER NOT NULL DEFAULT -1");
207+
208+
// Migrate the thumbnail_url to the thumbnail_stream_id
209+
database.execSQL("CREATE TEMPORARY TABLE temporary_table AS"
210+
+ " SELECT p.uid AS playlist_uid, s.uid AS stream_uid"
211+
+ " FROM playlists p"
212+
+ " LEFT JOIN playlist_stream_join ps ON p.uid = ps.playlist_id"
213+
+ " 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");
222+
223+
// Remove the thumbnail_url field in the playlist table
224+
database.execSQL("CREATE TABLE IF NOT EXISTS `playlists_new`"
225+
+ "(uid INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "
226+
+ "name TEXT, "
227+
+ "is_thumbnail_permanent INTEGER NOT NULL, "
228+
+ "thumbnail_stream_id INTEGER NOT NULL)");
229+
230+
database.execSQL("INSERT INTO playlists_new"
231+
+ " SELECT uid, name, is_thumbnail_permanent, thumbnail_stream_id "
232+
+ " FROM playlists");
233+
234+
235+
database.execSQL("DROP TABLE playlists");
236+
database.execSQL("ALTER TABLE playlists_new RENAME TO playlists");
237+
database.execSQL("CREATE INDEX IF NOT EXISTS "
238+
+ "`index_playlists_name` ON `playlists` (`name`)");
239+
}
240+
};
241+
200242
private Migrations() {
201243
}
202244
}

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515
import io.reactivex.rxjava3.core.Flowable;
1616

1717
import static org.schabi.newpipe.database.playlist.PlaylistMetadataEntry.PLAYLIST_STREAM_COUNT;
18+
import static org.schabi.newpipe.database.playlist.model.PlaylistEntity.DEFAULT_THUMBNAIL;
1819
import static org.schabi.newpipe.database.playlist.model.PlaylistEntity.PLAYLIST_ID;
1920
import static org.schabi.newpipe.database.playlist.model.PlaylistEntity.PLAYLIST_NAME;
2021
import static org.schabi.newpipe.database.playlist.model.PlaylistEntity.PLAYLIST_TABLE;
22+
import static org.schabi.newpipe.database.playlist.model.PlaylistEntity.PLAYLIST_THUMBNAIL_STREAM_ID;
2123
import static org.schabi.newpipe.database.playlist.model.PlaylistEntity.PLAYLIST_THUMBNAIL_URL;
2224
import static org.schabi.newpipe.database.playlist.model.PlaylistStreamEntity.JOIN_INDEX;
2325
import static org.schabi.newpipe.database.playlist.model.PlaylistStreamEntity.JOIN_PLAYLIST_ID;
@@ -54,14 +56,14 @@ default Flowable<List<PlaylistStreamEntity>> listByService(final int serviceId)
5456
+ " WHERE " + JOIN_PLAYLIST_ID + " = :playlistId")
5557
Flowable<Integer> getMaximumIndexOf(long playlistId);
5658

57-
@Query("SELECT CASE WHEN COUNT(*) != 0 then " + STREAM_THUMBNAIL_URL + " ELSE :defaultUrl END"
59+
@Query("SELECT CASE WHEN COUNT(*) != 0 then " + STREAM_ID + " ELSE -1 END"
5860
+ " FROM " + STREAM_TABLE
5961
+ " LEFT JOIN " + PLAYLIST_STREAM_JOIN_TABLE
6062
+ " ON " + STREAM_ID + " = " + JOIN_STREAM_ID
6163
+ " WHERE " + JOIN_PLAYLIST_ID + " = :playlistId "
6264
+ " LIMIT 1"
6365
)
64-
Flowable<String> getAutomaticThumbnailUrl(long playlistId, String defaultUrl);
66+
Flowable<Long> getAutomaticThumbnailUrl(long playlistId);
6567

6668
@RewriteQueriesToDropUnusedColumns
6769
@Transaction
@@ -84,12 +86,20 @@ default Flowable<List<PlaylistStreamEntity>> listByService(final int serviceId)
8486
Flowable<List<PlaylistStreamEntry>> getOrderedStreamsOf(long playlistId);
8587

8688
@Transaction
87-
@Query("SELECT " + PLAYLIST_ID + ", " + PLAYLIST_NAME + ", " + PLAYLIST_THUMBNAIL_URL + ", "
88-
+ "COALESCE(COUNT(" + JOIN_PLAYLIST_ID + "), 0) AS " + PLAYLIST_STREAM_COUNT
89+
@Query("SELECT " + PLAYLIST_ID + ", " + PLAYLIST_NAME + ","
90+
91+
+ " CASE WHEN " + PLAYLIST_THUMBNAIL_STREAM_ID + " = -1"
92+
+ " THEN " + "'" + DEFAULT_THUMBNAIL + "'"
93+
+ " ELSE (SELECT " + STREAM_THUMBNAIL_URL
94+
+ " FROM " + STREAM_TABLE
95+
+ " WHERE " + STREAM_TABLE + "." + STREAM_ID + " = " + PLAYLIST_THUMBNAIL_STREAM_ID
96+
+ " ) END AS " + PLAYLIST_THUMBNAIL_URL + ", "
8997

98+
+ PLAYLIST_NAME + ", "
99+
+ "COALESCE(COUNT(" + JOIN_PLAYLIST_ID + "), 0) AS " + PLAYLIST_STREAM_COUNT
90100
+ " FROM " + PLAYLIST_TABLE
91101
+ " LEFT JOIN " + PLAYLIST_STREAM_JOIN_TABLE
92-
+ " ON " + PLAYLIST_ID + " = " + JOIN_PLAYLIST_ID
102+
+ " ON " + PLAYLIST_TABLE + "." + PLAYLIST_ID + " = " + JOIN_PLAYLIST_ID
93103
+ " GROUP BY " + PLAYLIST_ID
94104
+ " ORDER BY " + PLAYLIST_NAME + " COLLATE NOCASE ASC")
95105
Flowable<List<PlaylistMetadataEntry>> getPlaylistMetadata();

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

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,20 @@
88
import static org.schabi.newpipe.database.playlist.model.PlaylistEntity.PLAYLIST_NAME;
99
import static org.schabi.newpipe.database.playlist.model.PlaylistEntity.PLAYLIST_TABLE;
1010

11+
import org.schabi.newpipe.R;
12+
1113
@Entity(tableName = PLAYLIST_TABLE,
1214
indices = {@Index(value = {PLAYLIST_NAME})})
1315
public class PlaylistEntity {
16+
17+
public static final String DEFAULT_THUMBNAIL = "drawable://"
18+
+ R.drawable.placeholder_thumbnail_playlist;
1419
public static final String PLAYLIST_TABLE = "playlists";
1520
public static final String PLAYLIST_ID = "uid";
1621
public static final String PLAYLIST_NAME = "name";
1722
public static final String PLAYLIST_THUMBNAIL_URL = "thumbnail_url";
1823
public static final String PLAYLIST_THUMBNAIL_PERMANENT = "is_thumbnail_permanent";
24+
public static final String PLAYLIST_THUMBNAIL_STREAM_ID = "thumbnail_stream_id";
1925

2026
@PrimaryKey(autoGenerate = true)
2127
@ColumnInfo(name = PLAYLIST_ID)
@@ -24,17 +30,17 @@ public class PlaylistEntity {
2430
@ColumnInfo(name = PLAYLIST_NAME)
2531
private String name;
2632

27-
@ColumnInfo(name = PLAYLIST_THUMBNAIL_URL)
28-
private String thumbnailUrl;
29-
3033
@ColumnInfo(name = PLAYLIST_THUMBNAIL_PERMANENT)
3134
private boolean isThumbnailPermanent;
3235

33-
public PlaylistEntity(final String name, final String thumbnailUrl,
34-
final boolean isThumbnailPermanent) {
36+
@ColumnInfo(name = PLAYLIST_THUMBNAIL_STREAM_ID)
37+
private long thumbnailStreamId;
38+
39+
public PlaylistEntity(final String name, final boolean isThumbnailPermanent,
40+
final long thumbnailStreamId) {
3541
this.name = name;
36-
this.thumbnailUrl = thumbnailUrl;
3742
this.isThumbnailPermanent = isThumbnailPermanent;
43+
this.thumbnailStreamId = thumbnailStreamId;
3844
}
3945

4046
public long getUid() {
@@ -53,12 +59,12 @@ public void setName(final String name) {
5359
this.name = name;
5460
}
5561

56-
public String getThumbnailUrl() {
57-
return thumbnailUrl;
62+
public long getThumbnailStreamId() {
63+
return thumbnailStreamId;
5864
}
5965

60-
public void setThumbnailUrl(final String thumbnailUrl) {
61-
this.thumbnailUrl = thumbnailUrl;
66+
public void setThumbnailStreamId(final long thumbnailStreamId) {
67+
this.thumbnailStreamId = thumbnailStreamId;
6268
}
6369

6470
public boolean getIsThumbnailPermanent() {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -280,10 +280,10 @@ private void showLocalDialog(final PlaylistMetadataEntry selectedItem) {
280280
showDeleteDialog(selectedItem.name,
281281
localPlaylistManager.deletePlaylist(selectedItem.uid));
282282
} else if (isThumbnailPermanent && items.get(index).equals(unsetThumbnail)) {
283-
final String thumbnailUrl = localPlaylistManager
284-
.getAutomaticPlaylistThumbnail(selectedItem.uid);
283+
final long thumbnailStreamId = localPlaylistManager
284+
.getAutomaticPlaylistThumbnailStreamId(selectedItem.uid);
285285
localPlaylistManager
286-
.changePlaylistThumbnail(selectedItem.uid, thumbnailUrl, false)
286+
.changePlaylistThumbnail(selectedItem.uid, thumbnailStreamId, false)
287287
.observeOn(AndroidSchedulers.mainThread())
288288
.subscribe();
289289
}

app/src/main/java/org/schabi/newpipe/local/dialog/PlaylistAppendDialog.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.schabi.newpipe.NewPipeDatabase;
1515
import org.schabi.newpipe.R;
1616
import org.schabi.newpipe.database.playlist.PlaylistMetadataEntry;
17+
import org.schabi.newpipe.database.playlist.model.PlaylistEntity;
1718
import org.schabi.newpipe.database.stream.model.StreamEntity;
1819
import org.schabi.newpipe.local.LocalItemListAdapter;
1920
import org.schabi.newpipe.local.playlist.LocalPlaylistManager;
@@ -131,17 +132,19 @@ private void onPlaylistSelected(@NonNull final LocalPlaylistManager manager,
131132
final Toast successToast = Toast.makeText(getContext(),
132133
R.string.playlist_add_stream_success, Toast.LENGTH_SHORT);
133134

134-
if (playlist.thumbnailUrl
135-
.equals("drawable://" + R.drawable.placeholder_thumbnail_playlist)) {
136-
playlistDisposables.add(manager
137-
.changePlaylistThumbnail(playlist.uid, streams.get(0).getThumbnailUrl(), false)
138-
.observeOn(AndroidSchedulers.mainThread())
139-
.subscribe(ignored -> successToast.show()));
140-
}
141-
142135
playlistDisposables.add(manager.appendToPlaylist(playlist.uid, streams)
143136
.observeOn(AndroidSchedulers.mainThread())
144-
.subscribe(ignored -> successToast.show()));
137+
.subscribe(ignored -> {
138+
successToast.show();
139+
140+
if (playlist.thumbnailUrl.equals(PlaylistEntity.DEFAULT_THUMBNAIL)) {
141+
playlistDisposables.add(manager
142+
.changePlaylistThumbnail(playlist.uid, streams.get(0).getUid(),
143+
false)
144+
.observeOn(AndroidSchedulers.mainThread())
145+
.subscribe(ignore -> successToast.show()));
146+
}
147+
}));
145148

146149
requireDialog().dismiss();
147150
}

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

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@
7070
import io.reactivex.rxjava3.subjects.PublishSubject;
7171

7272
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;
7375
// Save the list 10 seconds after the last change occurred
7476
private static final long SAVE_DEBOUNCE_MILLIS = 10000;
7577
private static final int MINIMUM_INITIAL_DRAG_VELOCITY = 12;
@@ -417,8 +419,8 @@ public void removeWatchedStreams(final boolean removePartiallyWatched) {
417419
if (indexInHistory < 0) {
418420
itemsToKeep.add(playlistItem);
419421
} else if (!isThumbnailPermanent && !thumbnailVideoRemoved
420-
&& playlistManager.getPlaylistThumbnail(playlistId)
421-
.equals(playlistItem.getStreamEntity().getThumbnailUrl())) {
422+
&& playlistManager.getPlaylistThumbnailStreamId(playlistId)
423+
== playlistItem.getStreamEntity().getUid()) {
422424
thumbnailVideoRemoved = true;
423425
}
424426
}
@@ -438,8 +440,8 @@ public void removeWatchedStreams(final boolean removePartiallyWatched) {
438440
&& !streamStateEntity.isFinished(duration))) {
439441
itemsToKeep.add(playlistItem);
440442
} else if (!isThumbnailPermanent && !thumbnailVideoRemoved
441-
&& playlistManager.getPlaylistThumbnail(playlistId)
442-
.equals(playlistItem.getStreamEntity().getThumbnailUrl())) {
443+
&& playlistManager.getPlaylistThumbnailStreamId(playlistId)
444+
== playlistItem.getStreamEntity().getUid()) {
443445
thumbnailVideoRemoved = true;
444446
}
445447
}
@@ -587,7 +589,7 @@ private void changePlaylistName(final String title) {
587589
disposables.add(disposable);
588590
}
589591

590-
private void changeThumbnailUrl(final String thumbnailUrl, final boolean isPermanent) {
592+
private void changeThumbnailStreamId(final long thumbnailStreamId, final boolean isPermanent) {
591593
if (playlistManager == null || (!isPermanent && playlistManager
592594
.getIsPlaylistThumbnailPermanent(playlistId))) {
593595
return;
@@ -599,11 +601,11 @@ private void changeThumbnailUrl(final String thumbnailUrl, final boolean isPerma
599601

600602
if (DEBUG) {
601603
Log.d(TAG, "Updating playlist id=[" + playlistId + "] "
602-
+ "with new thumbnail url=[" + thumbnailUrl + "]");
604+
+ "with new thumbnail stream id=[" + thumbnailStreamId + "]");
603605
}
604606

605607
final Disposable disposable = playlistManager
606-
.changePlaylistThumbnail(playlistId, thumbnailUrl, isPermanent)
608+
.changePlaylistThumbnail(playlistId, thumbnailStreamId, isPermanent)
607609
.observeOn(AndroidSchedulers.mainThread())
608610
.subscribe(ignore -> successToast.show(), throwable ->
609611
showError(new ErrorInfo(throwable, UserAction.REQUESTED_BOOKMARK,
@@ -616,16 +618,16 @@ private void updateThumbnailUrl() {
616618
return;
617619
}
618620

619-
final String newThumbnailUrl;
621+
final long thumbnailStreamId;
620622

621623
if (!itemListAdapter.getItemsList().isEmpty()) {
622-
newThumbnailUrl = ((PlaylistStreamEntry) itemListAdapter.getItemsList().get(0))
623-
.getStreamEntity().getThumbnailUrl();
624+
thumbnailStreamId = ((PlaylistStreamEntry) itemListAdapter.getItemsList().get(0))
625+
.getStreamEntity().getUid();
624626
} else {
625-
newThumbnailUrl = "drawable://" + R.drawable.placeholder_thumbnail_playlist;
627+
thumbnailStreamId = DEFAULT_THUMBNAIL_ID;
626628
}
627629

628-
changeThumbnailUrl(newThumbnailUrl, false);
630+
changeThumbnailStreamId(thumbnailStreamId, false);
629631
}
630632

631633
private void deleteItem(final PlaylistStreamEntry item) {
@@ -634,8 +636,7 @@ private void deleteItem(final PlaylistStreamEntry item) {
634636
}
635637

636638
itemListAdapter.removeItem(item);
637-
if (playlistManager.getPlaylistThumbnail(playlistId)
638-
.equals(item.getStreamEntity().getThumbnailUrl())) {
639+
if (playlistManager.getPlaylistThumbnailStreamId(playlistId) == item.getStreamId()) {
639640
updateThumbnailUrl();
640641
}
641642

@@ -793,7 +794,7 @@ context, getPlayQueueStartingAt(item), true))
793794
.setAction(
794795
StreamDialogDefaultEntry.SET_AS_PLAYLIST_THUMBNAIL,
795796
(f, i) ->
796-
changeThumbnailUrl(item.getStreamEntity().getThumbnailUrl(),
797+
changeThumbnailStreamId(item.getStreamEntity().getUid(),
797798
true))
798799
.setAction(
799800
StreamDialogDefaultEntry.DELETE,

0 commit comments

Comments
 (0)