From 8627efd0a1dd3e8f3afc4e7ade8bc7f7241f3fd5 Mon Sep 17 00:00:00 2001 From: ThetaDev Date: Sun, 23 Oct 2022 11:28:34 +0200 Subject: [PATCH] fix: get notified menu option on all tabs --- .../list/channel/ChannelFragment.java | 91 ++++++++++++++++++ .../list/channel/ChannelTabFragment.java | 8 ++ .../list/channel/ChannelVideosFragment.java | 95 ++++--------------- app/src/main/res/menu/menu_channel_videos.xml | 14 --- 4 files changed, 118 insertions(+), 90 deletions(-) delete mode 100644 app/src/main/res/menu/menu_channel_videos.xml 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 6989552f2..4938d1c00 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 @@ -1,5 +1,6 @@ package org.schabi.newpipe.fragments.list.channel; +import android.content.Context; import android.os.Bundle; import android.text.TextUtils; import android.util.Log; @@ -14,6 +15,8 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; import org.schabi.newpipe.R; +import org.schabi.newpipe.database.subscription.NotificationMode; +import org.schabi.newpipe.database.subscription.SubscriptionEntity; import org.schabi.newpipe.databinding.FragmentChannelBinding; import org.schabi.newpipe.error.ErrorInfo; import org.schabi.newpipe.error.UserAction; @@ -21,14 +24,21 @@ import org.schabi.newpipe.extractor.channel.ChannelInfo; import org.schabi.newpipe.extractor.linkhandler.ChannelTabHandler; import org.schabi.newpipe.fragments.BaseStateFragment; import org.schabi.newpipe.fragments.detail.TabAdapter; +import org.schabi.newpipe.local.feed.notifications.NotificationHelper; +import org.schabi.newpipe.local.subscription.SubscriptionManager; import org.schabi.newpipe.util.Constants; import org.schabi.newpipe.util.ExtractorHelper; import org.schabi.newpipe.util.NavigationHelper; import org.schabi.newpipe.util.external_communication.ShareUtils; +import java.util.List; + import icepick.State; import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers; +import io.reactivex.rxjava3.core.Observable; +import io.reactivex.rxjava3.disposables.CompositeDisposable; import io.reactivex.rxjava3.disposables.Disposable; +import io.reactivex.rxjava3.functions.Consumer; import io.reactivex.rxjava3.schedulers.Schedulers; public class ChannelFragment extends BaseStateFragment { @@ -41,8 +51,12 @@ public class ChannelFragment extends BaseStateFragment { private ChannelInfo currentInfo; private Disposable currentWorker; + private Disposable subscriptionMonitor; + private final CompositeDisposable disposables = new CompositeDisposable(); + private SubscriptionManager subscriptionManager; private MenuItem menuRssButton; + private MenuItem menuNotifyButton; /*////////////////////////////////////////////////////////////////////////// // Views @@ -78,6 +92,12 @@ public class ChannelFragment extends BaseStateFragment { setHasOptionsMenu(true); } + @Override + public void onAttach(@NonNull Context context) { + super.onAttach(context); + subscriptionManager = new SubscriptionManager(activity); + } + @Override public View onCreateView(@NonNull final LayoutInflater inflater, @Nullable final ViewGroup container, @@ -98,6 +118,13 @@ public class ChannelFragment extends BaseStateFragment { @Override public void onDestroy() { super.onDestroy(); + if (currentWorker != null) { + currentWorker.dispose(); + } + if (subscriptionMonitor != null) { + subscriptionMonitor.dispose(); + } + disposables.clear(); binding = null; } @@ -116,12 +143,19 @@ public class ChannelFragment extends BaseStateFragment { + "menu = [" + menu + "], inflater = [" + inflater + "]"); } menuRssButton = menu.findItem(R.id.menu_item_rss); + menuNotifyButton = menu.findItem(R.id.menu_item_notify); updateRssButton(); + monitorSubscription(); } @Override public boolean onOptionsItemSelected(final MenuItem item) { switch (item.getItemId()) { + case R.id.menu_item_notify: + final boolean value = !item.isChecked(); + item.setEnabled(false); + setNotify(value); + break; case R.id.action_settings: NavigationHelper.openSettings(requireContext()); break; @@ -153,6 +187,62 @@ public class ChannelFragment extends BaseStateFragment { } } + private void monitorSubscription() { + if (currentInfo != null) { + final Observable> observable = subscriptionManager + .subscriptionTable() + .getSubscriptionFlowable(currentInfo.getServiceId(), currentInfo.getUrl()) + .toObservable(); + + if (subscriptionMonitor != null) { + subscriptionMonitor.dispose(); + } + subscriptionMonitor = observable + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(getSubscribeUpdateMonitor()); + } + } + + private Consumer> getSubscribeUpdateMonitor() { + return (List subscriptionEntities) -> { + if (subscriptionEntities.isEmpty()) { + updateNotifyButton(null); + } else { + final SubscriptionEntity subscription = subscriptionEntities.get(0); + updateNotifyButton(subscription); + } + }; + } + + private void updateNotifyButton(@Nullable final SubscriptionEntity subscription) { + if (menuNotifyButton == null) { + return; + } + if (subscription != null) { + menuNotifyButton.setEnabled( + NotificationHelper.areNewStreamsNotificationsEnabled(requireContext()) + ); + menuNotifyButton.setChecked( + subscription.getNotificationMode() == NotificationMode.ENABLED + ); + } + + menuNotifyButton.setVisible(subscription != null); + } + + private void setNotify(final boolean isEnabled) { + disposables.add( + subscriptionManager + .updateNotificationMode( + currentInfo.getServiceId(), + currentInfo.getUrl(), + isEnabled ? NotificationMode.ENABLED : NotificationMode.DISABLED) + .subscribeOn(Schedulers.io()) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe() + ); + } + /*////////////////////////////////////////////////////////////////////////// // Init //////////////////////////////////////////////////////////////////////////*/ @@ -213,5 +303,6 @@ public class ChannelFragment extends BaseStateFragment { setInitialData(info.getServiceId(), info.getOriginalUrl(), info.getName()); updateTabs(); updateRssButton(); + monitorSubscription(); } } diff --git a/app/src/main/java/org/schabi/newpipe/fragments/list/channel/ChannelTabFragment.java b/app/src/main/java/org/schabi/newpipe/fragments/list/channel/ChannelTabFragment.java index 12514a55c..21613d717 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/list/channel/ChannelTabFragment.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/list/channel/ChannelTabFragment.java @@ -2,6 +2,8 @@ package org.schabi.newpipe.fragments.list.channel; import android.os.Bundle; import android.view.LayoutInflater; +import android.view.Menu; +import android.view.MenuInflater; import android.view.View; import android.view.ViewGroup; @@ -45,6 +47,12 @@ public class ChannelTabFragment extends BaseListInfoFragment - - -