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
|
|
|
|
|
2020-08-11 22:00:31 +02:00
|
|
|
class AddFolderViewController: UITableViewController {
|
2019-04-15 22:03:05 +02:00
|
|
|
|
2020-08-11 22:00:31 +02:00
|
|
|
@IBOutlet private weak var addButton: UIBarButtonItem!
|
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!
|
|
|
|
|
2020-08-11 22:00:31 +02:00
|
|
|
static let preferredContentSizeForFormSheetDisplay = CGSize(width: 460.0, height: 400.0)
|
|
|
|
|
2019-05-20 00:48:03 +02:00
|
|
|
private var shouldDisplayPicker: Bool {
|
|
|
|
return accounts.count > 1
|
|
|
|
}
|
2019-04-15 22:03:05 +02:00
|
|
|
|
2019-12-23 16:20:52 +01:00
|
|
|
private var accounts: [Account]! {
|
|
|
|
didSet {
|
2020-07-02 04:47:45 +02:00
|
|
|
if let predefinedAccount = accounts.first(where: { $0.accountID == AppDefaults.shared.addFolderAccountID }) {
|
2019-12-23 16:20:52 +01:00
|
|
|
selectedAccount = predefinedAccount
|
|
|
|
} else {
|
|
|
|
selectedAccount = accounts[0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private var selectedAccount: Account! {
|
|
|
|
didSet {
|
|
|
|
guard selectedAccount != oldValue else { return }
|
|
|
|
accountLabel.text = selectedAccount.flatMap { ($0 as DisplayNameProvider).nameForDisplay }
|
|
|
|
}
|
|
|
|
}
|
2019-04-15 22:03:05 +02:00
|
|
|
|
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
|
|
|
|
2020-05-07 16:03:04 +02:00
|
|
|
accounts = AccountManager.shared
|
|
|
|
.sortedActiveAccounts
|
|
|
|
.filter { !$0.behaviors.contains(.disallowFolderManagement) }
|
2019-05-20 00:48:03 +02:00
|
|
|
|
|
|
|
nameTextField.delegate = self
|
2019-09-05 22:07:35 +02:00
|
|
|
|
2019-05-20 00:48:03 +02:00
|
|
|
if shouldDisplayPicker {
|
|
|
|
accountPickerView.dataSource = self
|
|
|
|
accountPickerView.delegate = self
|
2019-11-16 22:28:57 +01:00
|
|
|
|
2019-12-23 16:20:52 +01:00
|
|
|
if let index = accounts.firstIndex(of: selectedAccount) {
|
2019-11-16 22:28:57 +01:00
|
|
|
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 23:03:30 +02:00
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(textDidChange(_:)), name: UITextField.textDidChangeNotification, object: nameTextField)
|
|
|
|
|
2020-08-11 22:00:31 +02:00
|
|
|
nameTextField.becomeFirstResponder()
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
2019-12-23 16:20:52 +01:00
|
|
|
|
|
|
|
private func didSelect(_ account: Account) {
|
2020-07-02 04:47:45 +02:00
|
|
|
AppDefaults.shared.addFolderAccountID = account.accountID
|
2019-12-23 16:20:52 +01:00
|
|
|
selectedAccount = account
|
|
|
|
}
|
2019-04-15 22:03:05 +02:00
|
|
|
|
2020-08-11 22:00:31 +02:00
|
|
|
@IBAction func cancel(_ sender: Any) {
|
|
|
|
dismiss(animated: true)
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
|
|
|
|
2020-08-11 22:00:31 +02:00
|
|
|
@IBAction func add(_ sender: Any) {
|
2019-12-23 16:20:52 +01:00
|
|
|
guard let folderName = nameTextField.text else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
selectedAccount.addFolder(folderName) { result in
|
|
|
|
switch result {
|
|
|
|
case .success:
|
2020-08-11 22:00:31 +02:00
|
|
|
self.dismiss(animated: true)
|
2019-12-23 16:20:52 +01:00
|
|
|
case .failure(let error):
|
|
|
|
self.presentError(error)
|
2020-08-11 22:00:31 +02:00
|
|
|
self.dismiss(animated: true)
|
2019-09-27 15:46:25 +02:00
|
|
|
}
|
2019-04-15 22:03:05 +02:00
|
|
|
}
|
2019-04-16 23:03:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@objc func textDidChange(_ note: Notification) {
|
2020-08-11 22:00:31 +02:00
|
|
|
addButton.isEnabled = !(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) {
|
2019-12-23 16:20:52 +01:00
|
|
|
didSelect(accounts[row])
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|