NetNewsWire/Mac/MainWindow/AddFeed/AddWebFeedWindowController....

137 lines
3.8 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
class AddWebFeedWindowController : NSWindowController, AddFeedWindowController {
2017-05-27 19:43:27 +02:00
@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?) {
self.init(windowNibName: NSNib.Name("AddWebFeedSheet"))
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;
}
2020-07-06 17:06:12 +02:00
guard let url = URL(unicodeString: normalizedURLString) else {
2017-10-22 00:56:01 +02:00
cancelSheet()
return
}
guard let container = selectedContainer() else { return }
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 AddWebFeedWindowController {
2017-05-27 19:43:27 +02:00
private func updateUI() {
addButton.isEnabled = urlTextField.stringValue.mayBeURL && selectedContainer() != nil
}
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? {
guard folderPopupButton.selectedItem?.isEnabled ?? false else { return nil }
2017-10-22 00:56:01 +02:00
return folderPopupButton.selectedItem?.representedObject as? Container
2017-05-27 19:43:27 +02:00
}
}