more work on notifications

This commit is contained in:
Stuart Breckenridge
2022-01-27 08:27:11 +08:00
parent a46b6b7d35
commit f85d8febf1
4 changed files with 37 additions and 25 deletions

View File

@@ -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)
}
}
}

View File

@@ -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
}

View File

@@ -43,7 +43,7 @@
<rect key="frame" x="0.0" y="0.0" width="345.5" height="43.5"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" multipleTouchEnabled="YES" contentMode="left" insetsLayoutMarginsFromSafeArea="NO" text="Notifications Management" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontForContentSizeCategory="YES" adjustsFontSizeToFit="NO" id="l7X-8L-61m">
<label opaque="NO" multipleTouchEnabled="YES" contentMode="left" insetsLayoutMarginsFromSafeArea="NO" text="Notifications" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontForContentSizeCategory="YES" adjustsFontSizeToFit="NO" id="l7X-8L-61m">
<rect key="frame" x="20" y="0.0" width="317.5" height="43.5"/>
<autoresizingMask key="autoresizingMask"/>
<fontDescription key="fontDescription" style="UICTFontTextStyleBody"/>

View File

@@ -139,14 +139,6 @@ class SettingsViewController: UITableViewController {
let cell: UITableViewCell
switch indexPath.section {
case 0:
let settingsCell = tableView.dequeueReusableCell(withIdentifier: "SettingsTableViewCell", for: indexPath)
settingsCell.imageView?.image = UIImage(named: "settingsGear")
settingsCell.imageView?.layer.masksToBounds = true
settingsCell.imageView?.layer.cornerRadius = 4
settingsCell.imageView?.frame = CGRect(x: 0, y: 0, width: 24, height: 24)
cell = settingsCell
case 1:
let sortedAccounts = AccountManager.shared.sortedAccounts