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
|
2024-03-22 01:21:50 +01:00
|
|
|
import Tree
|
2024-04-03 06:43:06 +02:00
|
|
|
import Parser
|
2024-03-21 04:54:21 +01:00
|
|
|
import Core
|
2024-04-07 07:24:15 +02:00
|
|
|
import CommonErrors
|
2019-04-15 22:03:05 +02:00
|
|
|
|
2024-05-04 08:10:57 +02:00
|
|
|
final class AddFeedViewController: UITableViewController {
|
2019-04-15 22:03:05 +02:00
|
|
|
|
2020-08-11 22:00:31 +02:00
|
|
|
@IBOutlet weak var activityIndicator: UIActivityIndicatorView!
|
|
|
|
@IBOutlet weak var addButton: UIBarButtonItem!
|
|
|
|
@IBOutlet weak var urlTextField: UITextField!
|
2020-05-03 00:21:01 +02:00
|
|
|
@IBOutlet weak var urlTextFieldToSuperViewConstraint: NSLayoutConstraint!
|
2020-08-11 22:00:31 +02:00
|
|
|
@IBOutlet weak var nameTextField: UITextField!
|
|
|
|
|
|
|
|
static let preferredContentSizeForFormSheetDisplay = CGSize(width: 460.0, height: 400.0)
|
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
|
|
|
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-08-11 22:00:31 +02:00
|
|
|
activityIndicator.isHidden = true
|
|
|
|
activityIndicator.color = .label
|
|
|
|
|
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
|
|
|
|
|
|
|
if initialFeed != nil {
|
2020-08-11 22:00:31 +02:00
|
|
|
addButton.isEnabled = true
|
2019-04-26 01:06:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
nameTextField.text = initialFeedName
|
2019-05-19 23:17:10 +02:00
|
|
|
nameTextField.delegate = self
|
2019-05-20 00:48:03 +02:00
|
|
|
|
2024-02-26 08:12:21 +01:00
|
|
|
if let defaultContainer = AddFeedDefaultContainer.defaultContainer {
|
2019-11-16 20:25:55 +01:00
|
|
|
container = defaultContainer
|
2019-05-20 00:48:03 +02:00
|
|
|
} else {
|
2020-08-11 22:00:31 +02:00
|
|
|
addButton.isEnabled = 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()
|
|
|
|
|
2020-08-12 00:04:11 +02:00
|
|
|
tableView.register(UINib(nibName: "AddFeedSelectFolderTableViewCell", bundle: nil), forCellReuseIdentifier: "AddFeedSelectFolderTableViewCell")
|
2019-12-08 18:39:13 +01:00
|
|
|
|
2019-04-16 23:03:30 +02:00
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(textDidChange(_:)), name: UITextField.textDidChangeNotification, object: urlTextField)
|
2020-08-11 22:00:31 +02:00
|
|
|
|
|
|
|
if initialFeed == nil {
|
|
|
|
urlTextField.becomeFirstResponder()
|
|
|
|
}
|
2020-04-23 11:44:26 +02:00
|
|
|
}
|
|
|
|
|
2020-08-11 22:00:31 +02:00
|
|
|
@IBAction func cancel(_ sender: Any) {
|
2019-04-15 22:03:05 +02:00
|
|
|
userCancelled = true
|
2020-08-11 22:00:31 +02:00
|
|
|
dismiss(animated: true)
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
|
|
|
|
2020-08-11 22:00:31 +02:00
|
|
|
@IBAction func add(_ sender: Any) {
|
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
|
|
|
|
2024-06-09 19:58:07 +02:00
|
|
|
guard !normalizedURLString.isEmpty, let url = URL(string: normalizedURLString) else {
|
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
|
|
|
}
|
|
|
|
|
2024-02-26 08:12:21 +01:00
|
|
|
if account!.hasFeed(withURL: url.absoluteString) {
|
2024-04-07 23:57:05 +02:00
|
|
|
presentError(AccountError.createErrorAlreadySubscribed)
|
2019-04-15 22:03:05 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-08-11 22:00:31 +02:00
|
|
|
addButton.isEnabled = false
|
|
|
|
activityIndicator.isHidden = false
|
|
|
|
activityIndicator.startAnimating()
|
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()
|
|
|
|
|
2024-04-03 05:46:28 +02:00
|
|
|
Task { @MainActor in
|
|
|
|
do {
|
|
|
|
let feed = try await account!.createFeed(url: url.absoluteString, name: feedName, container: container, validateFeed: true)
|
2020-08-11 22:00:31 +02:00
|
|
|
self.dismiss(animated: true)
|
2024-02-26 08:12:21 +01:00
|
|
|
NotificationCenter.default.post(name: .UserDidAddFeed, object: self, userInfo: [UserInfoKey.feed: feed])
|
2024-04-03 05:46:28 +02:00
|
|
|
|
|
|
|
} catch {
|
2020-08-11 22:00:31 +02:00
|
|
|
self.addButton.isEnabled = true
|
|
|
|
self.activityIndicator.isHidden = true
|
|
|
|
self.activityIndicator.stopAnimating()
|
2019-09-12 18:36:28 +02:00
|
|
|
self.presentError(error)
|
2019-05-09 00:41:19 +02:00
|
|
|
}
|
2019-05-10 17:22:28 +02:00
|
|
|
|
2024-04-03 05:46:28 +02:00
|
|
|
BatchUpdate.shared.end()
|
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 {
|
2020-08-12 00:04:11 +02:00
|
|
|
let cell = tableView.dequeueReusableCell(withIdentifier: "AddFeedSelectFolderTableViewCell", for: indexPath) as? AddFeedSelectFolderTableViewCell
|
2019-12-08 18:39:13 +01:00
|
|
|
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 {
|
2024-02-26 08:12:21 +01:00
|
|
|
let navController = UIStoryboard.add.instantiateViewController(withIdentifier: "AddFeedFolderNavViewController") as! UINavigationController
|
2019-11-16 21:19:51 +01:00
|
|
|
navController.modalPresentationStyle = .currentContext
|
2020-08-12 00:04:11 +02:00
|
|
|
let folderViewController = navController.topViewController as! AddFeedFolderViewController
|
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
|
|
|
}
|
|
|
|
|
2024-02-26 08:12:21 +01:00
|
|
|
// MARK: AddFeedFolderViewControllerDelegate
|
2019-11-16 19:02:58 +01:00
|
|
|
|
2020-08-12 00:04:11 +02:00
|
|
|
extension AddFeedViewController: AddFeedFolderViewControllerDelegate {
|
2024-05-04 08:04:13 +02:00
|
|
|
|
2019-11-16 19:02:58 +01:00
|
|
|
func didSelect(container: Container) {
|
|
|
|
self.container = container
|
|
|
|
updateFolderLabel()
|
2024-02-26 08:12:21 +01:00
|
|
|
AddFeedDefaultContainer.saveDefaultContainer(container)
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-16 19:02:58 +01:00
|
|
|
// MARK: UITextFieldDelegate
|
|
|
|
|
2020-08-12 00:04:11 +02:00
|
|
|
extension AddFeedViewController: 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
|
|
|
|
|
2020-08-12 00:04:11 +02:00
|
|
|
private extension AddFeedViewController {
|
2020-04-23 16:08:33 +02:00
|
|
|
|
|
|
|
func updateUI() {
|
2020-08-11 22:00:31 +02:00
|
|
|
addButton.isEnabled = (urlTextField.text?.mayBeURL ?? false)
|
2020-04-23 16:08:33 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|