From f85d8febf1ee2ffd29f5c0c09e8c0ff0bba71efd Mon Sep 17 00:00:00 2001 From: Stuart Breckenridge Date: Thu, 27 Jan 2022 08:27:11 +0800 Subject: [PATCH] more work on notifications --- iOS/Settings/NotificationsTableViewCell.swift | 24 ++++++++++++++-- .../NotificationsViewController.swift | 28 ++++++++++--------- iOS/Settings/Settings.storyboard | 2 +- iOS/Settings/SettingsViewController.swift | 8 ------ 4 files changed, 37 insertions(+), 25 deletions(-) diff --git a/iOS/Settings/NotificationsTableViewCell.swift b/iOS/Settings/NotificationsTableViewCell.swift index 7b1a07d4b..36c3882f8 100644 --- a/iOS/Settings/NotificationsTableViewCell.swift +++ b/iOS/Settings/NotificationsTableViewCell.swift @@ -10,6 +10,10 @@ import UIKit import Account import UserNotifications +extension Notification.Name { + static let NotificationPreferencesDidUpdate = Notification.Name("NotificationPreferencesDidUpdate") +} + class NotificationsTableViewCell: UITableViewCell { @IBOutlet weak var notificationsSwitch: UISwitch! @@ -45,11 +49,17 @@ class NotificationsTableViewCell: UITableViewCell { notificationsImageView.layer.cornerRadius = 4 } + + /// Used for notification permissions only. + /// - Parameter status: `UNAuthorizationStatus` func configure(_ status: UNAuthorizationStatus) { - notificationsSwitch.isOn = (status == .authorized) ? true : false - if status == .denied { notificationsSwitch.isEnabled = false } + notificationsSwitch.isOn = (status == .authorized || status == .provisional) ? true : false + if status == .denied || status == .authorized || status == .provisional { notificationsSwitch.isEnabled = false } + if status == .notDetermined { + notificationsSwitch.addTarget(self, action: #selector(requestNotificationPermissions(_:)), for: .touchUpInside) + } notificationsLabel.text = NSLocalizedString("Enable Notifications", comment: "") - notificationsImageView.image = UIImage(systemName: "bell") + notificationsImageView.image = UIImage(systemName: "app.badge") notificationsImageView.layer.cornerRadius = 4 } @@ -66,5 +76,13 @@ class NotificationsTableViewCell: UITableViewCell { } } + @objc + private func requestNotificationPermissions(_ sender: Any) { + UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { success, error in + NotificationCenter.default.post(name: .NotificationPreferencesDidUpdate, object: nil) + } + } + + } diff --git a/iOS/Settings/NotificationsViewController.swift b/iOS/Settings/NotificationsViewController.swift index e309c60bb..9d8b79c70 100644 --- a/iOS/Settings/NotificationsViewController.swift +++ b/iOS/Settings/NotificationsViewController.swift @@ -19,26 +19,25 @@ class NotificationsViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() self.title = NSLocalizedString("Notifications", comment: "Notifications") - notificationsTableView.sectionHeaderTopPadding = 15 + notificationsTableView.sectionHeaderTopPadding = 25 + + NotificationCenter.default.addObserver(self, selector: #selector(reloadNotificationTableView(_:)), name: .FaviconDidBecomeAvailable, object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(reloadNotificationTableView(_:)), name: .WebFeedIconDidBecomeAvailable, object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(reloadNotificationTableView(_:)), name: .NotificationPreferencesDidUpdate, object: nil) + reloadNotificationTableView() + } + + @objc + private func reloadNotificationTableView(_ sender: Any? = nil) { UNUserNotificationCenter.current().getNotificationSettings { settings in DispatchQueue.main.async { self.status = settings.authorizationStatus self.notificationsTableView.reloadData() } } - NotificationCenter.default.addObserver(self, selector: #selector(faviconDidBecomeAvailable(_:)), name: .FaviconDidBecomeAvailable, object: nil) - NotificationCenter.default.addObserver(self, selector: #selector(webFeedIconDidBecomeAvailable(_:)), name: .WebFeedIconDidBecomeAvailable, object: nil) - } - - @objc - private func faviconDidBecomeAvailable(_ sender: Any) { - notificationsTableView.reloadData() } - @objc - private func webFeedIconDidBecomeAvailable(_ sender: Any) { - notificationsTableView.reloadData() - } + private func sortedWebFeedsForAccount(_ account: Account) -> [WebFeed] { return Array(account.flattenedWebFeeds()).sorted(by: { $0.nameForDisplay.caseInsensitiveCompare($1.nameForDisplay) == .orderedAscending }) @@ -76,7 +75,7 @@ extension NotificationsViewController: UITableViewDataSource { func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { - if section == 0 { return NSLocalizedString("Enable Notifications", comment: "Enable Notifications") } + if section == 0 { return " " } return AccountManager.shared.sortedActiveAccounts[section - 1].nameForDisplay } @@ -85,6 +84,9 @@ extension NotificationsViewController: UITableViewDataSource { if status == .denied { return NSLocalizedString("Notification permissions are currently denied. Enable notifications in the Settings app.", comment: "Notifications denied.") } + if status == .notDetermined { + return NSLocalizedString("Turn on notifications to configure new article notifications.", comment: "Notifications not determined.") + } } return nil } diff --git a/iOS/Settings/Settings.storyboard b/iOS/Settings/Settings.storyboard index fcf109faa..6e1653a0d 100644 --- a/iOS/Settings/Settings.storyboard +++ b/iOS/Settings/Settings.storyboard @@ -43,7 +43,7 @@ -