Files
bundesmessenger-ios/Riot/Modules/Settings/Notifications/ViewModel/NotificationSettingsViewModel.swift
T

250 lines
9.3 KiB
Swift

// File created from ScreenTemplate
// $ createScreen.sh Settings/Notifications NotificationSettings
/*
Copyright 2021 New Vector Ltd
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import Foundation
import Combine
import SwiftUI
import OrderedCollections
@available(iOS 14.0, *)
final class NotificationSettingsViewModel: NotificationSettingsViewModelType, ObservableObject {
// MARK: - Properties
// MARK: Private
private let notificationSettingsService: NotificationSettingsServiceType
// The rule ids this view model allows the ui to enabled/disable.
private let ruleIds: [NotificationPushRuleId]
private var cancellables = Set<AnyCancellable>()
// The set of keywords the UI displays. We use an Ordered set to keep a consistent ordering
// so that the keywords don't jump around.
@Published private var keywordsSet = OrderedSet<String>()
// MARK: Public
@Published var viewState: NotificationSettingsViewState
weak var coordinatorDelegate: NotificationSettingsViewModelCoordinatorDelegate?
// MARK: - Setup
init(notificationSettingsService: NotificationSettingsServiceType, ruleIds: [NotificationPushRuleId], initialState: NotificationSettingsViewState) {
self.notificationSettingsService = notificationSettingsService
self.ruleIds = ruleIds
self.viewState = initialState
// Observe whent he rules updated to update the state of the settings.
notificationSettingsService.rulesPublisher
.sink(receiveValue: rulesUpdated(newRules:))
.store(in: &cancellables)
// Only observe keywords if this settings view has it as an id
if ruleIds.contains(.keywords) {
// Publisher of all the keyword push rules(do not start with '.')
let keywordsRules = notificationSettingsService.contentRulesPublisher
.map { $0.filter { !$0.ruleId.starts(with: ".")} }
// Map to just the keword strings
let keywords = keywordsRules
.map { Set($0.compactMap { $0.ruleId }) }
// Update the keyword set
keywords
.sink { [weak self] updatedKeywords in
guard let self = self else { return }
// We don't just assign the new set as it would cause all keywords to get sorted lexigraphically.
// We first sort lexigraphically by preserve the order the user added them.
// The following adds/removes any updates while preserving that ordering.
let newKeywordSet = OrderedSet<String>(updatedKeywords.sorted())
self.keywordsSet.removeAll { keyword in
!newKeywordSet.contains(keyword)
}
newKeywordSet.forEach { keyword in
self.keywordsSet.append(keyword)
}
}
.store(in: &cancellables)
// Keword rules were updates, may need to update the setting.
keywordsRules
.map { $0.contains { $0.enabled } }
.sink(receiveValue: keywordRuleUpdated(anyEnabled:))
.store(in: &cancellables)
// Update the viewState with the final keywords to be displayed.
$keywordsSet
.map(Array.init)
.weakAssign(to: \.viewState.keywords, on: self)
.store(in: &cancellables)
}
}
convenience init(notificationSettingsService: NotificationSettingsServiceType, ruleIds: [NotificationPushRuleId]) {
let ruleState = Dictionary(uniqueKeysWithValues: ruleIds.map({ ($0, selected: false) }))
self.init(notificationSettingsService: notificationSettingsService, ruleIds: ruleIds, initialState: NotificationSettingsViewState(saving: false, ruleIds: ruleIds, selectionState: ruleState))
}
// MARK: - Public
func check(ruleID: NotificationPushRuleId, checked: Bool) {
let index = NotificationIndex.index(enabled: checked)
if ruleID == .keywords {
// Keywords is handled differently to other settings
handleCheckKeywords(checked: checked)
return
}
// Get the static definition and update the actions/enabled state.
guard let standardActions = ruleID.standardActions(for: index) else { return }
let enabled = standardActions != .disabled
notificationSettingsService.updatePushRuleActions(
for: ruleID.rawValue,
enabled: enabled,
actions: standardActions.actions
)
}
private func handleCheckKeywords(checked: Bool) {
guard !keywordsSet.isEmpty else {
self.viewState.selectionState[.keywords]?.toggle()
return
}
// Get the static definition and update the actions/enabled state for every keyword.
let index = NotificationIndex.index(enabled: checked)
guard let standardActions = NotificationPushRuleId.keywords.standardActions(for: index) else { return }
let enabled = standardActions != .disabled
keywordsSet.forEach { keyword in
notificationSettingsService.updatePushRuleActions(
for: keyword,
enabled: enabled,
actions: standardActions.actions
)
}
}
func add(keyword: String) {
keywordsSet.append(keyword)
notificationSettingsService.add(keyword: keyword, enabled: true)
}
func remove(keyword: String) {
keywordsSet.remove(keyword)
notificationSettingsService.remove(keyword: keyword)
}
// MARK: - Private
private func rulesUpdated(newRules: [MXPushRule]) {
for rule in newRules {
guard let ruleId = NotificationPushRuleId(rawValue: rule.ruleId),
ruleIds.contains(ruleId) else { continue }
self.viewState.selectionState[ruleId] = self.isChecked(rule: rule)
}
}
private func keywordRuleUpdated(anyEnabled: Bool) {
if !keywordsSet.isEmpty {
self.viewState.selectionState[.keywords] = anyEnabled
}
}
/**
Given a push rule check which index/checked state does it match.
Matcing is done by comparing the rule against the static definitions for that rule.
Same logic is used on android.
*/
private func isChecked(rule: MXPushRule) -> Bool {
guard let ruleId = NotificationPushRuleId(rawValue: rule.ruleId) else { return false }
let firstIndex = NotificationIndex.allCases.first { nextIndex in
return ruleMaches(rule: rule, targetRule: ruleId.standardActions(for: nextIndex))
}
guard let index = firstIndex else {
return false
}
return index.enabled
}
/*
Given a rule does it match the actions int he static definition.
*/
private func ruleMaches(rule: MXPushRule, targetRule: NotificationStandardActions?) -> Bool {
guard let targetRule = targetRule else {
return false
}
if !rule.enabled && targetRule == .disabled {
return true
}
if rule.enabled,
let actions = targetRule.actions,
rule.highlight == actions.highlight,
rule.sound == actions.sound,
rule.notify == actions.notify,
rule.dontNotify == !actions.notify {
return true
}
return false
}
}
fileprivate extension MXPushRule {
func getAction(actionType: MXPushRuleActionType, tweakType: String? = nil) -> MXPushRuleAction? {
guard let actions = actions as? [MXPushRuleAction] else {
return nil
}
return actions.first { action in
var match = action.actionType == actionType
MXLog.debug("action \(action)")
if let tweakType = tweakType,
let actionTweak = action.parameters?["set_tweak"] as? String {
match = match && (tweakType == actionTweak)
}
return match
}
}
var highlight: Bool {
guard let action = getAction(actionType: MXPushRuleActionTypeSetTweak, tweakType: "highlight") else {
return false
}
if let highlight = action.parameters["value"] as? Bool {
return highlight
}
return true
}
var sound: String? {
guard let action = getAction(actionType: MXPushRuleActionTypeSetTweak, tweakType: "sound") else {
return nil
}
return action.parameters["value"] as? String
}
var notify: Bool {
return getAction(actionType: MXPushRuleActionTypeNotify) != nil
}
var dontNotify: Bool {
return getAction(actionType: MXPushRuleActionTypeDontNotify) != nil
}
}