NetNewsWire/iOS/Add/Twitter/TwitterEnterDetailTableViewController.swift

83 lines
2.6 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
2020-08-11 23:46:26 +02:00
class TwitterEnterDetailTableViewController: UITableViewController {
2020-04-23 17:43:09 +02:00
@IBOutlet weak var detailTextField: UITextField!
var doneBarButtonItem = UIBarButtonItem()
var twitterFeedType: TwitterFeedType?
override func viewDidLoad() {
super.viewDidLoad()
2020-08-11 23:46:26 +02:00
doneBarButtonItem.title = NSLocalizedString("Next", comment: "Next")
doneBarButtonItem.style = .plain
2020-04-23 17:43:09 +02:00
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-08-11 23:46:26 +02:00
let url: String?
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])
}
2020-08-11 23:46:26 +02:00
url = TwitterFeedProvider.buildURL(twitterFeedType, username: nil, screenName: text, searchField: nil)?.absoluteString
2020-04-23 17:43:09 +02:00
} else {
2020-08-11 23:46:26 +02:00
url = TwitterFeedProvider.buildURL(twitterFeedType, username: nil, screenName: nil, searchField: text)?.absoluteString
2020-04-23 17:43:09 +02:00
}
let addViewController = UIStoryboard.add.instantiateViewController(withIdentifier: "AddWebFeedViewController") as! AddFeedViewController
2020-08-11 23:46:26 +02:00
addViewController.addFeedType = .twitter
addViewController.initialFeed = url
navigationController?.pushViewController(addViewController, animated: true)
2020-04-23 17:43:09 +02:00
}
@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)
}
}