diff --git a/Mail/Views/Settings/General/SettingsNotificationsInstructionsView.swift b/Mail/Views/Settings/General/SettingsNotificationsInstructionsView.swift new file mode 100644 index 000000000..22d00fe0d --- /dev/null +++ b/Mail/Views/Settings/General/SettingsNotificationsInstructionsView.swift @@ -0,0 +1,53 @@ +/* + Infomaniak Mail - iOS App + Copyright (C) 2022 Infomaniak Network SA + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + */ + +import MailResources +import SwiftUI + +struct SettingsNotificationsInstructionsView: View { + @Environment(\.dismiss) private var dismiss + + var body: some View { + VStack(alignment: .leading, spacing: 24) { + Text(MailResourcesStrings.Localizable.alertNotificationsDisabledTitle) + .textStyle(.bodyMedium) + Text(LocalizedStringKey(MailResourcesStrings.Localizable.alertNotificationsDisabledDescription)) + .textStyle(.bodySecondary) + ModalButtonsView( + primaryButtonTitle: MailResourcesStrings.Localizable.alertNotificationsDisabledButton, + primaryButtonAction: openSettings + ) + } + } + + private func openSettings() { + guard let settingsUrl = URL(string: UIApplication.openSettingsURLString) else { + return + } + + if UIApplication.shared.canOpenURL(settingsUrl) { + UIApplication.shared.open(settingsUrl) + } + } +} + +struct SettingsNotificationsInstructionsView_Previews: PreviewProvider { + static var previews: some View { + SettingsNotificationsInstructionsView() + } +} diff --git a/Mail/Views/Settings/General/SettingsNotificationsView.swift b/Mail/Views/Settings/General/SettingsNotificationsView.swift index 39962cf34..6b364d3b0 100644 --- a/Mail/Views/Settings/General/SettingsNotificationsView.swift +++ b/Mail/Views/Settings/General/SettingsNotificationsView.swift @@ -32,9 +32,40 @@ struct SettingsNotificationsView: View { .notificationsEnabled @State var subscribedTopics: [String]? + @State private var showAlertNotification = false + @State private var showWarning = false + var body: some View { ScrollView { - VStack(spacing: 24) { + VStack(alignment: .leading, spacing: 24) { + if showWarning { + VStack(alignment: .leading, spacing: 8) { + Text(MailResourcesStrings.Localizable.warningNotificationsDisabledDescription) + .textStyle(.bodySecondary) + .frame(maxWidth: .infinity, alignment: .leading) + MailButton(label: MailResourcesStrings.Localizable.warningNotificationsDisabledButton) { + guard let settingsUrl = URL(string: UIApplication.openSettingsURLString) else { + return + } + + if UIApplication.shared.canOpenURL(settingsUrl) { + UIApplication.shared.open(settingsUrl) + } + } + .mailButtonStyle(.link) + } + .padding(16) + .frame(maxWidth: .infinity) + .background( + RoundedRectangle(cornerRadius: 8) + .fill(MailResourcesAsset.backgroundBlueNavBarColor.swiftUIColor) + ) + .padding(.vertical, 16) + } + + Text(Bundle.main.object(forInfoDictionaryKey: "CFBundleName") as! String) + .textStyle(.bodySmallSecondary) + Toggle(isOn: $notificationsEnabled) { Text(MailResourcesStrings.Localizable.settingsEnableNotifications) .textStyle(.body) @@ -43,9 +74,8 @@ struct SettingsNotificationsView: View { matomo.track(eventWithCategory: .settingsNotifications, name: "allNotifications", value: newValue) } - IKDivider() - - if subscribedTopics != nil { + if subscribedTopics != nil && notificationsEnabled { + IKDivider() ForEach(AccountManager.instance.mailboxes) { mailbox in Toggle(isOn: Binding(get: { notificationsEnabled && subscribedTopics?.contains(mailbox.notificationTopicName) == true @@ -60,7 +90,6 @@ struct SettingsNotificationsView: View { Text(mailbox.email) .textStyle(.body) } - .disabled(!notificationsEnabled) } } @@ -73,6 +102,21 @@ struct SettingsNotificationsView: View { .onChange(of: notificationsEnabled) { enabled in if !enabled { subscribedTopics = [] + } else { + if showWarning { + notificationsEnabled = false + showAlertNotification = true + } + } + } + .onReceive(NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification)) { _ in + settingsNotificationEnabled { enabled in + showWarning = !enabled + } + } + .onAppear { + settingsNotificationEnabled { enabled in + showWarning = !enabled } } .task { @@ -81,6 +125,9 @@ struct SettingsNotificationsView: View { .onDisappear { updateTopicsForCurrentUserIfNeeded() } + .customAlert(isPresented: $showAlertNotification) { + SettingsNotificationsInstructionsView() + } .matomoView(view: [MatomoUtils.View.settingsView.displayName, "Notifications"]) } @@ -98,6 +145,17 @@ struct SettingsNotificationsView: View { await notificationService.updateTopicsIfNeeded(subscribedTopics, userApiFetcher: currentApiFetcher) } } + + private func settingsNotificationEnabled(completion: @escaping ((Bool) -> Void)) { + UNUserNotificationCenter.current().getNotificationSettings { settings in + switch settings.authorizationStatus { + case .notDetermined, .denied: + completion(false) + default: + completion(true) + } + } + } } struct SettingsNotificationsView_Previews: PreviewProvider { diff --git a/MailResources/Localizable/de.lproj/Localizable.strings b/MailResources/Localizable/de.lproj/Localizable.strings index ed9474e88..d64b8e2ac 100644 Binary files a/MailResources/Localizable/de.lproj/Localizable.strings and b/MailResources/Localizable/de.lproj/Localizable.strings differ diff --git a/MailResources/Localizable/en.lproj/Localizable.strings b/MailResources/Localizable/en.lproj/Localizable.strings index 2e65a7a0a..9be7be675 100644 Binary files a/MailResources/Localizable/en.lproj/Localizable.strings and b/MailResources/Localizable/en.lproj/Localizable.strings differ diff --git a/MailResources/Localizable/es.lproj/Localizable.strings b/MailResources/Localizable/es.lproj/Localizable.strings index daa771ea5..51ce4e375 100644 Binary files a/MailResources/Localizable/es.lproj/Localizable.strings and b/MailResources/Localizable/es.lproj/Localizable.strings differ diff --git a/MailResources/Localizable/fr.lproj/Localizable.strings b/MailResources/Localizable/fr.lproj/Localizable.strings index 8d270f586..6a84482e2 100644 Binary files a/MailResources/Localizable/fr.lproj/Localizable.strings and b/MailResources/Localizable/fr.lproj/Localizable.strings differ diff --git a/MailResources/Localizable/it.lproj/Localizable.strings b/MailResources/Localizable/it.lproj/Localizable.strings index 54247611b..0c2cc0882 100644 Binary files a/MailResources/Localizable/it.lproj/Localizable.strings and b/MailResources/Localizable/it.lproj/Localizable.strings differ