NetNewsWire/Mac/Inspector/WebFeedInspectorViewControl...

208 lines
6.1 KiB
Swift
Raw Normal View History

//
// FeedInspectorViewController.swift
2018-08-29 07:18:24 +02:00
// NetNewsWire
//
// Created by Brent Simmons on 1/20/18.
// Copyright © 2018 Ranchero Software. All rights reserved.
//
import AppKit
import Articles
import Account
2020-08-01 17:50:57 +02:00
import UserNotifications
final class WebFeedInspectorViewController: NSViewController, Inspector {
@IBOutlet weak var iconView: IconView!
@IBOutlet weak var nameTextField: NSTextField?
@IBOutlet weak var homePageURLTextField: NSTextField?
@IBOutlet weak var urlTextField: NSTextField?
@IBOutlet weak var isNotifyAboutNewArticlesCheckBox: NSButton!
@IBOutlet weak var isReaderViewAlwaysOnCheckBox: NSButton?
private var feed: WebFeed? {
2018-01-22 06:01:18 +01:00
didSet {
2018-01-22 06:24:25 +01:00
if feed != oldValue {
updateUI()
}
2018-01-22 06:01:18 +01:00
}
}
2020-08-01 17:50:57 +02:00
private var userNotificationSettings: UNNotificationSettings?
2018-01-22 06:01:18 +01:00
// MARK: Inspector
2018-01-21 07:36:17 +01:00
let isFallbackInspector = false
var objects: [Any]? {
didSet {
2018-01-22 06:01:18 +01:00
updateFeed()
}
}
2018-01-21 07:36:17 +01:00
func canInspect(_ objects: [Any]) -> Bool {
return objects.count == 1 && objects.first is WebFeed
2018-01-21 07:36:17 +01:00
}
2018-01-22 06:01:18 +01:00
// MARK: NSViewController
override func viewDidLoad() {
updateUI()
NotificationCenter.default.addObserver(self, selector: #selector(imageDidBecomeAvailable(_:)), name: .ImageDidBecomeAvailable, object: nil)
}
override func viewDidAppear() {
updateNotificationSettings()
}
// MARK: Actions
@IBAction func isNotifyAboutNewArticlesChanged(_ sender: Any) {
guard let settings = userNotificationSettings else {
// Something went wront fetching the user notification settings,
// so toggle the checkbox back to its original state and return.
isNotifyAboutNewArticlesCheckBox.setNextState()
return
}
if settings.authorizationStatus == .denied {
// Notifications are not authorized, so toggle the checkbox back
// to its original state...
isNotifyAboutNewArticlesCheckBox.setNextState()
// ...then alert the user to the issue...
showNotificationsDeniedError()
} else if settings.authorizationStatus == .authorized {
// Notifications are authorized, so set the feed's isNotifyAboutNewArticles
// property to match the state of isNotifyAboutNewArticlesCheckbox.
feed?.isNotifyAboutNewArticles = (isNotifyAboutNewArticlesCheckBox?.state ?? .off) == .on ? true : false
} else {
// We're not sure what the status may be but we've /probably/ not requested
// permission to send notifications, so do that:
UNUserNotificationCenter.current().requestAuthorization(options: [.badge, .sound, .alert]) { (granted, error) in
self.updateNotificationSettings()
if granted {
// We've been given permission, so set the feed's isNotifyAboutNewArticles
// property to match the state of isNotifyAboutNewArticlesCheckbox and then
// register for remote notifications.
self.feed?.isNotifyAboutNewArticles = (self.isNotifyAboutNewArticlesCheckBox?.state ?? .off) == .on ? true : false
NSApplication.shared.registerForRemoteNotifications()
} else {
// We weren't given permission, so toggle the checkbox back to its
// original state.
self.isNotifyAboutNewArticlesCheckBox.setNextState()
}
}
}
}
@IBAction func isReaderViewAlwaysOnChanged(_ sender: Any) {
feed?.isArticleExtractorAlwaysOn = (isReaderViewAlwaysOnCheckBox?.state ?? .off) == .on ? true : false
}
2018-01-22 06:01:18 +01:00
// MARK: Notifications
@objc func imageDidBecomeAvailable(_ note: Notification) {
updateImage()
}
}
extension WebFeedInspectorViewController: NSTextFieldDelegate {
2018-12-09 21:32:33 +01:00
func controlTextDidChange(_ note: Notification) {
guard let feed = feed, let nameTextField = nameTextField else {
return
}
feed.editedName = nameTextField.stringValue
}
}
private extension WebFeedInspectorViewController {
2018-01-22 06:01:18 +01:00
func updateFeed() {
guard let objects = objects, objects.count == 1, let singleFeed = objects.first as? WebFeed else {
2018-01-22 06:01:18 +01:00
feed = nil
return
}
2018-01-22 06:01:18 +01:00
feed = singleFeed
}
func updateUI() {
2018-01-22 06:01:18 +01:00
updateImage()
updateName()
updateHomePageURL()
updateFeedURL()
updateNotifyAboutNewArticles()
updateIsReaderViewAlwaysOn()
2018-01-22 06:01:18 +01:00
view.needsLayout = true
}
2018-01-22 06:01:18 +01:00
func updateImage() {
guard let feed = feed, let iconView = iconView else {
2018-01-22 06:01:18 +01:00
return
}
if let feedIcon = appDelegate.webFeedIconDownloader.icon(for: feed) {
iconView.iconImage = feedIcon
return
}
2018-01-22 06:24:25 +01:00
if let favicon = appDelegate.faviconDownloader.favicon(for: feed) {
iconView.iconImage = favicon
return
}
iconView.iconImage = feed.smallIcon
}
2018-01-22 06:01:18 +01:00
func updateName() {
guard let nameTextField = nameTextField else {
return
}
let name = feed?.editedName ?? feed?.name ?? ""
if nameTextField.stringValue != name {
nameTextField.stringValue = name
}
}
2018-01-22 06:01:18 +01:00
func updateHomePageURL() {
homePageURLTextField?.stringValue = feed?.homePageURL ?? ""
}
2018-01-22 06:01:18 +01:00
func updateFeedURL() {
urlTextField?.stringValue = feed?.url ?? ""
2018-01-21 07:36:17 +01:00
}
func updateNotificationSettings() {
UNUserNotificationCenter.current().getNotificationSettings { (settings) in
DispatchQueue.main.async {
self.userNotificationSettings = settings
if settings.authorizationStatus == .authorized {
NSApplication.shared.registerForRemoteNotifications()
}
}
}
}
func showNotificationsDeniedError() {
let updateAlert = NSAlert()
updateAlert.alertStyle = .informational
updateAlert.messageText = NSLocalizedString("Enable Notifications", comment: "Notifications")
updateAlert.informativeText = NSLocalizedString("Notifications need to be enabled in System Preferences > Notifications.", comment: "Notifications need to be enabled in System Preferences > Notifications.")
updateAlert.addButton(withTitle: NSLocalizedString("Open System Preferences", comment: "Open System Preferences"))
updateAlert.addButton(withTitle: NSLocalizedString("Close", comment: "Close"))
let modalResponse = updateAlert.runModal()
if modalResponse == .alertFirstButtonReturn {
NSWorkspace.shared.open(URL(fileURLWithPath: "x-apple.systempreferences:com.apple.preference"))
}
}
func updateNotifyAboutNewArticles() {
isNotifyAboutNewArticlesCheckBox?.state = (feed?.isNotifyAboutNewArticles ?? false) ? .on : .off
}
func updateIsReaderViewAlwaysOn() {
isReaderViewAlwaysOnCheckBox?.state = (feed?.isArticleExtractorAlwaysOn ?? false) ? .on : .off
}
2018-01-21 06:35:59 +01:00
}