cleanup unused code and add comments for clarity

This commit is contained in:
David Langley 2021-08-10 10:54:13 +01:00
parent 886bd3cc8f
commit 756ff3d938
3 changed files with 7 additions and 64 deletions

View File

@ -29,6 +29,10 @@ enum class NotificationIndex(val index: Int) {
}
}
/**
* Given a push rule determine the NotificationIndex by comparing it to the static push rule definitions.
* Used when determining the selected state of the PushRulePreference.
*/
val PushRule.notificationIndex: NotificationIndex? get() =
NotificationIndex.values().firstOrNull {
// Get the actions for the index
@ -39,6 +43,9 @@ val PushRule.notificationIndex: NotificationIndex? get() =
ruleMatches(this, targetRule)
}
/**
* A check to determine if two push rules should be considered a match.
*/
private fun ruleMatches(rule: PushRule, targetRule: PushRule): Boolean {
// Rules match if both are disabled, or if both are enabled and their highlight/sound/notify actions match up.
return (!rule.enabled && !targetRule.enabled)

View File

@ -38,7 +38,6 @@ class VectorSettingsAdvancedNotificationPreferenceFragment @Inject constructor()
for (preferenceKey in prefKeyToPushRuleId.keys) {
val preference = findPreference<VectorPreference>(preferenceKey)
if (preference is PushRulePreference) {
// preference.isEnabled = null != rules && isConnected && pushManager.areDeviceNotificationsAllowed()
val ruleAndKind: PushRuleAndKind? = session.getPushRules().findDefaultRule(prefKeyToPushRuleId[preferenceKey])
if (ruleAndKind == null) {

View File

@ -22,10 +22,6 @@ import im.vector.app.core.preference.VectorCheckboxPreference
import im.vector.app.core.utils.toast
import im.vector.app.features.settings.VectorSettingsBaseFragment
import kotlinx.coroutines.launch
import org.matrix.android.sdk.api.pushrules.Action
import org.matrix.android.sdk.api.pushrules.RuleIds
import org.matrix.android.sdk.api.pushrules.RuleSetKey
import org.matrix.android.sdk.api.pushrules.rest.PushRule
import org.matrix.android.sdk.api.pushrules.rest.PushRuleAndKind
abstract class VectorSettingsPushRuleNotificationPreferenceFragment
@ -33,65 +29,6 @@ abstract class VectorSettingsPushRuleNotificationPreferenceFragment
abstract val prefKeyToPushRuleId: Map<String, String>
/**
* Create a push rule with the updated checkbox status.
*
* @param status boolean checkbox status
* @return a push rule with the updated flags
*/
fun createNewRule(ruleAndKind: PushRuleAndKind, status: Boolean): PushRule {
val safeRule = ruleAndKind.pushRule
val safeKind = ruleAndKind.kind
val ruleStatusIndex = ruleStatusIndexFor(ruleAndKind)
return if (status != ruleStatusIndex) {
if (safeRule.ruleId == RuleIds.RULE_ID_SUPPRESS_BOTS_NOTIFICATIONS) {
if (status) {
safeRule.copy(enabled = true)
.setNotify(true)
.setNotificationSound()
} else {
safeRule.copy(enabled = true)
.setNotify(false)
.removeNotificationSound()
}
} else {
if (status) {
safeRule.copy(enabled = true)
.setNotify(true)
.setHighlight(safeKind != RuleSetKey.UNDERRIDE
&& safeRule.ruleId != RuleIds.RULE_ID_INVITE_ME)
.setNotificationSound(
if (safeRule.ruleId == RuleIds.RULE_ID_CALL) {
Action.ACTION_OBJECT_VALUE_VALUE_RING
} else {
Action.ACTION_OBJECT_VALUE_VALUE_DEFAULT
}
)
} else {
if (safeKind == RuleSetKey.UNDERRIDE || safeRule.ruleId == RuleIds.RULE_ID_SUPPRESS_BOTS_NOTIFICATIONS) {
safeRule.setNotify(false)
} else {
safeRule.copy(enabled = false)
}
}
}
} else {
safeRule
}
}
/**
* @return the bing rule status boolean
*/
private fun ruleStatusIndexFor(ruleAndKind: PushRuleAndKind): Boolean {
val rule = ruleAndKind.pushRule
if (rule.ruleId == RuleIds.RULE_ID_SUPPRESS_BOTS_NOTIFICATIONS) {
return rule.shouldNotify() || rule.shouldNotNotify() && !rule.enabled
}
return rule.enabled && !rule.shouldNotNotify()
}
override fun bindPref() {
for (preferenceKey in prefKeyToPushRuleId.keys) {
val preference = findPreference<VectorCheckboxPreference>(preferenceKey)!!