|
| 1 | +package org.schabi.newpipe.local.playlist |
| 2 | + |
| 3 | +import io.reactivex.rxjava3.core.Completable |
| 4 | +import io.reactivex.rxjava3.core.Flowable |
| 5 | +import io.reactivex.rxjava3.core.Single |
| 6 | +import io.reactivex.rxjava3.schedulers.Schedulers |
| 7 | +import org.schabi.newpipe.database.AppDatabase |
| 8 | +import org.schabi.newpipe.database.playlist.model.PlaylistRemoteEntity |
| 9 | +import org.schabi.newpipe.extractor.playlist.PlaylistInfo |
| 10 | + |
| 11 | +class RemotePlaylistManager(private val database: AppDatabase) { |
| 12 | + private val playlistRemoteTable = database.playlistRemoteDAO() |
| 13 | + |
| 14 | + val playlists: Flowable<MutableList<PlaylistRemoteEntity>> |
| 15 | + get() = playlistRemoteTable.playlists.subscribeOn(Schedulers.io()) |
| 16 | + |
| 17 | + fun getPlaylist(playlistId: Long): Flowable<PlaylistRemoteEntity> { |
| 18 | + return playlistRemoteTable.getPlaylist(playlistId).subscribeOn(Schedulers.io()) |
| 19 | + } |
| 20 | + |
| 21 | + fun getPlaylist(info: PlaylistInfo): Flowable<MutableList<PlaylistRemoteEntity>> { |
| 22 | + return playlistRemoteTable.getPlaylist(info.serviceId.toLong(), info.url) |
| 23 | + .subscribeOn(Schedulers.io()) |
| 24 | + } |
| 25 | + |
| 26 | + fun deletePlaylist(playlistId: Long): Single<Int> { |
| 27 | + return Single.fromCallable { playlistRemoteTable.deletePlaylist(playlistId) } |
| 28 | + .subscribeOn(Schedulers.io()) |
| 29 | + } |
| 30 | + |
| 31 | + fun updatePlaylists( |
| 32 | + updateItems: List<PlaylistRemoteEntity>, |
| 33 | + deletedItems: List<Long> |
| 34 | + ): Completable { |
| 35 | + return Completable.fromRunnable { |
| 36 | + database.runInTransaction { |
| 37 | + for (uid in deletedItems) { |
| 38 | + playlistRemoteTable.deletePlaylist(uid) |
| 39 | + } |
| 40 | + for (item in updateItems) { |
| 41 | + playlistRemoteTable.upsert(item) |
| 42 | + } |
| 43 | + } |
| 44 | + }.subscribeOn(Schedulers.io()) |
| 45 | + } |
| 46 | + |
| 47 | + fun onBookmark(playlistInfo: PlaylistInfo): Single<Long> { |
| 48 | + return Single.fromCallable { |
| 49 | + val playlist = PlaylistRemoteEntity(playlistInfo) |
| 50 | + playlistRemoteTable.upsert(playlist) |
| 51 | + }.subscribeOn(Schedulers.io()) |
| 52 | + } |
| 53 | + |
| 54 | + fun onUpdate(playlistId: Long, playlistInfo: PlaylistInfo): Single<Int> { |
| 55 | + return Single.fromCallable { |
| 56 | + val playlist = PlaylistRemoteEntity(playlistInfo) |
| 57 | + playlist.uid = playlistId |
| 58 | + playlistRemoteTable.update(playlist) |
| 59 | + }.subscribeOn(Schedulers.io()) |
| 60 | + } |
| 61 | +} |
0 commit comments