mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-03 06:55:59 +00:00
Edit the default notification settings (#1468)
This commit is contained in:
+51
@@ -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))
|
||||
}
|
||||
}
|
||||
+73
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
+120
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
+25
@@ -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()
|
||||
}
|
||||
+100
@@ -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")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user