Add toggle not invite notifications. (#2207)

This commit is contained in:
Doug
2023-12-06 09:53:10 +00:00
committed by GitHub
parent 731333a1cf
commit c7531be44c
15 changed files with 203 additions and 14 deletions
@@ -44,6 +44,7 @@ struct NotificationSettingsScreenViewStateBindings {
var enableNotifications = false
var roomMentionsEnabled = false
var callsEnabled = false
var invitationsEnabled = false
var alertInfo: AlertInfo<NotificationSettingsScreenErrorType>?
}
@@ -52,6 +53,7 @@ struct NotificationSettingsScreenSettings {
let directChatsMode: RoomNotificationModeProxy
let roomMentionsEnabled: Bool?
let callsEnabled: Bool?
let invitationsEnabled: Bool?
// 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`)
let inconsistentSettings: [NotificationSettingsScreenInvalidSetting]
@@ -93,6 +95,7 @@ enum NotificationSettingsScreenViewAction {
case directChatsTapped
case roomMentionChanged
case callsChanged
case invitationsChanged
case close
case fixConfigurationMismatchTapped
}
@@ -73,6 +73,11 @@ class NotificationSettingsScreenViewModel: NotificationSettingsScreenViewModelTy
return
}
Task { await enableCalls(state.bindings.callsEnabled) }
case .invitationsChanged:
guard let settings = state.settings, settings.invitationsEnabled != state.bindings.invitationsEnabled else {
return
}
Task { await enableInvitations(state.bindings.invitationsEnabled) }
case .close:
actionsSubject.send(.close)
case .fixConfigurationMismatchTapped:
@@ -146,6 +151,7 @@ class NotificationSettingsScreenViewModel: NotificationSettingsScreenViewModelTy
// The following calls may fail if the associated push rule doesn't exist
let roomMentionsEnabled = try? await notificationSettingsProxy.isRoomMentionEnabled()
let callEnabled = try? await notificationSettingsProxy.isCallEnabled()
let invitationsEnabled = try? await notificationSettingsProxy.isInviteForMeEnabled()
guard !Task.isCancelled else { return }
@@ -153,11 +159,13 @@ class NotificationSettingsScreenViewModel: NotificationSettingsScreenViewModelTy
directChatsMode: directChatsMode,
roomMentionsEnabled: roomMentionsEnabled,
callsEnabled: callEnabled,
invitationsEnabled: invitationsEnabled,
inconsistentSettings: inconsistentSettings)
state.settings = notificationSettings
state.bindings.roomMentionsEnabled = notificationSettings.roomMentionsEnabled ?? false
state.bindings.callsEnabled = notificationSettings.callsEnabled ?? false
state.bindings.invitationsEnabled = notificationSettings.invitationsEnabled ?? false
}
}
@@ -187,7 +195,7 @@ class NotificationSettingsScreenViewModel: NotificationSettingsScreenViewModelTy
guard let notificationSettings = state.settings else { return }
do {
state.applyingChange = true
MXLog.info("[NotificationSettingsScreenViewMode] setRoomMentionEnabled(\(enable))")
MXLog.info("setRoomMentionEnabled(\(enable))")
try await notificationSettingsProxy.setRoomMentionEnabled(enabled: enable)
} catch {
state.bindings.alertInfo = AlertInfo(id: .alert)
@@ -200,7 +208,7 @@ class NotificationSettingsScreenViewModel: NotificationSettingsScreenViewModelTy
guard let notificationSettings = state.settings else { return }
do {
state.applyingChange = true
MXLog.info("[NotificationSettingsScreenViewMode] setCallEnabled(\(enable))")
MXLog.info("setCallEnabled(\(enable))")
try await notificationSettingsProxy.setCallEnabled(enabled: enable)
} catch {
state.bindings.alertInfo = AlertInfo(id: .alert)
@@ -208,6 +216,19 @@ class NotificationSettingsScreenViewModel: NotificationSettingsScreenViewModelTy
}
state.applyingChange = false
}
func enableInvitations(_ enable: Bool) async {
guard let notificationSettings = state.settings else { return }
do {
state.applyingChange = true
MXLog.info("setInviteForMeEnabled(\(enable))")
try await notificationSettingsProxy.setInviteForMeEnabled(enabled: enable)
} catch {
state.bindings.alertInfo = AlertInfo(id: .alert)
state.bindings.callsEnabled = notificationSettings.invitationsEnabled ?? false
}
state.applyingChange = false
}
}
extension UNUserNotificationCenter {
@@ -37,6 +37,9 @@ struct NotificationSettingsScreen: View {
if context.viewState.showCallsSettings, context.viewState.settings?.callsEnabled != nil {
callsSection
}
if context.viewState.settings?.invitationsEnabled != nil {
additionalSettingsSection
}
}
}
}
@@ -153,6 +156,21 @@ struct NotificationSettingsScreen: View {
}
}
private var additionalSettingsSection: some View {
Section {
ListRow(label: .plain(title: L10n.screenNotificationSettingsInviteForMeLabel),
kind: .toggle($context.invitationsEnabled))
.disabled(context.viewState.settings?.invitationsEnabled == nil)
.allowsHitTesting(!context.viewState.applyingChange)
.onChange(of: context.invitationsEnabled) { _ in
context.send(viewAction: .invitationsChanged)
}
} header: {
Text(L10n.screenNotificationSettingsAdditionalSettingsSectionTitle)
.compoundListSectionHeader()
}
}
private var configurationMismatchSection: some View {
Section {
ListRow(kind: .custom {
@@ -242,7 +260,9 @@ struct NotificationSettingsScreen_Previews: PreviewProvider, TestablePreview {
static var previews: some View {
NotificationSettingsScreen(context: viewModel.context)
.snapshot(delay: 0.1)
NotificationSettingsScreen(context: viewModelConfigurationMismatch.context)
.snapshot(delay: 0.1)
.previewDisplayName("Configuration mismatch")
}
}