2018-09-04 07:33:00 +02:00
//
// G e n e r a l P r e f e n c e s V i e w C o n t r o l l e r . s w i f t
// N e t N e w s W i r e
//
// C r e a t e d b y B r e n t S i m m o n s o n 9 / 3 / 1 8 .
// C o p y r i g h t © 2 0 1 8 R a n c h e r o S o f t w a r e . A l l r i g h t s r e s e r v e d .
//
import AppKit
2024-04-02 04:31:57 +02:00
import Web
2020-09-06 23:15:46 +02:00
import UserNotifications
2024-02-25 04:43:10 +01:00
import UniformTypeIdentifiers
2018-09-04 07:33:00 +02:00
final class GeneralPreferencesViewController : NSViewController {
2020-09-06 23:15:46 +02:00
private var userNotificationSettings : UNNotificationSettings ?
2021-09-08 23:38:05 +02:00
@IBOutlet weak var articleThemePopup : NSPopUpButton !
@IBOutlet weak var defaultBrowserPopup : NSPopUpButton !
2020-09-23 02:27:36 +02:00
2018-11-26 22:17:16 +01:00
public override init ( nibName nibNameOrNil : NSNib . Name ? , bundle nibBundleOrNil : Bundle ? ) {
super . init ( nibName : nibNameOrNil , bundle : nibBundleOrNil )
commonInit ( )
}
public required init ? ( coder : NSCoder ) {
super . init ( coder : coder )
commonInit ( )
}
2018-09-04 07:33:00 +02:00
override func viewWillAppear ( ) {
2018-11-26 22:17:16 +01:00
super . viewWillAppear ( )
updateUI ( )
2020-09-06 23:15:46 +02:00
updateNotificationSettings ( )
2018-11-26 22:17:16 +01:00
}
// MARK: - N o t i f i c a t i o n s
@objc func applicationWillBecomeActive ( _ note : Notification ) {
updateUI ( )
}
2021-09-09 00:36:52 +02:00
@objc func articleThemeNamesDidChangeNotification ( _ note : Notification ) {
updateArticleThemePopup ( )
}
2018-11-26 22:17:16 +01:00
// MARK: - A c t i o n s
2021-09-12 03:32:15 +02:00
@IBAction func showThemesFolder ( _ sender : Any ) {
let url = URL ( fileURLWithPath : ArticleThemesManager . shared . folderPath )
NSWorkspace . shared . open ( url )
}
2021-09-08 23:38:05 +02:00
@IBAction func articleThemePopUpDidChange ( _ sender : Any ) {
guard let menuItem = articleThemePopup . selectedItem else {
return
}
ArticleThemesManager . shared . currentThemeName = menuItem . title
updateArticleThemePopup ( )
}
2020-05-19 04:29:53 +02:00
@IBAction func browserPopUpDidChangeValue ( _ sender : Any ? ) {
guard let menuItem = defaultBrowserPopup . selectedItem else {
return
}
let bundleID = menuItem . representedObject as ? String
2020-07-02 05:17:38 +02:00
AppDefaults . shared . defaultBrowserID = bundleID
2021-09-08 23:38:05 +02:00
updateBrowserPopup ( )
2020-05-19 04:29:53 +02:00
}
2020-09-06 19:22:35 +02:00
2018-09-04 07:33:00 +02:00
}
2018-11-26 22:17:16 +01:00
// MARK: - P r i v a t e
2018-09-04 07:33:00 +02:00
private extension GeneralPreferencesViewController {
2018-11-26 22:17:16 +01:00
func commonInit ( ) {
NotificationCenter . default . addObserver ( self , selector : #selector ( applicationWillBecomeActive ( _ : ) ) , name : NSApplication . willBecomeActiveNotification , object : nil )
2021-09-09 00:36:52 +02:00
NotificationCenter . default . addObserver ( self , selector : #selector ( articleThemeNamesDidChangeNotification ( _ : ) ) , name : . ArticleThemeNamesDidChangeNotification , object : nil )
2018-11-26 22:17:16 +01:00
}
func updateUI ( ) {
2021-09-08 23:38:05 +02:00
updateArticleThemePopup ( )
2020-05-19 04:29:53 +02:00
updateBrowserPopup ( )
2018-11-26 22:17:16 +01:00
}
2021-09-08 23:38:05 +02:00
func updateArticleThemePopup ( ) {
let menu = articleThemePopup . menu !
menu . removeAllItems ( )
menu . addItem ( NSMenuItem ( title : ArticleTheme . defaultTheme . name , action : nil , keyEquivalent : " " ) )
menu . addItem ( NSMenuItem . separator ( ) )
2018-11-26 22:17:16 +01:00
2021-09-08 23:38:05 +02:00
for themeName in ArticleThemesManager . shared . themeNames {
menu . addItem ( NSMenuItem ( title : themeName , action : nil , keyEquivalent : " " ) )
}
articleThemePopup . selectItem ( withTitle : ArticleThemesManager . shared . currentThemeName )
2021-09-09 00:36:52 +02:00
if articleThemePopup . indexOfSelectedItem = = - 1 {
articleThemePopup . selectItem ( withTitle : ArticleTheme . defaultTheme . name )
}
2020-09-23 02:27:36 +02:00
}
2020-05-19 04:29:53 +02:00
func updateBrowserPopup ( ) {
let menu = defaultBrowserPopup . menu !
let allBrowsers = MacWebBrowser . sortedBrowsers ( )
menu . removeAllItems ( )
let defaultBrowser = MacWebBrowser . default
let defaultBrowserFormat = NSLocalizedString ( " System Default (%@) " , comment : " Default browser item title format " )
let defaultBrowserTitle = String ( format : defaultBrowserFormat , defaultBrowser . name ! )
let item = NSMenuItem ( title : defaultBrowserTitle , action : nil , keyEquivalent : " " )
let icon = defaultBrowser . icon !
icon . size = NSSize ( width : 16.0 , height : 16.0 )
item . image = icon
menu . addItem ( item )
menu . addItem ( NSMenuItem . separator ( ) )
for browser in allBrowsers {
2020-10-20 19:02:27 +02:00
guard let name = browser . name else { continue }
let item = NSMenuItem ( title : name , action : nil , keyEquivalent : " " )
2020-05-19 04:29:53 +02:00
item . representedObject = browser . bundleIdentifier
2024-02-25 04:43:10 +01:00
let icon = browser . icon ? ? NSWorkspace . shared . icon ( for : UTType . applicationBundle )
2020-05-19 04:29:53 +02:00
icon . size = NSSize ( width : 16.0 , height : 16.0 )
item . image = browser . icon
menu . addItem ( item )
}
2020-07-02 05:17:38 +02:00
defaultBrowserPopup . selectItem ( at : defaultBrowserPopup . indexOfItem ( withRepresentedObject : AppDefaults . shared . defaultBrowserID ) )
2020-05-19 04:29:53 +02:00
}
2020-09-06 19:22:35 +02:00
2020-09-06 23:15:46 +02:00
func updateNotificationSettings ( ) {
UNUserNotificationCenter . current ( ) . getNotificationSettings { ( settings ) in
self . userNotificationSettings = settings
if settings . authorizationStatus = = . authorized {
DispatchQueue . main . async {
NSApplication . shared . registerForRemoteNotifications ( )
}
}
}
}
func showNotificationsDeniedError ( ) {
let updateAlert = NSAlert ( )
updateAlert . alertStyle = . informational
updateAlert . messageText = NSLocalizedString ( " Enable Notifications " , comment : " Notifications " )
updateAlert . informativeText = NSLocalizedString ( " To enable notifications, open Notifications in System Preferences, then find NetNewsWire in the list. " , comment : " To enable notifications, open Notifications in System Preferences, then find NetNewsWire in the list. " )
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 {
2020-09-23 02:27:36 +02:00
NSWorkspace . shared . open ( URL ( string : " x-apple.systempreferences:com.apple.preference.notifications " ) ! )
2020-09-06 23:15:46 +02:00
}
}
2021-06-23 20:39:02 +02:00
@objc var openFeedsInDefaultNewsReader : Bool {
get {
return AppDefaults . shared . subscribeToFeedsInDefaultBrowser
}
set {
AppDefaults . shared . subscribeToFeedsInDefaultBrowser = newValue
}
}
2018-11-26 22:17:16 +01:00
}