Avoid certain cases where all feeds notifications permissions could be set unnecessarily in notifications permissions activity
This commit is contained in:
parent
d5e5bb7abb
commit
93b9336045
@ -1,7 +1,6 @@
|
||||
package com.readrops.app.activities
|
||||
|
||||
import android.content.Intent
|
||||
import android.drm.DrmInfoRequest.ACCOUNT_ID
|
||||
import android.os.Bundle
|
||||
import android.view.MenuItem
|
||||
import androidx.activity.viewModels
|
||||
@ -14,15 +13,24 @@ import com.readrops.app.R
|
||||
import com.readrops.app.adapters.NotificationPermissionListAdapter
|
||||
import com.readrops.app.databinding.ActivityNotificationPermissionBinding
|
||||
import com.readrops.app.utils.ReadropsKeys
|
||||
import com.readrops.app.utils.ReadropsKeys.ACCOUNT_ID
|
||||
import com.readrops.app.utils.SharedPreferencesManager
|
||||
import com.readrops.app.utils.Utils
|
||||
import com.readrops.app.viewmodels.NotificationPermissionViewModel
|
||||
import com.readrops.readropsdb.entities.Feed
|
||||
import com.readrops.readropsdb.entities.account.Account
|
||||
import io.reactivex.android.schedulers.AndroidSchedulers
|
||||
import io.reactivex.schedulers.Schedulers
|
||||
|
||||
class NotificationPermissionActivity : AppCompatActivity() {
|
||||
|
||||
lateinit var binding: ActivityNotificationPermissionBinding
|
||||
private lateinit var binding: ActivityNotificationPermissionBinding
|
||||
private val viewModel by viewModels<NotificationPermissionViewModel>()
|
||||
private var adapter: NotificationPermissionListAdapter? = null
|
||||
|
||||
private var isFirstCheck = true
|
||||
private var feedStateChanged = false
|
||||
private var feeds = listOf<Feed>()
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
@ -32,67 +40,82 @@ class NotificationPermissionActivity : AppCompatActivity() {
|
||||
supportActionBar?.setDisplayHomeAsUpEnabled(true)
|
||||
|
||||
val accountId = intent.getIntExtra(ACCOUNT_ID, 0)
|
||||
val viewModel by viewModels<NotificationPermissionViewModel>()
|
||||
var adapter: NotificationPermissionListAdapter? = null
|
||||
var feedStateChanged = false
|
||||
|
||||
viewModel.getAccount(accountId).observe(this, Observer { account ->
|
||||
viewModel.account = account
|
||||
|
||||
if (adapter == null) {
|
||||
// execute the following lines only once
|
||||
binding.notifPermissionAccountSwitch.isChecked = account.isNotificationsEnabled
|
||||
binding.notifPermissionAccountSwitch.setOnCheckedChangeListener { _, isChecked ->
|
||||
account.isNotificationsEnabled = isChecked
|
||||
binding.notifPermissionFeedsSwitch.isEnabled = isChecked
|
||||
// execute the method only once
|
||||
setupUI(account)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
adapter?.enableAll = isChecked
|
||||
adapter?.notifyDataSetChanged()
|
||||
private fun setupUI(account: Account) {
|
||||
binding.notifPermissionAccountSwitch.isChecked = account.isNotificationsEnabled
|
||||
binding.notifPermissionAccountSwitch.setOnCheckedChangeListener { _, isChecked ->
|
||||
account.isNotificationsEnabled = isChecked
|
||||
binding.notifPermissionFeedsSwitch.isEnabled = isChecked
|
||||
|
||||
viewModel.setAccountNotificationsState(isChecked)
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.doOnError { Utils.showSnackbar(binding.root, it.message) }
|
||||
.subscribe()
|
||||
adapter?.enableAll = isChecked
|
||||
adapter?.notifyDataSetChanged()
|
||||
|
||||
if (isChecked) displayAutoSynchroPopup()
|
||||
}
|
||||
viewModel.setAccountNotificationsState(isChecked)
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.doOnError { Utils.showSnackbar(binding.root, it.message) }
|
||||
.subscribe()
|
||||
|
||||
binding.notifPermissionFeedsSwitch.isEnabled = account.isNotificationsEnabled
|
||||
binding.notifPermissionFeedsSwitch.setOnCheckedChangeListener { _, isChecked ->
|
||||
if (!feedStateChanged) {
|
||||
viewModel.setAllFeedsNotificationState(isChecked)
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.doOnError { Utils.showSnackbar(binding.root, it.message) }
|
||||
.subscribe()
|
||||
}
|
||||
if (isChecked) displayAutoSynchroPopup()
|
||||
}
|
||||
|
||||
feedStateChanged = false
|
||||
}
|
||||
|
||||
adapter = NotificationPermissionListAdapter(account.isNotificationsEnabled) { feed ->
|
||||
feedStateChanged = true
|
||||
|
||||
viewModel.setFeedNotificationState(feed)
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.doOnError { Utils.showSnackbar(binding.root, it.message) }
|
||||
.subscribe()
|
||||
}
|
||||
|
||||
binding.notifPermissionAccountList.layoutManager = LinearLayoutManager(this)
|
||||
binding.notifPermissionAccountList.adapter = adapter
|
||||
|
||||
viewModel.getFeedsWithNotifPermission().observe(this, Observer { feeds ->
|
||||
binding.notifPermissionFeedsSwitch.isChecked = feeds.all { it.isNotificationEnabled }
|
||||
adapter?.submitList(feeds)
|
||||
})
|
||||
binding.notifPermissionFeedsSwitch.isEnabled = account.isNotificationsEnabled
|
||||
binding.notifPermissionFeedsSwitch.setOnCheckedChangeListener { _, isChecked ->
|
||||
if (canUpdateAllFeedsPermissions(isChecked)) {
|
||||
viewModel.setAllFeedsNotificationState(isChecked)
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.doOnError { Utils.showSnackbar(binding.root, it.message) }
|
||||
.subscribe()
|
||||
}
|
||||
|
||||
if (isFirstCheck) isFirstCheck = false
|
||||
if (feedStateChanged) feedStateChanged = false
|
||||
}
|
||||
|
||||
adapter = NotificationPermissionListAdapter(account.isNotificationsEnabled) { feed ->
|
||||
feedStateChanged = true
|
||||
|
||||
viewModel.setFeedNotificationState(feed)
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.doOnError { Utils.showSnackbar(binding.root, it.message) }
|
||||
.subscribe()
|
||||
}
|
||||
|
||||
binding.notifPermissionAccountList.layoutManager = LinearLayoutManager(this)
|
||||
binding.notifPermissionAccountList.adapter = adapter
|
||||
|
||||
viewModel.getFeedsWithNotifPermission().observe(this, Observer { newFeeds ->
|
||||
feeds = newFeeds
|
||||
|
||||
binding.notifPermissionFeedsSwitch.isChecked = newFeeds.all { it.isNotificationEnabled }
|
||||
adapter?.submitList(newFeeds)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Inform if is possible to update all feeds notifications permissions in the same time.
|
||||
* The method takes into account the following states :
|
||||
* - first check : when opening the activity with all feeds permissions enabled,
|
||||
* the enable all feeds permissions switch will be checked but the request mustn't be executed
|
||||
* - feed state : if all feeds permissions are enabled and a feed permission is disabled,
|
||||
* the enable all feeds permissions switch will be unchecked but the request mustn't be executed as only one feed permission is disabled
|
||||
* - all feeds permissions switch checked : if the setOnCheckedChangeListener method is triggered because all feeds permissions were enabled,
|
||||
* do not execute the request as it would be pointless
|
||||
*/
|
||||
private fun canUpdateAllFeedsPermissions(isChecked: Boolean): Boolean {
|
||||
return !isFirstCheck && (!feedStateChanged || (isChecked && !feeds.all { it.isNotificationEnabled }))
|
||||
}
|
||||
|
||||
private fun displayAutoSynchroPopup() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user