2019-10-21 11:51:33 -05:00
//
// SettingsViewController.swift
// NetNewsWire-iOS
//
// Created by Maurice Parker on 4/24/19.
// Copyright © 2019 Ranchero Software. All rights reserved.
//
import UIKit
2020-01-21 12:58:05 -07:00
import CoreServices
2019-10-21 17:52:29 -05:00
import SafariServices
2021-08-22 09:42:06 +08:00
import SwiftUI
2024-12-07 14:56:58 -08:00
import UniformTypeIdentifiers
2025-11-19 19:33:12 -08:00
import RSCore
import Account
2026-05-29 16:16:40 -07:00
import ActivityLog
2019-10-21 11:51:33 -05:00
2025-09-16 10:21:29 -07:00
final class SettingsViewController : UITableViewController {
2019-10-21 11:51:33 -05:00
2026-03-13 10:37:17 -07:00
private enum Section : Int {
case notifications = 0
case accounts = 1
case feeds = 2
case timeline = 3
case articles = 4
case appearance = 5
case troubleshooting = 6
case help = 7
}
2026-03-21 11:41:52 -07:00
private enum TroubleshootingRow : Int {
case errorLog = 0
2026-06-11 10:37:44 -07:00
case activityLog = 1
case accountStats = 2
case dinosaurs = 3
case cloudKitZoneStats = 4
2026-03-21 11:41:52 -07:00
}
2026-05-26 17:01:57 -07:00
private enum FeedsRow : Int {
case importSubscriptions = 0
case exportSubscriptions = 1
case addNetNewsWireNewsFeed = 2
}
private enum TimelineRow : Int {
case sortOrder = 0
case groupByFeed = 1
case refreshClearsReadArticles = 2
case confirmMarkAllAsRead = 3
case timelineLayout = 4
}
private enum ArticlesRow : Int , CaseIterable {
case theme = 0
case openLinksInNetNewsWire = 1
case enableJavaScript = 2
case enableFullScreenArticles = 3
}
private enum HelpRow : Int {
case help = 0
case forum = 1
case releaseNotes = 2
case bugTracker = 3
case about = 4
}
2019-10-22 10:32:35 -05:00
private weak var opmlAccount : Account ?
2025-11-05 11:22:10 -08:00
2025-11-19 19:43:09 -08:00
@IBOutlet var timelineSortOrderSwitch : UISwitch !
@IBOutlet var groupByFeedSwitch : UISwitch !
@IBOutlet var refreshClearsReadArticlesSwitch : UISwitch !
@IBOutlet var articleThemeDetailLabel : UILabel !
@IBOutlet var confirmMarkAllAsReadSwitch : UISwitch !
@IBOutlet var showFullscreenArticlesSwitch : UISwitch !
2026-10-06 11:31:40 -07:00
@IBOutlet var colorPaletteCell : UITableViewCell ?
@IBOutlet var unreadCountDisplayCell : UITableViewCell ?
2025-11-19 19:43:09 -08:00
@IBOutlet var openLinksInNetNewsWire : UISwitch !
@IBOutlet var enableJavaScriptSwitch : UISwitch !
2025-11-05 11:22:10 -08:00
2020-01-11 11:30:16 -07:00
var scrollToArticlesSection = false
2019-10-21 11:51:33 -05:00
weak var presentingParentController : UIViewController ?
2025-11-05 11:22:10 -08:00
2026-10-06 11:31:40 -07:00
private lazy var colorPalettePopUpButton = Self . makePopUpButton ()
private lazy var unreadCountDisplayPopUpButton = Self . makePopUpButton ()
2019-10-21 11:51:33 -05:00
override func viewDidLoad () {
// This hack mostly works around a bug in static tables with dynamic type. See: https://spin.atomicobject.com/2018/10/15/dynamic-type-static-uitableview/
NotificationCenter . default . removeObserver ( tableView !, name : UIContentSizeCategory . didChangeNotification , object : nil )
NotificationCenter . default . addObserver ( self , selector : #selector ( contentSizeCategoryDidChange ), name : UIContentSizeCategory . didChangeNotification , object : nil )
2019-10-22 19:20:35 -05:00
NotificationCenter . default . addObserver ( self , selector : #selector ( accountsDidChange ), name : . UserDidAddAccount , object : nil )
NotificationCenter . default . addObserver ( self , selector : #selector ( accountsDidChange ), name : . UserDidDeleteAccount , object : nil )
2019-10-23 19:58:18 -05:00
NotificationCenter . default . addObserver ( self , selector : #selector ( displayNameDidChange ), name : . DisplayNameDidChange , object : nil )
2026-10-06 11:31:40 -07:00
NotificationCenter . default . addObserver ( self , selector : #selector ( handleUserInterfaceColorPaletteDidUpdate ( _ :)), name : . userInterfaceColorPaletteDidUpdate , object : nil )
NotificationCenter . default . addObserver ( self , selector : #selector ( handleUnreadCountDisplaySettingDidChange ( _ :)), name : . unreadCountDisplaySettingDidChange , object : nil )
2019-10-22 19:20:35 -05:00
2020-04-16 08:25:40 -05:00
tableView . register ( UINib ( nibName : "SettingsComboTableViewCell" , bundle : nil ), forCellReuseIdentifier : "SettingsComboTableViewCell" )
2019-10-21 11:51:33 -05:00
tableView . register ( UINib ( nibName : "SettingsTableViewCell" , bundle : nil ), forCellReuseIdentifier : "SettingsTableViewCell" )
2025-11-05 11:22:10 -08:00
2020-03-12 17:56:43 +01:00
tableView . rowHeight = UITableView . automaticDimension
tableView . estimatedRowHeight = 44
2026-10-06 11:31:40 -07:00
2026-10-09 11:54:15 -07:00
addPopUpButton ( colorPalettePopUpButton , to : colorPaletteCell , accessibilityLabel : NSLocalizedString ( "Color Palette" , comment : "Color Palette" ))
addPopUpButton ( unreadCountDisplayPopUpButton , to : unreadCountDisplayCell , accessibilityLabel : NSLocalizedString ( "Unread Counts" , comment : "Unread Counts" ))
2026-10-06 11:31:40 -07:00
updateColorPalettePopUpButton ()
updateUnreadCountDisplayPopUpButton ()
2019-10-21 11:51:33 -05:00
}
2025-11-05 11:22:10 -08:00
2019-10-21 11:51:33 -05:00
override func viewWillAppear ( _ animated : Bool ) {
super . viewWillAppear ( animated )
2025-11-05 11:22:10 -08:00
2026-10-22 13:46:32 -07:00
if AppDefaults . timelineSortDirection == . orderedAscending {
2019-10-21 11:51:33 -05:00
timelineSortOrderSwitch . isOn = true
} else {
timelineSortOrderSwitch . isOn = false
}
2026-10-22 13:46:32 -07:00
if AppDefaults . timelineGroupByFeed {
2019-10-21 16:42:58 -05:00
groupByFeedSwitch . isOn = true
} else {
groupByFeedSwitch . isOn = false
}
2026-10-22 13:46:32 -07:00
if AppDefaults . refreshClearsReadArticles {
2020-01-10 18:14:21 -07:00
refreshClearsReadArticlesSwitch . isOn = true
} else {
refreshClearsReadArticlesSwitch . isOn = false
}
2021-10-27 21:17:04 +02:00
2021-09-12 16:18:55 -05:00
articleThemeDetailLabel . text = ArticleThemesManager . shared . currentTheme . name
2020-01-10 18:14:21 -07:00
2026-10-22 13:46:32 -07:00
if AppDefaults . confirmMarkAllAsRead {
2020-01-11 11:30:16 -07:00
confirmMarkAllAsReadSwitch . isOn = true
} else {
confirmMarkAllAsReadSwitch . isOn = false
}
2026-10-22 13:46:32 -07:00
if AppDefaults . articleFullscreenAvailable {
2019-11-24 14:18:58 -06:00
showFullscreenArticlesSwitch . isOn = true
} else {
showFullscreenArticlesSwitch . isOn = false
}
2025-11-05 11:22:10 -08:00
2026-10-22 13:46:32 -07:00
if AppDefaults . isArticleContentJavascriptEnabled {
2025-09-14 17:38:27 +08:00
enableJavaScriptSwitch . isOn = true
} else {
enableJavaScriptSwitch . isOn = false
}
2025-11-05 11:22:10 -08:00
2026-10-22 13:46:32 -07:00
openLinksInNetNewsWire . isOn = ! AppDefaults . useSystemBrowser
2025-11-05 11:22:10 -08:00
2026-10-21 08:55:46 -07:00
let buildLabel = CopyableLabel ( frame : CGRect ( x : 32.0 , y : 0.0 , width : 0.0 , height : 0.0 ))
2019-10-21 11:51:33 -05:00
buildLabel . font = UIFont . systemFont ( ofSize : 11.0 )
buildLabel . textColor = UIColor . gray
2026-10-21 08:55:46 -07:00
buildLabel . text = " \( Bundle . main . appName ) \( Bundle . main . versionNumber ) (build \( Bundle . main . buildNumber ) )"
2019-10-21 11:51:33 -05:00
buildLabel . sizeToFit ()
2025-11-05 11:22:10 -08:00
2026-10-20 21:55:08 -07:00
let wrapperView = UIView ( frame : CGRect ( x : 0 , y : 0 , width : tableView . bounds . width , height : buildLabel . frame . height + 10.0 ))
2019-10-30 19:47:28 -05:00
wrapperView . addSubview ( buildLabel )
tableView . tableFooterView = wrapperView
2019-10-21 11:51:33 -05:00
2019-10-22 19:20:35 -05:00
}
2025-11-05 11:22:10 -08:00
2019-10-22 19:20:35 -05:00
override func viewDidAppear ( _ animated : Bool ) {
super . viewDidAppear ( animated )
self . tableView . selectRow ( at : nil , animated : true , scrollPosition : . none )
2025-11-05 11:22:10 -08:00
2020-01-11 11:30:16 -07:00
if scrollToArticlesSection {
2026-03-13 10:37:17 -07:00
tableView . scrollToRow ( at : IndexPath ( row : 0 , section : Section . articles . rawValue ), at : . top , animated : true )
2020-01-11 11:30:16 -07:00
scrollToArticlesSection = false
}
2019-10-21 11:51:33 -05:00
}
2025-11-05 11:22:10 -08:00
2019-10-21 11:51:33 -05:00
// MARK: UITableView
2025-11-05 11:22:10 -08:00
2019-10-21 11:51:33 -05:00
override func tableView ( _ tableView : UITableView , numberOfRowsInSection section : Int ) -> Int {
2024-12-04 18:39:16 -08:00
2026-03-13 10:37:17 -07:00
switch Section ( rawValue : section ) {
case . accounts :
2019-10-22 08:43:39 -05:00
return AccountManager . shared . accounts . count + 1
2026-03-13 10:37:17 -07:00
case . feeds :
2020-01-19 23:12:02 -07:00
let defaultNumberOfRows = super . tableView ( tableView , numberOfRowsInSection : section )
2020-10-29 17:52:58 -05:00
if AccountManager . shared . activeAccounts . isEmpty || AccountManager . shared . anyAccountHasNetNewsWireNewsSubscription () {
2019-10-21 18:02:44 -05:00
return defaultNumberOfRows - 1
}
return defaultNumberOfRows
2026-03-13 10:37:17 -07:00
case . articles :
2026-05-26 17:01:57 -07:00
// The Full Screen Articles row is iPhone-only.
return traitCollection . userInterfaceIdiom == . phone ? ArticlesRow . allCases . count : ArticlesRow . allCases . count - 1
2026-03-25 11:41:00 -07:00
case . troubleshooting :
let defaultNumberOfRows = super . tableView ( tableView , numberOfRowsInSection : section )
if ! AccountManager . shared . hasiCloudAccount {
return defaultNumberOfRows - 1
}
return defaultNumberOfRows
2019-10-21 11:51:33 -05:00
default :
2020-01-19 23:12:02 -07:00
return super . tableView ( tableView , numberOfRowsInSection : section )
2019-10-21 11:51:33 -05:00
}
}
2024-12-04 18:39:16 -08:00
2019-10-21 11:51:33 -05:00
override func tableView ( _ tableView : UITableView , cellForRowAt indexPath : IndexPath ) -> UITableViewCell {
2019-11-24 14:18:58 -06:00
2019-10-21 11:51:33 -05:00
let cell : UITableViewCell
2026-03-13 10:37:17 -07:00
switch Section ( rawValue : indexPath . section ) {
case . accounts :
2024-12-04 18:39:16 -08:00
2019-10-21 11:51:33 -05:00
let sortedAccounts = AccountManager . shared . sortedAccounts
if indexPath . row == sortedAccounts . count {
2019-10-23 10:35:53 -05:00
cell = tableView . dequeueReusableCell ( withIdentifier : "SettingsTableViewCell" , for : indexPath )
2026-06-10 10:48:40 -07:00
cell . textLabel ?. text = NSLocalizedString ( "Add Account" , comment : "Add Account" )
2019-10-21 11:51:33 -05:00
} else {
2020-04-16 08:25:40 -05:00
let acctCell = tableView . dequeueReusableCell ( withIdentifier : "SettingsComboTableViewCell" , for : indexPath ) as ! SettingsComboTableViewCell
2019-10-23 11:47:21 -05:00
acctCell . applyThemeProperties ()
2019-10-23 10:35:53 -05:00
let account = sortedAccounts [ indexPath . row ]
2025-11-18 22:08:33 -08:00
acctCell . comboImage ?. image = Assets . accountImage ( account . type )
2020-04-16 08:25:40 -05:00
acctCell . comboNameLabel ?. text = account . nameForDisplay
cell = acctCell
}
2019-10-21 11:51:33 -05:00
default :
2020-01-19 23:12:02 -07:00
cell = super . tableView ( tableView , cellForRowAt : indexPath )
2024-12-04 18:39:16 -08:00
2019-10-21 11:51:33 -05:00
}
2024-12-04 18:39:16 -08:00
2019-10-21 11:51:33 -05:00
return cell
}
override func tableView ( _ tableView : UITableView , didSelectRowAt indexPath : IndexPath ) {
2019-11-24 14:18:58 -06:00
2026-03-13 10:37:17 -07:00
switch Section ( rawValue : indexPath . section ) {
case . notifications :
2019-10-22 08:43:39 -05:00
UIApplication . shared . open ( URL ( string : " \( UIApplication . openSettingsURLString ) " ) ! )
2019-10-22 19:20:35 -05:00
tableView . selectRow ( at : nil , animated : true , scrollPosition : . none )
2026-03-13 10:37:17 -07:00
case . accounts :
2019-10-21 11:51:33 -05:00
let sortedAccounts = AccountManager . shared . sortedAccounts
if indexPath . row == sortedAccounts . count {
2026-09-18 11:56:16 -07:00
let addAccountView = AddAccountView ( presentationAnchor : view . window ) { [ weak self ] in
self ?. navigationController ?. popViewController ( animated : false )
}
self . navigationController ?. pushViewController ( UIHostingController ( rootView : addAccountView ), animated : true )
2019-10-21 11:51:33 -05:00
} else {
2019-10-23 19:58:18 -05:00
let controller = UIStoryboard . inspector . instantiateController ( ofType : AccountInspectorViewController . self )
2019-10-21 11:51:33 -05:00
controller . account = sortedAccounts [ indexPath . row ]
self . navigationController ?. pushViewController ( controller , animated : true )
}
2026-03-13 10:37:17 -07:00
case . feeds :
2026-05-26 17:01:57 -07:00
switch FeedsRow ( rawValue : indexPath . row ) {
case . importSubscriptions :
2019-10-22 19:20:35 -05:00
tableView . selectRow ( at : nil , animated : true , scrollPosition : . none )
2019-10-22 10:32:35 -05:00
if let sourceView = tableView . cellForRow ( at : indexPath ) {
let sourceRect = tableView . rectForRow ( at : indexPath )
importOPML ( sourceView : sourceView , sourceRect : sourceRect )
}
2026-05-26 17:01:57 -07:00
case . exportSubscriptions :
2019-10-22 19:20:35 -05:00
tableView . selectRow ( at : nil , animated : true , scrollPosition : . none )
2019-10-22 10:32:35 -05:00
if let sourceView = tableView . cellForRow ( at : indexPath ) {
let sourceRect = tableView . rectForRow ( at : indexPath )
exportOPML ( sourceView : sourceView , sourceRect : sourceRect )
}
2026-05-26 17:01:57 -07:00
case . addNetNewsWireNewsFeed :
2019-10-30 11:51:01 -05:00
addFeed ()
tableView . selectRow ( at : nil , animated : true , scrollPosition : . none )
2019-10-21 16:27:54 -05:00
default :
2019-10-21 16:42:58 -05:00
break
2019-10-21 16:27:54 -05:00
}
2026-03-13 10:37:17 -07:00
case . timeline :
2026-05-26 17:01:57 -07:00
switch TimelineRow ( rawValue : indexPath . row ) {
case . timelineLayout :
2026-01-27 22:12:31 +08:00
let timeline = UIStoryboard . settings . instantiateController ( ofType : TimelineCustomizerCollectionViewController . self )
2019-11-08 17:16:09 -06:00
self . navigationController ?. pushViewController ( timeline , animated : true )
default :
break
}
2026-03-13 10:37:17 -07:00
case . articles :
2026-05-26 17:01:57 -07:00
switch ArticlesRow ( rawValue : indexPath . row ) {
case . theme :
2021-09-12 16:18:55 -05:00
let articleThemes = UIStoryboard . settings . instantiateController ( ofType : ArticleThemesTableViewController . self )
self . navigationController ?. pushViewController ( articleThemes , animated : true )
default :
break
}
2026-03-13 10:37:17 -07:00
case . troubleshooting :
2026-03-21 11:41:52 -07:00
let viewController : UIViewController ? = {
switch TroubleshootingRow ( rawValue : indexPath . row ) {
case . errorLog :
return UIHostingController ( rootView : ErrorLogView ())
2026-06-09 11:25:32 -07:00
case . accountStats :
return UIHostingController ( rootView : AccountStatsView ())
2026-03-21 11:41:52 -07:00
case . cloudKitZoneStats :
2026-03-21 20:55:51 -07:00
return UIHostingController ( rootView : CloudKitStatsView ())
2026-06-11 09:37:28 -07:00
case . activityLog :
return UIHostingController ( rootView : ActivityLogView ())
2026-06-10 21:25:09 +08:00
case . dinosaurs :
return UIHostingController ( rootView : DinosaursView ( dismissAndPresent : { [ weak self ] dinosaur in
guard let self else {
return
}
self . dismiss ( animated : true ) {
if let rootSplit = self . presentingParentController as ? RootSplitViewController {
2026-06-10 12:17:19 -07:00
rootSplit . coordinator . discloseFeed ( dinosaur . feed , animations : [. scroll , . navigation ])
2026-06-10 21:25:09 +08:00
}
}
}))
2026-03-21 11:41:52 -07:00
default :
return nil
}
}()
if let viewController {
self . navigationController ?. pushViewController ( viewController , animated : true )
}
2026-03-13 10:37:17 -07:00
case . help :
2026-05-26 17:01:57 -07:00
switch HelpRow ( rawValue : indexPath . row ) {
case . help :
2025-09-29 13:50:01 -07:00
openURL ( HelpURL . helpHome . rawValue )
2019-11-24 11:33:01 -06:00
tableView . selectRow ( at : nil , animated : true , scrollPosition : . none )
2026-05-26 17:01:57 -07:00
case . forum :
2026-01-01 21:14:18 +08:00
openURL ( HelpURL . discourse . rawValue )
2019-10-22 19:20:35 -05:00
tableView . selectRow ( at : nil , animated : true , scrollPosition : . none )
2026-05-26 17:01:57 -07:00
case . releaseNotes :
2026-01-01 21:14:18 +08:00
openURL ( HelpURL . releaseNotes . rawValue )
2019-10-22 19:20:35 -05:00
tableView . selectRow ( at : nil , animated : true , scrollPosition : . none )
2026-05-26 17:01:57 -07:00
case . bugTracker :
2026-01-01 21:14:18 +08:00
openURL ( HelpURL . bugTracker . rawValue )
tableView . selectRow ( at : nil , animated : true , scrollPosition : . none )
2026-05-26 17:01:57 -07:00
case . about :
2026-01-02 20:24:20 -08:00
let hosting = UIHostingController ( rootView : AboutView ())
self . navigationController ?. pushViewController ( hosting , animated : true )
2019-10-21 11:51:33 -05:00
default :
2019-10-21 17:52:29 -05:00
break
2019-10-21 11:51:33 -05:00
}
default :
2019-10-22 19:20:35 -05:00
tableView . selectRow ( at : nil , animated : true , scrollPosition : . none )
2019-10-21 11:51:33 -05:00
}
}
2024-12-04 18:39:16 -08:00
2019-10-21 11:51:33 -05:00
override func tableView ( _ tableView : UITableView , canEditRowAt indexPath : IndexPath ) -> Bool {
return false
}
2025-11-05 11:22:10 -08:00
2019-10-21 11:51:33 -05:00
override func tableView ( _ tableView : UITableView , canMoveRowAt indexPath : IndexPath ) -> Bool {
return false
}
override func tableView ( _ tableView : UITableView , editingStyleForRowAt indexPath : IndexPath ) -> UITableViewCell . EditingStyle {
return . none
}
2025-11-05 11:22:10 -08:00
2019-10-21 11:51:33 -05:00
override func tableView ( _ tableView : UITableView , heightForRowAt indexPath : IndexPath ) -> CGFloat {
2020-03-12 17:56:43 +01:00
return UITableView . automaticDimension
2019-10-21 11:51:33 -05:00
}
2025-11-05 11:22:10 -08:00
2019-10-21 11:51:33 -05:00
override func tableView ( _ tableView : UITableView , indentationLevelForRowAt indexPath : IndexPath ) -> Int {
2026-05-26 17:01:57 -07:00
return super . tableView ( tableView , indentationLevelForRowAt : IndexPath ( row : 0 , section : Section . accounts . rawValue ))
2019-10-21 11:51:33 -05:00
}
2025-11-05 11:22:10 -08:00
2019-10-21 11:51:33 -05:00
// MARK: Actions
2025-11-05 11:22:10 -08:00
2019-10-21 11:51:33 -05:00
@IBAction func done ( _ sender : Any ) {
dismiss ( animated : true )
}
2025-11-05 11:22:10 -08:00
2019-10-21 11:51:33 -05:00
@IBAction func switchTimelineOrder ( _ sender : Any ) {
if timelineSortOrderSwitch . isOn {
2026-10-22 13:46:32 -07:00
AppDefaults . timelineSortDirection = . orderedAscending
2019-10-21 11:51:33 -05:00
} else {
2026-10-22 13:46:32 -07:00
AppDefaults . timelineSortDirection = . orderedDescending
2019-10-21 11:51:33 -05:00
}
}
2025-11-05 11:22:10 -08:00
2019-10-21 16:42:58 -05:00
@IBAction func switchGroupByFeed ( _ sender : Any ) {
if groupByFeedSwitch . isOn {
2026-10-22 13:46:32 -07:00
AppDefaults . timelineGroupByFeed = true
2019-10-21 16:42:58 -05:00
} else {
2026-10-22 13:46:32 -07:00
AppDefaults . timelineGroupByFeed = false
2019-10-21 16:42:58 -05:00
}
}
2025-11-05 11:22:10 -08:00
2020-01-10 18:14:21 -07:00
@IBAction func switchClearsReadArticles ( _ sender : Any ) {
if refreshClearsReadArticlesSwitch . isOn {
2026-10-22 13:46:32 -07:00
AppDefaults . refreshClearsReadArticles = true
2020-01-10 18:14:21 -07:00
} else {
2026-10-22 13:46:32 -07:00
AppDefaults . refreshClearsReadArticles = false
2020-01-10 18:14:21 -07:00
}
}
2025-11-05 11:22:10 -08:00
2020-01-11 11:30:16 -07:00
@IBAction func switchConfirmMarkAllAsRead ( _ sender : Any ) {
if confirmMarkAllAsReadSwitch . isOn {
2026-10-22 13:46:32 -07:00
AppDefaults . confirmMarkAllAsRead = true
2020-01-11 11:30:16 -07:00
} else {
2026-10-22 13:46:32 -07:00
AppDefaults . confirmMarkAllAsRead = false
2020-01-11 11:30:16 -07:00
}
}
2025-11-05 11:22:10 -08:00
2019-11-24 14:18:58 -06:00
@IBAction func switchFullscreenArticles ( _ sender : Any ) {
if showFullscreenArticlesSwitch . isOn {
2026-10-22 13:46:32 -07:00
AppDefaults . articleFullscreenAvailable = true
2019-11-24 14:18:58 -06:00
} else {
2026-10-22 13:46:32 -07:00
AppDefaults . articleFullscreenAvailable = false
2019-11-24 14:18:58 -06:00
}
}
2025-11-05 11:22:10 -08:00
2021-08-26 10:27:23 +08:00
@IBAction func switchBrowserPreference ( _ sender : Any ) {
if openLinksInNetNewsWire . isOn {
2026-10-22 13:46:32 -07:00
AppDefaults . useSystemBrowser = false
2021-08-26 10:27:23 +08:00
} else {
2026-10-22 13:46:32 -07:00
AppDefaults . useSystemBrowser = true
2021-08-26 10:27:23 +08:00
}
}
2025-11-05 11:22:10 -08:00
2025-09-14 17:38:27 +08:00
@IBAction func switchJavaScriptPreference ( _ sender : Any ) {
2026-10-22 13:46:32 -07:00
AppDefaults . isArticleContentJavascriptEnabled = enableJavaScriptSwitch . isOn
2025-09-14 17:38:27 +08:00
}
2025-11-05 11:22:10 -08:00
2026-01-01 17:55:12 -08:00
// MARK: - Notifications
2025-11-05 11:22:10 -08:00
2019-10-21 11:51:33 -05:00
@objc func contentSizeCategoryDidChange () {
tableView . reloadData ()
}
2025-11-05 11:22:10 -08:00
2019-10-22 19:20:35 -05:00
@objc func accountsDidChange () {
tableView . reloadData ()
}
2019-10-23 19:58:18 -05:00
@objc func displayNameDidChange () {
tableView . reloadData ()
}
2025-11-05 11:22:10 -08:00
2021-08-22 09:42:06 +08:00
@objc func browserPreferenceDidChange () {
tableView . reloadData ()
}
2025-11-05 11:22:10 -08:00
2026-10-06 11:31:40 -07:00
@objc func handleUserInterfaceColorPaletteDidUpdate ( _ notification : Notification ) {
updateColorPalettePopUpButton ()
}
@objc func handleUnreadCountDisplaySettingDidChange ( _ notification : Notification ) {
updateUnreadCountDisplayPopUpButton ()
}
2019-10-21 11:51:33 -05:00
}
2026-01-01 17:55:12 -08:00
// MARK: - OPML Document Picker
2019-10-21 11:51:33 -05:00
extension SettingsViewController : UIDocumentPickerDelegate {
2025-11-05 11:22:10 -08:00
2019-10-21 11:51:33 -05:00
func documentPicker ( _ controller : UIDocumentPickerViewController , didPickDocumentsAt urls : [ URL ]) {
for url in urls {
2019-10-30 12:04:39 -05:00
opmlAccount ?. importOPML ( url ) { result in
switch result {
case . success :
break
2020-01-20 14:57:07 -07:00
case . failure :
2019-10-30 12:04:39 -05:00
let title = NSLocalizedString ( "Import Failed" , comment : "Import Failed" )
2020-01-20 14:57:07 -07:00
let message = NSLocalizedString ( "We were unable to process the selected file. Please ensure that it is a properly formatted OPML file." , comment : "Import Failed Message" )
self . presentError ( title : title , message : message )
2019-10-30 12:04:39 -05:00
}
}
2019-10-21 11:51:33 -05:00
}
}
2025-11-05 11:22:10 -08:00
2019-10-21 11:51:33 -05:00
}
2026-01-01 17:55:12 -08:00
// MARK: - Private
2019-10-21 11:51:33 -05:00
private extension SettingsViewController {
2025-11-05 11:22:10 -08:00
2026-10-06 11:31:40 -07:00
static func makePopUpButton () -> UIButton {
var configuration = UIButton . Configuration . plain ()
configuration . baseForegroundColor = . secondaryLabel
let button = UIButton ( configuration : configuration )
button . showsMenuAsPrimaryAction = true
button . changesSelectionAsPrimaryAction = true
return button
}
/// Auto Layout lets the button resize itself when choosing an item changes its title.
2026-10-09 11:54:15 -07:00
/// The accessibility label names the setting, since the button’ s title is only the current choice.
func addPopUpButton ( _ button : UIButton , to cell : UITableViewCell ?, accessibilityLabel : String ) {
2026-10-06 11:31:40 -07:00
guard let cell else {
return
}
2026-10-09 11:54:15 -07:00
button . accessibilityLabel = accessibilityLabel
2026-10-06 11:31:40 -07:00
button . translatesAutoresizingMaskIntoConstraints = false
button . setContentCompressionResistancePriority (. required , for : . horizontal )
cell . contentView . addSubview ( button )
NSLayoutConstraint . activate ([
button . trailingAnchor . constraint ( equalTo : cell . contentView . layoutMarginsGuide . trailingAnchor ),
button . centerYAnchor . constraint ( equalTo : cell . contentView . centerYAnchor )
])
}
func updateColorPalettePopUpButton () {
let currentColorPalette = AppDefaults . userInterfaceColorPalette
let actions = UserInterfaceColorPalette . allCases . map { colorPalette in
UIAction ( title : String ( describing : colorPalette ), state : colorPalette == currentColorPalette ? . on : . off ) { _ in
AppDefaults . userInterfaceColorPalette = colorPalette
}
}
colorPalettePopUpButton . menu = UIMenu ( children : actions )
2026-10-09 11:54:15 -07:00
colorPalettePopUpButton . accessibilityValue = String ( describing : currentColorPalette )
2026-10-06 11:31:40 -07:00
}
func updateUnreadCountDisplayPopUpButton () {
2026-10-22 13:46:32 -07:00
let currentUnreadCountDisplay = AppDefaults . unreadCountDisplay
2026-10-06 11:31:40 -07:00
let actions = UnreadCountDisplay . allCases . map { unreadCountDisplay in
UIAction ( title : String ( describing : unreadCountDisplay ), state : unreadCountDisplay == currentUnreadCountDisplay ? . on : . off ) { _ in
2026-10-22 13:46:32 -07:00
AppDefaults . unreadCountDisplay = unreadCountDisplay
2026-10-06 11:31:40 -07:00
}
}
unreadCountDisplayPopUpButton . menu = UIMenu ( children : actions )
2026-10-09 11:54:15 -07:00
unreadCountDisplayPopUpButton . accessibilityValue = String ( describing : currentUnreadCountDisplay )
2026-10-06 11:31:40 -07:00
}
2019-10-21 11:51:33 -05:00
func addFeed () {
self . dismiss ( animated : true )
2020-08-11 15:00:31 -05:00
2026-09-18 12:52:29 -07:00
let addFeedView = AddFeedView ( initialFeed : AccountManager . netNewsWireNewsURL , initialFeedName : NSLocalizedString ( "NetNewsWire News" , comment : "NetNewsWire News" ))
let hostingController = UIHostingController ( rootView : addFeedView )
hostingController . modalPresentationStyle = . formSheet
2026-10-09 09:23:52 -07:00
hostingController . preferredContentSize = UIViewController . formSheetSize
2025-11-05 11:22:10 -08:00
2026-09-18 12:52:29 -07:00
presentingParentController ?. present ( hostingController , animated : true )
2019-10-21 11:51:33 -05:00
}
2025-11-05 11:22:10 -08:00
2019-10-22 10:32:35 -05:00
func importOPML ( sourceView : UIView , sourceRect : CGRect ) {
switch AccountManager . shared . activeAccounts . count {
case 0 :
presentError ( title : "Error" , message : NSLocalizedString ( "You must have at least one active account." , comment : "Missing active account" ))
case 1 :
opmlAccount = AccountManager . shared . activeAccounts . first
importOPMLDocumentPicker ()
default :
importOPMLAccountPicker ( sourceView : sourceView , sourceRect : sourceRect )
}
}
2025-11-05 11:22:10 -08:00
2019-10-22 10:32:35 -05:00
func importOPMLAccountPicker ( sourceView : UIView , sourceRect : CGRect ) {
2019-11-26 10:23:33 -06:00
let title = NSLocalizedString ( "Choose an account to receive the imported feeds and folders" , comment : "Import Account" )
2019-10-22 10:32:35 -05:00
let alert = UIAlertController ( title : title , message : nil , preferredStyle : . actionSheet )
2025-11-05 11:22:10 -08:00
2019-10-22 10:32:35 -05:00
if let popoverController = alert . popoverPresentationController {
popoverController . sourceView = view
popoverController . sourceRect = sourceRect
}
2019-10-22 17:18:37 -05:00
for account in AccountManager . shared . sortedActiveAccounts {
2026-01-01 16:17:54 -08:00
let action = UIAlertAction ( title : account . nameForDisplay , style : . default ) { [ weak self ] _ in
2019-10-22 10:32:35 -05:00
self ?. opmlAccount = account
self ?. importOPMLDocumentPicker ()
}
alert . addAction ( action )
}
2026-06-06 14:43:31 -07:00
let cancelTitle = NSLocalizedString ( "Cancel" , comment : "Cancel button" )
2019-10-22 10:32:35 -05:00
alert . addAction ( UIAlertAction ( title : cancelTitle , style : . cancel ))
self . present ( alert , animated : true )
}
2025-11-05 11:22:10 -08:00
2019-10-22 10:32:35 -05:00
func importOPMLDocumentPicker () {
2025-12-05 09:15:44 -08:00
var contentTypes : [ UTType ] = []
2025-11-05 11:22:10 -08:00
2025-12-05 09:15:44 -08:00
// Create UTType for .opml files by extension, without requiring conformance.
// This ensures files ending in .opml can be selected no matter how OPML is registered.
// <https://github.com/Ranchero-Software/NetNewsWire/issues/4858>
if let opmlByExtension = UTType ( filenameExtension : "opml" ) {
contentTypes . append ( opmlByExtension )
}
// Also try the registered org.opml.opml UTI if it exists
if let registeredOPML = UTType ( "org.opml.opml" ) {
contentTypes . append ( registeredOPML )
}
// Include XML as a fallback
contentTypes . append (. xml )
let documentPicker = UIDocumentPickerViewController ( forOpeningContentTypes : contentTypes , asCopy : true )
documentPicker . delegate = self
documentPicker . modalPresentationStyle = . formSheet
self . present ( documentPicker , animated : true )
2019-10-21 11:51:33 -05:00
}
2025-11-05 11:22:10 -08:00
2019-10-22 10:32:35 -05:00
func exportOPML ( sourceView : UIView , sourceRect : CGRect ) {
if AccountManager . shared . accounts . count == 1 {
2019-12-26 12:35:39 -07:00
opmlAccount = AccountManager . shared . accounts . first !
2019-10-22 10:32:35 -05:00
exportOPMLDocumentPicker ()
} else {
exportOPMLAccountPicker ( sourceView : sourceView , sourceRect : sourceRect )
}
}
2025-11-05 11:22:10 -08:00
2019-10-22 10:32:35 -05:00
func exportOPMLAccountPicker ( sourceView : UIView , sourceRect : CGRect ) {
2019-11-26 10:21:18 -06:00
let title = NSLocalizedString ( "Choose an account with the subscriptions to export" , comment : "Export Account" )
2019-10-22 10:32:35 -05:00
let alert = UIAlertController ( title : title , message : nil , preferredStyle : . actionSheet )
2025-11-05 11:22:10 -08:00
2019-10-22 10:32:35 -05:00
if let popoverController = alert . popoverPresentationController {
popoverController . sourceView = view
popoverController . sourceRect = sourceRect
}
2019-10-22 17:18:37 -05:00
for account in AccountManager . shared . sortedAccounts {
2026-01-01 16:17:54 -08:00
let action = UIAlertAction ( title : account . nameForDisplay , style : . default ) { [ weak self ] _ in
2019-10-22 10:32:35 -05:00
self ?. opmlAccount = account
self ?. exportOPMLDocumentPicker ()
}
alert . addAction ( action )
}
2026-06-06 14:43:31 -07:00
let cancelTitle = NSLocalizedString ( "Cancel" , comment : "Cancel button" )
2019-10-22 10:32:35 -05:00
alert . addAction ( UIAlertAction ( title : cancelTitle , style : . cancel ))
self . present ( alert , animated : true )
}
2025-11-05 11:22:10 -08:00
2019-10-22 10:32:35 -05:00
func exportOPMLDocumentPicker () {
guard let account = opmlAccount else { return }
2025-11-05 11:22:10 -08:00
2019-10-22 10:32:35 -05:00
let accountName = account . nameForDisplay . replacingOccurrences ( of : " " , with : "" ). trimmingCharacters ( in : . whitespaces )
let filename = "Subscriptions- \( accountName ) .opml"
2019-10-21 11:51:33 -05:00
let tempFile = FileManager . default . temporaryDirectory . appendingPathComponent ( filename )
do {
2026-05-29 16:16:40 -07:00
try account . logActivity ( kind : . exportOPML , detail : filename ) {
let opmlString = OPMLExporter . OPMLString ( with : account , title : filename )
try opmlString . write ( to : tempFile , atomically : true , encoding : String . Encoding . utf8 )
}
2019-10-21 11:51:33 -05:00
} catch {
self . presentError ( title : "OPML Export Error" , message : error . localizedDescription )
}
2025-11-05 11:22:10 -08:00
2024-12-07 14:59:17 -08:00
let docPicker = UIDocumentPickerViewController ( forExporting : [ tempFile ])
2019-10-21 11:51:33 -05:00
docPicker . modalPresentationStyle = . formSheet
self . present ( docPicker , animated : true )
}
2025-11-05 11:22:10 -08:00
2019-10-21 17:52:29 -05:00
func openURL ( _ urlString : String ) {
2026-07-05 13:19:38 -07:00
guard let url = URL ( string : urlString ) else {
return
}
// Open GitHub links in the GitHub app when installed.
if let host = url . host , host == "github.com" || host . hasSuffix ( ".github.com" ) {
UIApplication . shared . open ( url , options : [. universalLinksOnly : true ]) { [ weak self ] openedInApp in
if ! openedInApp {
self ?. presentSafariViewController ( for : url )
}
}
return
}
presentSafariViewController ( for : url )
}
private func presentSafariViewController ( for url : URL ) {
let vc = SFSafariViewController ( url : url )
2019-10-21 17:52:29 -05:00
vc . modalPresentationStyle = . pageSheet
present ( vc , animated : true )
}
2019-10-21 11:51:33 -05:00
}