Skip to content

Commit c34549a

Browse files
committed
Update database migrations and getter/setter
1 parent 96d6b30 commit c34549a

8 files changed

Lines changed: 82 additions & 22 deletions

File tree

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"formatVersion": 1,
33
"database": {
44
"version": 6,
5-
"identityHash": "cc9c4d84f52f49105b1c4216b948b5f7",
5+
"identityHash": "9ffc14521c566beed378d77430de3f0c",
66
"entities": [
77
{
88
"tableName": "subscriptions",
@@ -447,7 +447,7 @@
447447
},
448448
{
449449
"tableName": "remote_playlists",
450-
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`uid` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `service_id` INTEGER NOT NULL, `name` TEXT, `url` TEXT, `thumbnail_url` TEXT, `uploader` TEXT, `stream_count` INTEGER)",
450+
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`uid` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `service_id` INTEGER NOT NULL, `name` TEXT, `url` TEXT, `thumbnail_url` TEXT, `uploader` TEXT, `display_index` INTEGER NOT NULL, `stream_count` INTEGER)",
451451
"fields": [
452452
{
453453
"fieldPath": "uid",
@@ -485,6 +485,12 @@
485485
"affinity": "TEXT",
486486
"notNull": false
487487
},
488+
{
489+
"fieldPath": "displayIndex",
490+
"columnName": "display_index",
491+
"affinity": "INTEGER",
492+
"notNull": true
493+
},
488494
{
489495
"fieldPath": "streamCount",
490496
"columnName": "stream_count",
@@ -731,7 +737,7 @@
731737
"views": [],
732738
"setupQueries": [
733739
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
734-
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, 'cc9c4d84f52f49105b1c4216b948b5f7')"
740+
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '9ffc14521c566beed378d77430de3f0c')"
735741
]
736742
}
737743
}

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

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -195,31 +195,45 @@ public void migrate(@NonNull final SupportSQLiteDatabase database) {
195195
try {
196196
database.beginTransaction();
197197

198+
// update playlists
198199
// create a temp table to initialize display_index
199200
database.execSQL("CREATE TABLE `playlists_tmp` "
200-
+ "(`uid` INTEGER NOT NULL, "
201+
+ "(`uid` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "
201202
+ "`name` TEXT, `thumbnail_url` TEXT,"
202-
+ "`display_index` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL)");
203+
+ "`display_index` INTEGER NOT NULL DEFAULT 0)");
203204
database.execSQL("INSERT INTO `playlists_tmp` (`uid`, `name`, `thumbnail_url`)"
204205
+ "SELECT `uid`, `name`, `thumbnail_url` FROM `playlists`");
205206

206-
// drop the old table and create new one
207+
// replace the old table
207208
database.execSQL("DROP TABLE `playlists`");
208-
database.execSQL("CREATE TABLE `playlists` "
209-
+ "(`uid` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "
210-
+ "`name` TEXT, `thumbnail_url` TEXT,"
211-
+ "`display_index` INTEGER NOT NULL UNIQUE)");
209+
database.execSQL("ALTER TABLE `playlists_tmp` RENAME TO `playlists`");
212210

213-
// insert temp data into the new table
214-
// set display_index start from zero
215-
database.execSQL("INSERT INTO `playlists` SELECT * FROM `playlists_tmp`");
216-
database.execSQL("UPDATE `playlists` SET `display_index` = `display_index` - 1");
211+
// create index on the new table
212+
database.execSQL("CREATE INDEX `index_playlists_name` ON `playlists` (`name`)");
217213

218-
// drop tmp table
219-
database.execSQL("DROP TABLE `playlists_tmp`");
214+
215+
// update remote_playlists
216+
// create a temp table to initialize display_index
217+
database.execSQL("CREATE TABLE `remote_playlists_tmp` "
218+
+ "(`uid` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "
219+
+ "`service_id` INTEGER NOT NULL, `name` TEXT, `url` TEXT, "
220+
+ "`thumbnail_url` TEXT, `uploader` TEXT, "
221+
+ "`display_index` INTEGER NOT NULL DEFAULT 0,"
222+
+ "`stream_count` INTEGER)");
223+
database.execSQL("INSERT INTO `remote_playlists_tmp` (`uid`, `service_id`, "
224+
+ "`name`, `url`, `thumbnail_url`, `uploader`, `stream_count`)"
225+
+ "SELECT `uid`, `service_id`, `name`, `url`, `thumbnail_url`, `uploader`, "
226+
+ "`stream_count` FROM `remote_playlists`");
227+
228+
// replace the old table
229+
database.execSQL("DROP TABLE `remote_playlists`");
230+
database.execSQL("ALTER TABLE `remote_playlists_tmp` RENAME TO `remote_playlists`");
220231

221232
// create index on the new table
222-
database.execSQL("CREATE INDEX `index_playlists_name` ON `playlists` (`name`)");
233+
database.execSQL("CREATE INDEX `index_remote_playlists_name` "
234+
+ "ON `remote_playlists` (`name`)");
235+
database.execSQL("CREATE UNIQUE INDEX `index_remote_playlists_service_id_url` "
236+
+ "ON `remote_playlists` (`service_id`, `url`)");
223237

224238
database.setTransactionSuccessful();
225239
} finally {

app/src/main/java/org/schabi/newpipe/database/playlist/PlaylistLocalItem.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public interface PlaylistLocalItem extends LocalItem {
1414
static List<PlaylistLocalItem> merge(
1515
final List<PlaylistMetadataEntry> localPlaylists,
1616
final List<PlaylistRemoteEntity> remotePlaylists) {
17+
// todo: merge algorithm
1718
final List<PlaylistLocalItem> items = new ArrayList<>(
1819
localPlaylists.size() + remotePlaylists.size());
1920
items.addAll(localPlaylists);

app/src/main/java/org/schabi/newpipe/database/playlist/PlaylistMetadataEntry.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import androidx.room.ColumnInfo;
44

5+
import static org.schabi.newpipe.database.playlist.model.PlaylistEntity.PLAYLIST_DISPLAY_INDEX;
56
import static org.schabi.newpipe.database.playlist.model.PlaylistEntity.PLAYLIST_ID;
67
import static org.schabi.newpipe.database.playlist.model.PlaylistEntity.PLAYLIST_NAME;
78
import static org.schabi.newpipe.database.playlist.model.PlaylistEntity.PLAYLIST_THUMBNAIL_URL;
@@ -15,14 +16,17 @@ public class PlaylistMetadataEntry implements PlaylistLocalItem {
1516
public final String name;
1617
@ColumnInfo(name = PLAYLIST_THUMBNAIL_URL)
1718
public final String thumbnailUrl;
19+
@ColumnInfo(name = PLAYLIST_DISPLAY_INDEX)
20+
public final long displayIndex;
1821
@ColumnInfo(name = PLAYLIST_STREAM_COUNT)
1922
public final long streamCount;
2023

2124
public PlaylistMetadataEntry(final long uid, final String name, final String thumbnailUrl,
22-
final long streamCount) {
25+
final long displayIndex, final long streamCount) {
2326
this.uid = uid;
2427
this.name = name;
2528
this.thumbnailUrl = thumbnailUrl;
29+
this.displayIndex = displayIndex;
2630
this.streamCount = streamCount;
2731
}
2832

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
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.PLAYLIST_DISPLAY_INDEX;
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;
@@ -75,6 +76,7 @@ default Flowable<List<PlaylistStreamEntity>> listByService(final int serviceId)
7576

7677
@Transaction
7778
@Query("SELECT " + PLAYLIST_ID + ", " + PLAYLIST_NAME + ", " + PLAYLIST_THUMBNAIL_URL + ", "
79+
+ PLAYLIST_DISPLAY_INDEX + ", "
7880
+ "COALESCE(COUNT(" + JOIN_PLAYLIST_ID + "), 0) AS " + PLAYLIST_STREAM_COUNT
7981

8082
+ " FROM " + PLAYLIST_TABLE

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public class PlaylistRemoteEntity implements PlaylistLocalItem {
3131
public static final String REMOTE_PLAYLIST_URL = "url";
3232
public static final String REMOTE_PLAYLIST_THUMBNAIL_URL = "thumbnail_url";
3333
public static final String REMOTE_PLAYLIST_UPLOADER_NAME = "uploader";
34+
public static final String REMOTE_PLAYLIST_DISPLAY_INDEX = "display_index";
3435
public static final String REMOTE_PLAYLIST_STREAM_COUNT = "stream_count";
3536

3637
@PrimaryKey(autoGenerate = true)
@@ -52,6 +53,9 @@ public class PlaylistRemoteEntity implements PlaylistLocalItem {
5253
@ColumnInfo(name = REMOTE_PLAYLIST_UPLOADER_NAME)
5354
private String uploader;
5455

56+
@ColumnInfo(name = REMOTE_PLAYLIST_DISPLAY_INDEX)
57+
private long displayIndex;
58+
5559
@ColumnInfo(name = REMOTE_PLAYLIST_STREAM_COUNT)
5660
private Long streamCount;
5761

@@ -66,6 +70,19 @@ public PlaylistRemoteEntity(final int serviceId, final String name, final String
6670
this.streamCount = streamCount;
6771
}
6872

73+
@Ignore
74+
public PlaylistRemoteEntity(final int serviceId, final String name, final String url,
75+
final String thumbnailUrl, final String uploader,
76+
final long displayIndex, final Long streamCount) {
77+
this.serviceId = serviceId;
78+
this.name = name;
79+
this.url = url;
80+
this.thumbnailUrl = thumbnailUrl;
81+
this.uploader = uploader;
82+
this.displayIndex = displayIndex;
83+
this.streamCount = streamCount;
84+
}
85+
6986
@Ignore
7087
public PlaylistRemoteEntity(final PlaylistInfo info) {
7188
this(info.getServiceId(), info.getName(), info.getUrl(),
@@ -136,6 +153,14 @@ public void setUploader(final String uploader) {
136153
this.uploader = uploader;
137154
}
138155

156+
public long getDisplayIndex() {
157+
return displayIndex;
158+
}
159+
160+
public void setDisplayIndex(final long displayIndex) {
161+
this.displayIndex = displayIndex;
162+
}
163+
139164
public Long getStreamCount() {
140165
return streamCount;
141166
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,6 @@ private void changeLocalPlaylistName(final long id, final String name) {
308308
+ "with new name=[" + name + "] items");
309309
}
310310

311-
localPlaylistManager.renamePlaylist(id, name);
312311
final Disposable disposable = localPlaylistManager.renamePlaylist(id, name)
313312
.observeOn(AndroidSchedulers.mainThread())
314313
.subscribe(longs -> { /*Do nothing on success*/ }, throwable -> showError(

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,17 @@ public Single<Integer> deletePlaylist(final long playlistId) {
9696
}
9797

9898
public Maybe<Integer> renamePlaylist(final long playlistId, final String name) {
99-
return modifyPlaylist(playlistId, name, null);
99+
return modifyPlaylist(playlistId, name, null, -1);
100100
}
101101

102102
public Maybe<Integer> changePlaylistThumbnail(final long playlistId,
103103
final String thumbnailUrl) {
104-
return modifyPlaylist(playlistId, null, thumbnailUrl);
104+
return modifyPlaylist(playlistId, null, thumbnailUrl, -1);
105+
}
106+
107+
public Maybe<Integer> changePlaylistDisplayIndex(final long playlistId,
108+
final long displayIndex) {
109+
return modifyPlaylist(playlistId, null, null, displayIndex);
105110
}
106111

107112
public String getPlaylistThumbnail(final long playlistId) {
@@ -110,7 +115,8 @@ public String getPlaylistThumbnail(final long playlistId) {
110115

111116
private Maybe<Integer> modifyPlaylist(final long playlistId,
112117
@Nullable final String name,
113-
@Nullable final String thumbnailUrl) {
118+
@Nullable final String thumbnailUrl,
119+
final long displayIndex) {
114120
return playlistTable.getPlaylist(playlistId)
115121
.firstElement()
116122
.filter(playlistEntities -> !playlistEntities.isEmpty())
@@ -122,6 +128,9 @@ private Maybe<Integer> modifyPlaylist(final long playlistId,
122128
if (thumbnailUrl != null) {
123129
playlist.setThumbnailUrl(thumbnailUrl);
124130
}
131+
if (displayIndex != -1) {
132+
playlist.setDisplayIndex(displayIndex);
133+
}
125134
return playlistTable.update(playlist);
126135
}).subscribeOn(Schedulers.io());
127136
}

0 commit comments

Comments
 (0)