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
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
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 !
@IBOutlet var colorPaletteDetailLabel : UILabel !
@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
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 )
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
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
2020-07-02 10:47:45 +08:00
if AppDefaults . shared . timelineSortDirection == . orderedAscending {
2019-10-21 11:51:33 -05:00
timelineSortOrderSwitch . isOn = true
} else {
timelineSortOrderSwitch . isOn = false
}
2020-07-02 10:47:45 +08:00
if AppDefaults . shared . timelineGroupByFeed {
2019-10-21 16:42:58 -05:00
groupByFeedSwitch . isOn = true
} else {
groupByFeedSwitch . isOn = false
}
2020-07-02 10:47:45 +08:00
if AppDefaults . shared . 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
2020-07-02 10:47:45 +08:00
if AppDefaults . shared . confirmMarkAllAsRead {
2020-01-11 11:30:16 -07:00
confirmMarkAllAsReadSwitch . isOn = true
} else {
confirmMarkAllAsReadSwitch . isOn = false
}
2020-07-02 10:47:45 +08:00
if AppDefaults . shared . articleFullscreenAvailable {
2019-11-24 14:18:58 -06:00
showFullscreenArticlesSwitch . isOn = true
} else {
showFullscreenArticlesSwitch . isOn = false
}
2025-11-05 11:22:10 -08:00
2025-09-14 17:38:27 +08:00
if AppDefaults . shared . isArticleContentJavascriptEnabled {
enableJavaScriptSwitch . isOn = true
} else {
enableJavaScriptSwitch . isOn = false
}
2025-11-05 11:22:10 -08:00
2020-03-15 04:25:25 -05:00
colorPaletteDetailLabel . text = String ( describing : AppDefaults . userInterfaceColorPalette )
2025-11-05 11:22:10 -08:00
2021-08-26 10:27:23 +08:00
openLinksInNetNewsWire . isOn = ! AppDefaults . shared . useSystemBrowser
2025-11-05 11:22:10 -08:00
2019-11-26 10:16:58 -06:00
let buildLabel = NonIntrinsicLabel ( 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
2019-11-24 11:25:42 -06:00
buildLabel . text = " \( Bundle . main . appName ) \( Bundle . main . versionNumber ) (Build \( Bundle . main . buildNumber ) )"
2019-10-21 11:51:33 -05:00
buildLabel . sizeToFit ()
buildLabel . translatesAutoresizingMaskIntoConstraints = false
2025-11-05 11:22:10 -08:00
2019-10-30 19:47:28 -05:00
let wrapperView = UIView ( frame : CGRect ( x : 0 , y : 0 , width : buildLabel . frame . width , height : buildLabel . frame . height + 10.0 ))
wrapperView . translatesAutoresizingMaskIntoConstraints = false
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 {
tableView . scrollToRow ( at : IndexPath ( row : 0 , section : 4 ), at : . top , animated : true )
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
2020-01-19 23:12:02 -07:00
switch section {
2019-10-21 11:51:33 -05:00
case 1 :
2019-10-22 08:43:39 -05:00
return AccountManager . shared . accounts . count + 1
2019-10-30 11:51:01 -05:00
case 2 :
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
2024-12-04 18:39:16 -08:00
case 4 :
2025-09-14 17:38:27 +08:00
return traitCollection . userInterfaceIdiom == . phone ? 5 : 4
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
2020-01-19 23:12:02 -07:00
switch indexPath . section {
2019-10-22 08:43:39 -05:00
case 1 :
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 )
2019-10-21 11:51:33 -05:00
cell . textLabel ?. text = NSLocalizedString ( "Add Account" , comment : "Accounts" )
} 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
2020-01-19 23:12:02 -07:00
switch indexPath . section {
2019-10-21 11:51:33 -05:00
case 0 :
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 )
2019-10-22 08:43:39 -05:00
case 1 :
2019-10-21 11:51:33 -05:00
let sortedAccounts = AccountManager . shared . sortedAccounts
if indexPath . row == sortedAccounts . count {
let controller = UIStoryboard . settings . instantiateController ( ofType : AddAccountViewController . self )
self . navigationController ?. pushViewController ( controller , animated : true )
} 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 )
}
2019-10-30 11:51:01 -05:00
case 2 :
2019-10-21 16:27:54 -05:00
switch indexPath . row {
case 0 :
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 )
}
2019-11-13 17:13:06 -06:00
case 1 :
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 )
}
2019-11-13 17:13:06 -06:00
case 2 :
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
}
2024-12-04 18:39:16 -08:00
case 3 :
2019-11-08 17:16:09 -06:00
switch indexPath . row {
2020-01-16 14:44:30 -07:00
case 3 :
2026-01-02 10:30:01 -08:00
let timeline = UIStoryboard . settings . instantiateController ( ofType : TimelineCustomizerTableViewController . self )
2019-11-08 17:16:09 -06:00
self . navigationController ?. pushViewController ( timeline , animated : true )
default :
break
}
2024-12-04 18:39:16 -08:00
case 4 :
2021-09-12 16:18:55 -05:00
switch indexPath . row {
case 0 :
let articleThemes = UIStoryboard . settings . instantiateController ( ofType : ArticleThemesTableViewController . self )
self . navigationController ?. pushViewController ( articleThemes , animated : true )
default :
break
}
2024-12-04 18:39:16 -08:00
case 5 :
2020-03-15 04:25:25 -05:00
let colorPalette = UIStoryboard . settings . instantiateController ( ofType : ColorPaletteTableViewController . self )
self . navigationController ?. pushViewController ( colorPalette , animated : true )
2024-12-04 18:39:16 -08:00
case 6 :
2019-10-21 11:51:33 -05:00
switch indexPath . row {
case 0 :
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 )
2019-10-21 11:51:33 -05:00
case 1 :
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 )
2024-12-04 19:38:39 -08:00
case 2 :
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 )
2024-12-04 19:38:39 -08:00
case 3 :
2026-01-01 21:14:18 +08:00
openURL ( HelpURL . bugTracker . rawValue )
tableView . selectRow ( at : nil , animated : true , scrollPosition : . none )
case 4 :
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 {
2019-11-24 14:18:58 -06:00
return super . tableView ( tableView , indentationLevelForRowAt : IndexPath ( row : 0 , section : 1 ))
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 {
2020-07-02 10:47:45 +08:00
AppDefaults . shared . timelineSortDirection = . orderedAscending
2019-10-21 11:51:33 -05:00
} else {
2020-07-02 10:47:45 +08:00
AppDefaults . shared . 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 {
2020-07-02 10:47:45 +08:00
AppDefaults . shared . timelineGroupByFeed = true
2019-10-21 16:42:58 -05:00
} else {
2020-07-02 10:47:45 +08:00
AppDefaults . shared . 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 {
2020-07-02 10:47:45 +08:00
AppDefaults . shared . refreshClearsReadArticles = true
2020-01-10 18:14:21 -07:00
} else {
2020-07-02 10:47:45 +08:00
AppDefaults . shared . 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 {
2020-07-02 10:47:45 +08:00
AppDefaults . shared . confirmMarkAllAsRead = true
2020-01-11 11:30:16 -07:00
} else {
2020-07-02 10:47:45 +08:00
AppDefaults . shared . 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 {
2020-07-02 10:47:45 +08:00
AppDefaults . shared . articleFullscreenAvailable = true
2019-11-24 14:18:58 -06:00
} else {
2020-07-02 10:47:45 +08:00
AppDefaults . shared . 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 {
AppDefaults . shared . useSystemBrowser = false
} else {
AppDefaults . shared . useSystemBrowser = true
}
}
2025-11-05 11:22:10 -08:00
2025-09-14 17:38:27 +08:00
@IBAction func switchJavaScriptPreference ( _ sender : Any ) {
AppDefaults . shared . isArticleContentJavascriptEnabled = enableJavaScriptSwitch . isOn
}
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
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
2019-10-21 11:51:33 -05:00
func addFeed () {
self . dismiss ( animated : true )
2020-08-11 15:00:31 -05:00
2025-11-03 11:00:42 -08:00
let addNavViewController = UIStoryboard . add . instantiateViewController ( withIdentifier : "AddFeedViewControllerNav" ) as ! UINavigationController
2020-08-11 17:04:11 -05:00
let addViewController = addNavViewController . topViewController as ! AddFeedViewController
2020-10-29 17:52:58 -05:00
addViewController . initialFeed = AccountManager . netNewsWireNewsURL
2020-08-11 15:00:31 -05:00
addViewController . initialFeedName = NSLocalizedString ( "NetNewsWire News" , comment : "NetNewsWire News" )
addNavViewController . modalPresentationStyle = . formSheet
2020-08-11 17:04:11 -05:00
addNavViewController . preferredContentSize = AddFeedViewController . preferredContentSizeForFormSheetDisplay
2025-11-05 11:22:10 -08:00
2019-10-21 11:51:33 -05:00
presentingParentController ?. present ( addNavViewController , animated : true )
}
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 )
}
let cancelTitle = NSLocalizedString ( "Cancel" , comment : "Cancel" )
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 )
}
let cancelTitle = NSLocalizedString ( "Cancel" , comment : "Cancel" )
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 )
2019-10-22 10:32:35 -05:00
let opmlString = OPMLExporter . OPMLString ( with : account , title : filename )
2019-10-21 11:51:33 -05:00
do {
try opmlString . write ( to : tempFile , atomically : true , encoding : String . Encoding . utf8 )
} 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 ) {
let vc = SFSafariViewController ( url : URL ( string : urlString ) ! )
vc . modalPresentationStyle = . pageSheet
present ( vc , animated : true )
}
2019-10-21 11:51:33 -05:00
}