From 85a82cbb9b99fd6260ac0f0c24d3e1010dc02db8 Mon Sep 17 00:00:00 2001 From: Nathan Mattes Date: Tue, 26 Sep 2023 16:10:08 +0200 Subject: [PATCH] Disable notifications if they aren't enabled (IOS-14) --- .../NotificationSettingTableViewCell.swift | 23 ++++++++++++------- ...tificationSettingTableViewToggleCell.swift | 10 ++++++-- .../NotificationSettingsViewController.swift | 23 ++++++++++--------- .../Scene/Settings/SettingsCoordinator.swift | 3 ++- 4 files changed, 37 insertions(+), 22 deletions(-) diff --git a/Mastodon/Scene/Settings/Notification Settings/NotificationSettingTableViewCell.swift b/Mastodon/Scene/Settings/Notification Settings/NotificationSettingTableViewCell.swift index f1efbca0a..7fb17750d 100644 --- a/Mastodon/Scene/Settings/Notification Settings/NotificationSettingTableViewCell.swift +++ b/Mastodon/Scene/Settings/Notification Settings/NotificationSettingTableViewCell.swift @@ -6,18 +6,25 @@ import MastodonLocalization class NotificationSettingTableViewCell: UITableViewCell { static let reuseIdentifier = "NotificationSettingTableViewCell" - func configure(with entry: NotificationSettingEntry, viewModel: NotificationSettingsViewModel) { + func configure(with entry: NotificationSettingEntry, viewModel: NotificationSettingsViewModel, notificationsEnabled: Bool) { switch entry { case.alert(_): - // we use toggle cells for these - break - case .policy: - var content = UIListContentConfiguration.valueCell() - content.text = L10n.Scene.Settings.Notifications.Policy.title - content.secondaryText = viewModel.selectedPolicy.title + // we use toggle cells for these + break + case .policy: + var content = UIListContentConfiguration.valueCell() + content.text = L10n.Scene.Settings.Notifications.Policy.title + content.secondaryText = viewModel.selectedPolicy.title + if notificationsEnabled { + content.textProperties.color = .label + content.secondaryTextProperties.color = .secondaryLabel + } else { + content.textProperties.color = .secondaryLabel + content.secondaryTextProperties.color = .tertiaryLabel + } - contentConfiguration = content + contentConfiguration = content } } diff --git a/Mastodon/Scene/Settings/Notification Settings/NotificationSettingTableViewToggleCell.swift b/Mastodon/Scene/Settings/Notification Settings/NotificationSettingTableViewToggleCell.swift index 032b20ed6..2d4bf4d6b 100644 --- a/Mastodon/Scene/Settings/Notification Settings/NotificationSettingTableViewToggleCell.swift +++ b/Mastodon/Scene/Settings/Notification Settings/NotificationSettingTableViewToggleCell.swift @@ -24,7 +24,7 @@ class NotificationSettingTableViewToggleCell: ToggleTableViewCell { required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } - func configure(with alert: NotificationAlert, viewModel: NotificationSettingsViewModel) { + func configure(with alert: NotificationAlert, viewModel: NotificationSettingsViewModel, notificationsEnabled: Bool) { self.alert = alert let toggleIsOn: Bool @@ -40,7 +40,13 @@ class NotificationSettingTableViewToggleCell: ToggleTableViewCell { } label.text = alert.title - toggle.isOn = toggleIsOn + if notificationsEnabled { + label.textColor = .label + } else { + label.textColor = .secondaryLabel + } + toggle.isOn = toggleIsOn && notificationsEnabled + toggle.isEnabled = notificationsEnabled } @objc diff --git a/Mastodon/Scene/Settings/Notification Settings/NotificationSettingsViewController.swift b/Mastodon/Scene/Settings/Notification Settings/NotificationSettingsViewController.swift index 50eaeb9be..729697447 100644 --- a/Mastodon/Scene/Settings/Notification Settings/NotificationSettingsViewController.swift +++ b/Mastodon/Scene/Settings/Notification Settings/NotificationSettingsViewController.swift @@ -19,7 +19,7 @@ class NotificationSettingsViewController: UIViewController { let sections: [NotificationSettingsSection] var viewModel: NotificationSettingsViewModel - init(currentSetting: Setting?) { + init(currentSetting: Setting?, notificationsEnabled: Bool) { let activeSubscription = currentSetting?.activeSubscription let alert = activeSubscription?.alert viewModel = NotificationSettingsViewModel(selectedPolicy: activeSubscription?.notificationPolicy ?? .noone, @@ -37,6 +37,7 @@ class NotificationSettingsViewController: UIViewController { tableView.translatesAutoresizingMaskIntoConstraints = false tableView.register(NotificationSettingTableViewCell.self, forCellReuseIdentifier: NotificationSettingTableViewCell.reuseIdentifier) tableView.register(NotificationSettingTableViewToggleCell.self, forCellReuseIdentifier: NotificationSettingTableViewToggleCell.reuseIdentifier) + tableView.isUserInteractionEnabled = notificationsEnabled super.init(nibName: nil, bundle: nil) @@ -45,18 +46,18 @@ class NotificationSettingsViewController: UIViewController { let cell: UITableViewCell switch itemIdentifier { - case .policy: - guard let self, - let notificationCell = tableView.dequeueReusableCell(withIdentifier: NotificationSettingTableViewCell.reuseIdentifier, for: indexPath) as? NotificationSettingTableViewCell else { fatalError("WTF Wrong cell!?") } + case .policy: + guard let self, + let notificationCell = tableView.dequeueReusableCell(withIdentifier: NotificationSettingTableViewCell.reuseIdentifier, for: indexPath) as? NotificationSettingTableViewCell else { fatalError("WTF Wrong cell!?") } - notificationCell.configure(with: .policy, viewModel: self.viewModel) - cell = notificationCell + notificationCell.configure(with: .policy, viewModel: self.viewModel, notificationsEnabled: notificationsEnabled) + cell = notificationCell - case .alert(let alert): - guard let self, - let toggleCell = tableView.dequeueReusableCell(withIdentifier: NotificationSettingTableViewToggleCell.reuseIdentifier, for: indexPath) as? NotificationSettingTableViewToggleCell else { fatalError("WTF Wrong cell!?") } - - toggleCell.configure(with: alert, viewModel: self.viewModel) + case .alert(let alert): + guard let self, + let toggleCell = tableView.dequeueReusableCell(withIdentifier: NotificationSettingTableViewToggleCell.reuseIdentifier, for: indexPath) as? NotificationSettingTableViewToggleCell else { fatalError("WTF Wrong cell!?") } + + toggleCell.configure(with: alert, viewModel: self.viewModel, notificationsEnabled: notificationsEnabled) toggleCell.delegate = self cell = toggleCell } diff --git a/Mastodon/Scene/Settings/SettingsCoordinator.swift b/Mastodon/Scene/Settings/SettingsCoordinator.swift index b945632a7..7bc735507 100644 --- a/Mastodon/Scene/Settings/SettingsCoordinator.swift +++ b/Mastodon/Scene/Settings/SettingsCoordinator.swift @@ -61,7 +61,8 @@ extension SettingsCoordinator: SettingsViewControllerDelegate { case .notifications: let currentSetting = appContext.settingService.currentSetting.value - let notificationViewController = NotificationSettingsViewController(currentSetting: currentSetting) + let notificationsEnabled = appContext.notificationService.isNotificationPermissionGranted.value + let notificationViewController = NotificationSettingsViewController(currentSetting: currentSetting, notificationsEnabled: notificationsEnabled) notificationViewController.delegate = self self.navigationController.pushViewController(notificationViewController, animated: true)