From 9d249904bdcd944bdae006f9e7d3a7ab6dec1285 Mon Sep 17 00:00:00 2001 From: Koitharu Date: Sat, 4 Sep 2021 15:28:11 +0300 Subject: [PATCH] Toggle all subscriptions notification mode --- .../settings/NotificationsSettingsFragment.kt | 7 +- .../NotificationsChannelsConfigFragment.java | 84 ------------- .../NotificationsChannelsConfigFragment.kt | 112 ++++++++++++++++++ .../NotificationsConfigAdapter.kt | 2 + app/src/main/res/drawable/ic_list_check.xml | 10 ++ .../res/menu/menu_notifications_channels.xml | 10 ++ app/src/main/res/values-ru/strings.xml | 1 + app/src/main/res/values/strings.xml | 1 + 8 files changed, 137 insertions(+), 90 deletions(-) delete mode 100644 app/src/main/java/org/schabi/newpipe/settings/notifications/NotificationsChannelsConfigFragment.java create mode 100644 app/src/main/java/org/schabi/newpipe/settings/notifications/NotificationsChannelsConfigFragment.kt create mode 100644 app/src/main/res/drawable/ic_list_check.xml create mode 100644 app/src/main/res/menu/menu_notifications_channels.xml diff --git a/app/src/main/java/org/schabi/newpipe/settings/NotificationsSettingsFragment.kt b/app/src/main/java/org/schabi/newpipe/settings/NotificationsSettingsFragment.kt index 01a3ca6eb..50fb95450 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/NotificationsSettingsFragment.kt +++ b/app/src/main/java/org/schabi/newpipe/settings/NotificationsSettingsFragment.kt @@ -87,12 +87,7 @@ class NotificationsSettingsFragment : BasePreferenceFragment(), OnSharedPreferen } private fun updateSubscriptions(subscriptions: List) { - var notified = 0 - for (subscription in subscriptions) { - if (subscription.notificationMode != NotificationMode.DISABLED) { - notified++ - } - } + val notified = subscriptions.count { it.notificationMode != NotificationMode.DISABLED } val preference = findPreference(getString(R.string.streams_notifications_channels_key)) if (preference != null) { preference.summary = preference.context.getString( diff --git a/app/src/main/java/org/schabi/newpipe/settings/notifications/NotificationsChannelsConfigFragment.java b/app/src/main/java/org/schabi/newpipe/settings/notifications/NotificationsChannelsConfigFragment.java deleted file mode 100644 index 7aa0826e5..000000000 --- a/app/src/main/java/org/schabi/newpipe/settings/notifications/NotificationsChannelsConfigFragment.java +++ /dev/null @@ -1,84 +0,0 @@ -package org.schabi.newpipe.settings.notifications; - -import android.os.Bundle; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; - -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; -import androidx.fragment.app.Fragment; -import androidx.recyclerview.widget.RecyclerView; - -import org.schabi.newpipe.R; -import org.schabi.newpipe.database.subscription.NotificationMode; -import org.schabi.newpipe.local.subscription.SubscriptionManager; -import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers; -import io.reactivex.rxjava3.disposables.CompositeDisposable; -import io.reactivex.rxjava3.disposables.Disposable; -import io.reactivex.rxjava3.schedulers.Schedulers; - -public final class NotificationsChannelsConfigFragment extends Fragment - implements NotificationsConfigAdapter.ModeToggleListener { - - private NotificationsConfigAdapter adapter; - @Nullable - private Disposable loader = null; - private CompositeDisposable updaters; - - @Override - public void onCreate(@Nullable final Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - adapter = new NotificationsConfigAdapter(this); - updaters = new CompositeDisposable(); - } - - @Nullable - @Override - public View onCreateView(@NonNull final LayoutInflater inflater, - @Nullable final ViewGroup container, - @Nullable final Bundle savedInstanceState) { - return inflater.inflate(R.layout.fragment_channels_notifications, container, false); - } - - @Override - public void onViewCreated(@NonNull final View view, @Nullable final Bundle savedInstanceState) { - super.onViewCreated(view, savedInstanceState); - - final RecyclerView recyclerView = view.findViewById(R.id.recycler_view); - recyclerView.setAdapter(adapter); - } - - @Override - public void onActivityCreated(@Nullable final Bundle savedInstanceState) { - super.onActivityCreated(savedInstanceState); - if (loader != null) { - loader.dispose(); - } - loader = new SubscriptionManager(requireContext()) - .subscriptions() - .observeOn(AndroidSchedulers.mainThread()) - .subscribe(adapter::update); - } - - @Override - public void onDestroy() { - if (loader != null) { - loader.dispose(); - } - updaters.dispose(); - super.onDestroy(); - } - - @Override - public void onModeToggle(final int position, @NotificationMode final int mode) { - final NotificationsConfigAdapter.SubscriptionItem subscription = adapter.getItem(position); - updaters.add( - new SubscriptionManager(requireContext()) - .updateNotificationMode(subscription.getServiceId(), - subscription.getUrl(), mode) - .subscribeOn(Schedulers.io()) - .subscribe() - ); - } -} diff --git a/app/src/main/java/org/schabi/newpipe/settings/notifications/NotificationsChannelsConfigFragment.kt b/app/src/main/java/org/schabi/newpipe/settings/notifications/NotificationsChannelsConfigFragment.kt new file mode 100644 index 000000000..eb94fb843 --- /dev/null +++ b/app/src/main/java/org/schabi/newpipe/settings/notifications/NotificationsChannelsConfigFragment.kt @@ -0,0 +1,112 @@ +package org.schabi.newpipe.settings.notifications + +import android.os.Bundle +import android.view.LayoutInflater +import android.view.Menu +import android.view.MenuInflater +import android.view.MenuItem +import android.view.View +import android.view.ViewGroup +import androidx.fragment.app.Fragment +import androidx.recyclerview.widget.RecyclerView +import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers +import io.reactivex.rxjava3.disposables.CompositeDisposable +import io.reactivex.rxjava3.disposables.Disposable +import io.reactivex.rxjava3.schedulers.Schedulers +import org.schabi.newpipe.R +import org.schabi.newpipe.database.subscription.NotificationMode +import org.schabi.newpipe.local.subscription.SubscriptionManager +import org.schabi.newpipe.settings.notifications.NotificationsConfigAdapter.ModeToggleListener + +class NotificationsChannelsConfigFragment : Fragment(), ModeToggleListener { + + private lateinit var updaters: CompositeDisposable + private var loader: Disposable? = null + private var adapter: NotificationsConfigAdapter? = null + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + updaters = CompositeDisposable() + setHasOptionsMenu(true) + } + + override fun onCreateView( + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle?, + ): View = inflater.inflate(R.layout.fragment_channels_notifications, container, false) + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + val recyclerView: RecyclerView = view.findViewById(R.id.recycler_view) + adapter = NotificationsConfigAdapter(this) + recyclerView.adapter = adapter + loader?.dispose() + loader = SubscriptionManager(requireContext()) + .subscriptions() + .observeOn(AndroidSchedulers.mainThread()) + .subscribe { newData -> adapter?.update(newData) } + } + + override fun onDestroyView() { + loader?.dispose() + loader = null + super.onDestroyView() + } + + override fun onDestroy() { + updaters.dispose() + super.onDestroy() + } + + override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) { + super.onCreateOptionsMenu(menu, inflater) + inflater.inflate(R.menu.menu_notifications_channels, menu) + } + + override fun onOptionsItemSelected(item: MenuItem): Boolean { + return when (item.itemId) { + R.id.action_toggle_all -> { + toggleAll() + true + } + else -> super.onOptionsItemSelected(item) + } + } + + override fun onModeToggle(position: Int, @NotificationMode mode: Int) { + val subscription = adapter?.getItem(position) ?: return + updaters.add( + SubscriptionManager(requireContext()) + .updateNotificationMode( + subscription.serviceId, + subscription.url, + mode + ) + .subscribeOn(Schedulers.io()) + .subscribe() + ) + } + + private fun toggleAll() { + val subscriptions = adapter?.getCurrentList() ?: return + val mode = subscriptions.firstOrNull()?.notificationMode ?: return + val newMode = when (mode) { + NotificationMode.DISABLED -> NotificationMode.ENABLED_DEFAULT + else -> NotificationMode.DISABLED + } + val subscriptionManager = SubscriptionManager(requireContext()) + updaters.add( + CompositeDisposable( + subscriptions.map { item -> + subscriptionManager.updateNotificationMode( + serviceId = item.serviceId, + url = item.url, + mode = newMode + ).subscribeOn(Schedulers.io()) + .subscribe() + } + ) + ) + } +} diff --git a/app/src/main/java/org/schabi/newpipe/settings/notifications/NotificationsConfigAdapter.kt b/app/src/main/java/org/schabi/newpipe/settings/notifications/NotificationsConfigAdapter.kt index 44d2256af..1689747e2 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/notifications/NotificationsConfigAdapter.kt +++ b/app/src/main/java/org/schabi/newpipe/settings/notifications/NotificationsConfigAdapter.kt @@ -40,6 +40,8 @@ class NotificationsConfigAdapter( return differ.currentList[position].id } + fun getCurrentList(): List = differ.currentList + fun update(newData: List) { differ.submitList( newData.map { diff --git a/app/src/main/res/drawable/ic_list_check.xml b/app/src/main/res/drawable/ic_list_check.xml new file mode 100644 index 000000000..37d806044 --- /dev/null +++ b/app/src/main/res/drawable/ic_list_check.xml @@ -0,0 +1,10 @@ + + + + \ No newline at end of file diff --git a/app/src/main/res/menu/menu_notifications_channels.xml b/app/src/main/res/menu/menu_notifications_channels.xml new file mode 100644 index 000000000..79b9cd7c1 --- /dev/null +++ b/app/src/main/res/menu/menu_notifications_channels.xml @@ -0,0 +1,10 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/values-ru/strings.xml b/app/src/main/res/values-ru/strings.xml index 30821be51..0e29edcc4 100644 --- a/app/src/main/res/values-ru/strings.xml +++ b/app/src/main/res/values-ru/strings.xml @@ -705,4 +705,5 @@ Уведомления отключены Уведомлять Вы подписались на канал + Переключить все \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index d8906200f..4da2ac60c 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -730,4 +730,5 @@ , %s • %s %d/%d + Toggle all \ No newline at end of file