2019-04-28 22:43:54 +02:00
|
|
|
package org.schabi.newpipe.local.subscription
|
|
|
|
|
|
|
|
import android.content.Context
|
2020-10-31 21:55:45 +01:00
|
|
|
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
|
|
|
|
import io.reactivex.rxjava3.core.Completable
|
|
|
|
import io.reactivex.rxjava3.core.Flowable
|
|
|
|
import io.reactivex.rxjava3.schedulers.Schedulers
|
2019-04-28 22:43:54 +02:00
|
|
|
import org.schabi.newpipe.NewPipeDatabase
|
2020-04-09 18:07:02 +02:00
|
|
|
import org.schabi.newpipe.database.feed.model.FeedGroupEntity
|
2019-05-08 19:17:54 +02:00
|
|
|
import org.schabi.newpipe.database.stream.model.StreamEntity
|
|
|
|
import org.schabi.newpipe.database.subscription.NotificationMode
|
2019-04-28 22:43:54 +02:00
|
|
|
import org.schabi.newpipe.database.subscription.SubscriptionDAO
|
|
|
|
import org.schabi.newpipe.database.subscription.SubscriptionEntity
|
2019-12-16 08:36:04 +01:00
|
|
|
import org.schabi.newpipe.extractor.ListInfo
|
2019-04-28 22:43:54 +02:00
|
|
|
import org.schabi.newpipe.extractor.channel.ChannelInfo
|
2019-12-16 08:36:04 +01:00
|
|
|
import org.schabi.newpipe.extractor.feed.FeedInfo
|
|
|
|
import org.schabi.newpipe.extractor.stream.StreamInfoItem
|
2019-04-28 22:43:54 +02:00
|
|
|
import org.schabi.newpipe.local.feed.FeedDatabaseManager
|
2019-05-08 19:17:54 +02:00
|
|
|
import org.schabi.newpipe.util.ExtractorHelper
|
2019-04-28 22:43:54 +02:00
|
|
|
|
|
|
|
class SubscriptionManager(context: Context) {
|
|
|
|
private val database = NewPipeDatabase.getInstance(context)
|
|
|
|
private val subscriptionTable = database.subscriptionDAO()
|
|
|
|
private val feedDatabaseManager = FeedDatabaseManager(context)
|
|
|
|
|
|
|
|
fun subscriptionTable(): SubscriptionDAO = subscriptionTable
|
|
|
|
fun subscriptions() = subscriptionTable.all
|
|
|
|
|
2020-04-09 18:07:02 +02:00
|
|
|
fun getSubscriptions(
|
|
|
|
currentGroupId: Long = FeedGroupEntity.GROUP_ALL_ID,
|
|
|
|
filterQuery: String = "",
|
|
|
|
showOnlyUngrouped: Boolean = false
|
|
|
|
): Flowable<List<SubscriptionEntity>> {
|
|
|
|
return when {
|
|
|
|
filterQuery.isNotEmpty() -> {
|
|
|
|
return if (showOnlyUngrouped) {
|
|
|
|
subscriptionTable.getSubscriptionsOnlyUngroupedFiltered(
|
2020-10-31 21:55:45 +01:00
|
|
|
currentGroupId, filterQuery
|
|
|
|
)
|
2020-04-09 18:07:02 +02:00
|
|
|
} else {
|
|
|
|
subscriptionTable.getSubscriptionsFiltered(filterQuery)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
showOnlyUngrouped -> subscriptionTable.getSubscriptionsOnlyUngrouped(currentGroupId)
|
|
|
|
else -> subscriptionTable.all
|
|
|
|
}
|
|
|
|
}
|
2020-04-05 21:25:44 +02:00
|
|
|
|
2019-04-28 22:43:54 +02:00
|
|
|
fun upsertAll(infoList: List<ChannelInfo>): List<SubscriptionEntity> {
|
|
|
|
val listEntities = subscriptionTable.upsertAll(
|
2020-10-31 21:55:45 +01:00
|
|
|
infoList.map { SubscriptionEntity.from(it) }
|
|
|
|
)
|
2019-04-28 22:43:54 +02:00
|
|
|
|
|
|
|
database.runInTransaction {
|
|
|
|
infoList.forEachIndexed { index, info ->
|
|
|
|
feedDatabaseManager.upsertAll(listEntities[index].uid, info.relatedItems)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return listEntities
|
|
|
|
}
|
|
|
|
|
|
|
|
fun updateChannelInfo(info: ChannelInfo): Completable = subscriptionTable.getSubscription(info.serviceId, info.url)
|
2020-04-09 18:07:02 +02:00
|
|
|
.flatMapCompletable {
|
|
|
|
Completable.fromRunnable {
|
|
|
|
it.setData(info.name, info.avatarUrl, info.description, info.subscriberCount)
|
|
|
|
subscriptionTable.update(it)
|
|
|
|
feedDatabaseManager.upsertAll(it.uid, info.relatedItems)
|
2019-04-28 22:43:54 +02:00
|
|
|
}
|
2020-04-09 18:07:02 +02:00
|
|
|
}
|
2019-04-28 22:43:54 +02:00
|
|
|
|
2021-11-05 13:17:33 +01:00
|
|
|
fun updateNotificationMode(serviceId: Int, url: String, @NotificationMode mode: Int): Completable {
|
|
|
|
return subscriptionTable().getSubscription(serviceId, url)
|
2021-05-24 18:32:56 +02:00
|
|
|
.flatMapCompletable { entity: SubscriptionEntity ->
|
|
|
|
Completable.fromAction {
|
|
|
|
entity.notificationMode = mode
|
|
|
|
subscriptionTable().update(entity)
|
2021-12-31 18:34:02 +01:00
|
|
|
}.apply {
|
|
|
|
if (mode != NotificationMode.DISABLED) {
|
|
|
|
// notifications have just been enabled, mark all streams as "old"
|
|
|
|
andThen(rememberAllStreams(entity))
|
|
|
|
}
|
|
|
|
}
|
2021-05-24 18:32:56 +02:00
|
|
|
}
|
2019-05-08 19:17:54 +02:00
|
|
|
}
|
|
|
|
|
2019-12-16 08:36:04 +01:00
|
|
|
fun updateFromInfo(subscriptionId: Long, info: ListInfo<StreamInfoItem>) {
|
2019-04-28 22:43:54 +02:00
|
|
|
val subscriptionEntity = subscriptionTable.getSubscription(subscriptionId)
|
2019-12-16 08:36:04 +01:00
|
|
|
|
|
|
|
if (info is FeedInfo) {
|
|
|
|
subscriptionEntity.name = info.name
|
|
|
|
} else if (info is ChannelInfo) {
|
2022-02-23 19:45:49 +01:00
|
|
|
subscriptionEntity.setData(
|
|
|
|
info.name,
|
|
|
|
info.avatarUrl,
|
|
|
|
info.description,
|
|
|
|
info.subscriberCount
|
|
|
|
)
|
2019-12-16 08:36:04 +01:00
|
|
|
}
|
2019-04-28 22:43:54 +02:00
|
|
|
|
|
|
|
subscriptionTable.update(subscriptionEntity)
|
|
|
|
}
|
|
|
|
|
|
|
|
fun deleteSubscription(serviceId: Int, url: String): Completable {
|
|
|
|
return Completable.fromCallable { subscriptionTable.deleteSubscription(serviceId, url) }
|
2020-04-09 18:07:02 +02:00
|
|
|
.subscribeOn(Schedulers.io())
|
|
|
|
.observeOn(AndroidSchedulers.mainThread())
|
2019-04-28 22:43:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fun insertSubscription(subscriptionEntity: SubscriptionEntity, info: ChannelInfo) {
|
|
|
|
database.runInTransaction {
|
|
|
|
val subscriptionId = subscriptionTable.insert(subscriptionEntity)
|
|
|
|
feedDatabaseManager.upsertAll(subscriptionId, info.relatedItems)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fun deleteSubscription(subscriptionEntity: SubscriptionEntity) {
|
|
|
|
subscriptionTable.delete(subscriptionEntity)
|
|
|
|
}
|
2019-05-08 19:17:54 +02:00
|
|
|
|
2021-12-31 18:34:02 +01:00
|
|
|
/**
|
|
|
|
* Fetches the list of videos for the provided channel and saves them in the database, so that
|
2022-02-23 19:45:49 +01:00
|
|
|
* they will be considered as "old"/"already seen" streams and the user will never be notified
|
2021-12-31 18:34:02 +01:00
|
|
|
* about any one of them.
|
|
|
|
*/
|
|
|
|
private fun rememberAllStreams(subscription: SubscriptionEntity): Completable {
|
2019-05-08 19:17:54 +02:00
|
|
|
return ExtractorHelper.getChannelInfo(subscription.serviceId, subscription.url, false)
|
2021-05-24 18:32:56 +02:00
|
|
|
.map { channel -> channel.relatedItems.map { stream -> StreamEntity(stream) } }
|
|
|
|
.flatMapCompletable { entities ->
|
|
|
|
Completable.fromAction {
|
|
|
|
database.streamDAO().upsertAll(entities)
|
|
|
|
}
|
|
|
|
}.onErrorComplete()
|
2019-05-08 19:17:54 +02:00
|
|
|
}
|
2019-04-28 22:43:54 +02:00
|
|
|
}
|