Files
NetNewsWire/iOS/Settings/SettingsViewController.swift
T

545 lines
18 KiB
Swift

//
// SettingsViewController.swift
// NetNewsWire-iOS
//
// Created by Maurice Parker on 4/24/19.
// Copyright © 2019 Ranchero Software. All rights reserved.
//
import UIKit
import CoreServices
import SafariServices
import SwiftUI
import UniformTypeIdentifiers
import RSCore
import Account
import ActivityLog
final class SettingsViewController: UITableViewController {
private enum Section: Int {
case notifications = 0
case accounts = 1
case feeds = 2
case moreScreens = 3
case troubleshooting = 4
case help = 5
}
private enum NotificationsRow: Int {
case systemSettings = 0
}
private enum MoreScreensRow: Int {
case appearance = 0
case sidebar = 1
case timeline = 2
case articles = 3
}
private enum TroubleshootingRow: Int {
case errorLog = 0
case activityLog = 1
case accountStats = 2
case dinosaurs = 3
case cloudKitZoneStats = 4
}
private enum HelpRow: Int {
case help = 0
case forum = 1
case releaseNotes = 2
case bugTracker = 3
case about = 4
}
private enum FeedsRow: Int {
case importSubscriptions = 0
case exportSubscriptions = 1
case addNetNewsWireNewsFeed = 2
}
private weak var opmlAccount: Account?
/// Pushes the Timeline screen on appearing, so a user can turn off the Mark All as Read confirmation.
var showTimelineSettings = false
weak var presentingParentController: UIViewController?
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)
NotificationCenter.default.addObserver(self, selector: #selector(accountsDidChange), name: .UserDidAddAccount, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(accountsDidChange), name: .UserDidDeleteAccount, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(displayNameDidChange), name: .DisplayNameDidChange, object: nil)
tableView.register(UINib(nibName: "SettingsTableViewCell", bundle: nil), forCellReuseIdentifier: "SettingsTableViewCell")
tableView.rowHeight = UITableView.automaticDimension
tableView.estimatedRowHeight = 44
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let buildLabel = CopyableLabel(frame: CGRect(x: 32.0, y: 0.0, width: 0.0, height: 0.0))
buildLabel.font = UIFont.systemFont(ofSize: 11.0)
buildLabel.textColor = UIColor.gray
buildLabel.text = "\(Bundle.main.appName) \(Bundle.main.versionNumber) (build \(Bundle.main.buildNumber))"
buildLabel.sizeToFit()
let wrapperView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.bounds.width, height: buildLabel.frame.height + 10.0))
wrapperView.addSubview(buildLabel)
tableView.tableFooterView = wrapperView
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.tableView.selectRow(at: nil, animated: true, scrollPosition: .none)
if showTimelineSettings {
showTimelineSettings = false
navigationController?.pushViewController(TimelineSettingsViewController(), animated: true)
}
}
// MARK: UITableView
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch Section(rawValue: section) {
case .accounts:
return AccountManager.shared.accounts.count + 1
case .feeds:
let defaultNumberOfRows = super.tableView(tableView, numberOfRowsInSection: section)
if AccountManager.shared.activeAccounts.isEmpty || AccountManager.shared.anyAccountHasNetNewsWireNewsSubscription() {
return defaultNumberOfRows - 1
}
return defaultNumberOfRows
case .troubleshooting:
let defaultNumberOfRows = super.tableView(tableView, numberOfRowsInSection: section)
if !AccountManager.shared.hasiCloudAccount {
return defaultNumberOfRows - 1
}
return defaultNumberOfRows
default:
return super.tableView(tableView, numberOfRowsInSection: section)
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: UITableViewCell
switch Section(rawValue: indexPath.section) {
case .accounts:
cell = tableView.dequeueReusableCell(withIdentifier: "SettingsTableViewCell", for: indexPath)
let sortedAccounts = AccountManager.shared.sortedAccounts
if indexPath.row == sortedAccounts.count {
cell.textLabel?.text = NSLocalizedString("Add Account", comment: "Add Account")
} else {
cell.textLabel?.text = sortedAccounts[indexPath.row].nameForDisplay
}
cell.imageView?.image = iconImage(for: indexPath)
default:
cell = super.tableView(tableView, cellForRowAt: indexPath)
cell.imageView?.image = iconImage(for: indexPath)
}
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
switch Section(rawValue: indexPath.section) {
case .notifications:
UIApplication.shared.open(URL(string: "\(UIApplication.openSettingsURLString)")!)
tableView.selectRow(at: nil, animated: true, scrollPosition: .none)
case .accounts:
let sortedAccounts = AccountManager.shared.sortedAccounts
if indexPath.row == sortedAccounts.count {
let addAccountView = AddAccountView(presentationAnchor: view.window) { [weak self] in
self?.navigationController?.popViewController(animated: false)
}
self.navigationController?.pushViewController(UIHostingController(rootView: addAccountView), animated: true)
} else {
let controller = UIStoryboard.inspector.instantiateController(ofType: AccountInspectorViewController.self)
controller.account = sortedAccounts[indexPath.row]
self.navigationController?.pushViewController(controller, animated: true)
}
case .feeds:
switch FeedsRow(rawValue: indexPath.row) {
case .importSubscriptions:
tableView.selectRow(at: nil, animated: true, scrollPosition: .none)
if let sourceView = tableView.cellForRow(at: indexPath) {
let sourceRect = tableView.rectForRow(at: indexPath)
importOPML(sourceView: sourceView, sourceRect: sourceRect)
}
case .exportSubscriptions:
tableView.selectRow(at: nil, animated: true, scrollPosition: .none)
if let sourceView = tableView.cellForRow(at: indexPath) {
let sourceRect = tableView.rectForRow(at: indexPath)
exportOPML(sourceView: sourceView, sourceRect: sourceRect)
}
case .addNetNewsWireNewsFeed:
addFeed()
tableView.selectRow(at: nil, animated: true, scrollPosition: .none)
default:
break
}
case .moreScreens:
switch MoreScreensRow(rawValue: indexPath.row) {
case .sidebar:
self.navigationController?.pushViewController(SidebarSettingsViewController(), animated: true)
case .timeline:
self.navigationController?.pushViewController(TimelineSettingsViewController(), animated: true)
case .articles:
self.navigationController?.pushViewController(ArticlesSettingsViewController(), animated: true)
case .appearance:
self.navigationController?.pushViewController(GeneralSettingsViewController(), animated: true)
default:
break
}
case .troubleshooting:
let viewController: UIViewController? = {
switch TroubleshootingRow(rawValue: indexPath.row) {
case .errorLog:
return UIHostingController(rootView: ErrorLogView())
case .accountStats:
return UIHostingController(rootView: AccountStatsView())
case .cloudKitZoneStats:
return UIHostingController(rootView: CloudKitStatsView())
case .activityLog:
return UIHostingController(rootView: ActivityLogView())
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 {
rootSplit.coordinator.discloseFeed(dinosaur.feed, animations: [.scroll, .navigation])
}
}
}))
default:
return nil
}
}()
if let viewController {
self.navigationController?.pushViewController(viewController, animated: true)
}
case .help:
switch HelpRow(rawValue: indexPath.row) {
case .help:
openURL(HelpURL.helpHome.rawValue)
tableView.selectRow(at: nil, animated: true, scrollPosition: .none)
case .forum:
openURL(HelpURL.discourse.rawValue)
tableView.selectRow(at: nil, animated: true, scrollPosition: .none)
case .releaseNotes:
openURL(HelpURL.releaseNotes.rawValue)
tableView.selectRow(at: nil, animated: true, scrollPosition: .none)
case .bugTracker:
openURL(HelpURL.bugTracker.rawValue)
tableView.selectRow(at: nil, animated: true, scrollPosition: .none)
case .about:
let hosting = UIHostingController(rootView: AboutView())
self.navigationController?.pushViewController(hosting, animated: true)
default:
break
}
default:
tableView.selectRow(at: nil, animated: true, scrollPosition: .none)
}
}
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return false
}
override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
return false
}
override func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
return .none
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
override func tableView(_ tableView: UITableView, indentationLevelForRowAt indexPath: IndexPath) -> Int {
return super.tableView(tableView, indentationLevelForRowAt: IndexPath(row: 0, section: Section.accounts.rawValue))
}
// MARK: Actions
@IBAction func done(_ sender: Any) {
dismiss(animated: true)
}
// MARK: - Notifications
@objc func contentSizeCategoryDidChange() {
tableView.reloadData()
}
@objc func accountsDidChange() {
tableView.reloadData()
}
@objc func displayNameDidChange() {
tableView.reloadData()
}
}
// MARK: - OPML Document Picker
extension SettingsViewController: UIDocumentPickerDelegate {
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
for url in urls {
opmlAccount?.importOPML(url) { result in
switch result {
case .success:
break
case .failure:
let title = NSLocalizedString("Import Failed", comment: "Import Failed")
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)
}
}
}
}
}
// MARK: - Private
private extension SettingsViewController {
func iconImage(for indexPath: IndexPath) -> UIImage? {
switch Section(rawValue: indexPath.section) {
case .notifications:
switch NotificationsRow(rawValue: indexPath.row) {
case .systemSettings:
return Assets.Images.settingsSystemSettings
case nil:
return nil
}
case .accounts:
let sortedAccounts = AccountManager.shared.sortedAccounts
if indexPath.row == sortedAccounts.count {
return Assets.Images.plus
}
return Assets.Images.settingsAccountImage(sortedAccounts[indexPath.row].type)
case .feeds:
switch FeedsRow(rawValue: indexPath.row) {
case .importSubscriptions:
return Assets.Images.settingsImportSubscriptions
case .exportSubscriptions:
return Assets.Images.settingsExportSubscriptions
case .addNetNewsWireNewsFeed:
return Assets.Images.settingsNetNewsWireNewsFeed
case nil:
return nil
}
case .moreScreens:
switch MoreScreensRow(rawValue: indexPath.row) {
case .appearance:
return Assets.Images.preferencesToolbarAppearance
case .sidebar:
return Assets.Images.settingsSidebar
case .timeline:
return Assets.Images.settingsTimeline
case .articles:
return Assets.Images.settingsArticles
case nil:
return nil
}
case .troubleshooting:
switch TroubleshootingRow(rawValue: indexPath.row) {
case .errorLog:
return Assets.Images.settingsErrorLog
case .activityLog:
return Assets.Images.settingsActivityLog
case .accountStats:
return Assets.Images.settingsAccountStats
case .dinosaurs:
return Assets.Images.settingsDinosaurs
case .cloudKitZoneStats:
return Assets.Images.settingsCloudKitStats
case nil:
return nil
}
case .help:
switch HelpRow(rawValue: indexPath.row) {
case .help:
return Assets.Images.settingsHelp
case .forum:
return Assets.Images.settingsForum
case .releaseNotes:
return Assets.Images.settingsReleaseNotes
case .bugTracker:
return Assets.Images.settingsBugTracker
case .about:
return Assets.Images.info
case nil:
return nil
}
case nil:
return nil
}
}
func addFeed() {
self.dismiss(animated: true)
let addFeedView = AddFeedView(initialFeed: AccountManager.netNewsWireNewsURL, initialFeedName: NSLocalizedString("NetNewsWire News", comment: "NetNewsWire News"))
let hostingController = UIHostingController(rootView: addFeedView)
hostingController.modalPresentationStyle = .formSheet
hostingController.preferredContentSize = UIViewController.formSheetSize
presentingParentController?.present(hostingController, animated: true)
}
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)
}
}
func importOPMLAccountPicker(sourceView: UIView, sourceRect: CGRect) {
let title = NSLocalizedString("Choose an account to receive the imported feeds and folders", comment: "Import Account")
let alert = UIAlertController(title: title, message: nil, preferredStyle: .actionSheet)
if let popoverController = alert.popoverPresentationController {
popoverController.sourceView = view
popoverController.sourceRect = sourceRect
}
for account in AccountManager.shared.sortedActiveAccounts {
let action = UIAlertAction(title: account.nameForDisplay, style: .default) { [weak self] _ in
self?.opmlAccount = account
self?.importOPMLDocumentPicker()
}
alert.addAction(action)
}
let cancelTitle = NSLocalizedString("Cancel", comment: "Cancel button")
alert.addAction(UIAlertAction(title: cancelTitle, style: .cancel))
self.present(alert, animated: true)
}
func importOPMLDocumentPicker() {
var contentTypes: [UTType] = []
// 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)
}
func exportOPML(sourceView: UIView, sourceRect: CGRect) {
if AccountManager.shared.accounts.count == 1 {
opmlAccount = AccountManager.shared.accounts.first!
exportOPMLDocumentPicker()
} else {
exportOPMLAccountPicker(sourceView: sourceView, sourceRect: sourceRect)
}
}
func exportOPMLAccountPicker(sourceView: UIView, sourceRect: CGRect) {
let title = NSLocalizedString("Choose an account with the subscriptions to export", comment: "Export Account")
let alert = UIAlertController(title: title, message: nil, preferredStyle: .actionSheet)
if let popoverController = alert.popoverPresentationController {
popoverController.sourceView = view
popoverController.sourceRect = sourceRect
}
for account in AccountManager.shared.sortedAccounts {
let action = UIAlertAction(title: account.nameForDisplay, style: .default) { [weak self] _ in
self?.opmlAccount = account
self?.exportOPMLDocumentPicker()
}
alert.addAction(action)
}
let cancelTitle = NSLocalizedString("Cancel", comment: "Cancel button")
alert.addAction(UIAlertAction(title: cancelTitle, style: .cancel))
self.present(alert, animated: true)
}
func exportOPMLDocumentPicker() {
guard let account = opmlAccount else { return }
let accountName = account.nameForDisplay.replacingOccurrences(of: " ", with: "").trimmingCharacters(in: .whitespaces)
let filename = "Subscriptions-\(accountName).opml"
let tempFile = FileManager.default.temporaryDirectory.appendingPathComponent(filename)
do {
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)
}
} catch {
self.presentError(title: "OPML Export Error", message: error.localizedDescription)
}
let docPicker = UIDocumentPickerViewController(forExporting: [tempFile])
docPicker.modalPresentationStyle = .formSheet
self.present(docPicker, animated: true)
}
func openURL(_ urlString: String) {
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)
vc.modalPresentationStyle = .pageSheet
present(vc, animated: true)
}
}