Toggle all subscriptions notification mode

This commit is contained in:
Koitharu 2021-09-04 15:28:11 +03:00
parent 111dc4963d
commit 9d249904bd
No known key found for this signature in database
GPG Key ID: 8E861F8CE6E7CE27
8 changed files with 137 additions and 90 deletions

View File

@ -87,12 +87,7 @@ class NotificationsSettingsFragment : BasePreferenceFragment(), OnSharedPreferen
} }
private fun updateSubscriptions(subscriptions: List<SubscriptionEntity>) { private fun updateSubscriptions(subscriptions: List<SubscriptionEntity>) {
var notified = 0 val notified = subscriptions.count { it.notificationMode != NotificationMode.DISABLED }
for (subscription in subscriptions) {
if (subscription.notificationMode != NotificationMode.DISABLED) {
notified++
}
}
val preference = findPreference<Preference>(getString(R.string.streams_notifications_channels_key)) val preference = findPreference<Preference>(getString(R.string.streams_notifications_channels_key))
if (preference != null) { if (preference != null) {
preference.summary = preference.context.getString( preference.summary = preference.context.getString(

View File

@ -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()
);
}
}

View File

@ -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()
}
)
)
}
}

View File

@ -40,6 +40,8 @@ class NotificationsConfigAdapter(
return differ.currentList[position].id return differ.currentList[position].id
} }
fun getCurrentList(): List<SubscriptionItem> = differ.currentList
fun update(newData: List<SubscriptionEntity>) { fun update(newData: List<SubscriptionEntity>) {
differ.submitList( differ.submitList(
newData.map { newData.map {

View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#FFFFFF"
android:pathData="M3,5H9V11H3V5M5,7V9H7V7H5M11,7H21V9H11V7M11,15H21V17H11V15M5,20L1.5,16.5L2.91,15.09L5,17.17L9.59,12.59L11,14L5,20Z" />
</vector>

View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_toggle_all"
android:icon="@drawable/ic_list_check"
android:title="@string/toggle_all"
app:showAsAction="ifRoom" />
</menu>

View File

@ -705,4 +705,5 @@
<string name="notifications_disabled">Уведомления отключены</string> <string name="notifications_disabled">Уведомления отключены</string>
<string name="get_notified">Уведомлять</string> <string name="get_notified">Уведомлять</string>
<string name="you_successfully_subscribed">Вы подписались на канал</string> <string name="you_successfully_subscribed">Вы подписались на канал</string>
<string name="toggle_all">Переключить все</string>
</resources> </resources>

View File

@ -730,4 +730,5 @@
<string name="enumeration_comma">,</string> <string name="enumeration_comma">,</string>
<string name="notification_title_pattern" translatable="false">%s • %s</string> <string name="notification_title_pattern" translatable="false">%s • %s</string>
<string name="streams_notifications_channels_summary" translatable="false">%d/%d</string> <string name="streams_notifications_channels_summary" translatable="false">%d/%d</string>
<string name="toggle_all">Toggle all</string>
</resources> </resources>