Files
NetNewsWire/iOS/Settings/NotificationsViewController.swift

104 lines
3.6 KiB
Swift
Raw Normal View History

2022-01-26 10:25:42 +08:00
//
// NotificationsViewController.swift
// NetNewsWire-iOS
//
// Created by Stuart Breckenridge on 26/01/2022.
// Copyright © 2022 Ranchero Software. All rights reserved.
//
import UIKit
import Account
import UserNotifications
class NotificationsViewController: UIViewController {
@IBOutlet weak var notificationsTableView: UITableView!
private var status: UNAuthorizationStatus = .notDetermined
override func viewDidLoad() {
super.viewDidLoad()
self.title = NSLocalizedString("New Article Notifications", comment: "Notifications")
2022-01-27 08:27:11 +08:00
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)
NotificationCenter.default.addObserver(self, selector: #selector(reloadNotificationTableView(_:)), name: UIScene.willEnterForegroundNotification, object: nil)
2022-01-27 08:27:11 +08:00
reloadNotificationTableView()
}
@objc
private func reloadNotificationTableView(_ sender: Any? = nil) {
2022-01-26 10:25:42 +08:00
UNUserNotificationCenter.current().getNotificationSettings { settings in
DispatchQueue.main.async {
self.status = settings.authorizationStatus
self.notificationsTableView.reloadData()
}
}
}
private func sortedWebFeedsForAccount(_ account: Account) -> [WebFeed] {
return Array(account.flattenedWebFeeds()).sorted(by: { $0.nameForDisplay.caseInsensitiveCompare($1.nameForDisplay) == .orderedAscending })
}
}
// MARK: UITableViewDataSource
extension NotificationsViewController: UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
if status == .denied { return 1 }
2022-01-26 10:25:42 +08:00
return 1 + AccountManager.shared.activeAccounts.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
if status == .denied { return 1 }
return 0
}
2022-01-26 10:25:42 +08:00
return AccountManager.shared.sortedActiveAccounts[section - 1].flattenedWebFeeds().count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
2022-01-26 10:25:42 +08:00
if indexPath.section == 0 {
let openSettingsCell = tableView.dequeueReusableCell(withIdentifier: "OpenSettingsCell") as! VibrantBasicTableViewCell
return openSettingsCell
2022-01-26 10:25:42 +08:00
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: "NotificationsCell") as! NotificationsTableViewCell
2022-01-26 10:25:42 +08:00
let account = AccountManager.shared.sortedActiveAccounts[indexPath.section - 1]
let feed = sortedWebFeedsForAccount(account)[indexPath.row]
cell.configure(feed, status)
return cell
2022-01-26 10:25:42 +08:00
}
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if section == 0 { return nil }
2022-01-26 10:25:42 +08:00
return AccountManager.shared.sortedActiveAccounts[section - 1].nameForDisplay
}
func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
if section == 0 {
if status == .denied {
return NSLocalizedString("Notification permissions are currently denied. Enable notifications in the Settings app.", comment: "Notifications denied.")
}
}
return nil
}
}
extension NotificationsViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
UIApplication.shared.open(URL(string: "\(UIApplication.openSettingsURLString)")!)
}
2022-01-26 10:25:42 +08:00
}