NetNewsWire/Mac/MainWindow/AddFeed/AddFeedWindowController.swift

144 lines
4.0 KiB
Swift
Raw Normal View History

2017-05-27 19:43:27 +02:00
//
// AddFeedWindowController.swift
2018-08-29 07:18:24 +02:00
// NetNewsWire
2017-05-27 19:43:27 +02:00
//
// Created by Brent Simmons on 8/1/15.
// Copyright © 2015 Ranchero Software, LLC. All rights reserved.
//
import AppKit
2017-05-27 19:43:27 +02:00
import RSCore
import RSTree
import Articles
2017-09-17 21:34:10 +02:00
import Account
2017-05-27 19:43:27 +02:00
protocol AddFeedWindowControllerDelegate: class {
// userEnteredURL will have already been validated and normalized.
func addFeedWindowController(_: AddFeedWindowController, userEnteredURL: URL, userEnteredTitle: String?, container: Container)
2017-05-27 19:43:27 +02:00
func addFeedWindowControllerUserDidCancel(_: AddFeedWindowController)
}
class AddFeedWindowController : NSWindowController {
@IBOutlet var urlTextField: NSTextField!
@IBOutlet var nameTextField: NSTextField!
@IBOutlet var addButton: NSButton!
@IBOutlet var folderPopupButton: NSPopUpButton!
private var urlString: String?
private var initialName: String?
private weak var initialAccount: Account?
private var initialFolder: Folder?
private weak var delegate: AddFeedWindowControllerDelegate?
private var folderTreeController: TreeController!
2017-05-27 19:43:27 +02:00
private var userEnteredTitle: String? {
var s = nameTextField.stringValue
s = s.collapsingWhitespace
if s.isEmpty {
return nil
2017-05-27 19:43:27 +02:00
}
return s
2017-05-27 19:43:27 +02:00
}
var hostWindow: NSWindow!
convenience init(urlString: String?, name: String?, account: Account?, folder: Folder?, folderTreeController: TreeController, delegate: AddFeedWindowControllerDelegate?) {
2018-12-09 21:32:33 +01:00
self.init(windowNibName: NSNib.Name("AddFeedSheet"))
2017-05-27 19:43:27 +02:00
self.urlString = urlString
self.initialName = name
self.initialAccount = account
self.initialFolder = folder
2017-05-27 19:43:27 +02:00
self.delegate = delegate
self.folderTreeController = folderTreeController
}
func runSheetOnWindow(_ hostWindow: NSWindow) {
hostWindow.beginSheet(window!) { (returnCode: NSApplication.ModalResponse) -> Void in
2017-05-27 19:43:27 +02:00
}
}
override func windowDidLoad() {
if let urlString = urlString {
urlTextField.stringValue = urlString
}
if let initialName = initialName, !initialName.isEmpty {
nameTextField.stringValue = initialName
}
folderPopupButton.menu = FolderTreeMenu.createFolderPopupMenu(with: folderTreeController.rootNode)
if let account = initialAccount {
FolderTreeMenu.select(account: account, folder: initialFolder, in: folderPopupButton)
} else if let container = AddWebFeedDefaultContainer.defaultContainer {
if let folder = container as? Folder, let account = folder.account {
FolderTreeMenu.select(account: account, folder: folder, in: folderPopupButton)
} else {
if let account = container as? Account {
FolderTreeMenu.select(account: account, folder: nil, in: folderPopupButton)
}
}
}
2017-05-27 19:43:27 +02:00
updateUI()
}
// MARK: Actions
@IBAction func cancel(_ sender: Any?) {
2017-05-27 19:43:27 +02:00
cancelSheet()
}
@IBAction func addFeed(_ sender: Any?) {
2017-10-22 00:56:01 +02:00
let urlString = urlTextField.stringValue
let normalizedURLString = urlString.normalizedURL
2017-10-22 00:56:01 +02:00
if normalizedURLString.isEmpty {
cancelSheet()
return;
}
guard let url = URL(string: normalizedURLString) else {
cancelSheet()
return
}
let container = selectedContainer()!
AddWebFeedDefaultContainer.saveDefaultContainer(container)
2017-10-22 00:56:01 +02:00
delegate?.addFeedWindowController(self, userEnteredURL: url, userEnteredTitle: userEnteredTitle, container: container)
2017-05-27 19:43:27 +02:00
}
@IBAction func localShowFeedList(_ sender: Any?) {
2017-09-17 21:34:10 +02:00
NSApplication.shared.sendAction(NSSelectorFromString("showFeedList:"), to: nil, from: sender)
2017-09-17 21:54:08 +02:00
hostWindow.endSheet(window!, returnCode: NSApplication.ModalResponse.continue)
2017-05-27 19:43:27 +02:00
}
// MARK: NSTextFieldDelegate
@objc func controlTextDidEndEditing(_ obj: Notification) {
2017-05-27 19:43:27 +02:00
updateUI()
}
@objc func controlTextDidChange(_ obj: Notification) {
2017-05-27 19:43:27 +02:00
updateUI()
}
}
private extension AddFeedWindowController {
private func updateUI() {
addButton.isEnabled = urlTextField.stringValue.mayBeURL
}
2017-05-27 19:43:27 +02:00
func cancelSheet() {
2017-05-27 19:43:27 +02:00
delegate?.addFeedWindowControllerUserDidCancel(self)
}
2017-10-22 00:56:01 +02:00
func selectedContainer() -> Container? {
return folderPopupButton.selectedItem?.representedObject as? Container
2017-05-27 19:43:27 +02:00
}
}