2019-04-16 20:38:07 +02:00
|
|
|
//
|
|
|
|
// AddFeedViewController.swift
|
|
|
|
// NetNewsWire
|
|
|
|
//
|
|
|
|
// Created by Maurice Parker on 4/16/19.
|
|
|
|
// Copyright © 2019 Ranchero Software, LLC. All rights reserved.
|
|
|
|
//
|
2019-04-15 22:03:05 +02:00
|
|
|
|
|
|
|
import UIKit
|
|
|
|
import Account
|
|
|
|
import RSCore
|
|
|
|
import RSTree
|
|
|
|
import RSParser
|
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
class AddWebFeedViewController: UITableViewController, AddContainerViewControllerChild {
|
2019-04-15 22:03:05 +02:00
|
|
|
|
2019-05-20 00:48:03 +02:00
|
|
|
@IBOutlet private weak var urlTextField: UITextField!
|
2020-05-03 00:21:01 +02:00
|
|
|
@IBOutlet weak var urlTextFieldToSuperViewConstraint: NSLayoutConstraint!
|
|
|
|
@IBOutlet weak var urlBuilderButton: UIButton!
|
2019-05-20 00:48:03 +02:00
|
|
|
@IBOutlet private weak var nameTextField: UITextField!
|
2019-04-15 22:03:05 +02:00
|
|
|
|
2019-12-08 18:39:13 +01:00
|
|
|
private var folderLabel = ""
|
2019-04-15 22:03:05 +02:00
|
|
|
private var userCancelled = false
|
2019-04-16 23:03:30 +02:00
|
|
|
|
2019-04-26 01:06:53 +02:00
|
|
|
weak var delegate: AddContainerViewControllerChildDelegate?
|
|
|
|
var initialFeed: String?
|
|
|
|
var initialFeedName: String?
|
|
|
|
|
2019-11-16 19:02:58 +01:00
|
|
|
var container: Container?
|
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
override func viewDidLoad() {
|
|
|
|
|
|
|
|
super.viewDidLoad()
|
|
|
|
|
2020-01-17 03:09:18 +01:00
|
|
|
if initialFeed == nil, let urlString = UIPasteboard.general.string {
|
|
|
|
if urlString.mayBeURL {
|
|
|
|
initialFeed = urlString.normalizedURL
|
2019-09-02 22:54:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
urlTextField.autocorrectionType = .no
|
|
|
|
urlTextField.autocapitalizationType = .none
|
2019-04-26 01:06:53 +02:00
|
|
|
urlTextField.text = initialFeed
|
2019-05-19 23:17:10 +02:00
|
|
|
urlTextField.delegate = self
|
2019-04-26 01:06:53 +02:00
|
|
|
|
2020-05-03 00:21:01 +02:00
|
|
|
if ExtensionPointManager.shared.isTwitterEnabled {
|
|
|
|
urlTextFieldToSuperViewConstraint.isActive = false
|
|
|
|
urlTextField.trailingAnchor.constraint(equalTo: urlBuilderButton.leadingAnchor, constant: -8).isActive = true
|
|
|
|
} else {
|
|
|
|
urlBuilderButton.isHidden = true
|
|
|
|
}
|
|
|
|
|
2019-04-26 01:06:53 +02:00
|
|
|
if initialFeed != nil {
|
|
|
|
delegate?.readyToAdd(state: true)
|
|
|
|
}
|
|
|
|
|
|
|
|
nameTextField.text = initialFeedName
|
2019-05-19 23:17:10 +02:00
|
|
|
nameTextField.delegate = self
|
2019-05-20 00:48:03 +02:00
|
|
|
|
2019-11-16 20:25:55 +01:00
|
|
|
if let defaultContainer = AddWebFeedDefaultContainer.defaultContainer {
|
|
|
|
container = defaultContainer
|
2019-05-20 00:48:03 +02:00
|
|
|
} else {
|
2019-11-16 19:02:58 +01:00
|
|
|
delegate?.readyToAdd(state: false)
|
2019-05-20 00:48:03 +02:00
|
|
|
}
|
2019-04-15 22:03:05 +02:00
|
|
|
|
2019-11-16 19:02:58 +01:00
|
|
|
updateFolderLabel()
|
|
|
|
|
2019-04-16 20:38:07 +02:00
|
|
|
// I couldn't figure out the gap at the top of the UITableView, so I took a hammer to it.
|
|
|
|
tableView.contentInset = UIEdgeInsets(top: -28, left: 0, bottom: 0, right: 0)
|
2019-04-15 22:03:05 +02:00
|
|
|
|
2019-12-08 18:39:13 +01:00
|
|
|
tableView.register(UINib(nibName: "AddWebFeedSelectFolderTableViewCell", bundle: nil), forCellReuseIdentifier: "AddWebFeedSelectFolderTableViewCell")
|
|
|
|
|
2019-04-16 23:03:30 +02:00
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(textDidChange(_:)), name: UITextField.textDidChangeNotification, object: urlTextField)
|
|
|
|
|
|
|
|
}
|
2019-04-15 22:03:05 +02:00
|
|
|
|
2020-04-23 11:44:26 +02:00
|
|
|
@IBAction func showURLBuilder(_ sender: Any) {
|
|
|
|
let navController = UIStoryboard.add.instantiateViewController(withIdentifier: "SelectURLBuilderNavViewController") as! UINavigationController
|
|
|
|
navController.modalPresentationStyle = .currentContext
|
2020-04-23 16:08:33 +02:00
|
|
|
let selectURLBuilder = navController.topViewController as! SelectURLBuilderTableViewController
|
|
|
|
selectURLBuilder.delegate = self
|
2020-04-23 11:44:26 +02:00
|
|
|
present(navController, animated: true)
|
|
|
|
}
|
|
|
|
|
2019-04-16 23:03:30 +02:00
|
|
|
func cancel() {
|
2019-04-15 22:03:05 +02:00
|
|
|
userCancelled = true
|
2019-04-17 15:54:39 +02:00
|
|
|
delegate?.processingDidCancel()
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
|
|
|
|
2019-04-16 23:03:30 +02:00
|
|
|
func add() {
|
2019-04-15 22:03:05 +02:00
|
|
|
|
|
|
|
let urlString = urlTextField.text ?? ""
|
2020-01-17 03:09:18 +01:00
|
|
|
let normalizedURLString = urlString.normalizedURL
|
2019-04-15 22:03:05 +02:00
|
|
|
|
|
|
|
guard !normalizedURLString.isEmpty, let url = URL(string: normalizedURLString) else {
|
2019-04-17 15:54:39 +02:00
|
|
|
delegate?.processingDidCancel()
|
2019-04-15 22:03:05 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-16 19:02:58 +01:00
|
|
|
guard let container = container else { return }
|
2019-04-15 22:03:05 +02:00
|
|
|
|
2019-05-09 00:41:19 +02:00
|
|
|
var account: Account?
|
|
|
|
if let containerAccount = container as? Account {
|
|
|
|
account = containerAccount
|
2019-05-28 16:45:02 +02:00
|
|
|
} else if let containerFolder = container as? Folder, let containerAccount = containerFolder.account {
|
2019-05-09 00:41:19 +02:00
|
|
|
account = containerAccount
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
if account!.hasWebFeed(withURL: url.absoluteString) {
|
2019-09-12 18:36:28 +02:00
|
|
|
presentError(AccountError.createErrorAlreadySubscribed)
|
2019-04-15 22:03:05 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-16 23:03:30 +02:00
|
|
|
delegate?.processingDidBegin()
|
2019-08-04 20:20:37 +02:00
|
|
|
|
|
|
|
let feedName = (nameTextField.text?.isEmpty ?? true) ? nil : nameTextField.text
|
|
|
|
|
2019-05-28 16:45:02 +02:00
|
|
|
BatchUpdate.shared.start()
|
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
account!.createWebFeed(url: url.absoluteString, name: feedName, container: container) { result in
|
2019-04-16 23:03:30 +02:00
|
|
|
|
2019-05-28 16:45:02 +02:00
|
|
|
BatchUpdate.shared.end()
|
2019-05-09 00:41:19 +02:00
|
|
|
|
|
|
|
switch result {
|
2019-05-10 17:22:28 +02:00
|
|
|
case .success(let feed):
|
2019-05-28 16:45:02 +02:00
|
|
|
self.delegate?.processingDidEnd()
|
2019-11-15 03:11:41 +01:00
|
|
|
NotificationCenter.default.post(name: .UserDidAddFeed, object: self, userInfo: [UserInfoKey.webFeed: feed])
|
2019-05-10 17:22:28 +02:00
|
|
|
case .failure(let error):
|
2019-09-12 18:36:28 +02:00
|
|
|
self.presentError(error)
|
|
|
|
self.delegate?.processingDidCancel()
|
2019-05-09 00:41:19 +02:00
|
|
|
}
|
2019-05-10 17:22:28 +02:00
|
|
|
|
2019-05-09 00:41:19 +02:00
|
|
|
}
|
2019-04-15 22:03:05 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-04-16 23:03:30 +02:00
|
|
|
@objc func textDidChange(_ note: Notification) {
|
2020-04-23 16:08:33 +02:00
|
|
|
updateUI()
|
2019-04-16 23:03:30 +02:00
|
|
|
}
|
|
|
|
|
2019-12-08 18:39:13 +01:00
|
|
|
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
|
|
|
if indexPath.row == 2 {
|
|
|
|
let cell = tableView.dequeueReusableCell(withIdentifier: "AddWebFeedSelectFolderTableViewCell", for: indexPath) as? AddWebFeedSelectFolderTableViewCell
|
|
|
|
cell!.detailLabel.text = folderLabel
|
|
|
|
return cell!
|
|
|
|
} else {
|
|
|
|
return super.tableView(tableView, cellForRowAt: indexPath)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-16 19:02:58 +01:00
|
|
|
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
|
|
|
if indexPath.row == 2 {
|
2019-11-16 21:19:51 +01:00
|
|
|
let navController = UIStoryboard.add.instantiateViewController(withIdentifier: "AddWebFeedFolderNavViewController") as! UINavigationController
|
|
|
|
navController.modalPresentationStyle = .currentContext
|
|
|
|
let folderViewController = navController.topViewController as! AddWebFeedFolderViewController
|
2019-11-16 19:02:58 +01:00
|
|
|
folderViewController.delegate = self
|
|
|
|
folderViewController.initialContainer = container
|
2019-11-16 21:19:51 +01:00
|
|
|
present(navController, animated: true)
|
2019-05-20 00:48:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
|
|
|
|
2019-11-16 19:02:58 +01:00
|
|
|
// MARK: AddWebFeedFolderViewControllerDelegate
|
|
|
|
|
|
|
|
extension AddWebFeedViewController: AddWebFeedFolderViewControllerDelegate {
|
|
|
|
func didSelect(container: Container) {
|
|
|
|
self.container = container
|
|
|
|
updateFolderLabel()
|
2019-11-17 02:44:01 +01:00
|
|
|
AddWebFeedDefaultContainer.saveDefaultContainer(container)
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-23 16:08:33 +02:00
|
|
|
// MARK: AddWebFeedFolderViewControllerDelegate
|
|
|
|
|
|
|
|
extension AddWebFeedViewController: SelectURLBuilderDelegate {
|
|
|
|
|
|
|
|
func selectURLBuilderDidBuildURL(_ url: URL) {
|
|
|
|
urlTextField.text = url.absoluteString
|
|
|
|
updateUI()
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-11-16 19:02:58 +01:00
|
|
|
// MARK: UITextFieldDelegate
|
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
extension AddWebFeedViewController: UITextFieldDelegate {
|
2019-05-19 23:17:10 +02:00
|
|
|
|
|
|
|
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
|
|
|
|
textField.resignFirstResponder()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-11-16 19:02:58 +01:00
|
|
|
|
|
|
|
// MARK: Private
|
|
|
|
|
|
|
|
private extension AddWebFeedViewController {
|
2020-04-23 16:08:33 +02:00
|
|
|
|
|
|
|
func updateUI() {
|
|
|
|
delegate?.readyToAdd(state: urlTextField.text?.mayBeURL ?? false)
|
|
|
|
}
|
|
|
|
|
2019-11-16 19:02:58 +01:00
|
|
|
func updateFolderLabel() {
|
|
|
|
if let containerName = (container as? DisplayNameProvider)?.nameForDisplay {
|
|
|
|
if container is Folder {
|
2019-12-08 18:39:13 +01:00
|
|
|
folderLabel = "\(container?.account?.nameForDisplay ?? "") / \(containerName)"
|
2019-11-16 19:02:58 +01:00
|
|
|
} else {
|
2019-12-08 18:39:13 +01:00
|
|
|
folderLabel = containerName
|
2019-11-16 19:02:58 +01:00
|
|
|
}
|
2019-12-08 18:39:13 +01:00
|
|
|
tableView.reloadData()
|
2019-11-16 19:02:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|