Add a few comments and rename a few methods

This commit is contained in:
TobiGr 2021-10-15 20:57:49 +02:00
parent 4f7cdcce55
commit 793ff1a728
4 changed files with 28 additions and 7 deletions

View File

@ -259,7 +259,7 @@ public class ChannelFragment extends BaseListInfoFragment<ChannelInfo>
.map(List::isEmpty) .map(List::isEmpty)
.distinctUntilChanged() .distinctUntilChanged()
.skip(1) // channel has just been opened .skip(1) // channel has just been opened
.filter(x -> NotificationHelper.isNewStreamsNotificationsEnabled(requireContext())) .filter(x -> NotificationHelper.areNewStreamsNotificationsEnabled(requireContext()))
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe(isEmpty -> { .subscribe(isEmpty -> {
if (!isEmpty) { if (!isEmpty) {
@ -402,13 +402,13 @@ public class ChannelFragment extends BaseListInfoFragment<ChannelInfo>
} }
if (subscription != null) { if (subscription != null) {
menuNotifyButton.setEnabled( menuNotifyButton.setEnabled(
NotificationHelper.isNewStreamsNotificationsEnabled(requireContext()) NotificationHelper.areNewStreamsNotificationsEnabled(requireContext())
); );
menuNotifyButton.setChecked( menuNotifyButton.setChecked(
subscription.getNotificationMode() == NotificationMode.ENABLED subscription.getNotificationMode() == NotificationMode.ENABLED
); );
} }
menuNotifyButton.setVisible(subscription != null); menuNotifyButton.setVisible(subscription != null);
} }
@ -423,6 +423,9 @@ public class ChannelFragment extends BaseListInfoFragment<ChannelInfo>
); );
} }
/**
* Show a snackbar with the option to enable notifications on new streams for this channel.
*/
private void showNotifySnackbar() { private void showNotifySnackbar() {
Snackbar.make(itemsList, R.string.you_successfully_subscribed, Snackbar.LENGTH_LONG) Snackbar.make(itemsList, R.string.you_successfully_subscribed, Snackbar.LENGTH_LONG)
.setAction(R.string.get_notified, v -> setNotify(true)) .setAction(R.string.get_notified, v -> setNotify(true))

View File

@ -21,13 +21,20 @@ import org.schabi.newpipe.extractor.stream.StreamInfoItem
import org.schabi.newpipe.local.feed.service.FeedUpdateInfo import org.schabi.newpipe.local.feed.service.FeedUpdateInfo
import org.schabi.newpipe.util.NavigationHelper import org.schabi.newpipe.util.NavigationHelper
/**
* Helper for everything related to show notifications about new streams to the user.
*/
class NotificationHelper(val context: Context) { class NotificationHelper(val context: Context) {
private val manager = context.getSystemService( private val manager = context.getSystemService(
Context.NOTIFICATION_SERVICE Context.NOTIFICATION_SERVICE
) as NotificationManager ) as NotificationManager
fun notify(data: FeedUpdateInfo): Completable { /**
* Show a notification about new streams from a single channel.
* Opening the notification will open the corresponding channel page.
*/
fun displayNewStreamsNotification(data: FeedUpdateInfo): Completable {
val newStreams: List<StreamInfoItem> = data.newStreams val newStreams: List<StreamInfoItem> = data.newStreams
val summary = context.resources.getQuantityString( val summary = context.resources.getQuantityString(
R.plurals.new_streams, newStreams.size, newStreams.size R.plurals.new_streams, newStreams.size, newStreams.size
@ -69,11 +76,14 @@ class NotificationHelper(val context: Context) {
style.setSummaryText(summary) style.setSummaryText(summary)
style.setBigContentTitle(data.name) style.setBigContentTitle(data.name)
builder.setStyle(style) builder.setStyle(style)
// open the channel page when clicking on the notification
builder.setContentIntent( builder.setContentIntent(
PendingIntent.getActivity( PendingIntent.getActivity(
context, context,
data.pseudoId, data.pseudoId,
NavigationHelper.getChannelIntent(context, data.listInfo.serviceId, data.listInfo.url) NavigationHelper.getChannelIntent(
context, data.listInfo.serviceId, data.listInfo.url
)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK), .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK),
0 0
) )
@ -110,7 +120,7 @@ class NotificationHelper(val context: Context) {
} }
@JvmStatic @JvmStatic
fun isNewStreamsNotificationsEnabled(context: Context): Boolean { fun areNewStreamsNotificationsEnabled(context: Context): Boolean {
return ( return (
PreferenceManager.getDefaultSharedPreferences(context) PreferenceManager.getDefaultSharedPreferences(context)
.getBoolean(context.getString(R.string.enable_streams_notifications), false) && .getBoolean(context.getString(R.string.enable_streams_notifications), false) &&

View File

@ -21,6 +21,10 @@ 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
/*
* Worker which checks for new streams of subscribed channels
* in intervals which can be set by the user in the settings.
*/
class NotificationWorker( class NotificationWorker(
appContext: Context, appContext: Context,
workerParams: WorkerParameters, workerParams: WorkerParameters,
@ -43,7 +47,7 @@ class NotificationWorker(
} }
.doOnSubscribe { setForegroundAsync(createForegroundInfo()) } .doOnSubscribe { setForegroundAsync(createForegroundInfo()) }
.flatMapObservable { Observable.fromIterable(it) } .flatMapObservable { Observable.fromIterable(it) }
.flatMapCompletable { x -> notificationHelper.notify(x) } .flatMapCompletable { x -> notificationHelper.displayNewStreamsNotification(x) }
.toSingleDefault(Result.success()) .toSingleDefault(Result.success())
.onErrorReturnItem(Result.failure()) .onErrorReturnItem(Result.failure())
} else Single.just(Result.success()) } else Single.just(Result.success())

View File

@ -5,6 +5,10 @@ import androidx.preference.PreferenceManager
import org.schabi.newpipe.R import org.schabi.newpipe.R
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
/**
* Information for the Scheduler which checks for new streams.
* See [NotificationWorker]
*/
data class ScheduleOptions( data class ScheduleOptions(
val interval: Long, val interval: Long,
val isRequireNonMeteredNetwork: Boolean val isRequireNonMeteredNetwork: Boolean