Fixed up the Add Feed and Add Folder functionality so that it functions again.

This commit is contained in:
Maurice Parker 2019-04-16 16:03:30 -05:00
parent 615e183242
commit 41926b4c69
5 changed files with 113 additions and 137 deletions

View File

@ -33,9 +33,6 @@
<nil key="textColor"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits"/>
<connections>
<outlet property="delegate" destination="7aE-6a-iP7" id="zCK-Sy-4Zr"/>
</connections>
</textField>
</subviews>
<constraints>
@ -131,36 +128,15 @@
</connections>
</tableView>
<navigationItem key="navigationItem" title="Add Feed" id="i1W-2z-PAk">
<barButtonItem key="leftBarButtonItem" systemItem="cancel" id="vdU-kc-SkI">
<connections>
<action selector="cancel:" destination="7aE-6a-iP7" id="v9C-5Y-7Pf"/>
</connections>
<barButtonItem key="rightBarButtonItem" style="plain" id="r7V-oB-aHz">
<view key="customView" contentMode="scaleToFill" id="4in-Eb-Rxp">
<rect key="frame" x="339" y="12" width="20" height="20"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</view>
</barButtonItem>
<rightBarButtonItems>
<barButtonItem enabled="NO" title="Add" id="M4A-Uu-rC9">
<connections>
<action selector="add:" destination="7aE-6a-iP7" id="WZ4-RF-9k2"/>
</connections>
</barButtonItem>
<barButtonItem style="plain" id="r7V-oB-aHz">
<view key="customView" contentMode="scaleToFill" id="4in-Eb-Rxp">
<rect key="frame" x="335" y="12" width="20" height="20"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<subviews>
<activityIndicatorView opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="750" verticalHuggingPriority="750" fixedFrame="YES" style="gray" translatesAutoresizingMaskIntoConstraints="NO" id="3ZH-9O-T3i">
<rect key="frame" x="0.0" y="0.0" width="20" height="20"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
</activityIndicatorView>
</subviews>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</view>
</barButtonItem>
</rightBarButtonItems>
</navigationItem>
<connections>
<outlet property="activityIndicatorView" destination="3ZH-9O-T3i" id="Z7G-Qb-tV9"/>
<outlet property="addButton" destination="M4A-Uu-rC9" id="HXl-Sg-Zgw"/>
<outlet property="cancelButton" destination="vdU-kc-SkI" id="AKF-i4-V5Q"/>
<outlet property="folderLabel" destination="vaV-kY-CaE" id="xeO-Ks-LIy"/>
<outlet property="folderPickerView" destination="v2n-nX-8jq" id="qwz-Gg-GdQ"/>
<outlet property="nameTextField" destination="u7n-VL-Ho9" id="YQV-Xq-f9q"/>
@ -228,7 +204,7 @@
</connections>
</barButtonItem>
<rightBarButtonItems>
<barButtonItem title="Add" id="tVq-rz-5pe">
<barButtonItem enabled="NO" title="Add" id="tVq-rz-5pe">
<connections>
<action selector="add:" destination="G8j-dh-7Ji" id="fWb-l7-nc0"/>
</connections>
@ -253,6 +229,7 @@
<outlet property="addButton" destination="tVq-rz-5pe" id="kNP-KC-naP"/>
<outlet property="cancelButton" destination="Uz9-n0-t2M" id="dZu-nz-p4J"/>
<outlet property="containerView" destination="TUN-mZ-uLE" id="8p5-N0-LRa"/>
<outlet property="typeSelectorSegmentedControl" destination="1Ce-E7-rG2" id="3JA-Su-PFn"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="KrE-EC-ioA" userLabel="First Responder" sceneMemberID="firstResponder"/>
@ -282,9 +259,6 @@
<nil key="textColor"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits"/>
<connections>
<outlet property="delegate" destination="3dI-34-ljo" id="rrs-yw-joz"/>
</connections>
</textField>
</subviews>
<constraints>
@ -356,22 +330,10 @@
<outlet property="delegate" destination="3dI-34-ljo" id="ZXX-gI-6ii"/>
</connections>
</tableView>
<navigationItem key="navigationItem" title="Add Folder" id="LuA-AC-n99">
<barButtonItem key="leftBarButtonItem" systemItem="cancel" id="v7P-5Z-FqV">
<connections>
<action selector="cancel:" destination="3dI-34-ljo" id="FA8-9i-9CA"/>
</connections>
</barButtonItem>
<barButtonItem key="rightBarButtonItem" enabled="NO" title="Add" id="dgF-XT-J4d">
<connections>
<action selector="add:" destination="3dI-34-ljo" id="BIP-Db-2K5"/>
</connections>
</barButtonItem>
</navigationItem>
<navigationItem key="navigationItem" title="Add Folder" id="LuA-AC-n99"/>
<connections>
<outlet property="accountLabel" destination="mxj-Bw-Jfx" id="l4A-ta-dUi"/>
<outlet property="accountPickerView" destination="eGY-V8-gzJ" id="zGk-k4-Jnd"/>
<outlet property="addButton" destination="dgF-XT-J4d" id="Zu1-fQ-xdl"/>
<outlet property="nameTextField" destination="lZK-wx-jbo" id="Xvq-LH-kwC"/>
</connections>
</tableViewController>

View File

@ -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() {
}
}

View File

@ -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)

View File

@ -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<FeedSpecifier>) {
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("Cant add this feed because youve 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()
}
}

View File

@ -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)
}
}