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

View file

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