Only enabled keyword entry when checkbox enabled.

This commit is contained in:
David Langley 2021-08-24 10:45:46 +01:00
parent 8d7e3b6544
commit 2598a67525
2 changed files with 24 additions and 11 deletions

View file

@ -20,6 +20,7 @@ import android.content.Context
import android.util.AttributeSet
import android.view.inputmethod.EditorInfo
import android.widget.EditText
import androidx.core.view.children
import androidx.preference.PreferenceViewHolder
import com.google.android.material.chip.Chip
import com.google.android.material.chip.ChipGroup
@ -46,6 +47,8 @@ class KeywordPreference : VectorPreference {
var listener: Listener? = null
var keywordsEnabled = true
private var _keywords: LinkedHashSet<String> = linkedSetOf()
constructor(context: Context) : super(context)
@ -58,6 +61,12 @@ class KeywordPreference : VectorPreference {
layoutResource = R.layout.vector_preference_chip_group
}
override fun setEnabled(enabled: Boolean) {
super.setEnabled(enabled)
keywordsEnabled = enabled
notifyChanged()
}
override fun onBindViewHolder(holder: PreferenceViewHolder) {
super.onBindViewHolder(holder)
@ -74,6 +83,10 @@ class KeywordPreference : VectorPreference {
addChipToGroup(it, chipGroup)
}
chipEditText.isEnabled = keywordsEnabled
chipGroup.isEnabled = keywordsEnabled
chipGroup.children.forEach { it.isEnabled = keywordsEnabled }
chipEditText.setOnEditorActionListener { _, actionId, _ ->
if (actionId != EditorInfo.IME_ACTION_DONE) {
return@setOnEditorActionListener false
@ -102,6 +115,8 @@ class KeywordPreference : VectorPreference {
chipGroup.addView(chip)
chip.setOnCloseIconClickListener {
if (!keywordsEnabled)
return@setOnCloseIconClickListener
_keywords.remove(keyword)
listener?.didRemoveKeyword(keyword)
onPreferenceChangeListener?.onPreferenceChange(this, _keywords)

View file

@ -50,15 +50,14 @@ class VectorSettingsKeywordAndMentionsNotificationPreferenceFragment
override fun bindPref() {
super.bindPref()
val keywordRules = session.getPushRules().content?.filter { !it.ruleId.startsWith(".") }.orEmpty()
// val keywords = keywordRules.map(PushRule::ruleId).toSortedSet()
val editKeywordPreference = findPreference<KeywordPreference>("SETTINGS_KEYWORD_EDIT")!!
val keywordPreference = findPreference<VectorCheckboxPreference>("SETTINGS_PUSH_RULE_MESSAGES_CONTAINING_KEYWORDS_PREFERENCE_KEY")!!
val anyEnabledKeywords = keywordRules.any(PushRule::enabled)
keywordPreference.isChecked = anyEnabledKeywords
keywordPreference.isEnabled = keywordRules.isNotEmpty()
var currentChecked = anyEnabledKeywords
editKeywordPreference.isEnabled = anyEnabledKeywords
keywordPreference.onPreferenceChangeListener = Preference.OnPreferenceChangeListener { _, newValue ->
val keywords = session.getKeywords().value ?: return@OnPreferenceChangeListener false
val keywords = editKeywordPreference.keywords
val newChecked = newValue as Boolean
displayLoadingView()
session.getKeywords()
@ -68,8 +67,8 @@ class VectorSettingsKeywordAndMentionsNotificationPreferenceFragment
return@updateKeywordPushRules
}
result.onSuccess {
currentChecked = newChecked
keywordPreference.isChecked = newChecked
editKeywordPreference.isEnabled = newChecked
}
result.onFailure {
refreshDisplay()
@ -79,11 +78,11 @@ class VectorSettingsKeywordAndMentionsNotificationPreferenceFragment
false
}
val editKeywordPreference = findPreference<KeywordPreference>("SETTINGS_KEYWORD_EDIT")!!
editKeywordPreference.keywords = session.getKeywords().value.orEmpty()
editKeywordPreference.listener = object: KeywordPreference.Listener {
override fun didAddKeyword(keyword: String) {
addKeyword(keyword, currentChecked)
addKeyword(keyword)
scrollToPreference(editKeywordPreference)
}
@ -126,9 +125,8 @@ class VectorSettingsKeywordAndMentionsNotificationPreferenceFragment
editKeywordPreference.keywords = keywords
}
fun addKeyword(keyword: String, checked: Boolean) {
val newIndex = if (checked) NotificationIndex.NOISY else NotificationIndex.OFF
val standardAction = getStandardAction(RuleIds.RULE_ID_KEYWORDS, newIndex) ?: return
fun addKeyword(keyword: String) {
val standardAction = getStandardAction(RuleIds.RULE_ID_KEYWORDS, NotificationIndex.NOISY) ?: return
val enabled = standardAction != StandardActions.Disabled
val newActions = standardAction.actions ?: return
val newRule = PushRule(actions = newActions.toJson(), pattern = keyword, enabled = enabled, ruleId = keyword)