NetNewsWire/Mac/MainWindow/OPML/ExportOPMLController.swift
2019-10-22 12:14:38 -05:00

71 lines
2.6 KiB
Swift

//
// ExportOPMLController.swift
// NetNewsWire
//
// Created by Maurice Parker on 5/1/19.
// Copyright © 2019 Ranchero Software. All rights reserved.
//
import AppKit
import Account
class ExportOPMLController {
weak var savePanel: NSSavePanel?
func runSheetOnWindow(_ hostWindow: NSWindow) {
let accessoryViewController = ExportOPMLAccessoryViewController()
let panel = NSSavePanel()
panel.allowedFileTypes = ["opml"]
panel.allowsOtherFileTypes = false
panel.prompt = NSLocalizedString("Export OPML", comment: "Export OPML")
panel.title = NSLocalizedString("Export OPML", comment: "Export OPML")
panel.nameFieldLabel = NSLocalizedString("Export to:", comment: "Export OPML")
panel.message = NSLocalizedString("Choose a location for the exported OPML file.", comment: "Export OPML")
panel.isExtensionHidden = false
panel.accessoryView = accessoryViewController.view
NotificationCenter.default.addObserver(self, selector: #selector(selectedAccountDidChange(_:)), name: .ExportOPMLSelectedAccountDidChange, object: accessoryViewController)
updateNameFieldStringValueIfAppropriate(savePanel: panel, from: accessoryViewController, force: true)
savePanel = panel
panel.beginSheetModal(for: hostWindow) { result in
if result == NSApplication.ModalResponse.OK, let url = panel.url {
DispatchQueue.main.async {
guard let account = accessoryViewController.selectedAccount else { return }
let filename = url.lastPathComponent
let opmlString = OPMLExporter.OPMLString(with: account, title: filename)
do {
try opmlString.write(to: url, atomically: true, encoding: .utf8)
}
catch let error as NSError {
NSApplication.shared.presentError(error)
}
}
}
NotificationCenter.default.removeObserver(self)
}
}
private func updateNameFieldStringValueIfAppropriate(savePanel panel: NSSavePanel, from accessoryViewController: ExportOPMLAccessoryViewController, force: Bool = false) {
if !force && !panel.nameFieldStringValue.hasPrefix("Subscriptions-") { return }
guard let account = accessoryViewController.selectedAccount else { return }
let accountName = account.nameForDisplay.replacingOccurrences(of: " ", with: "").trimmingCharacters(in: .whitespaces)
panel.nameFieldStringValue = "Subscriptions-\(accountName).opml"
}
@objc private func selectedAccountDidChange(_ notification: Notification) {
if let savePanel = savePanel, let accessoryViewController = notification.object as? ExportOPMLAccessoryViewController {
self.updateNameFieldStringValueIfAppropriate(savePanel: savePanel, from: accessoryViewController)
}
}
}