Edit the default notification settings (#1468)

This commit is contained in:
Nicolas Mauri
2023-08-11 16:10:23 +02:00
committed by GitHub
parent f413a6b01a
commit 9a52b968f1
23 changed files with 760 additions and 48 deletions
@@ -0,0 +1,51 @@
//
// Copyright 2022 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 Combine
import SwiftUI
struct NotificationSettingsEditScreenCoordinatorParameters {
let isDirect: Bool
let notificationSettings: NotificationSettingsProxyProtocol
}
enum NotificationSettingsEditScreenCoordinatorAction { }
final class NotificationSettingsEditScreenCoordinator: CoordinatorProtocol {
private let parameters: NotificationSettingsEditScreenCoordinatorParameters
private var viewModel: NotificationSettingsEditScreenViewModelProtocol
private let actionsSubject: PassthroughSubject<NotificationSettingsEditScreenCoordinatorAction, Never> = .init()
private var cancellables: Set<AnyCancellable> = .init()
var actions: AnyPublisher<NotificationSettingsEditScreenCoordinatorAction, Never> {
actionsSubject.eraseToAnyPublisher()
}
init(parameters: NotificationSettingsEditScreenCoordinatorParameters) {
self.parameters = parameters
viewModel = NotificationSettingsEditScreenViewModel(isDirect: parameters.isDirect,
notificationSettingsProxy: parameters.notificationSettings)
}
func start() {
viewModel.fetchInitialContent()
}
func toPresentable() -> AnyView {
AnyView(NotificationSettingsEditScreen(context: viewModel.context))
}
}
@@ -0,0 +1,73 @@
//
// Copyright 2022 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 NotificationSettingsEditScreenViewModelAction { }
enum NotificationSettingsEditScreenDefaultMode {
case allMessages
case mentionsAndKeywordsOnly
}
struct NotificationSettingsEditScreenViewState: BindableState {
var bindings: NotificationSettingsEditScreenViewStateBindings
var strings: NotificationSettingsEditScreenStrings
var isDirect: Bool
var availableDefaultModes: [NotificationSettingsEditScreenDefaultMode] = [.allMessages, .mentionsAndKeywordsOnly]
var defaultMode: NotificationSettingsEditScreenDefaultMode?
var pendingMode: NotificationSettingsEditScreenDefaultMode?
func isSelected(mode: NotificationSettingsEditScreenDefaultMode) -> Bool {
pendingMode == nil && defaultMode == mode
}
}
struct NotificationSettingsEditScreenViewStateBindings {
var alertInfo: AlertInfo<NotificationSettingsEditScreenErrorType>?
}
enum NotificationSettingsEditScreenViewAction {
case setMode(NotificationSettingsEditScreenDefaultMode)
}
enum NotificationSettingsEditScreenErrorType: Hashable {
case setModeFailed
}
struct NotificationSettingsEditScreenStrings {
let navigationTitle: String
let modeSectionTitle: String
init(isDirect: Bool) {
if isDirect {
navigationTitle = L10n.screenNotificationSettingsDirectChats
modeSectionTitle = L10n.screenNotificationSettingsEditScreenDirectSectionHeader
} else {
navigationTitle = L10n.screenNotificationSettingsGroupChats
modeSectionTitle = L10n.screenNotificationSettingsEditScreenGroupSectionHeader
}
}
func string(for mode: NotificationSettingsEditScreenDefaultMode) -> String {
switch mode {
case .allMessages:
return L10n.screenNotificationSettingsModeAll
case .mentionsAndKeywordsOnly:
return L10n.screenNotificationSettingsModeMentions
}
}
}
@@ -0,0 +1,120 @@
//
// Copyright 2022 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 Combine
import SwiftUI
typealias NotificationSettingsEditScreenViewModelType = StateStoreViewModel<NotificationSettingsEditScreenViewState, NotificationSettingsEditScreenViewAction>
class NotificationSettingsEditScreenViewModel: NotificationSettingsEditScreenViewModelType, NotificationSettingsEditScreenViewModelProtocol {
private var actionsSubject: PassthroughSubject<NotificationSettingsEditScreenViewModelAction, Never> = .init()
private let isDirect: Bool
private let notificationSettingsProxy: NotificationSettingsProxyProtocol
@CancellableTask private var fetchSettingsTask: Task<Void, Error>?
var actions: AnyPublisher<NotificationSettingsEditScreenViewModelAction, Never> {
actionsSubject.eraseToAnyPublisher()
}
init(isDirect: Bool, notificationSettingsProxy: NotificationSettingsProxyProtocol) {
let bindings = NotificationSettingsEditScreenViewStateBindings()
self.isDirect = isDirect
self.notificationSettingsProxy = notificationSettingsProxy
super.init(initialViewState: NotificationSettingsEditScreenViewState(bindings: bindings,
strings: NotificationSettingsEditScreenStrings(isDirect: isDirect),
isDirect: isDirect))
setupNotificationSettingsSubscription()
}
func fetchInitialContent() {
fetchSettings()
}
// MARK: - Public
override func process(viewAction: NotificationSettingsEditScreenViewAction) {
switch viewAction {
case .setMode(let mode):
setMode(mode)
}
}
// MARK: - Private
private func setupNotificationSettingsSubscription() {
notificationSettingsProxy.callbacks
.receive(on: DispatchQueue.main)
.sink { [weak self] callback in
guard let self else { return }
switch callback {
case .settingsDidChange:
self.fetchSettings()
}
}
.store(in: &cancellables)
}
private func fetchSettings() {
fetchSettingsTask = Task {
var mode: RoomNotificationModeProxy?
let encrypted_mode = await notificationSettingsProxy.getDefaultRoomNotificationMode(isEncrypted: true, isOneToOne: isDirect)
let unencrypted_mode = await notificationSettingsProxy.getDefaultRoomNotificationMode(isEncrypted: false, isOneToOne: isDirect)
if encrypted_mode == unencrypted_mode {
mode = encrypted_mode
}
guard !Task.isCancelled else { return }
switch mode {
case .allMessages:
state.defaultMode = .allMessages
case .mentionsAndKeywordsOnly:
state.defaultMode = .mentionsAndKeywordsOnly
default:
state.defaultMode = nil
}
}
}
private func setMode(_ mode: NotificationSettingsEditScreenDefaultMode) {
guard state.pendingMode == nil, !state.isSelected(mode: mode) else { return }
let roomNotificationModeProxy: RoomNotificationModeProxy
switch mode {
case .allMessages:
roomNotificationModeProxy = .allMessages
case .mentionsAndKeywordsOnly:
roomNotificationModeProxy = .mentionsAndKeywordsOnly
}
state.pendingMode = mode
Task {
do {
try await notificationSettingsProxy.setDefaultRoomNotificationMode(isEncrypted: true, isOneToOne: isDirect, mode: roomNotificationModeProxy)
try await notificationSettingsProxy.setDefaultRoomNotificationMode(isEncrypted: false, isOneToOne: isDirect, mode: roomNotificationModeProxy)
} catch {
let retryAction: () -> Void = { [weak self] in
self?.setMode(mode)
}
state.bindings.alertInfo = AlertInfo(id: .setModeFailed,
title: L10n.commonError,
message: L10n.screenNotificationSettingsEditFailedUpdatingDefaultMode,
primaryButton: .init(title: L10n.actionCancel, role: .cancel, action: nil),
secondaryButton: .init(title: L10n.actionRetry, action: retryAction))
}
state.pendingMode = nil
}
}
}
@@ -0,0 +1,25 @@
//
// Copyright 2022 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 Combine
@MainActor
protocol NotificationSettingsEditScreenViewModelProtocol {
var actions: AnyPublisher<NotificationSettingsEditScreenViewModelAction, Never> { get }
var context: NotificationSettingsEditScreenViewModelType.Context { get }
func fetchInitialContent()
}
@@ -0,0 +1,100 @@
//
// Copyright 2022 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 SwiftUI
struct NotificationSettingsEditScreen: View {
@ObservedObject var context: NotificationSettingsEditScreenViewModel.Context
var body: some View {
Form {
notificationModeSection
}
.compoundForm()
.navigationTitle(context.viewState.strings.navigationTitle)
.alert(item: $context.alertInfo)
.track(screen: .settingsDefaultNotifications)
}
// MARK: - Private
private var notificationModeSection: some View {
Section {
ForEach(context.viewState.availableDefaultModes, id: \.self) { mode in
Button {
context.send(viewAction: .setMode(mode))
} label: {
LabeledContent {
if context.viewState.pendingMode == mode {
ProgressView()
} else {
EmptyView()
}
} label: {
Text(context.viewState.strings.string(for: mode))
}
}
.buttonStyle(.compoundForm(accessory: .selected(context.viewState.isSelected(mode: mode))))
.disabled(context.viewState.pendingMode != nil)
}
} header: {
Text(context.viewState.strings.modeSectionTitle)
.compoundFormSectionHeader()
}
.compoundFormSection()
}
}
// MARK: - Previews
struct NotificationSettingsEditScreen_Previews: PreviewProvider {
static let viewModelGroupChats: NotificationSettingsEditScreenViewModel = {
let notificationSettingsProxy = NotificationSettingsProxyMock(with: .init())
notificationSettingsProxy.getDefaultRoomNotificationModeIsEncryptedIsOneToOneReturnValue = .allMessages
var viewModel = NotificationSettingsEditScreenViewModel(isDirect: false,
notificationSettingsProxy: notificationSettingsProxy)
viewModel.fetchInitialContent()
return viewModel
}()
static let viewModelDirectChats: NotificationSettingsEditScreenViewModel = {
let notificationSettingsProxy = NotificationSettingsProxyMock(with: .init())
notificationSettingsProxy.getDefaultRoomNotificationModeIsEncryptedIsOneToOneReturnValue = .mentionsAndKeywordsOnly
var viewModel = NotificationSettingsEditScreenViewModel(isDirect: true,
notificationSettingsProxy: notificationSettingsProxy)
viewModel.fetchInitialContent()
return viewModel
}()
static let viewModelDirectApplyingChange: NotificationSettingsEditScreenViewModel = {
let notificationSettingsProxy = NotificationSettingsProxyMock(with: .init())
notificationSettingsProxy.getDefaultRoomNotificationModeIsEncryptedIsOneToOneReturnValue = .mentionsAndKeywordsOnly
var viewModel = NotificationSettingsEditScreenViewModel(isDirect: true,
notificationSettingsProxy: notificationSettingsProxy)
viewModel.state.pendingMode = .mentionsAndKeywordsOnly
viewModel.fetchInitialContent()
return viewModel
}()
static var previews: some View {
NotificationSettingsEditScreen(context: viewModelGroupChats.context)
.previewDisplayName("Group Chats")
NotificationSettingsEditScreen(context: viewModelDirectChats.context)
.previewDisplayName("Direct Chats")
NotificationSettingsEditScreen(context: viewModelDirectApplyingChange.context)
.previewDisplayName("Applying change")
}
}
@@ -18,6 +18,7 @@ import Combine
import SwiftUI
struct NotificationSettingsScreenCoordinatorParameters {
weak var navigationStackCoordinator: NavigationStackCoordinator?
let userNotificationCenter: UserNotificationCenterProtocol
let notificationSettings: NotificationSettingsProxyProtocol
let isModallyPresented: Bool
@@ -32,7 +33,11 @@ final class NotificationSettingsScreenCoordinator: CoordinatorProtocol {
private var viewModel: NotificationSettingsScreenViewModelProtocol
private let actionsSubject: PassthroughSubject<NotificationSettingsScreenCoordinatorAction, Never> = .init()
private var cancellables: Set<AnyCancellable> = .init()
private var navigationStackCoordinator: NavigationStackCoordinator? {
parameters.navigationStackCoordinator
}
var actions: AnyPublisher<NotificationSettingsScreenCoordinatorAction, Never> {
actionsSubject.eraseToAnyPublisher()
}
@@ -50,15 +55,27 @@ final class NotificationSettingsScreenCoordinator: CoordinatorProtocol {
viewModel.fetchInitialContent()
viewModel.actions.sink { [weak self] action in
guard let self else { return }
switch action {
case .close:
self?.actionsSubject.send(.close)
self.actionsSubject.send(.close)
case .editDefaultMode(let isDirect):
self.presentEditScreen(isDirect: isDirect)
}
}
.store(in: &cancellables)
}
func toPresentable() -> AnyView {
AnyView(NotificationSettingsScreen(context: viewModel.context))
}
// MARK: - Private
private func presentEditScreen(isDirect: Bool) {
let editSettingsParameters = NotificationSettingsEditScreenCoordinatorParameters(isDirect: isDirect,
notificationSettings: parameters.notificationSettings)
let editSettingsCoordinator = NotificationSettingsEditScreenCoordinator(parameters: editSettingsParameters)
navigationStackCoordinator?.push(editSettingsCoordinator)
}
}
@@ -19,6 +19,7 @@ import UIKit
enum NotificationSettingsScreenViewModelAction {
case close
case editDefaultMode(isDirect: Bool)
}
struct NotificationSettingsScreenViewState: BindableState {
@@ -60,9 +60,9 @@ class NotificationSettingsScreenViewModel: NotificationSettingsScreenViewModelTy
case .changedEnableNotifications:
toggleNotifications()
case .groupChatsTapped:
break
actionsSubject.send(.editDefaultMode(isDirect: false))
case .directChatsTapped:
break
actionsSubject.send(.editDefaultMode(isDirect: true))
case .roomMentionChanged:
guard let settings = state.settings, settings.roomMentionsEnabled != state.bindings.roomMentionsEnabled else {
return
@@ -120,14 +120,12 @@ class NotificationSettingsScreenViewModel: NotificationSettingsScreenViewModelTy
private func fetchSettings() {
fetchSettingsTask = Task {
// Group chats
// A group chat is a chat having more than 2 active members
var groupChatsMode = await notificationSettingsProxy.getDefaultNotificationRoomMode(isEncrypted: false, isOneToOne: false)
let encryptedGroupChatsMode = await notificationSettingsProxy.getDefaultNotificationRoomMode(isEncrypted: true, isOneToOne: false)
var groupChatsMode = await notificationSettingsProxy.getDefaultRoomNotificationMode(isEncrypted: false, isOneToOne: false)
let encryptedGroupChatsMode = await notificationSettingsProxy.getDefaultRoomNotificationMode(isEncrypted: true, isOneToOne: false)
// Direct chats
// A direct chat is a chat having exactly 2 active members
var directChatsMode = await notificationSettingsProxy.getDefaultNotificationRoomMode(isEncrypted: false, isOneToOne: true)
let encryptedDirectChatsMode = await notificationSettingsProxy.getDefaultNotificationRoomMode(isEncrypted: true, isOneToOne: true)
var directChatsMode = await notificationSettingsProxy.getDefaultRoomNotificationMode(isEncrypted: false, isOneToOne: true)
let encryptedDirectChatsMode = await notificationSettingsProxy.getDefaultRoomNotificationMode(isEncrypted: true, isOneToOne: true)
// Old clients were having specific settings for encrypted and unencrypted rooms,
// so it's possible for `group chats` and `direct chats` settings to be inconsistent (e.g. encrypted `direct chats` can have a different mode that unencrypted `direct chats`)
@@ -183,7 +183,7 @@ struct NotificationSettingsScreen_Previews: PreviewProvider {
let notificationCenter = UserNotificationCenterMock()
notificationCenter.authorizationStatusReturnValue = .notDetermined
let notificationSettingsProxy = NotificationSettingsProxyMock(with: .init())
notificationSettingsProxy.getDefaultNotificationRoomModeIsEncryptedIsOneToOneClosure = { isEncrypted, isOneToOne in
notificationSettingsProxy.getDefaultRoomNotificationModeIsEncryptedIsOneToOneClosure = { isEncrypted, isOneToOne in
switch (isEncrypted, isOneToOne) {
case (_, true):
return .allMessages
@@ -141,7 +141,8 @@ final class SettingsScreenCoordinator: CoordinatorProtocol {
}
private func presentNotificationSettings() {
let notificationParameters = NotificationSettingsScreenCoordinatorParameters(userNotificationCenter: UNUserNotificationCenter.current(),
let notificationParameters = NotificationSettingsScreenCoordinatorParameters(navigationStackCoordinator: parameters.navigationStackCoordinator,
userNotificationCenter: UNUserNotificationCenter.current(),
notificationSettings: parameters.notificationSettings,
isModallyPresented: false)
let coordinator = NotificationSettingsScreenCoordinator(parameters: notificationParameters)