Consolidate notification utility settings

This commit is contained in:
ajp_anton
2026-08-31 01:19:58 +00:00
parent dc11e605d1
commit 3289d62ca0
8 changed files with 129 additions and 188 deletions
@@ -1,16 +1,35 @@
package se.ajpanton.notificationsmaster
import android.Manifest
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.fragment.app.Fragment
import com.google.android.material.switchmaterial.SwitchMaterial
import se.ajpanton.notificationsmaster.databinding.FragmentMiscellaneousBinding
import se.ajpanton.notificationsmaster.debug.DebugNotifications
import se.ajpanton.notificationsmaster.visibility.SystemUiMiscSettings
import se.ajpanton.notificationsmaster.visibility.VisibilityPolicyStore
class MiscellaneousFragment : Fragment(R.layout.fragment_miscellaneous) {
private var binding: FragmentMiscellaneousBinding? = null
private lateinit var store: VisibilityPolicyStore
private var pendingCount: Int? = null
private var pendingCycle = false
private var updatingCycle = false
private val permissionRequest = registerForActivityResult(ActivityResultContracts.RequestPermission()) { granted ->
val context = context ?: return@registerForActivityResult
if (granted) {
pendingCount?.let { DebugNotifications.setCount(context, it) }
if (pendingCycle) DebugNotifications.setCycling(context, true)
} else {
Toast.makeText(context, R.string.debug_notification_permission_required, Toast.LENGTH_SHORT).show()
}
pendingCount = null
pendingCycle = false
refreshDebugNotifications()
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
binding = FragmentMiscellaneousBinding.bind(view)
@@ -26,11 +45,27 @@ class MiscellaneousFragment : Fragment(R.layout.fragment_miscellaneous) {
bind(binding!!.showAodBatteryPercent, settings.showAodBatteryPercentWhenUnplugged) { current, checked ->
current.copy(showAodBatteryPercentWhenUnplugged = checked)
}
binding!!.debugMinus10.setOnClickListener { changeDebugCount(-10) }
binding!!.debugMinus1.setOnClickListener { changeDebugCount(-1) }
binding!!.debugPlus1.setOnClickListener { changeDebugCount(1) }
binding!!.debugPlus10.setOnClickListener { changeDebugCount(10) }
binding!!.debugReset.setOnClickListener { setDebugCount(0) }
binding!!.debugCycle.setOnCheckedChangeListener { _, enabled ->
if (updatingCycle) return@setOnCheckedChangeListener
if (enabled && !DebugNotifications.hasPermission(requireContext())) {
pendingCycle = true
permissionRequest.launch(Manifest.permission.POST_NOTIFICATIONS)
} else {
DebugNotifications.setCycling(requireContext(), enabled)
}
}
}
override fun onResume() {
super.onResume()
activity?.title = getString(R.string.page_miscellaneous)
DebugNotifications.apply(requireContext())
refreshDebugNotifications()
}
override fun onDestroyView() {
@@ -48,4 +83,27 @@ class MiscellaneousFragment : Fragment(R.layout.fragment_miscellaneous) {
store.update { policy -> policy.copy(systemUiMisc = update(policy.systemUiMisc, checked)) }
}
}
private fun changeDebugCount(delta: Int) = setDebugCount(DebugNotifications.count(requireContext()) + delta)
private fun setDebugCount(count: Int) {
val desired = count.coerceIn(0, DebugNotifications.MAX_COUNT)
if (desired > 0 && !DebugNotifications.hasPermission(requireContext())) {
pendingCount = desired
permissionRequest.launch(Manifest.permission.POST_NOTIFICATIONS)
return
}
DebugNotifications.setCount(requireContext(), desired)
refreshDebugNotifications()
}
private fun refreshDebugNotifications() {
val context = context ?: return
val count = DebugNotifications.count(context)
binding?.debugCount?.text = count.toString()
binding?.debugCycle?.isEnabled = count > 0
updatingCycle = true
binding?.debugCycle?.isChecked = DebugNotifications.isCycling(context)
updatingCycle = false
}
}