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