2019-04-16 20:38:07 +02:00
|
|
|
//
|
|
|
|
// AddFolderViewController.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
|
|
|
|
import RSCore
|
|
|
|
|
2019-04-16 23:03:30 +02:00
|
|
|
class AddFolderViewController: UITableViewController, AddContainerViewControllerChild {
|
2019-04-15 22:03:05 +02:00
|
|
|
|
2019-05-20 00:48:03 +02:00
|
|
|
@IBOutlet private weak var nameTextField: UITextField!
|
|
|
|
@IBOutlet private weak var accountLabel: UILabel!
|
|
|
|
@IBOutlet private weak var accountPickerView: UIPickerView!
|
|
|
|
|
|
|
|
private var shouldDisplayPicker: Bool {
|
|
|
|
return accounts.count > 1
|
|
|
|
}
|
2019-04-15 22:03:05 +02:00
|
|
|
|
|
|
|
private var accounts: [Account]!
|
|
|
|
|
2019-04-26 01:06:53 +02:00
|
|
|
weak var delegate: AddContainerViewControllerChildDelegate?
|
2019-04-16 23:03:30 +02:00
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
override func viewDidLoad() {
|
|
|
|
|
|
|
|
super.viewDidLoad()
|
|
|
|
|
2019-05-02 13:01:30 +02:00
|
|
|
accounts = AccountManager.shared.sortedActiveAccounts
|
2019-05-20 00:48:03 +02:00
|
|
|
|
|
|
|
nameTextField.delegate = self
|
2019-09-05 22:07:35 +02:00
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
accountLabel.text = (accounts[0] as DisplayNameProvider).nameForDisplay
|
|
|
|
|
2019-05-20 00:48:03 +02:00
|
|
|
if shouldDisplayPicker {
|
|
|
|
accountPickerView.dataSource = self
|
|
|
|
accountPickerView.delegate = self
|
2019-11-16 22:28:57 +01:00
|
|
|
|
|
|
|
if let index = accounts.firstIndex(where: { $0.accountID == AppDefaults.addFolderAccountID }) {
|
|
|
|
accountPickerView.selectRow(index, inComponent: 0, animated: false)
|
|
|
|
}
|
|
|
|
|
2019-05-20 00:48:03 +02:00
|
|
|
} else {
|
|
|
|
accountPickerView.isHidden = true
|
|
|
|
}
|
2019-04-15 22:03:05 +02:00
|
|
|
|
2019-04-16 20:38:07 +02:00
|
|
|
// 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)
|
2019-04-16 23:03:30 +02:00
|
|
|
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(textDidChange(_:)), name: UITextField.textDidChangeNotification, object: nameTextField)
|
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
|
|
|
|
2019-04-16 23:03:30 +02:00
|
|
|
func cancel() {
|
|
|
|
delegate?.processingDidEnd()
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
|
|
|
|
2019-04-16 23:03:30 +02:00
|
|
|
func add() {
|
2019-04-15 22:03:05 +02:00
|
|
|
let account = accounts[accountPickerView.selectedRow(inComponent: 0)]
|
|
|
|
if let folderName = nameTextField.text {
|
2019-09-27 15:46:25 +02:00
|
|
|
account.addFolder(folderName) { result in
|
|
|
|
switch result {
|
|
|
|
case .success:
|
|
|
|
self.delegate?.processingDidEnd()
|
|
|
|
case .failure(let error):
|
|
|
|
self.presentError(error)
|
|
|
|
}
|
|
|
|
}
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
2019-04-16 23:03:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@objc func textDidChange(_ note: Notification) {
|
|
|
|
delegate?.readyToAdd(state: !(nameTextField.text?.isEmpty ?? false))
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
|
|
|
|
2019-05-20 00:48:03 +02:00
|
|
|
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
2019-05-20 02:20:35 +02:00
|
|
|
let defaultNumberOfRows = super.tableView(tableView, numberOfRowsInSection: section)
|
2019-05-20 00:48:03 +02:00
|
|
|
if section == 1 && !shouldDisplayPicker {
|
2019-05-20 02:20:35 +02:00
|
|
|
return defaultNumberOfRows - 1
|
2019-05-20 00:48:03 +02:00
|
|
|
}
|
|
|
|
|
2019-05-20 02:20:35 +02:00
|
|
|
return defaultNumberOfRows
|
2019-05-20 00:48:03 +02:00
|
|
|
}
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
extension AddFolderViewController: UIPickerViewDataSource, UIPickerViewDelegate {
|
|
|
|
|
|
|
|
func numberOfComponents(in pickerView: UIPickerView) ->Int {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
|
|
|
|
return accounts.count
|
|
|
|
}
|
|
|
|
|
|
|
|
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
|
|
|
|
return (accounts[row] as DisplayNameProvider).nameForDisplay
|
|
|
|
}
|
|
|
|
|
|
|
|
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
|
|
|
|
accountLabel.text = (accounts[row] as DisplayNameProvider).nameForDisplay
|
2019-11-16 22:28:57 +01:00
|
|
|
AppDefaults.addFolderAccountID = accounts[row].accountID
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-05-20 00:48:03 +02:00
|
|
|
|
|
|
|
extension AddFolderViewController: UITextFieldDelegate {
|
|
|
|
|
|
|
|
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
|
|
|
|
textField.resignFirstResponder()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|