NetNewsWire/iOS/Add/Twitter/TwitterEnterDetailTableViewController.swift

84 lines
2.5 KiB
Swift
Raw Normal View History

2020-04-23 17:43:09 +02:00
//
// TwitterEnterDetailTableViewController.swift
// NetNewsWire-iOS
//
// Created by Maurice Parker on 4/23/20.
// Copyright © 2020 Ranchero Software. All rights reserved.
//
import UIKit
import Account
class TwitterEnterDetailTableViewController: UITableViewController, SelectURLBuilder {
@IBOutlet weak var detailTextField: UITextField!
var doneBarButtonItem = UIBarButtonItem()
var twitterFeedType: TwitterFeedType?
weak var delegate: SelectURLBuilderDelegate?
override func viewDidLoad() {
super.viewDidLoad()
doneBarButtonItem.title = NSLocalizedString("Done", comment: "Done")
doneBarButtonItem.style = .done
doneBarButtonItem.target = self
doneBarButtonItem.action = #selector(done)
navigationItem.rightBarButtonItem = doneBarButtonItem
if case .screenName = twitterFeedType {
navigationItem.title = NSLocalizedString("Enter Name", comment: "Enter Name")
detailTextField.placeholder = NSLocalizedString("Screen Name", comment: "Screen Name")
} else {
navigationItem.title = NSLocalizedString("Enter Search", comment: "Enter Search")
detailTextField.placeholder = NSLocalizedString("Search Term or #hashtag", comment: "Search Term")
2020-04-23 17:43:09 +02:00
}
2020-04-23 18:50:29 +02:00
detailTextField.delegate = self
2020-04-23 17:43:09 +02:00
NotificationCenter.default.addObserver(self, selector: #selector(textDidChange(_:)), name: UITextField.textDidChangeNotification, object: detailTextField)
updateUI()
}
@objc func done() {
2020-04-23 18:50:29 +02:00
guard let twitterFeedType = twitterFeedType, var text = detailTextField.text?.collapsingWhitespace else { return }
2020-04-23 17:43:09 +02:00
if twitterFeedType == .screenName {
2020-04-23 18:50:29 +02:00
if text.starts(with: "@") {
text = String(text[text.index(text.startIndex, offsetBy: 1)..<text.endIndex])
}
if let url = TwitterFeedProvider.buildURL(twitterFeedType, username: nil, screenName: text, searchField: nil) {
2020-04-23 17:43:09 +02:00
delegate?.selectURLBuilderDidBuildURL(url)
}
} else {
2020-04-23 18:50:29 +02:00
if let url = TwitterFeedProvider.buildURL(twitterFeedType, username: nil, screenName: nil, searchField: text) {
2020-04-23 17:43:09 +02:00
delegate?.selectURLBuilderDidBuildURL(url)
}
}
dismiss(animated: true)
}
@objc func textDidChange(_ note: Notification) {
updateUI()
}
}
2020-04-23 18:50:29 +02:00
extension TwitterEnterDetailTableViewController: UITextFieldDelegate {
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
}
2020-04-23 17:43:09 +02:00
private extension TwitterEnterDetailTableViewController {
func updateUI() {
doneBarButtonItem.isEnabled = !(detailTextField.text?.isEmpty ?? false)
}
}