Only check for new streams of subscriptions with enabled notifications automatically

This commit is contained in:
TobiGr 2021-11-21 22:53:10 +01:00
parent 892a1df280
commit 8ce996e065
4 changed files with 43 additions and 7 deletions

View file

@ -12,6 +12,7 @@ import org.schabi.newpipe.database.feed.model.FeedEntity
import org.schabi.newpipe.database.feed.model.FeedLastUpdatedEntity import org.schabi.newpipe.database.feed.model.FeedLastUpdatedEntity
import org.schabi.newpipe.database.stream.StreamWithState import org.schabi.newpipe.database.stream.StreamWithState
import org.schabi.newpipe.database.stream.model.StreamStateEntity import org.schabi.newpipe.database.stream.model.StreamStateEntity
import org.schabi.newpipe.database.subscription.NotificationMode
import org.schabi.newpipe.database.subscription.SubscriptionEntity import org.schabi.newpipe.database.subscription.SubscriptionEntity
import java.time.OffsetDateTime import java.time.OffsetDateTime
@ -252,4 +253,21 @@ abstract class FeedDAO {
""" """
) )
abstract fun getAllOutdatedForGroup(groupId: Long, outdatedThreshold: OffsetDateTime): Flowable<List<SubscriptionEntity>> abstract fun getAllOutdatedForGroup(groupId: Long, outdatedThreshold: OffsetDateTime): Flowable<List<SubscriptionEntity>>
@Query(
"""
SELECT s.* FROM subscriptions s
LEFT JOIN feed_last_updated lu
ON s.uid = lu.subscription_id
WHERE
(lu.last_updated IS NULL OR lu.last_updated < :outdatedThreshold)
AND s.notification_mode = :notificationMode
"""
)
abstract fun getOutdatedWithNotificationMode(
outdatedThreshold: OffsetDateTime,
@NotificationMode notificationMode: Int
): Flowable<List<SubscriptionEntity>>
} }

View file

@ -14,6 +14,7 @@ import org.schabi.newpipe.database.feed.model.FeedGroupEntity
import org.schabi.newpipe.database.feed.model.FeedLastUpdatedEntity import org.schabi.newpipe.database.feed.model.FeedLastUpdatedEntity
import org.schabi.newpipe.database.stream.StreamWithState import org.schabi.newpipe.database.stream.StreamWithState
import org.schabi.newpipe.database.stream.model.StreamEntity import org.schabi.newpipe.database.stream.model.StreamEntity
import org.schabi.newpipe.database.subscription.NotificationMode
import org.schabi.newpipe.extractor.stream.StreamInfoItem import org.schabi.newpipe.extractor.stream.StreamInfoItem
import org.schabi.newpipe.extractor.stream.StreamType import org.schabi.newpipe.extractor.stream.StreamType
import org.schabi.newpipe.local.subscription.FeedGroupIcon import org.schabi.newpipe.local.subscription.FeedGroupIcon
@ -57,6 +58,11 @@ class FeedDatabaseManager(context: Context) {
fun outdatedSubscriptions(outdatedThreshold: OffsetDateTime) = feedTable.getAllOutdated(outdatedThreshold) fun outdatedSubscriptions(outdatedThreshold: OffsetDateTime) = feedTable.getAllOutdated(outdatedThreshold)
fun outdatedSubscriptionsWithNotificationMode(
outdatedThreshold: OffsetDateTime,
@NotificationMode notificationMode: Int
) = feedTable.getOutdatedWithNotificationMode(outdatedThreshold, notificationMode)
fun notLoadedCount(groupId: Long = FeedGroupEntity.GROUP_ALL_ID): Flowable<Long> { fun notLoadedCount(groupId: Long = FeedGroupEntity.GROUP_ALL_ID): Flowable<Long> {
return when (groupId) { return when (groupId) {
FeedGroupEntity.GROUP_ALL_ID -> feedTable.notLoadedCount() FeedGroupEntity.GROUP_ALL_ID -> feedTable.notLoadedCount()

View file

@ -16,7 +16,6 @@ import androidx.work.rxjava3.RxWorker
import io.reactivex.rxjava3.core.Observable import io.reactivex.rxjava3.core.Observable
import io.reactivex.rxjava3.core.Single import io.reactivex.rxjava3.core.Single
import org.schabi.newpipe.R import org.schabi.newpipe.R
import org.schabi.newpipe.database.subscription.NotificationMode
import org.schabi.newpipe.local.feed.service.FeedLoadManager import org.schabi.newpipe.local.feed.service.FeedLoadManager
import org.schabi.newpipe.local.feed.service.FeedLoadService import org.schabi.newpipe.local.feed.service.FeedLoadService
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
@ -36,12 +35,14 @@ class NotificationWorker(
private val feedLoadManager = FeedLoadManager(appContext) private val feedLoadManager = FeedLoadManager(appContext)
override fun createWork(): Single<Result> = if (isEnabled(applicationContext)) { override fun createWork(): Single<Result> = if (isEnabled(applicationContext)) {
feedLoadManager.startLoading(ignoreOutdatedThreshold = true) feedLoadManager.startLoading(
ignoreOutdatedThreshold = true,
groupId = FeedLoadManager.GROUP_NOTIFICATION_ENABLED
)
.map { feed -> .map { feed ->
feed.mapNotNull { x -> feed.mapNotNull { x ->
x.value?.takeIf { x.value?.takeIf {
it.notificationMode == NotificationMode.ENABLED && it.newStreamsCount > 0
it.newStreamsCount > 0
} }
} }
} }

View file

@ -12,6 +12,7 @@ import io.reactivex.rxjava3.processors.PublishProcessor
import io.reactivex.rxjava3.schedulers.Schedulers import io.reactivex.rxjava3.schedulers.Schedulers
import org.schabi.newpipe.R import org.schabi.newpipe.R
import org.schabi.newpipe.database.feed.model.FeedGroupEntity import org.schabi.newpipe.database.feed.model.FeedGroupEntity
import org.schabi.newpipe.database.subscription.NotificationMode
import org.schabi.newpipe.extractor.ListInfo import org.schabi.newpipe.extractor.ListInfo
import org.schabi.newpipe.extractor.stream.StreamInfoItem import org.schabi.newpipe.extractor.stream.StreamInfoItem
import org.schabi.newpipe.local.feed.FeedDatabaseManager import org.schabi.newpipe.local.feed.FeedDatabaseManager
@ -41,6 +42,8 @@ class FeedLoadManager(private val context: Context) {
* Start checking for new streams of a subscription group. * Start checking for new streams of a subscription group.
* @param groupId The ID of the subscription group to load. * @param groupId The ID of the subscription group to load.
* When using [FeedGroupEntity.GROUP_ALL_ID], all subscriptions are loaded. * When using [FeedGroupEntity.GROUP_ALL_ID], all subscriptions are loaded.
* When using [GROUP_NOTIFICATION_ENABLED], only subscriptions with enabled notifications
* for new streams are loaded.
* @param ignoreOutdatedThreshold When `false`, only subscriptions which have not been updated * @param ignoreOutdatedThreshold When `false`, only subscriptions which have not been updated
* within the `feed_update_threshold` are checked for updates. * within the `feed_update_threshold` are checked for updates.
* This threshold can be set by the user in the app settings. * This threshold can be set by the user in the app settings.
@ -73,6 +76,9 @@ class FeedLoadManager(private val context: Context) {
*/ */
val outdatedSubscriptions = when (groupId) { val outdatedSubscriptions = when (groupId) {
FeedGroupEntity.GROUP_ALL_ID -> feedDatabaseManager.outdatedSubscriptions(outdatedThreshold) FeedGroupEntity.GROUP_ALL_ID -> feedDatabaseManager.outdatedSubscriptions(outdatedThreshold)
GROUP_NOTIFICATION_ENABLED -> feedDatabaseManager.outdatedSubscriptionsWithNotificationMode(
outdatedThreshold, NotificationMode.ENABLED
)
else -> feedDatabaseManager.outdatedSubscriptionsForGroup(groupId, outdatedThreshold) else -> feedDatabaseManager.outdatedSubscriptionsForGroup(groupId, outdatedThreshold)
} }
@ -248,16 +254,21 @@ class FeedLoadManager(private val context: Context) {
} }
} }
private companion object { companion object {
/**
*
*/
const val GROUP_NOTIFICATION_ENABLED = -2L
/** /**
* How many extractions will be running in parallel. * How many extractions will be running in parallel.
*/ */
const val PARALLEL_EXTRACTIONS = 6 private const val PARALLEL_EXTRACTIONS = 6
/** /**
* Number of items to buffer to mass-insert in the database. * Number of items to buffer to mass-insert in the database.
*/ */
const val BUFFER_COUNT_BEFORE_INSERT = 20 private const val BUFFER_COUNT_BEFORE_INSERT = 20
} }
} }