Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(NotificationSettings): Hide mail and alert user #786

Merged
merged 4 commits into from
Jun 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/

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()
}
}
68 changes: 63 additions & 5 deletions Mail/Views/Settings/General/SettingsNotificationsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -60,7 +90,6 @@ struct SettingsNotificationsView: View {
Text(mailbox.email)
.textStyle(.body)
}
.disabled(!notificationsEnabled)
}
}

Expand All @@ -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 {
Expand All @@ -81,6 +125,9 @@ struct SettingsNotificationsView: View {
.onDisappear {
updateTopicsForCurrentUserIfNeeded()
}
.customAlert(isPresented: $showAlertNotification) {
SettingsNotificationsInstructionsView()
}
.matomoView(view: [MatomoUtils.View.settingsView.displayName, "Notifications"])
}

Expand All @@ -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 {
Expand Down
Binary file modified MailResources/Localizable/de.lproj/Localizable.strings
Binary file not shown.
Binary file modified MailResources/Localizable/en.lproj/Localizable.strings
Binary file not shown.
Binary file modified MailResources/Localizable/es.lproj/Localizable.strings
Binary file not shown.
Binary file modified MailResources/Localizable/fr.lproj/Localizable.strings
Binary file not shown.
Binary file modified MailResources/Localizable/it.lproj/Localizable.strings
Binary file not shown.