diff --git a/app/src/main/java/org/schabi/newpipe/fragments/list/channel/ChannelFragment.java b/app/src/main/java/org/schabi/newpipe/fragments/list/channel/ChannelFragment.java index 752b05d5a..5e2e24fab 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/list/channel/ChannelFragment.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/list/channel/ChannelFragment.java @@ -259,7 +259,7 @@ public class ChannelFragment extends BaseListInfoFragment .map(List::isEmpty) .distinctUntilChanged() .skip(1) // channel has just been opened - .filter(x -> NotificationHelper.isNewStreamsNotificationsEnabled(requireContext())) + .filter(x -> NotificationHelper.areNewStreamsNotificationsEnabled(requireContext())) .observeOn(AndroidSchedulers.mainThread()) .subscribe(isEmpty -> { if (!isEmpty) { @@ -402,13 +402,13 @@ public class ChannelFragment extends BaseListInfoFragment } if (subscription != null) { menuNotifyButton.setEnabled( - NotificationHelper.isNewStreamsNotificationsEnabled(requireContext()) + NotificationHelper.areNewStreamsNotificationsEnabled(requireContext()) ); menuNotifyButton.setChecked( subscription.getNotificationMode() == NotificationMode.ENABLED ); } - + menuNotifyButton.setVisible(subscription != null); } @@ -423,6 +423,9 @@ public class ChannelFragment extends BaseListInfoFragment ); } + /** + * Show a snackbar with the option to enable notifications on new streams for this channel. + */ private void showNotifySnackbar() { Snackbar.make(itemsList, R.string.you_successfully_subscribed, Snackbar.LENGTH_LONG) .setAction(R.string.get_notified, v -> setNotify(true)) diff --git a/app/src/main/java/org/schabi/newpipe/local/feed/notifications/NotificationHelper.kt b/app/src/main/java/org/schabi/newpipe/local/feed/notifications/NotificationHelper.kt index ec5cb790f..fac3b8f72 100644 --- a/app/src/main/java/org/schabi/newpipe/local/feed/notifications/NotificationHelper.kt +++ b/app/src/main/java/org/schabi/newpipe/local/feed/notifications/NotificationHelper.kt @@ -21,13 +21,20 @@ import org.schabi.newpipe.extractor.stream.StreamInfoItem import org.schabi.newpipe.local.feed.service.FeedUpdateInfo import org.schabi.newpipe.util.NavigationHelper +/** + * Helper for everything related to show notifications about new streams to the user. + */ class NotificationHelper(val context: Context) { private val manager = context.getSystemService( Context.NOTIFICATION_SERVICE ) 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 = data.newStreams val summary = context.resources.getQuantityString( R.plurals.new_streams, newStreams.size, newStreams.size @@ -69,11 +76,14 @@ class NotificationHelper(val context: Context) { style.setSummaryText(summary) style.setBigContentTitle(data.name) builder.setStyle(style) + // open the channel page when clicking on the notification builder.setContentIntent( PendingIntent.getActivity( context, 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), 0 ) @@ -110,7 +120,7 @@ class NotificationHelper(val context: Context) { } @JvmStatic - fun isNewStreamsNotificationsEnabled(context: Context): Boolean { + fun areNewStreamsNotificationsEnabled(context: Context): Boolean { return ( PreferenceManager.getDefaultSharedPreferences(context) .getBoolean(context.getString(R.string.enable_streams_notifications), false) && diff --git a/app/src/main/java/org/schabi/newpipe/local/feed/notifications/NotificationWorker.kt b/app/src/main/java/org/schabi/newpipe/local/feed/notifications/NotificationWorker.kt index 82e923d94..daae52fdd 100644 --- a/app/src/main/java/org/schabi/newpipe/local/feed/notifications/NotificationWorker.kt +++ b/app/src/main/java/org/schabi/newpipe/local/feed/notifications/NotificationWorker.kt @@ -21,6 +21,10 @@ import org.schabi.newpipe.local.feed.service.FeedLoadManager import org.schabi.newpipe.local.feed.service.FeedLoadService 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( appContext: Context, workerParams: WorkerParameters, @@ -43,7 +47,7 @@ class NotificationWorker( } .doOnSubscribe { setForegroundAsync(createForegroundInfo()) } .flatMapObservable { Observable.fromIterable(it) } - .flatMapCompletable { x -> notificationHelper.notify(x) } + .flatMapCompletable { x -> notificationHelper.displayNewStreamsNotification(x) } .toSingleDefault(Result.success()) .onErrorReturnItem(Result.failure()) } else Single.just(Result.success()) diff --git a/app/src/main/java/org/schabi/newpipe/local/feed/notifications/ScheduleOptions.kt b/app/src/main/java/org/schabi/newpipe/local/feed/notifications/ScheduleOptions.kt index 30e8d5515..0dbc15395 100644 --- a/app/src/main/java/org/schabi/newpipe/local/feed/notifications/ScheduleOptions.kt +++ b/app/src/main/java/org/schabi/newpipe/local/feed/notifications/ScheduleOptions.kt @@ -5,6 +5,10 @@ import androidx.preference.PreferenceManager import org.schabi.newpipe.R import java.util.concurrent.TimeUnit +/** + * Information for the Scheduler which checks for new streams. + * See [NotificationWorker] + */ data class ScheduleOptions( val interval: Long, val isRequireNonMeteredNetwork: Boolean