From 41926b4c698ae674d29a4e2e2e6c529e388922bc Mon Sep 17 00:00:00 2001 From: Maurice Parker Date: Tue, 16 Apr 2019 16:03:30 -0500 Subject: [PATCH] Fixed up the Add Feed and Add Folder functionality so that it functions again. --- iOS/Add/Add.storyboard | 56 +++------------- iOS/Add/AddAccountViewController.swift | 13 +++- iOS/Add/AddContainerViewController.swift | 52 ++++++++++++++- iOS/Add/AddFeedViewController.swift | 85 ++++++++---------------- iOS/Add/AddFolderViewController.swift | 44 ++++-------- 5 files changed, 113 insertions(+), 137 deletions(-) diff --git a/iOS/Add/Add.storyboard b/iOS/Add/Add.storyboard index 2eb9e7470..c622165bd 100644 --- a/iOS/Add/Add.storyboard +++ b/iOS/Add/Add.storyboard @@ -33,9 +33,6 @@ - - - @@ -131,36 +128,15 @@ - - - - + + + + + + - - - - - - - - - - - - - - - - - - - - - - - @@ -228,7 +204,7 @@ - + @@ -253,6 +229,7 @@ + @@ -282,9 +259,6 @@ - - - @@ -356,22 +330,10 @@ - - - - - - - - - - - - + - diff --git a/iOS/Add/AddAccountViewController.swift b/iOS/Add/AddAccountViewController.swift index 0fe1e73c0..1c6ab89c6 100644 --- a/iOS/Add/AddAccountViewController.swift +++ b/iOS/Add/AddAccountViewController.swift @@ -8,8 +8,9 @@ import UIKit -class AddAccountViewController: UITableViewController { - +class AddAccountViewController: UITableViewController, AddContainerViewControllerChild { + var delegate: AddContainerViewControllerChildDelegate? + override func viewDidLoad() { super.viewDidLoad() @@ -18,4 +19,12 @@ class AddAccountViewController: UITableViewController { } + func cancel() { + + } + + func add() { + + } + } diff --git a/iOS/Add/AddContainerViewController.swift b/iOS/Add/AddContainerViewController.swift index bd98bbfce..4c3eb6523 100644 --- a/iOS/Add/AddContainerViewController.swift +++ b/iOS/Add/AddContainerViewController.swift @@ -8,14 +8,27 @@ import UIKit +protocol AddContainerViewControllerChild: UIViewController { + var delegate: AddContainerViewControllerChildDelegate? {get set} + func cancel() + func add() +} + +protocol AddContainerViewControllerChildDelegate: UIViewController { + func readyToAdd(state: Bool) + func processingDidBegin() + func processingDidEnd() +} + class AddContainerViewController: UIViewController { @IBOutlet weak var cancelButton: UIBarButtonItem! @IBOutlet weak var activityIndicatorView: UIActivityIndicatorView! @IBOutlet weak var addButton: UIBarButtonItem! + @IBOutlet weak var typeSelectorSegmentedControl: UISegmentedControl! @IBOutlet weak var containerView: UIView! - private var currentViewController: UIViewController? + private var currentViewController: AddContainerViewControllerChild? override func viewDidLoad() { @@ -40,20 +53,46 @@ class AddContainerViewController: UIViewController { } @IBAction func cancel(_ sender: Any) { + currentViewController?.cancel() dismiss(animated: true) } @IBAction func add(_ sender: Any) { + currentViewController?.add() } } +extension AddContainerViewController: AddContainerViewControllerChildDelegate { + + func readyToAdd(state: Bool) { + addButton.isEnabled = state + } + + func processingDidBegin() { + addButton.isEnabled = false + typeSelectorSegmentedControl.isEnabled = false + activityIndicatorView.isHidden = false + activityIndicatorView.startAnimating() + } + + func processingDidEnd() { + addButton.isEnabled = true + typeSelectorSegmentedControl.isEnabled = true + activityIndicatorView.isHidden = true + activityIndicatorView.stopAnimating() + dismiss(animated: true) + } + +} + private extension AddContainerViewController { func switchToFeed() { guard !(currentViewController is AddFeedViewController) else { return } + resetUI() hideCurrentController() displayContentController(UIStoryboard.add.instantiateController(ofType: AddFeedViewController.self)) } @@ -62,6 +101,7 @@ private extension AddContainerViewController { guard !(currentViewController is AddFolderViewController) else { return } + resetUI() hideCurrentController() displayContentController(UIStoryboard.add.instantiateController(ofType: AddFolderViewController.self)) } @@ -70,11 +110,19 @@ private extension AddContainerViewController { guard !(currentViewController is AddAccountViewController) else { return } + resetUI() hideCurrentController() displayContentController(UIStoryboard.add.instantiateController(ofType: AddAccountViewController.self)) } - func displayContentController(_ controller: UIViewController) { + func resetUI() { + addButton.isEnabled = false + } + + func displayContentController(_ controller: AddContainerViewControllerChild) { + + currentViewController = controller + controller.delegate = self addChild(controller) diff --git a/iOS/Add/AddFeedViewController.swift b/iOS/Add/AddFeedViewController.swift index 9b7d44974..4a359bfa8 100644 --- a/iOS/Add/AddFeedViewController.swift +++ b/iOS/Add/AddFeedViewController.swift @@ -12,11 +12,7 @@ import RSCore import RSTree import RSParser -class AddFeedViewController: UITableViewController { - - @IBOutlet weak var activityIndicatorView: UIActivityIndicatorView! - @IBOutlet weak var cancelButton: UIBarButtonItem! - @IBOutlet weak var addButton: UIBarButtonItem! +class AddFeedViewController: UITableViewController, AddContainerViewControllerChild { @IBOutlet weak var urlTextField: UITextField! @IBOutlet weak var nameTextField: UITextField! @@ -35,13 +31,13 @@ class AddFeedViewController: UITableViewController { private var titleFromFeed: String? private var userCancelled = false + + var delegate: AddContainerViewControllerChildDelegate? override func viewDidLoad() { super.viewDidLoad() - activityIndicatorView.isHidden = true - urlTextField.autocorrectionType = .no urlTextField.autocapitalizationType = .none @@ -54,20 +50,22 @@ class AddFeedViewController: UITableViewController { // 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) - } - - @IBAction func cancel(_ sender: Any) { - userCancelled = true - dismiss(animated: true) + NotificationCenter.default.addObserver(self, selector: #selector(textDidChange(_:)), name: UITextField.textDidChangeNotification, object: urlTextField) + } - @IBAction func add(_ sender: Any) { + func cancel() { + userCancelled = true + delegate?.processingDidEnd() + } + + func add() { let urlString = urlTextField.text ?? "" let normalizedURLString = (urlString as NSString).rs_normalizedURL() guard !normalizedURLString.isEmpty, let url = URL(string: normalizedURLString) else { - dismiss(animated: true) + delegate?.processingDidEnd() return } @@ -93,12 +91,16 @@ class AddFeedViewController: UITableViewController { return } - beginShowingProgress() - + delegate?.processingDidBegin() + feedFinder = FeedFinder(url: url, delegate: self) } + @objc func textDidChange(_ note: Notification) { + delegate?.readyToAdd(state: urlTextField.text?.rs_stringMayBeURL() ?? false) + } + } extension AddFeedViewController: UIPickerViewDataSource, UIPickerViewDelegate { @@ -121,42 +123,28 @@ extension AddFeedViewController: UIPickerViewDataSource, UIPickerViewDelegate { } -extension AddFeedViewController: UITextFieldDelegate { - - func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { - updateUI() - return true - } - - func textFieldDidEndEditing(_ textField: UITextField) { - updateUI() - } - -} - extension AddFeedViewController: FeedFinderDelegate { public func feedFinder(_ feedFinder: FeedFinder, didFindFeeds feedSpecifiers: Set) { if userCancelled { - endShowingProgress() return } if let error = feedFinder.initialDownloadError { if feedFinder.initialDownloadStatusCode == 404 { - endShowingProgress() showNoFeedsErrorMessage() + delegate?.processingDidEnd() } else { - endShowingProgress() showInitialDownloadError(error) + delegate?.processingDidEnd() } return } guard let bestFeedSpecifier = FeedSpecifier.bestFeed(in: feedSpecifiers) else { - endShowingProgress() showNoFeedsErrorMessage() + delegate?.processingDidEnd() return } @@ -170,7 +158,7 @@ extension AddFeedViewController: FeedFinderDelegate { } } else { // Shouldn't happen. - endShowingProgress() + delegate?.processingDidEnd() showNoFeedsErrorMessage() } @@ -180,22 +168,6 @@ extension AddFeedViewController: FeedFinderDelegate { private extension AddFeedViewController { - private func updateUI() { - addButton.isEnabled = urlTextField.text?.rs_stringMayBeURL() ?? false - } - - private func beginShowingProgress() { - activityIndicatorView.isHidden = false - activityIndicatorView.startAnimating() - addButton.isEnabled = false - } - - private func endShowingProgress() { - activityIndicatorView.isHidden = true - activityIndicatorView.stopAnimating() - addButton.isEnabled = true - } - private func showAlreadySubscribedError() { let title = NSLocalizedString("Already subscribed", comment: "Feed finder") let message = NSLocalizedString("Can’t add this feed because you’ve already subscribed to it.", comment: "Feed finder") @@ -218,27 +190,29 @@ private extension AddFeedViewController { func addFeedIfPossible(_ parsedFeed: ParsedFeed?) { if userCancelled { - endShowingProgress() return } guard let account = userEnteredAccount else { assertionFailure("Expected account.") + delegate?.processingDidEnd() return } + guard let feedURLString = foundFeedURLString else { assertionFailure("Expected feedURLString.") + delegate?.processingDidEnd() return } if account.hasFeed(withURL: feedURLString) { - endShowingProgress() showAlreadySubscribedError() + delegate?.processingDidEnd() return } guard let feed = account.createFeed(with: titleFromFeed, editedName: userEnteredTitle, url: feedURLString) else { - endShowingProgress() + delegate?.processingDidEnd() return } @@ -249,9 +223,8 @@ private extension AddFeedViewController { account.addFeed(feed, to: userEnteredFolder) NotificationCenter.default.post(name: .UserDidAddFeed, object: self, userInfo: [UserInfoKey.feed: feed]) - endShowingProgress() - dismiss(animated: true) - + delegate?.processingDidEnd() + } } diff --git a/iOS/Add/AddFolderViewController.swift b/iOS/Add/AddFolderViewController.swift index 362f5f006..c91abda0f 100644 --- a/iOS/Add/AddFolderViewController.swift +++ b/iOS/Add/AddFolderViewController.swift @@ -10,15 +10,16 @@ import UIKit import Account import RSCore -class AddFolderViewController: UITableViewController { +class AddFolderViewController: UITableViewController, AddContainerViewControllerChild { - @IBOutlet weak var addButton: UIBarButtonItem! @IBOutlet weak var nameTextField: UITextField! @IBOutlet weak var accountLabel: UILabel! @IBOutlet weak var accountPickerView: UIPickerView! private var accounts: [Account]! + var delegate: AddContainerViewControllerChildDelegate? + override func viewDidLoad() { super.viewDidLoad() @@ -31,21 +32,25 @@ class AddFolderViewController: UITableViewController { // 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) + + NotificationCenter.default.addObserver(self, selector: #selector(textDidChange(_:)), name: UITextField.textDidChangeNotification, object: nameTextField) + } - @IBAction func cancel(_ sender: Any) { - dismiss(animated: true) + func cancel() { + delegate?.processingDidEnd() } - @IBAction func add(_ sender: Any) { - + func add() { let account = accounts[accountPickerView.selectedRow(inComponent: 0)] if let folderName = nameTextField.text { account.ensureFolder(with: folderName) } - - dismiss(animated: true) - + delegate?.processingDidEnd() + } + + @objc func textDidChange(_ note: Notification) { + delegate?.readyToAdd(state: !(nameTextField.text?.isEmpty ?? false)) } } @@ -69,24 +74,3 @@ extension AddFolderViewController: UIPickerViewDataSource, UIPickerViewDelegate } } - -extension AddFolderViewController: UITextFieldDelegate { - - func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { - updateUI() - return true - } - - func textFieldDidEndEditing(_ textField: UITextField) { - updateUI() - } - -} - -private extension AddFolderViewController { - - private func updateUI() { - addButton.isEnabled = !(nameTextField.text?.isEmpty ?? false) - } - -}