2021-08-05 08:30:01 +00:00
|
|
|
// 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
|
2021-08-18 08:33:56 +00:00
|
|
|
import Combine
|
|
|
|
import SwiftUI
|
2021-08-05 08:30:01 +00:00
|
|
|
|
2021-08-18 08:33:56 +00:00
|
|
|
@available(iOS 14.0, *)
|
|
|
|
final class NotificationSettingsViewModel: NotificationSettingsViewModelType, ObservableObject {
|
2021-08-05 08:30:01 +00:00
|
|
|
|
|
|
|
// MARK: - Properties
|
|
|
|
|
|
|
|
// MARK: Private
|
|
|
|
|
2021-08-25 12:03:36 +00:00
|
|
|
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>()
|
|
|
|
|
2021-08-25 15:53:15 +00:00
|
|
|
// The ordered array of keywords the UI displays.
|
|
|
|
// We keep it ordered so keywords don't jump around when being added and removed.
|
|
|
|
@Published private var keywordsOrdered = [String]()
|
2021-08-25 12:03:36 +00:00
|
|
|
|
2021-08-05 08:30:01 +00:00
|
|
|
// MARK: Public
|
|
|
|
|
2021-08-18 08:33:56 +00:00
|
|
|
@Published var viewState: NotificationSettingsViewState
|
|
|
|
|
2021-08-25 12:03:36 +00:00
|
|
|
weak var coordinatorDelegate: NotificationSettingsViewModelCoordinatorDelegate?
|
|
|
|
|
2021-08-05 08:30:01 +00:00
|
|
|
// MARK: - Setup
|
|
|
|
|
2021-08-25 12:03:36 +00:00
|
|
|
init(notificationSettingsService: NotificationSettingsServiceType, ruleIds: [NotificationPushRuleId], initialState: NotificationSettingsViewState) {
|
|
|
|
self.notificationSettingsService = notificationSettingsService
|
|
|
|
self.ruleIds = ruleIds
|
2021-08-18 08:33:56 +00:00
|
|
|
self.viewState = initialState
|
2021-08-25 12:03:36 +00:00
|
|
|
|
2021-08-25 14:17:05 +00:00
|
|
|
// Observe when the rules are updated, to subsequently update the state of the settings.
|
2021-08-25 12:03:36 +00:00
|
|
|
notificationSettingsService.rulesPublisher
|
|
|
|
.sink(receiveValue: rulesUpdated(newRules:))
|
|
|
|
.store(in: &cancellables)
|
|
|
|
|
2021-08-25 14:17:05 +00:00
|
|
|
// Only observe keywords if the current settings view displays it.
|
2021-08-25 12:03:36 +00:00
|
|
|
if ruleIds.contains(.keywords) {
|
2021-08-25 14:17:05 +00:00
|
|
|
// Publisher of all the keyword push rules (keyword rules do not start with '.')
|
2021-08-25 12:03:36 +00:00
|
|
|
let keywordsRules = notificationSettingsService.contentRulesPublisher
|
|
|
|
.map { $0.filter { !$0.ruleId.starts(with: ".")} }
|
|
|
|
|
2021-08-25 18:24:33 +00:00
|
|
|
// Map to just the keyword strings
|
2021-08-25 12:03:36 +00:00
|
|
|
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 }
|
2021-08-25 14:17:05 +00:00
|
|
|
// We avoid simply assigning the new set as it would cause all keywords to get sorted lexigraphically.
|
|
|
|
// We first sort lexigraphically, and secondly preserve the order the user added them.
|
2021-08-25 12:03:36 +00:00
|
|
|
// The following adds/removes any updates while preserving that ordering.
|
2021-08-25 15:53:15 +00:00
|
|
|
|
|
|
|
// Remove keywords not in the updated set.
|
|
|
|
var newKeywordsOrdered = self.keywordsOrdered.filter { keyword in
|
|
|
|
updatedKeywords.contains(keyword)
|
2021-08-25 12:03:36 +00:00
|
|
|
}
|
2021-08-25 15:53:15 +00:00
|
|
|
// Append items in the updated set if they are not already added.
|
|
|
|
// O(n)² here. Will change keywordsOrdered back to an `OrderedSet` in future to fix this.
|
|
|
|
updatedKeywords.sorted().forEach { keyword in
|
|
|
|
if !newKeywordsOrdered.contains(keyword) {
|
|
|
|
newKeywordsOrdered.append(keyword)
|
|
|
|
}
|
2021-08-25 12:03:36 +00:00
|
|
|
}
|
2021-08-25 15:53:15 +00:00
|
|
|
self.keywordsOrdered = newKeywordsOrdered
|
2021-08-25 12:03:36 +00:00
|
|
|
}
|
|
|
|
.store(in: &cancellables)
|
|
|
|
|
2021-08-25 18:24:33 +00:00
|
|
|
// Keyword rules were updates, check if we need to update the setting.
|
2021-08-25 12:03:36 +00:00
|
|
|
keywordsRules
|
|
|
|
.map { $0.contains { $0.enabled } }
|
|
|
|
.sink(receiveValue: keywordRuleUpdated(anyEnabled:))
|
|
|
|
.store(in: &cancellables)
|
|
|
|
|
|
|
|
// Update the viewState with the final keywords to be displayed.
|
2021-08-25 15:53:15 +00:00
|
|
|
$keywordsOrdered
|
2021-08-25 12:03:36 +00:00
|
|
|
.weakAssign(to: \.viewState.keywords, on: self)
|
|
|
|
.store(in: &cancellables)
|
|
|
|
}
|
2021-08-18 08:33:56 +00:00
|
|
|
}
|
|
|
|
|
2021-08-25 12:03:36 +00:00
|
|
|
convenience init(notificationSettingsService: NotificationSettingsServiceType, ruleIds: [NotificationPushRuleId]) {
|
2021-08-26 11:13:17 +00:00
|
|
|
let ruleState = Dictionary(uniqueKeysWithValues: ruleIds.map({ ($0, selected: true) }))
|
2021-08-25 12:03:36 +00:00
|
|
|
self.init(notificationSettingsService: notificationSettingsService, ruleIds: ruleIds, initialState: NotificationSettingsViewState(saving: false, ruleIds: ruleIds, selectionState: ruleState))
|
2021-08-05 08:30:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Public
|
|
|
|
|
2021-08-25 18:24:33 +00:00
|
|
|
func update(ruleID: NotificationPushRuleId, isChecked: Bool) {
|
|
|
|
let index = NotificationIndex.index(when: isChecked)
|
2021-08-25 12:03:36 +00:00
|
|
|
if ruleID == .keywords {
|
|
|
|
// Keywords is handled differently to other settings
|
2021-08-25 18:24:33 +00:00
|
|
|
updateKeywords(isChecked: isChecked)
|
2021-08-25 12:03:36 +00:00
|
|
|
return
|
|
|
|
}
|
2021-08-25 14:17:05 +00:00
|
|
|
// Get the static definition and update the actions and enabled state.
|
2021-08-25 12:03:36 +00:00
|
|
|
guard let standardActions = ruleID.standardActions(for: index) else { return }
|
|
|
|
let enabled = standardActions != .disabled
|
|
|
|
notificationSettingsService.updatePushRuleActions(
|
|
|
|
for: ruleID.rawValue,
|
|
|
|
enabled: enabled,
|
|
|
|
actions: standardActions.actions
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-08-25 18:24:33 +00:00
|
|
|
private func updateKeywords(isChecked: Bool) {
|
2021-08-25 15:53:15 +00:00
|
|
|
guard !keywordsOrdered.isEmpty else {
|
2021-08-25 12:03:36 +00:00
|
|
|
self.viewState.selectionState[.keywords]?.toggle()
|
|
|
|
return
|
|
|
|
}
|
2021-08-25 14:17:05 +00:00
|
|
|
// Get the static definition and update the actions and enabled state for every keyword.
|
2021-08-25 18:24:33 +00:00
|
|
|
let index = NotificationIndex.index(when: isChecked)
|
2021-08-25 12:03:36 +00:00
|
|
|
guard let standardActions = NotificationPushRuleId.keywords.standardActions(for: index) else { return }
|
|
|
|
let enabled = standardActions != .disabled
|
2021-08-25 15:53:15 +00:00
|
|
|
keywordsOrdered.forEach { keyword in
|
2021-08-25 12:03:36 +00:00
|
|
|
notificationSettingsService.updatePushRuleActions(
|
|
|
|
for: keyword,
|
|
|
|
enabled: enabled,
|
|
|
|
actions: standardActions.actions
|
|
|
|
)
|
2021-08-05 08:30:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-25 12:03:36 +00:00
|
|
|
func add(keyword: String) {
|
2021-08-25 15:53:15 +00:00
|
|
|
if !keywordsOrdered.contains(keyword) {
|
|
|
|
keywordsOrdered.append(keyword)
|
|
|
|
}
|
2021-08-25 12:03:36 +00:00
|
|
|
notificationSettingsService.add(keyword: keyword, enabled: true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func remove(keyword: String) {
|
2021-08-25 15:53:15 +00:00
|
|
|
keywordsOrdered = keywordsOrdered.filter({ $0 != keyword })
|
2021-08-25 12:03:36 +00:00
|
|
|
notificationSettingsService.remove(keyword: keyword)
|
|
|
|
}
|
|
|
|
|
2021-08-05 08:30:01 +00:00
|
|
|
// MARK: - Private
|
2021-09-02 13:09:29 +00:00
|
|
|
private func rulesUpdated(newRules: [NotificationPushRuleType]) {
|
2021-08-25 12:03:36 +00:00
|
|
|
for rule in newRules {
|
|
|
|
guard let ruleId = NotificationPushRuleId(rawValue: rule.ruleId),
|
|
|
|
ruleIds.contains(ruleId) else { continue }
|
|
|
|
self.viewState.selectionState[ruleId] = self.isChecked(rule: rule)
|
|
|
|
}
|
|
|
|
}
|
2021-08-05 08:30:01 +00:00
|
|
|
|
2021-08-25 12:03:36 +00:00
|
|
|
private func keywordRuleUpdated(anyEnabled: Bool) {
|
2021-08-25 15:53:15 +00:00
|
|
|
if !keywordsOrdered.isEmpty {
|
2021-08-25 12:03:36 +00:00
|
|
|
self.viewState.selectionState[.keywords] = anyEnabled
|
|
|
|
}
|
|
|
|
}
|
2021-09-13 10:36:33 +00:00
|
|
|
|
|
|
|
/// Given a push rule check which index/checked state it matches.
|
|
|
|
///
|
|
|
|
/// Matching is done by comparing the rule against the static definitions for that rule.
|
|
|
|
/// The same logic is used on android.
|
|
|
|
/// - Parameter rule: The push rule type to check.
|
|
|
|
/// - Returns: Wether it should be displayed as checked or not checked.
|
2021-09-02 13:09:29 +00:00
|
|
|
private func isChecked(rule: NotificationPushRuleType) -> Bool {
|
2021-08-25 12:03:36 +00:00
|
|
|
guard let ruleId = NotificationPushRuleId(rawValue: rule.ruleId) else { return false }
|
|
|
|
|
|
|
|
let firstIndex = NotificationIndex.allCases.first { nextIndex in
|
2021-08-27 15:26:56 +00:00
|
|
|
return rule.matches(standardActions: ruleId.standardActions(for: nextIndex))
|
2021-08-25 12:03:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
guard let index = firstIndex else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return index.enabled
|
|
|
|
}
|
2021-08-05 08:30:01 +00:00
|
|
|
|
|
|
|
}
|