Add NotificationRepository, ViewModel and ViewController

This commit is contained in:
langleyd
2021-06-25 22:38:31 +01:00
parent d27a0627d7
commit f74adb5909
11 changed files with 956 additions and 1 deletions
@@ -0,0 +1,339 @@
//
// 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
enum RoomNotificationState: CaseIterable {
case all
case mentionsOnly
case mute
}
protocol RoomNotificationRepository {
typealias Completion = () -> Void
typealias NotificationSettingCallback = (RoomNotificationState) -> Void
func observeNotificationState(listener: @escaping NotificationSettingCallback)
func update(state: RoomNotificationState, completion: @escaping Completion)
var notificationState: RoomNotificationState { get }
}
final class RoomNotificationRepositoryImpl: RoomNotificationRepository {
// MARK: - Properties
// MARK: Private
private let room: MXRoom
private var notificationCenterDidUpdateObserver: NSObjectProtocol?
private var notificationCenterDidFailObserver: NSObjectProtocol?
private var observers: [ObjectIdentifier] = []
// MARK: Public
var notificationState: RoomNotificationState {
room.notificationState
}
// MARK: - Setup
init(room: MXRoom) {
self.room = room
}
deinit {
observers.forEach(NotificationCenter.default.removeObserver)
}
// MARK: - Public
func observeNotificationState(listener: @escaping NotificationSettingCallback) {
let observer = NotificationCenter.default.addObserver(
forName: NSNotification.Name(rawValue: kMXNotificationCenterDidUpdateRules),
object: nil,
queue: OperationQueue.main) { [weak self] _ in
guard let self = self else { return }
listener(self.room.notificationState)
}
observers += [ObjectIdentifier(observer)]
}
func update(state: RoomNotificationState, completion: @escaping Completion) {
switch state {
case .all:
allMessages(completion: completion)
case .mentionsOnly:
mentionsOnly(completion: completion)
case .mute:
mute(completion: completion)
}
}
// MARK: - Private
private func mute(completion: @escaping Completion) {
guard !room.isMuted else {
completion()
return
}
if let rule = room.roomPushRule, room.isMentionsOnly {
removePushRule(rule: rule) {
self.mute(completion: completion)
}
return
}
guard let rule = room.overridePushRule else {
self.addPushRuleToMute(completion: completion)
return
}
guard notificationCenterDidUpdateObserver == nil else {
MXLog.debug("[MXRoom+Riot] Request in progress: ignore push rule update")
completion()
return
}
// if the user defined one, use it
if rule.actionsContains(actionType: MXPushRuleActionTypeDontNotify) {
enablePushRule(rule: rule, completion: completion)
} else {
removePushRule(rule: rule) {
self.addPushRuleToMute(completion: completion)
}
}
}
private func mentionsOnly(completion: @escaping Completion) {
guard !room.isMentionsOnly else {
completion()
return
}
if let rule = room.overridePushRule, room.isMuted {
removePushRule(rule: rule) {
self.mentionsOnly(completion: completion)
}
return
}
guard let rule = room.roomPushRule else {
addPushRuleToMentionOnly(completion: completion)
return
}
guard notificationCenterDidUpdateObserver == nil else {
MXLog.debug("[MXRoom+Riot] Request in progress: ignore push rule update")
completion()
return
}
// if the user defined one, use it
if rule.actionsContains(actionType: MXPushRuleActionTypeDontNotify) {
enablePushRule(rule: rule, completion: completion)
} else {
removePushRule(rule: rule) {
self.addPushRuleToMentionOnly(completion: completion)
}
}
}
private func allMessages(completion: @escaping Completion) {
if !room.isMentionsOnly && !room.isMuted {
completion()
return
}
if let rule = room.overridePushRule, room.isMuted {
removePushRule(rule: rule) {
self.allMessages(completion: completion)
}
return
}
if let rule = room.roomPushRule, room.isMentionsOnly {
removePushRule(rule: rule, completion: completion)
}
}
private func addPushRuleToMentionOnly(completion: @escaping Completion) {
handleUpdateCallback(completion) { [weak self] in
guard let self = self else { return true }
return self.room.roomPushRule != nil
}
handleFailureCallback(completion)
room.mxSession.notificationCenter.addRoomRule(
room.roomId,
notify: false,
sound: false,
highlight: false)
}
private func addPushRuleToMute(completion: @escaping Completion) {
guard let roomId = room.roomId else {
return
}
handleUpdateCallback(completion) { [weak self] in
guard let self = self else { return true }
return self.room.overridePushRule != nil
}
handleFailureCallback(completion)
room.mxSession.notificationCenter.addOverrideRule(
withId: roomId,
conditions: [["kind": "event_match", "key": "room_id", "pattern": roomId]],
notify: false,
sound: false,
highlight: false
)
}
private func removePushRule(rule: MXPushRule, completion: @escaping Completion) {
handleUpdateCallback(completion) { [weak self] in
guard let self = self else { return true }
return self.room.mxSession.notificationCenter.rule(byId: rule.ruleId) == nil
}
handleFailureCallback(completion)
room.mxSession.notificationCenter.removeRule(rule)
}
private func enablePushRule(rule: MXPushRule, completion: @escaping Completion) {
handleUpdateCallback(completion) {
// No way to check whether this notification concerns the push rule. Consider the change is applied.
return true
}
handleFailureCallback(completion)
room.mxSession.notificationCenter.enableRule(rule, isEnabled: true)
}
private func handleUpdateCallback(_ completion: @escaping Completion, releaseCheck: @escaping () -> Bool) {
notificationCenterDidUpdateObserver = NotificationCenter.default.addObserver(
forName: NSNotification.Name(rawValue: kMXNotificationCenterDidUpdateRules),
object: nil,
queue: OperationQueue.main) { [weak self] _ in
guard let self = self else { return }
if releaseCheck() {
self.removeObservers()
completion()
}
}
}
private func handleFailureCallback(_ completion: @escaping Completion) {
notificationCenterDidFailObserver = NotificationCenter.default.addObserver(
forName: NSNotification.Name(rawValue: kMXNotificationCenterDidFailRulesUpdate),
object: nil,
queue: OperationQueue.main) { [weak self] _ in
guard let self = self else { return }
self.removeObservers()
completion()
}
}
func removeObservers() {
if let observer = self.notificationCenterDidUpdateObserver {
NotificationCenter.default.removeObserver(observer)
self.notificationCenterDidUpdateObserver = nil
}
if let observer = self.notificationCenterDidFailObserver {
NotificationCenter.default.removeObserver(observer)
self.notificationCenterDidFailObserver = nil
}
}
}
// We could move these to their own file and make available in global namespace or move to sdk but they are only used here at the moment
fileprivate extension MXRoom {
typealias Completion = () -> Void
func getRoomRule(from rules: [Any]) -> MXPushRule? {
guard let pushRules = rules as? [MXPushRule] else {
return nil
}
return pushRules.first(where: { self.roomId == $0.ruleId })
}
var overridePushRule: MXPushRule? {
getRoomRule(from: mxSession.notificationCenter.rules.global.override)
}
var roomPushRule: MXPushRule? {
getRoomRule(from: mxSession.notificationCenter.rules.global.room)
}
var notificationState: RoomNotificationState {
if isMuted {
return .mute
}
if isMentionsOnly {
return .mentionsOnly
}
return .all
}
var isMuted: Bool {
// Check whether an override rule has been defined with the roomm id as rule id.
// This kind of rule is created to mute the room
guard let rule = self.overridePushRule,
rule.actionsContains(actionType: MXPushRuleActionTypeDontNotify),
rule.conditionIsEnabled(kind: .eventMatch, for: roomId) else {
return false
}
return rule.enabled
}
var isMentionsOnly: Bool {
// Check push rules at room level
guard let rule = roomPushRule else { return false }
return rule.enabled && rule.actionsContains(actionType: MXPushRuleActionTypeDontNotify)
}
}
fileprivate extension MXPushRule {
func actionsContains(actionType: MXPushRuleActionType) -> Bool {
guard let actions = actions as? [MXPushRuleAction] else {
return false
}
return actions.contains(where: { $0.actionType == actionType })
}
func conditionIsEnabled(kind: MXPushRuleConditionType, for roomId: String) -> Bool {
guard let conditions = conditions as? [MXPushRuleCondition] else {
return false
}
let ruleContainsCondition = conditions.contains { condition in
guard case kind = MXPushRuleConditionType(identifier: condition.kind),
let key = condition.parameters["key"] as? String,
let pattern = condition.parameters["pattern"] as? String
else { return false }
return key == "room_id" && pattern == roomId
}
return ruleContainsCondition && enabled
}
}
@@ -0,0 +1,68 @@
// File created from ScreenTemplate
// $ createScreen.sh Room/NotificationSettings RoomNotificationSettings
/*
Copyright 2020 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 UIKit
final class RoomNotificationSettingsCoordinator: RoomNotificationSettingsCoordinatorType {
// MARK: - Properties
// MARK: Private
private var roomNotificationSettingsViewModel: RoomNotificationSettingsViewModelType
private let roomNotificationSettingsViewController: RoomNotificationSettingsViewController
// MARK: Public
// Must be used only internally
var childCoordinators: [Coordinator] = []
weak var delegate: RoomNotificationSettingsCoordinatorDelegate?
// MARK: - Setup
init(room: MXRoom) {
let repository = RoomNotificationRepositoryImpl(room: room)
let roomNotificationSettingsViewModel = RoomNotificationSettingsViewModel(roomNotificationRepository: repository)
let roomNotificationSettingsViewController = RoomNotificationSettingsViewController.instantiate(with: roomNotificationSettingsViewModel)
self.roomNotificationSettingsViewModel = roomNotificationSettingsViewModel
self.roomNotificationSettingsViewController = roomNotificationSettingsViewController
}
// MARK: - Public methods
func start() {
self.roomNotificationSettingsViewModel.coordinatorDelegate = self
}
func toPresentable() -> UIViewController {
return self.roomNotificationSettingsViewController
}
}
// MARK: - RoomNotificationSettingsViewModelCoordinatorDelegate
extension RoomNotificationSettingsCoordinator: RoomNotificationSettingsViewModelCoordinatorDelegate {
func roomNotificationSettingsViewModelDidComplete(_ viewModel: RoomNotificationSettingsViewModelType) {
self.delegate?.roomNotificationSettingsCoordinatorDidComplete(self)
}
func roomNotificationSettingsViewModelDidCancel(_ viewModel: RoomNotificationSettingsViewModelType) {
self.delegate?.roomNotificationSettingsCoordinatorDidCancel(self)
}
}
@@ -0,0 +1,29 @@
// File created from ScreenTemplate
// $ createScreen.sh Room/NotificationSettings RoomNotificationSettings
/*
Copyright 2020 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
protocol RoomNotificationSettingsCoordinatorDelegate: AnyObject {
func roomNotificationSettingsCoordinatorDidComplete(_ coordinator: RoomNotificationSettingsCoordinatorType)
func roomNotificationSettingsCoordinatorDidCancel(_ coordinator: RoomNotificationSettingsCoordinatorType)
}
/// `RoomNotificationSettingsCoordinatorType` is a protocol describing a Coordinator that handle key backup setup passphrase navigation flow.
protocol RoomNotificationSettingsCoordinatorType: Coordinator, Presentable {
var delegate: RoomNotificationSettingsCoordinatorDelegate? { get }
}
@@ -0,0 +1,26 @@
// File created from ScreenTemplate
// $ createScreen.sh Room/NotificationSettings RoomNotificationSettings
/*
Copyright 2020 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
/// RoomNotificationSettingsViewController view actions exposed to view model
enum RoomNotificationSettingsViewAction {
case load
case selectNotificationState(RoomNotificationState)
case save
case cancel
}
@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="18122" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="V8j-Lb-PgC">
<device id="retina6_1" orientation="portrait" appearance="light"/>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="18093"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="System colors in document resources" minToolsVersion="11.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--Room Notification Settings View Controller-->
<scene sceneID="mt5-wz-YKA">
<objects>
<viewController extendedLayoutIncludesOpaqueBars="YES" automaticallyAdjustsScrollViewInsets="NO" id="V8j-Lb-PgC" customClass="RoomNotificationSettingsViewController" customModule="Riot" customModuleProvider="target" sceneMemberID="viewController">
<view key="view" contentMode="scaleToFill" id="EL9-GA-lwo">
<rect key="frame" x="0.0" y="0.0" width="414" height="896"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<tableView clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="grouped" separatorStyle="default" rowHeight="-1" estimatedRowHeight="-1" sectionHeaderHeight="18" sectionFooterHeight="18" translatesAutoresizingMaskIntoConstraints="NO" id="1Jo-Pf-c9m">
<rect key="frame" x="0.0" y="44" width="414" height="818"/>
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
<connections>
<outlet property="dataSource" destination="V8j-Lb-PgC" id="pQ7-Q4-4cn"/>
<outlet property="delegate" destination="V8j-Lb-PgC" id="snv-x4-IWg"/>
</connections>
</tableView>
</subviews>
<viewLayoutGuide key="safeArea" id="bFg-jh-JZB"/>
<color key="backgroundColor" red="0.94509803921568625" green="0.96078431372549022" blue="0.97254901960784312" alpha="1" colorSpace="calibratedRGB"/>
<constraints>
<constraint firstItem="bFg-jh-JZB" firstAttribute="bottom" secondItem="1Jo-Pf-c9m" secondAttribute="bottom" id="TYv-T2-NmY"/>
<constraint firstItem="1Jo-Pf-c9m" firstAttribute="leading" secondItem="bFg-jh-JZB" secondAttribute="leading" id="f6H-cf-mjJ"/>
<constraint firstItem="1Jo-Pf-c9m" firstAttribute="top" secondItem="bFg-jh-JZB" secondAttribute="top" id="gcX-5S-aMb"/>
<constraint firstItem="bFg-jh-JZB" firstAttribute="trailing" secondItem="1Jo-Pf-c9m" secondAttribute="trailing" id="hJ7-5d-23W"/>
</constraints>
</view>
<connections>
<outlet property="mainTableView" destination="1Jo-Pf-c9m" id="Edg-Ng-fo9"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="zK0-v6-7Wt" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="-3198" y="-647"/>
</scene>
</scenes>
<resources>
<systemColor name="systemBackgroundColor">
<color white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</systemColor>
</resources>
</document>
@@ -0,0 +1,274 @@
// File created from ScreenTemplate
// $ createScreen.sh Room/NotificationSettings RoomNotificationSettings
/*
Copyright 2020 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 UIKit
final class RoomNotificationSettingsViewController: UIViewController {
// MARK: - Properties
private enum Constants {
static let plainStyleCellReuseIdentifier = "plain"
static let linkToAccountSettings = "linkToAccountSettings"
}
// MARK: Outlets
@IBOutlet private weak var mainTableView: UITableView!
// MARK: Private
private var viewModel: RoomNotificationSettingsViewModelType!
private var theme: Theme!
private var errorPresenter: MXKErrorPresentation!
private var activityPresenter: ActivityIndicatorPresenter!
private enum RowType {
case plain
}
private struct Row {
var type: RowType
var setting: RoomNotificationState
var text: String?
var accessoryType: UITableViewCell.AccessoryType = .none
var action: (() -> Void)?
}
private struct Section {
var header: String?
var rows: [Row]
var footer: NSAttributedString?
}
private var sections: [Section] = [] {
didSet {
mainTableView.reloadData()
}
}
private var viewState: RoomNotificationSettingsViewState!
// MARK: - Setup
class func instantiate(with viewModel: RoomNotificationSettingsViewModelType) -> RoomNotificationSettingsViewController {
let viewController = StoryboardScene.RoomNotificationSettingsViewController.initialScene.instantiate()
viewController.viewModel = viewModel
viewController.theme = ThemeService.shared().theme
return viewController
}
// MARK: - Life cycle
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
setupViews()
activityPresenter = ActivityIndicatorPresenter()
errorPresenter = MXKErrorAlertPresentation()
registerThemeServiceDidChangeThemeNotification()
update(theme: theme)
viewModel.viewDelegate = self
viewModel.process(viewAction: .load)
}
override var preferredStatusBarStyle: UIStatusBarStyle {
return theme.statusBarStyle
}
// MARK: - Private
private func update(theme: Theme) {
self.theme = theme
view.backgroundColor = theme.headerBackgroundColor
mainTableView.backgroundColor = theme.headerBackgroundColor
if let navigationBar = navigationController?.navigationBar {
theme.applyStyle(onNavigationBar: navigationBar)
}
}
private func registerThemeServiceDidChangeThemeNotification() {
NotificationCenter.default.addObserver(self, selector: #selector(themeDidChange), name: .themeServiceDidChangeTheme, object: nil)
}
@objc private func themeDidChange() {
update(theme: ThemeService.shared().theme)
}
private func setupViews() {
let doneBarButtonItem = MXKBarButtonItem(title: "Done", style: .plain) { [weak self] in
self?.viewModel.process(viewAction: .save)
}
let cancelBarButtonItem = MXKBarButtonItem(title: "Cancel", style: .plain) { [weak self] in
self?.viewModel.process(viewAction: .cancel)
}
if navigationController?.navigationBar.backItem == nil {
navigationItem.leftBarButtonItem = cancelBarButtonItem
}
navigationItem.rightBarButtonItem = doneBarButtonItem
}
private func render(viewState: RoomNotificationSettingsViewState) {
if viewState.saving {
activityPresenter.presentActivityIndicator(on: view, animated: true)
} else {
activityPresenter.removeCurrentActivityIndicator(animated: true)
}
self.viewState = viewState
updateSections()
}
private func updateSections() {
let rows = RoomNotificationState.allCases.map({ (setting) -> Row in
return Row(type: .plain,
setting: setting,
text: setting.title,
accessoryType: viewState.notificationState == setting ? .checkmark : .none,
action: {
self.viewModel.process(viewAction: .selectNotificationState(setting))
})
})
let formatStr = "You can manage keywords in the %@"
let linkStr = "Account Settings"
let formattedStr = String(format: formatStr, arguments: [linkStr])
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineHeightMultiple = 1.16
let footer_0 = NSMutableAttributedString(string: formattedStr, attributes: [
NSAttributedString.Key.kern: -0.08,
NSAttributedString.Key.paragraphStyle: paragraphStyle,
NSAttributedString.Key.font: UIFont.systemFont(ofSize: 13.0)
])
let linkRange = (footer_0.string as NSString).range(of: linkStr)
footer_0.addAttribute(NSAttributedString.Key.link, value: Constants.linkToAccountSettings, range: linkRange)
let section0 = Section(header: nil, rows: rows, footer: footer_0)
sections = [
section0
]
}
}
// MARK - UITableViewDataSource
extension RoomNotificationSettingsViewController: UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return sections.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return sections[section].rows.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let row = sections[indexPath.section].rows[indexPath.row]
switch row.type {
case .plain:
var cell: UITableViewCell! = tableView.dequeueReusableCell(withIdentifier: Constants.plainStyleCellReuseIdentifier)
if cell == nil {
cell = UITableViewCell(style: .value1, reuseIdentifier: Constants.plainStyleCellReuseIdentifier)
}
cell.textLabel?.font = .systemFont(ofSize: 17)
cell.detailTextLabel?.font = .systemFont(ofSize: 16)
cell.textLabel?.text = row.text
if row.accessoryType == .checkmark {
cell.accessoryView = UIImageView(image: Asset.Images.checkmark.image)
} else {
cell.accessoryView = nil
cell.accessoryType = row.accessoryType
}
cell.textLabel?.textColor = theme.textPrimaryColor
cell.detailTextLabel?.textColor = theme.textSecondaryColor
cell.backgroundColor = theme.backgroundColor
cell.contentView.backgroundColor = .clear
cell.tintColor = theme.tintColor
return cell
}
}
}
// MARK - UITableViewDelegate
extension RoomNotificationSettingsViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cell.backgroundColor = theme.backgroundColor
cell.selectedBackgroundView = UIView()
cell.selectedBackgroundView?.backgroundColor = theme.selectedBackgroundColor
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return sections[section].header
}
func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
return sections[section].footer?.string
}
// func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
// if sections[section].footer == nil {
// return nil
// }
//
// let view = tableView.dequeueReusableHeaderFooterView(RiotTableViewHeaderFooterView.self)
//
// view?.textView.attributedText = sections[section].footer
// view?.update(theme: theme)
// view?.delegate = self
//
// return view
// }
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
let row = sections[indexPath.section].rows[indexPath.row]
row.action?()
}
}
// MARK: - RoomNotificationSettingsViewModelViewDelegate
extension RoomNotificationSettingsViewController: RoomNotificationSettingsViewModelViewDelegate {
func roomNotificationSettingsViewModel(_ viewModel: RoomNotificationSettingsViewModelType, didUpdateViewState viewSate: RoomNotificationSettingsViewState) {
render(viewState: viewSate)
}
}
extension RoomNotificationState {
var title: String {
switch self {
case .all:
return "All Messages"
case .mentionsOnly:
return "Mentions and Keywords only"
case .mute:
return "None"
}
}
}
@@ -0,0 +1,75 @@
// File created from ScreenTemplate
// $ createScreen.sh Room/NotificationSettings RoomNotificationSettings
/*
Copyright 2020 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
final class RoomNotificationSettingsViewModel: RoomNotificationSettingsViewModelType {
// MARK: - Properties
// MARK: Private
private let roomNotificationRepository: RoomNotificationRepository
private var state: RoomNotificationSettingsViewStateImpl {
willSet {
update(viewState: newValue)
}
}
// MARK: Public
weak var viewDelegate: RoomNotificationSettingsViewModelViewDelegate?
weak var coordinatorDelegate: RoomNotificationSettingsViewModelCoordinatorDelegate?
// MARK: - Setup
init(roomNotificationRepository: RoomNotificationRepository) {
self.roomNotificationRepository = roomNotificationRepository
self.state = RoomNotificationSettingsViewStateImpl(saving: false, notificationState: roomNotificationRepository.notificationState)
self.roomNotificationRepository.observeNotificationState { state in
self.state.notificationState = state
}
}
// MARK: - Public
func process(viewAction: RoomNotificationSettingsViewAction) {
switch viewAction {
case .load:
update(viewState: self.state)
case .selectNotificationState(let state):
self.state.notificationState = state
case .save:
self.state.saving = true
roomNotificationRepository.update(state: state.notificationState) { [weak self] in
guard let self = self else { return }
self.state.saving = false
self.coordinatorDelegate?.roomNotificationSettingsViewModelDidComplete(self)
}
case .cancel:
coordinatorDelegate?.roomNotificationSettingsViewModelDidCancel(self)
}
}
// MARK: - Private
private func update(viewState: RoomNotificationSettingsViewState) {
self.viewDelegate?.roomNotificationSettingsViewModel(self, didUpdateViewState: viewState)
}
}
@@ -0,0 +1,37 @@
// File created from ScreenTemplate
// $ createScreen.sh Room/NotificationSettings RoomNotificationSettings
/*
Copyright 2020 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
protocol RoomNotificationSettingsViewModelViewDelegate: AnyObject {
func roomNotificationSettingsViewModel(_ viewModel: RoomNotificationSettingsViewModelType, didUpdateViewState viewSate: RoomNotificationSettingsViewState)
}
protocol RoomNotificationSettingsViewModelCoordinatorDelegate: AnyObject {
func roomNotificationSettingsViewModelDidComplete(_ viewModel: RoomNotificationSettingsViewModelType)
func roomNotificationSettingsViewModelDidCancel(_ viewModel: RoomNotificationSettingsViewModelType)
}
/// Protocol describing the view model used by `RoomNotificationSettingsViewController`
protocol RoomNotificationSettingsViewModelType {
var viewDelegate: RoomNotificationSettingsViewModelViewDelegate? { get set }
var coordinatorDelegate: RoomNotificationSettingsViewModelCoordinatorDelegate? { get set }
func process(viewAction: RoomNotificationSettingsViewAction)
}
@@ -0,0 +1,30 @@
// File created from ScreenTemplate
// $ createScreen.sh Room/NotificationSettings RoomNotificationSettings
/*
Copyright 2020 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
/// RoomNotificationSettingsViewController view state
struct RoomNotificationSettingsViewStateImpl: RoomNotificationSettingsViewState {
var saving: Bool
var notificationState: RoomNotificationState
}
protocol RoomNotificationSettingsViewState {
var saving: Bool { get }
var notificationState: RoomNotificationState { get }
}