Add per-event logging settings pages
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
package se.ajpanton.notificationlog
|
||||
|
||||
import android.os.Bundle
|
||||
import android.view.View
|
||||
import androidx.annotation.StringRes
|
||||
import androidx.fragment.app.Fragment
|
||||
import se.ajpanton.notificationlog.databinding.FragmentEventSettingsBinding
|
||||
import se.ajpanton.notificationlog.settings.AppRuleMode
|
||||
import se.ajpanton.notificationlog.settings.LoggingRuleStore
|
||||
import se.ajpanton.notificationlog.settings.LoggingType
|
||||
|
||||
class EventSettingsFragment : Fragment(R.layout.fragment_event_settings) {
|
||||
private var binding: FragmentEventSettingsBinding? = null
|
||||
private lateinit var type: LoggingType
|
||||
private lateinit var store: LoggingRuleStore
|
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
binding = FragmentEventSettingsBinding.bind(view)
|
||||
type = requireArguments().getSerializable(ARG_TYPE, LoggingType::class.java)!!
|
||||
store = LoggingRuleStore(requireContext())
|
||||
binding!!.pageTitle.setText(requireArguments().getInt(ARG_TITLE))
|
||||
bindRule()
|
||||
}
|
||||
|
||||
override fun onDestroyView() { binding = null; super.onDestroyView() }
|
||||
|
||||
private fun bindRule() {
|
||||
val rule = store.ruleFor(type)
|
||||
listOf(
|
||||
binding!!.masterToggle,
|
||||
binding!!.appRuleToggle,
|
||||
binding!!.onlySeenToggle,
|
||||
binding!!.seenFirstToggle,
|
||||
binding!!.routineUpdatesToggle,
|
||||
).forEach { it.setOnCheckedChangeListener(null) }
|
||||
binding!!.masterToggle.isChecked = rule.enabled
|
||||
binding!!.appRuleToggle.isChecked = rule.appRuleMode == AppRuleMode.WHITELIST
|
||||
binding!!.onlySeenToggle.isChecked = rule.onlySeenApps
|
||||
binding!!.seenFirstToggle.isChecked = rule.seenAppsFirst
|
||||
binding!!.seenFirstToggle.isEnabled = !rule.onlySeenApps
|
||||
binding!!.routineUpdatesToggle.visibility = if (type == LoggingType.EDITS) View.VISIBLE else View.GONE
|
||||
binding!!.routineUpdatesToggle.isChecked = rule.ignoreRoutineUpdates
|
||||
updateRuleLabels()
|
||||
binding!!.masterToggle.setOnCheckedChangeListener { _, checked -> save { copy(enabled = checked) } }
|
||||
binding!!.appRuleToggle.setOnCheckedChangeListener { _, checked -> save { copy(appRuleMode = if (checked) AppRuleMode.WHITELIST else AppRuleMode.BLACKLIST) } }
|
||||
binding!!.onlySeenToggle.setOnCheckedChangeListener { _, checked -> save { copy(onlySeenApps = checked) } }
|
||||
binding!!.seenFirstToggle.setOnCheckedChangeListener { _, checked -> save { copy(seenAppsFirst = checked) } }
|
||||
binding!!.routineUpdatesToggle.setOnCheckedChangeListener { _, checked -> save { copy(ignoreRoutineUpdates = checked) } }
|
||||
}
|
||||
|
||||
private fun save(change: se.ajpanton.notificationlog.settings.LoggingRule.() -> se.ajpanton.notificationlog.settings.LoggingRule) {
|
||||
store.save(type, store.ruleFor(type).change())
|
||||
bindRule()
|
||||
}
|
||||
|
||||
private fun updateRuleLabels() {
|
||||
val rule = store.ruleFor(type)
|
||||
binding!!.appRuleToggle.text = if (rule.appRuleMode == AppRuleMode.WHITELIST) "Whitelist chosen" else "Blacklist chosen"
|
||||
binding!!.onlySeenToggle.text = if (rule.onlySeenApps) "Show only seen apps" else "Show all apps"
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val ARG_TITLE = "title"
|
||||
private const val ARG_TYPE = "type"
|
||||
fun newInstance(@StringRes title: Int, type: LoggingType) = EventSettingsFragment().apply {
|
||||
arguments = Bundle().apply { putInt(ARG_TITLE, title); putSerializable(ARG_TYPE, type) }
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user