mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-03 15:45:11 +00:00
New notifications section in settings (#1273)
This commit is contained in:
@@ -133,12 +133,14 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationCoordinatorDelegate,
|
||||
|
||||
// MARK: - NotificationManagerDelegate
|
||||
|
||||
func authorizationStatusUpdated(_ service: NotificationManagerProtocol, granted: Bool) {
|
||||
if granted {
|
||||
UIApplication.shared.registerForRemoteNotifications()
|
||||
}
|
||||
func registerForRemoteNotifications() {
|
||||
UIApplication.shared.registerForRemoteNotifications()
|
||||
}
|
||||
|
||||
func unregisterForRemoteNotifications() {
|
||||
UIApplication.shared.unregisterForRemoteNotifications()
|
||||
}
|
||||
|
||||
func shouldDisplayInAppNotification(_ service: NotificationManagerProtocol, content: UNNotificationContent) -> Bool {
|
||||
guard let roomId = content.roomID else {
|
||||
return true
|
||||
|
||||
@@ -26,6 +26,7 @@ final class AppSettings {
|
||||
case migratedAccounts
|
||||
case timelineStyle
|
||||
case analyticsConsentState
|
||||
case enableNotifications
|
||||
case enableInAppNotifications
|
||||
case pusherProfileTag
|
||||
case shouldCollapseRoomStateEvents
|
||||
@@ -34,6 +35,7 @@ final class AppSettings {
|
||||
case locationEventsEnabled
|
||||
case shareLocationEnabled
|
||||
case hasShownWelcomeScreen
|
||||
case notificationSettingsEnabled
|
||||
}
|
||||
|
||||
private static var suiteName: String = InfoPlistReader.main.appGroupIdentifier
|
||||
@@ -178,6 +180,9 @@ final class AppSettings {
|
||||
|
||||
// MARK: - Notifications
|
||||
|
||||
@UserPreference(key: UserDefaultsKeys.enableNotifications, defaultValue: true, storageType: .userDefaults(store))
|
||||
var enableNotifications
|
||||
|
||||
@UserPreference(key: UserDefaultsKeys.enableInAppNotifications, defaultValue: true, storageType: .userDefaults(store))
|
||||
var enableInAppNotifications
|
||||
|
||||
@@ -218,4 +223,7 @@ final class AppSettings {
|
||||
|
||||
@UserPreference(key: UserDefaultsKeys.shareLocationEnabled, defaultValue: false, storageType: .userDefaults(store))
|
||||
var shareLocationEnabled
|
||||
|
||||
@UserPreference(key: UserDefaultsKeys.notificationSettingsEnabled, defaultValue: false, storageType: .userDefaults(store))
|
||||
var notificationSettingsEnabled
|
||||
}
|
||||
|
||||
@@ -1317,4 +1317,86 @@ class UserIndicatorControllerMock: UserIndicatorControllerProtocol {
|
||||
}
|
||||
}
|
||||
}
|
||||
class UserNotificationCenterMock: UserNotificationCenterProtocol {
|
||||
var delegate: UNUserNotificationCenterDelegate?
|
||||
|
||||
//MARK: - add
|
||||
|
||||
var addThrowableError: Error?
|
||||
var addCallsCount = 0
|
||||
var addCalled: Bool {
|
||||
return addCallsCount > 0
|
||||
}
|
||||
var addReceivedRequest: UNNotificationRequest?
|
||||
var addReceivedInvocations: [UNNotificationRequest] = []
|
||||
var addClosure: ((UNNotificationRequest) async throws -> Void)?
|
||||
|
||||
func add(_ request: UNNotificationRequest) async throws {
|
||||
if let error = addThrowableError {
|
||||
throw error
|
||||
}
|
||||
addCallsCount += 1
|
||||
addReceivedRequest = request
|
||||
addReceivedInvocations.append(request)
|
||||
try await addClosure?(request)
|
||||
}
|
||||
//MARK: - requestAuthorization
|
||||
|
||||
var requestAuthorizationOptionsThrowableError: Error?
|
||||
var requestAuthorizationOptionsCallsCount = 0
|
||||
var requestAuthorizationOptionsCalled: Bool {
|
||||
return requestAuthorizationOptionsCallsCount > 0
|
||||
}
|
||||
var requestAuthorizationOptionsReceivedOptions: UNAuthorizationOptions?
|
||||
var requestAuthorizationOptionsReceivedInvocations: [UNAuthorizationOptions] = []
|
||||
var requestAuthorizationOptionsReturnValue: Bool!
|
||||
var requestAuthorizationOptionsClosure: ((UNAuthorizationOptions) async throws -> Bool)?
|
||||
|
||||
func requestAuthorization(options: UNAuthorizationOptions) async throws -> Bool {
|
||||
if let error = requestAuthorizationOptionsThrowableError {
|
||||
throw error
|
||||
}
|
||||
requestAuthorizationOptionsCallsCount += 1
|
||||
requestAuthorizationOptionsReceivedOptions = options
|
||||
requestAuthorizationOptionsReceivedInvocations.append(options)
|
||||
if let requestAuthorizationOptionsClosure = requestAuthorizationOptionsClosure {
|
||||
return try await requestAuthorizationOptionsClosure(options)
|
||||
} else {
|
||||
return requestAuthorizationOptionsReturnValue
|
||||
}
|
||||
}
|
||||
//MARK: - setNotificationCategories
|
||||
|
||||
var setNotificationCategoriesCallsCount = 0
|
||||
var setNotificationCategoriesCalled: Bool {
|
||||
return setNotificationCategoriesCallsCount > 0
|
||||
}
|
||||
var setNotificationCategoriesReceivedCategories: Set<UNNotificationCategory>?
|
||||
var setNotificationCategoriesReceivedInvocations: [Set<UNNotificationCategory>] = []
|
||||
var setNotificationCategoriesClosure: ((Set<UNNotificationCategory>) -> Void)?
|
||||
|
||||
func setNotificationCategories(_ categories: Set<UNNotificationCategory>) {
|
||||
setNotificationCategoriesCallsCount += 1
|
||||
setNotificationCategoriesReceivedCategories = categories
|
||||
setNotificationCategoriesReceivedInvocations.append(categories)
|
||||
setNotificationCategoriesClosure?(categories)
|
||||
}
|
||||
//MARK: - authorizationStatus
|
||||
|
||||
var authorizationStatusCallsCount = 0
|
||||
var authorizationStatusCalled: Bool {
|
||||
return authorizationStatusCallsCount > 0
|
||||
}
|
||||
var authorizationStatusReturnValue: UNAuthorizationStatus!
|
||||
var authorizationStatusClosure: (() async -> UNAuthorizationStatus)?
|
||||
|
||||
func authorizationStatus() async -> UNAuthorizationStatus {
|
||||
authorizationStatusCallsCount += 1
|
||||
if let authorizationStatusClosure = authorizationStatusClosure {
|
||||
return await authorizationStatusClosure()
|
||||
} else {
|
||||
return authorizationStatusReturnValue
|
||||
}
|
||||
}
|
||||
}
|
||||
// swiftlint:enable all
|
||||
|
||||
+2
@@ -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
|
||||
}
|
||||
|
||||
+4
-1
@@ -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)
|
||||
}
|
||||
|
||||
+7
@@ -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 {
|
||||
|
||||
+48
@@ -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))
|
||||
}
|
||||
}
|
||||
+53
@@ -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
|
||||
}
|
||||
+76
@@ -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()
|
||||
}
|
||||
}
|
||||
+23
@@ -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 }
|
||||
}
|
||||
+88
@@ -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)
|
||||
}()
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
//
|
||||
// Copyright 2023 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 UserNotifications
|
||||
|
||||
extension UNUserNotificationCenter {
|
||||
func authorizationStatus() async -> UNAuthorizationStatus {
|
||||
await notificationSettings().authorizationStatus
|
||||
}
|
||||
}
|
||||
@@ -24,7 +24,9 @@ class NotificationManager: NSObject, NotificationManagerProtocol {
|
||||
private let appSettings: AppSettings
|
||||
|
||||
private var userSession: UserSessionProtocol?
|
||||
|
||||
private var cancellables = Set<AnyCancellable>()
|
||||
private var notificationsEnabled = false
|
||||
|
||||
init(notificationCenter: UserNotificationCenterProtocol,
|
||||
appSettings: AppSettings) {
|
||||
self.notificationCenter = notificationCenter
|
||||
@@ -50,15 +52,35 @@ class NotificationManager: NSObject, NotificationManagerProtocol {
|
||||
options: [])
|
||||
notificationCenter.setNotificationCategories([messageCategory, inviteCategory])
|
||||
notificationCenter.delegate = self
|
||||
|
||||
notificationsEnabled = appSettings.enableNotifications
|
||||
MXLog.info("[NotificationManager] app setting 'enableNotifications' is '\(notificationsEnabled)'")
|
||||
|
||||
// Listen for changes to AppSettings.enableNotifications
|
||||
appSettings.$enableNotifications
|
||||
.sink { [weak self] newValue in
|
||||
self?.enableNotifications(newValue)
|
||||
}
|
||||
.store(in: &cancellables)
|
||||
|
||||
// Request authorization uppon UIApplication.didBecomeActiveNotification notification
|
||||
NotificationCenter.default.publisher(for: UIApplication.didBecomeActiveNotification)
|
||||
.sink { [weak self] _ in
|
||||
self?.requestAuthorization()
|
||||
}
|
||||
.store(in: &cancellables)
|
||||
}
|
||||
|
||||
|
||||
func requestAuthorization() {
|
||||
guard appSettings.enableNotifications, !userSession.isNil else { return }
|
||||
Task {
|
||||
do {
|
||||
let granted = try await notificationCenter.requestAuthorization(options: [.alert, .sound, .badge])
|
||||
MXLog.info("[NotificationManager] permission granted: \(granted)")
|
||||
let permissionGranted = try await notificationCenter.requestAuthorization(options: [.alert, .sound, .badge])
|
||||
MXLog.info("[NotificationManager] permission granted: \(permissionGranted)")
|
||||
await MainActor.run {
|
||||
self.delegate?.authorizationStatusUpdated(self, granted: granted)
|
||||
if permissionGranted {
|
||||
self.delegate?.registerForRemoteNotifications()
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
MXLog.error("[NotificationManager] request authorization failed: \(error)")
|
||||
@@ -136,6 +158,17 @@ class NotificationManager: NSObject, NotificationManagerProtocol {
|
||||
appSettings.pusherProfileTag = newTag
|
||||
return newTag
|
||||
}
|
||||
|
||||
private func enableNotifications(_ enable: Bool) {
|
||||
guard notificationsEnabled != enable else { return }
|
||||
notificationsEnabled = enable
|
||||
MXLog.info("[NotificationManager] app setting 'enableNotifications' changed to '\(enable)'")
|
||||
if enable {
|
||||
requestAuthorization()
|
||||
} else {
|
||||
delegate?.unregisterForRemoteNotifications()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - UNUserNotificationCenterDelegate
|
||||
|
||||
@@ -18,8 +18,6 @@ import Foundation
|
||||
import UserNotifications
|
||||
|
||||
protocol NotificationManagerDelegate: AnyObject {
|
||||
func authorizationStatusUpdated(_ service: NotificationManagerProtocol,
|
||||
granted: Bool)
|
||||
func shouldDisplayInAppNotification(_ service: NotificationManagerProtocol,
|
||||
content: UNNotificationContent) -> Bool
|
||||
func notificationTapped(_ service: NotificationManagerProtocol,
|
||||
@@ -27,6 +25,8 @@ protocol NotificationManagerDelegate: AnyObject {
|
||||
func handleInlineReply(_ service: NotificationManagerProtocol,
|
||||
content: UNNotificationContent,
|
||||
replyText: String) async
|
||||
func registerForRemoteNotifications()
|
||||
func unregisterForRemoteNotifications()
|
||||
}
|
||||
|
||||
// MARK: - NotificationManagerProtocol
|
||||
|
||||
@@ -22,4 +22,8 @@ protocol UserNotificationCenterProtocol: AnyObject {
|
||||
func add(_ request: UNNotificationRequest) async throws
|
||||
func requestAuthorization(options: UNAuthorizationOptions) async throws -> Bool
|
||||
func setNotificationCategories(_ categories: Set<UNNotificationCategory>)
|
||||
func authorizationStatus() async -> UNAuthorizationStatus
|
||||
}
|
||||
|
||||
// sourcery: AutoMockable
|
||||
extension UserNotificationCenterProtocol { }
|
||||
|
||||
@@ -191,6 +191,11 @@ class MockScreen: Identifiable {
|
||||
isModallyPresented: false))
|
||||
navigationStackCoordinator.setRootCoordinator(coordinator)
|
||||
return navigationStackCoordinator
|
||||
case .notificationSettingsScreen:
|
||||
let userNotificationCenter = UserNotificationCenterMock()
|
||||
userNotificationCenter.authorizationStatusReturnValue = .denied
|
||||
let parameters = NotificationSettingsScreenCoordinatorParameters(userNotificationCenter: userNotificationCenter)
|
||||
return NotificationSettingsScreenCoordinator(parameters: parameters)
|
||||
case .onboarding:
|
||||
return OnboardingCoordinator()
|
||||
case .roomPlainNoAvatar:
|
||||
|
||||
@@ -34,6 +34,7 @@ enum UITestsScreenIdentifier: String {
|
||||
case settings
|
||||
case bugReport
|
||||
case bugReportWithScreenshot
|
||||
case notificationSettingsScreen
|
||||
case onboarding
|
||||
case roomPlainNoAvatar
|
||||
case roomEncryptedWithAvatar
|
||||
|
||||
Reference in New Issue
Block a user