Simplify disposables handling in notification mode settings

This commit is contained in:
Stypox 2023-01-02 14:45:11 +01:00
parent fb1360b72a
commit b1d9080a0f
No known key found for this signature in database
GPG key ID: 4BDF1B40A49FDD23

View file

@ -27,7 +27,7 @@ class NotificationModeConfigFragment : Fragment() {
private var _binding: FragmentChannelsNotificationsBinding? = null private var _binding: FragmentChannelsNotificationsBinding? = null
private val binding get() = _binding!! private val binding get() = _binding!!
private val updaters = CompositeDisposable() private val disposables = CompositeDisposable()
private var loader: Disposable? = null private var loader: Disposable? = null
private lateinit var adapter: NotificationModeConfigAdapter private lateinit var adapter: NotificationModeConfigAdapter
private lateinit var subscriptionManager: SubscriptionManager private lateinit var subscriptionManager: SubscriptionManager
@ -56,7 +56,7 @@ class NotificationModeConfigFragment : Fragment() {
adapter = NotificationModeConfigAdapter { position, mode -> adapter = NotificationModeConfigAdapter { position, mode ->
// Notification mode has been changed via the UI. // Notification mode has been changed via the UI.
// Now change it in the database. // Now change it in the database.
updaters.add(updateNotificationMode(adapter.currentList[position], mode)) updateNotificationMode(adapter.currentList[position], mode)
} }
binding.recyclerView.adapter = adapter binding.recyclerView.adapter = adapter
loader?.dispose() loader?.dispose()
@ -73,7 +73,7 @@ class NotificationModeConfigFragment : Fragment() {
} }
override fun onDestroy() { override fun onDestroy() {
updaters.dispose() disposables.dispose()
super.onDestroy() super.onDestroy()
} }
@ -98,16 +98,14 @@ class NotificationModeConfigFragment : Fragment() {
NotificationMode.DISABLED -> NotificationMode.ENABLED NotificationMode.DISABLED -> NotificationMode.ENABLED
else -> NotificationMode.DISABLED else -> NotificationMode.DISABLED
} }
val disposables = adapter.currentList.map { updateNotificationMode(it, newMode) } adapter.currentList.forEach { updateNotificationMode(it, newMode) }
updaters.add(CompositeDisposable(disposables))
} }
private fun updateNotificationMode( private fun updateNotificationMode(item: SubscriptionItem, @NotificationMode mode: Int) {
item: SubscriptionItem, disposables.add(
@NotificationMode mode: Int subscriptionManager.updateNotificationMode(item.serviceId, item.url, mode)
): Disposable { .subscribeOn(Schedulers.io())
return subscriptionManager.updateNotificationMode(item.serviceId, item.url, mode) .subscribe()
.subscribeOn(Schedulers.io()) )
.subscribe()
} }
} }