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