New notifications section in settings (#1273)

This commit is contained in:
Nicolas Mauri
2023-07-10 19:26:49 +02:00
committed by GitHub
parent 2cec1858ff
commit 5ae227179c
26 changed files with 639 additions and 14 deletions
@@ -31,6 +31,7 @@ struct DeveloperOptionsScreenViewStateBindings {
var isEncryptionSyncEnabled: Bool
var locationEventsEnabled: Bool
var shareLocationEnabled: Bool
var notificationSettingsEnabled: Bool
}
enum DeveloperOptionsScreenViewAction {
@@ -40,5 +41,6 @@ enum DeveloperOptionsScreenViewAction {
case changedIsEncryptionSyncEnabled
case changedLocationEventsEnabled
case changedShareLocationEnabled
case changedNotificationSettingsEnabled
case clearCache
}
@@ -31,7 +31,8 @@ class DeveloperOptionsScreenViewModel: DeveloperOptionsScreenViewModelType, Deve
readReceiptsEnabled: appSettings.readReceiptsEnabled,
isEncryptionSyncEnabled: appSettings.isEncryptionSyncEnabled,
locationEventsEnabled: appSettings.locationEventsEnabled,
shareLocationEnabled: appSettings.shareLocationEnabled)
shareLocationEnabled: appSettings.shareLocationEnabled,
notificationSettingsEnabled: appSettings.notificationSettingsEnabled)
let state = DeveloperOptionsScreenViewState(bindings: bindings)
super.init(initialViewState: state)
@@ -55,6 +56,8 @@ class DeveloperOptionsScreenViewModel: DeveloperOptionsScreenViewModelType, Deve
appSettings.locationEventsEnabled = state.bindings.locationEventsEnabled
case .changedShareLocationEnabled:
appSettings.shareLocationEnabled = state.bindings.shareLocationEnabled
case .changedNotificationSettingsEnabled:
appSettings.notificationSettingsEnabled = state.bindings.notificationSettingsEnabled
case .clearCache:
callback?(.clearCache)
}
@@ -66,6 +66,13 @@ struct DeveloperOptionsScreen: View {
.onChange(of: context.shareLocationEnabled) { _ in
context.send(viewAction: .changedShareLocationEnabled)
}
Toggle(isOn: $context.notificationSettingsEnabled) {
Text("Show notification settings")
}
.onChange(of: context.notificationSettingsEnabled) { _ in
context.send(viewAction: .changedNotificationSettingsEnabled)
}
}
Section {
@@ -0,0 +1,48 @@
//
// 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 NotificationSettingsScreenCoordinatorParameters {
let userNotificationCenter: UserNotificationCenterProtocol
}
enum NotificationSettingsScreenCoordinatorAction { }
final class NotificationSettingsScreenCoordinator: CoordinatorProtocol {
private let parameters: NotificationSettingsScreenCoordinatorParameters
private var viewModel: NotificationSettingsScreenViewModelProtocol
private let actionsSubject: PassthroughSubject<NotificationSettingsScreenCoordinatorAction, Never> = .init()
private var cancellables: Set<AnyCancellable> = .init()
var actions: AnyPublisher<NotificationSettingsScreenCoordinatorAction, Never> {
actionsSubject.eraseToAnyPublisher()
}
init(parameters: NotificationSettingsScreenCoordinatorParameters) {
self.parameters = parameters
viewModel = NotificationSettingsScreenViewModel(appSettings: ServiceLocator.shared.settings,
userNotificationCenter: parameters.userNotificationCenter)
}
func start() { }
func toPresentable() -> AnyView {
AnyView(NotificationSettingsScreen(context: viewModel.context))
}
}
@@ -0,0 +1,53 @@
//
// 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
import UIKit
enum NotificationSettingsScreenViewModelAction { }
struct NotificationSettingsScreenViewState: BindableState {
var bindings: NotificationSettingsScreenViewStateBindings
var strings = NotificationSettingsScreenStrings()
var isUserPermissionGranted: Bool?
var showSystemNotificationsAlert: Bool {
bindings.enableNotifications && isUserPermissionGranted == false
}
}
struct NotificationSettingsScreenViewStateBindings {
var enableNotifications = false
}
struct NotificationSettingsScreenStrings {
let changeYourSystemSettings: AttributedString = {
let linkPlaceholder = "{link}"
var text = AttributedString(L10n.screenNotificationSettingsSystemNotificationsActionRequired(linkPlaceholder))
var linkString = AttributedString(L10n.screenNotificationSettingsSystemNotificationsActionRequiredContentLink)
// Note: On the simulator, `UIApplication.openNotificationSettingsURLString` opens the `Settings` application instead of the application's notification settings screen.
linkString.link = URL(string: UIApplication.openNotificationSettingsURLString)
linkString.bold()
text.replace(linkPlaceholder, with: linkString)
return text
}()
}
enum NotificationSettingsScreenViewAction {
case linkClicked(url: URL)
case changedEnableNotifications
}
@@ -0,0 +1,76 @@
//
// 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 NotificationSettingsScreenViewModelType = StateStoreViewModel<NotificationSettingsScreenViewState, NotificationSettingsScreenViewAction>
class NotificationSettingsScreenViewModel: NotificationSettingsScreenViewModelType, NotificationSettingsScreenViewModelProtocol {
private var actionsSubject: PassthroughSubject<NotificationSettingsScreenViewModelAction, Never> = .init()
private let appSettings: AppSettings
private let userNotificationCenter: UserNotificationCenterProtocol
var actions: AnyPublisher<NotificationSettingsScreenViewModelAction, Never> {
actionsSubject.eraseToAnyPublisher()
}
init(appSettings: AppSettings, userNotificationCenter: UserNotificationCenterProtocol) {
self.appSettings = appSettings
self.userNotificationCenter = userNotificationCenter
let bindings = NotificationSettingsScreenViewStateBindings(enableNotifications: appSettings.enableNotifications)
super.init(initialViewState: NotificationSettingsScreenViewState(bindings: bindings))
// Listen for changes to AppSettings.enableNotifications
appSettings.$enableNotifications
.weakAssign(to: \.state.bindings.enableNotifications, on: self)
.store(in: &cancellables)
// Refresh authorization status uppon UIApplication.didBecomeActiveNotification notification
NotificationCenter.default.publisher(for: UIApplication.didBecomeActiveNotification)
.sink { [weak self] _ in
Task {
await self?.readSystemAuthorizationStatus()
}
}
.store(in: &cancellables)
Task {
await readSystemAuthorizationStatus()
}
}
// MARK: - Public
override func process(viewAction: NotificationSettingsScreenViewAction) {
switch viewAction {
case .linkClicked(let url):
MXLog.warning("Link clicked: \(url)")
case .changedEnableNotifications:
toggleNotifications()
}
}
// MARK: - Private
func readSystemAuthorizationStatus() async {
state.isUserPermissionGranted = await userNotificationCenter.authorizationStatus() == .authorized
}
func toggleNotifications() {
appSettings.enableNotifications.toggle()
}
}
@@ -0,0 +1,23 @@
//
// 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 NotificationSettingsScreenViewModelProtocol {
var actions: AnyPublisher<NotificationSettingsScreenViewModelAction, Never> { get }
var context: NotificationSettingsScreenViewModelType.Context { get }
}
@@ -0,0 +1,88 @@
//
// 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 NotificationSettingsScreen: View {
@ObservedObject var context: NotificationSettingsScreenViewModel.Context
var body: some View {
Form {
if context.viewState.showSystemNotificationsAlert {
userPermissionSection
}
enableNotificationSection
}
.compoundForm()
.navigationTitle(L10n.screenNotificationSettingsTitle)
.track(screen: .settingsNotifications)
}
// MARK: - Private
private var userPermissionSection: some View {
Section {
HStack(alignment: .firstTextBaseline, spacing: 13) {
Image(systemName: "exclamationmark.circle.fill")
.foregroundColor(.compound.iconTertiaryAlpha)
VStack(alignment: .leading, spacing: 2) {
Text(L10n.screenNotificationSettingsSystemNotificationsTurnedOff)
.font(.compound.bodyLG)
.foregroundColor(.compound.textPrimary)
Text(context.viewState.strings.changeYourSystemSettings)
.font(.compound.bodySM)
.foregroundColor(.compound.textSecondary)
.tint(.compound.textPrimary)
.environment(\.openURL, OpenURLAction { url in
context.send(viewAction: .linkClicked(url: url))
return .systemAction
})
}
.padding(.vertical, 5)
}
}
.compoundFormSection()
}
private var enableNotificationSection: some View {
Section {
Toggle(isOn: $context.enableNotifications) {
Text(L10n.screenNotificationSettingsEnableNotifications)
}
.toggleStyle(.compoundForm)
.onChange(of: context.enableNotifications) { _ in
context.send(viewAction: .changedEnableNotifications)
}
}
.compoundFormSection()
}
}
// MARK: - Previews
struct NotificationSettingsScreen_Previews: PreviewProvider {
static let viewModel: NotificationSettingsScreenViewModel = {
let appSettings = AppSettings()
let notificationCenter = UserNotificationCenterMock()
notificationCenter.authorizationStatusReturnValue = .notDetermined
var viewModel = NotificationSettingsScreenViewModel(appSettings: appSettings, userNotificationCenter: notificationCenter)
return viewModel
}()
static var previews: some View {
NotificationSettingsScreen(context: viewModel.context)
}
}
@@ -59,6 +59,8 @@ final class SettingsScreenCoordinator: CoordinatorProtocol {
self.presentDeveloperOptions()
case .logout:
self.callback?(.logout)
case .notifications:
self.presentNotificationSettings()
}
}
}
@@ -136,4 +138,12 @@ final class SettingsScreenCoordinator: CoordinatorProtocol {
private func showSuccess(label: String) {
parameters.userIndicatorController?.submitIndicator(UserIndicator(title: label))
}
private func presentNotificationSettings() {
let notificationParameters = NotificationSettingsScreenCoordinatorParameters(
userNotificationCenter: UNUserNotificationCenter.current()
)
let coordinator = NotificationSettingsScreenCoordinator(parameters: notificationParameters)
parameters.navigationStackCoordinator?.push(coordinator)
}
}
@@ -24,6 +24,7 @@ enum SettingsScreenViewModelAction {
case about
case sessionVerification
case developerOptions
case notifications
case logout
}
@@ -34,6 +35,7 @@ struct SettingsScreenViewState: BindableState {
var userAvatarURL: URL?
var userDisplayName: String?
var showSessionVerificationSection: Bool
var showNotificationSettings: Bool
var showDeveloperOptions: Bool
}
@@ -50,4 +52,5 @@ enum SettingsScreenViewAction {
case logout
case changedTimelineStyle
case developerOptions
case notifications
}
@@ -40,6 +40,7 @@ class SettingsScreenViewModel: SettingsScreenViewModelType, SettingsScreenViewMo
deviceID: userSession.deviceID,
userID: userSession.userID,
showSessionVerificationSection: showSessionVerificationSection,
showNotificationSettings: appSettings.notificationSettingsEnabled,
showDeveloperOptions: appSettings.canShowDeveloperOptions),
imageProvider: userSession.mediaProvider)
@@ -51,6 +52,10 @@ class SettingsScreenViewModel: SettingsScreenViewModelType, SettingsScreenViewMo
.weakAssign(to: \.state.userAvatarURL, on: self)
.store(in: &cancellables)
appSettings.$notificationSettingsEnabled
.weakAssign(to: \.state.showNotificationSettings, on: self)
.store(in: &cancellables)
Task {
await userSession.clientProxy.loadUserAvatarURL()
}
@@ -92,6 +97,8 @@ class SettingsScreenViewModel: SettingsScreenViewModelType, SettingsScreenViewMo
callback?(.sessionVerification)
case .changedTimelineStyle:
appSettings.timelineStyle = state.bindings.timelineStyle
case .notifications:
callback?(.notifications)
case .developerOptions:
callback?(.developerOptions)
}
@@ -113,6 +113,15 @@ struct SettingsScreen: View {
context.send(viewAction: .changedTimelineStyle)
}
// Notifications
if context.viewState.showNotificationSettings {
Button { context.send(viewAction: .notifications) } label: {
Label(L10n.screenNotificationSettingsTitle, systemImage: "bell")
}
.buttonStyle(.compoundForm(accessory: .navigationLink))
.accessibilityIdentifier("notificationsButton")
}
// Analytics
Button { context.send(viewAction: .analytics) } label: {
Label(L10n.commonAnalytics, systemImage: "chart.bar")
@@ -194,6 +203,7 @@ struct SettingsScreen_Previews: PreviewProvider {
static let viewModel = {
let userSession = MockUserSession(clientProxy: MockClientProxy(userID: "@userid:example.com"),
mediaProvider: MockMediaProvider())
ServiceLocator.shared.settings.notificationSettingsEnabled = true
return SettingsScreenViewModel(userSession: userSession,
appSettings: ServiceLocator.shared.settings)
}()