Disable notifications if they aren't enabled (IOS-14)

This commit is contained in:
Nathan Mattes 2023-09-26 16:10:08 +02:00
parent f5365a9d6e
commit 85a82cbb9b
4 changed files with 37 additions and 22 deletions

View File

@ -6,18 +6,25 @@ import MastodonLocalization
class NotificationSettingTableViewCell: UITableViewCell { class NotificationSettingTableViewCell: UITableViewCell {
static let reuseIdentifier = "NotificationSettingTableViewCell" static let reuseIdentifier = "NotificationSettingTableViewCell"
func configure(with entry: NotificationSettingEntry, viewModel: NotificationSettingsViewModel) { func configure(with entry: NotificationSettingEntry, viewModel: NotificationSettingsViewModel, notificationsEnabled: Bool) {
switch entry { switch entry {
case.alert(_): case.alert(_):
// we use toggle cells for these // we use toggle cells for these
break break
case .policy: case .policy:
var content = UIListContentConfiguration.valueCell() var content = UIListContentConfiguration.valueCell()
content.text = L10n.Scene.Settings.Notifications.Policy.title content.text = L10n.Scene.Settings.Notifications.Policy.title
content.secondaryText = viewModel.selectedPolicy.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
} }
} }

View File

@ -24,7 +24,7 @@ class NotificationSettingTableViewToggleCell: ToggleTableViewCell {
required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } 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 self.alert = alert
let toggleIsOn: Bool let toggleIsOn: Bool
@ -40,7 +40,13 @@ class NotificationSettingTableViewToggleCell: ToggleTableViewCell {
} }
label.text = alert.title label.text = alert.title
toggle.isOn = toggleIsOn if notificationsEnabled {
label.textColor = .label
} else {
label.textColor = .secondaryLabel
}
toggle.isOn = toggleIsOn && notificationsEnabled
toggle.isEnabled = notificationsEnabled
} }
@objc @objc

View File

@ -19,7 +19,7 @@ class NotificationSettingsViewController: UIViewController {
let sections: [NotificationSettingsSection] let sections: [NotificationSettingsSection]
var viewModel: NotificationSettingsViewModel var viewModel: NotificationSettingsViewModel
init(currentSetting: Setting?) { init(currentSetting: Setting?, notificationsEnabled: Bool) {
let activeSubscription = currentSetting?.activeSubscription let activeSubscription = currentSetting?.activeSubscription
let alert = activeSubscription?.alert let alert = activeSubscription?.alert
viewModel = NotificationSettingsViewModel(selectedPolicy: activeSubscription?.notificationPolicy ?? .noone, viewModel = NotificationSettingsViewModel(selectedPolicy: activeSubscription?.notificationPolicy ?? .noone,
@ -37,6 +37,7 @@ class NotificationSettingsViewController: UIViewController {
tableView.translatesAutoresizingMaskIntoConstraints = false tableView.translatesAutoresizingMaskIntoConstraints = false
tableView.register(NotificationSettingTableViewCell.self, forCellReuseIdentifier: NotificationSettingTableViewCell.reuseIdentifier) tableView.register(NotificationSettingTableViewCell.self, forCellReuseIdentifier: NotificationSettingTableViewCell.reuseIdentifier)
tableView.register(NotificationSettingTableViewToggleCell.self, forCellReuseIdentifier: NotificationSettingTableViewToggleCell.reuseIdentifier) tableView.register(NotificationSettingTableViewToggleCell.self, forCellReuseIdentifier: NotificationSettingTableViewToggleCell.reuseIdentifier)
tableView.isUserInteractionEnabled = notificationsEnabled
super.init(nibName: nil, bundle: nil) super.init(nibName: nil, bundle: nil)
@ -45,18 +46,18 @@ class NotificationSettingsViewController: UIViewController {
let cell: UITableViewCell let cell: UITableViewCell
switch itemIdentifier { switch itemIdentifier {
case .policy: case .policy:
guard let self, guard let self,
let notificationCell = tableView.dequeueReusableCell(withIdentifier: NotificationSettingTableViewCell.reuseIdentifier, for: indexPath) as? NotificationSettingTableViewCell else { fatalError("WTF Wrong cell!?") } let notificationCell = tableView.dequeueReusableCell(withIdentifier: NotificationSettingTableViewCell.reuseIdentifier, for: indexPath) as? NotificationSettingTableViewCell else { fatalError("WTF Wrong cell!?") }
notificationCell.configure(with: .policy, viewModel: self.viewModel) notificationCell.configure(with: .policy, viewModel: self.viewModel, notificationsEnabled: notificationsEnabled)
cell = notificationCell cell = notificationCell
case .alert(let alert): case .alert(let alert):
guard let self, guard let self,
let toggleCell = tableView.dequeueReusableCell(withIdentifier: NotificationSettingTableViewToggleCell.reuseIdentifier, for: indexPath) as? NotificationSettingTableViewToggleCell else { fatalError("WTF Wrong cell!?") } let toggleCell = tableView.dequeueReusableCell(withIdentifier: NotificationSettingTableViewToggleCell.reuseIdentifier, for: indexPath) as? NotificationSettingTableViewToggleCell else { fatalError("WTF Wrong cell!?") }
toggleCell.configure(with: alert, viewModel: self.viewModel) toggleCell.configure(with: alert, viewModel: self.viewModel, notificationsEnabled: notificationsEnabled)
toggleCell.delegate = self toggleCell.delegate = self
cell = toggleCell cell = toggleCell
} }

View File

@ -61,7 +61,8 @@ extension SettingsCoordinator: SettingsViewControllerDelegate {
case .notifications: case .notifications:
let currentSetting = appContext.settingService.currentSetting.value 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 notificationViewController.delegate = self
self.navigationController.pushViewController(notificationViewController, animated: true) self.navigationController.pushViewController(notificationViewController, animated: true)