Add a few comments and rename a few methods
This commit is contained in:
parent
4f7cdcce55
commit
793ff1a728
4 changed files with 28 additions and 7 deletions
|
@ -259,7 +259,7 @@ public class ChannelFragment extends BaseListInfoFragment<ChannelInfo>
|
|||
.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,7 +402,7 @@ public class ChannelFragment extends BaseListInfoFragment<ChannelInfo>
|
|||
}
|
||||
if (subscription != null) {
|
||||
menuNotifyButton.setEnabled(
|
||||
NotificationHelper.isNewStreamsNotificationsEnabled(requireContext())
|
||||
NotificationHelper.areNewStreamsNotificationsEnabled(requireContext())
|
||||
);
|
||||
menuNotifyButton.setChecked(
|
||||
subscription.getNotificationMode() == NotificationMode.ENABLED
|
||||
|
@ -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() {
|
||||
Snackbar.make(itemsList, R.string.you_successfully_subscribed, Snackbar.LENGTH_LONG)
|
||||
.setAction(R.string.get_notified, v -> setNotify(true))
|
||||
|
|
|
@ -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<StreamInfoItem> = 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) &&
|
||||
|
|
|
@ -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())
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue