2018-01-17 06:12:03 +01:00
|
|
|
package org.schabi.newpipe.fragments.local;
|
2018-01-15 21:30:52 +01:00
|
|
|
|
|
|
|
import org.schabi.newpipe.database.AppDatabase;
|
|
|
|
import org.schabi.newpipe.database.playlist.PlaylistMetadataEntry;
|
|
|
|
import org.schabi.newpipe.database.playlist.dao.PlaylistDAO;
|
|
|
|
import org.schabi.newpipe.database.playlist.dao.PlaylistStreamDAO;
|
|
|
|
import org.schabi.newpipe.database.playlist.model.PlaylistEntity;
|
|
|
|
import org.schabi.newpipe.database.playlist.model.PlaylistStreamEntity;
|
|
|
|
import org.schabi.newpipe.database.stream.dao.StreamDAO;
|
|
|
|
import org.schabi.newpipe.database.stream.model.StreamEntity;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import io.reactivex.Completable;
|
2018-01-17 06:12:03 +01:00
|
|
|
import io.reactivex.Flowable;
|
2018-01-15 21:30:52 +01:00
|
|
|
import io.reactivex.Maybe;
|
2018-01-17 06:12:03 +01:00
|
|
|
import io.reactivex.Single;
|
2018-01-16 20:48:52 +01:00
|
|
|
import io.reactivex.schedulers.Schedulers;
|
2018-01-15 21:30:52 +01:00
|
|
|
|
|
|
|
public class LocalPlaylistManager {
|
|
|
|
|
|
|
|
private final AppDatabase database;
|
|
|
|
private final StreamDAO streamTable;
|
|
|
|
private final PlaylistDAO playlistTable;
|
|
|
|
private final PlaylistStreamDAO playlistStreamTable;
|
|
|
|
|
|
|
|
public LocalPlaylistManager(final AppDatabase db) {
|
|
|
|
database = db;
|
|
|
|
streamTable = db.streamDAO();
|
|
|
|
playlistTable = db.playlistDAO();
|
|
|
|
playlistStreamTable = db.playlistStreamDAO();
|
|
|
|
}
|
|
|
|
|
|
|
|
public Maybe<List<Long>> createPlaylist(final String name, final List<StreamEntity> streams) {
|
2018-01-22 04:32:49 +01:00
|
|
|
// Disallow creation of empty playlists
|
2018-01-15 21:30:52 +01:00
|
|
|
if (streams.isEmpty()) return Maybe.empty();
|
|
|
|
final StreamEntity defaultStream = streams.get(0);
|
2018-01-22 04:32:49 +01:00
|
|
|
final PlaylistEntity newPlaylist =
|
|
|
|
new PlaylistEntity(name, defaultStream.getThumbnailUrl());
|
2018-01-15 21:30:52 +01:00
|
|
|
|
2018-01-22 04:32:49 +01:00
|
|
|
return Maybe.fromCallable(() -> database.runInTransaction(() ->
|
|
|
|
upsertStreams(playlistTable.insert(newPlaylist), streams, 0))
|
|
|
|
).subscribeOn(Schedulers.io());
|
|
|
|
}
|
2018-01-15 21:30:52 +01:00
|
|
|
|
2018-01-22 04:32:49 +01:00
|
|
|
public Maybe<List<Long>> appendToPlaylist(final long playlistId,
|
|
|
|
final List<StreamEntity> streams) {
|
|
|
|
return playlistStreamTable.getMaximumIndexOf(playlistId)
|
|
|
|
.firstElement()
|
|
|
|
.map(maxJoinIndex -> database.runInTransaction(() ->
|
|
|
|
upsertStreams(playlistId, streams, maxJoinIndex + 1))
|
|
|
|
).subscribeOn(Schedulers.io());
|
2018-01-15 21:30:52 +01:00
|
|
|
}
|
|
|
|
|
2018-01-22 04:32:49 +01:00
|
|
|
private List<Long> upsertStreams(final long playlistId,
|
|
|
|
final List<StreamEntity> streams,
|
|
|
|
final int indexOffset) {
|
2018-01-15 21:30:52 +01:00
|
|
|
|
2018-01-22 04:32:49 +01:00
|
|
|
List<PlaylistStreamEntity> joinEntities = new ArrayList<>(streams.size());
|
|
|
|
for (int index = 0; index < streams.size(); index++) {
|
|
|
|
// Upsert streams and get their ids
|
|
|
|
final long streamId = streamTable.upsert(streams.get(index));
|
|
|
|
joinEntities.add(new PlaylistStreamEntity(playlistId, streamId,
|
|
|
|
index + indexOffset));
|
|
|
|
}
|
|
|
|
return playlistStreamTable.insertAll(joinEntities);
|
2018-01-15 21:30:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public Completable updateJoin(final long playlistId, final List<Long> streamIds) {
|
|
|
|
List<PlaylistStreamEntity> joinEntities = new ArrayList<>(streamIds.size());
|
|
|
|
for (int i = 0; i < streamIds.size(); i++) {
|
|
|
|
joinEntities.add(new PlaylistStreamEntity(playlistId, streamIds.get(i), i));
|
|
|
|
}
|
|
|
|
|
|
|
|
return Completable.fromRunnable(() -> database.runInTransaction(() -> {
|
|
|
|
playlistStreamTable.deleteBatch(playlistId);
|
|
|
|
playlistStreamTable.insertAll(joinEntities);
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2018-01-17 06:12:03 +01:00
|
|
|
public Flowable<List<PlaylistMetadataEntry>> getPlaylists() {
|
|
|
|
return playlistStreamTable.getPlaylistMetadata().subscribeOn(Schedulers.io());
|
|
|
|
}
|
|
|
|
|
|
|
|
public Flowable<List<StreamEntity>> getPlaylist(final long playlistId) {
|
|
|
|
return playlistStreamTable.getOrderedStreamsOf(playlistId).subscribeOn(Schedulers.io());
|
|
|
|
}
|
|
|
|
|
|
|
|
public Single<Integer> deletePlaylist(final long playlistId) {
|
|
|
|
return Single.fromCallable(() -> playlistTable.deletePlaylist(playlistId))
|
2018-01-16 20:48:52 +01:00
|
|
|
.subscribeOn(Schedulers.io());
|
2018-01-15 21:30:52 +01:00
|
|
|
}
|
|
|
|
}
|