2019-04-16 13:38:07 -05:00
|
|
|
//
|
|
|
|
// AddFolderViewController.swift
|
|
|
|
// NetNewsWire
|
|
|
|
//
|
|
|
|
// Created by Maurice Parker on 4/16/19.
|
|
|
|
// Copyright © 2019 Ranchero Software, LLC. All rights reserved.
|
|
|
|
//
|
2019-04-15 15:03:05 -05:00
|
|
|
|
|
|
|
import UIKit
|
|
|
|
import Account
|
2024-03-20 20:49:15 -07:00
|
|
|
import Core
|
2019-04-15 15:03:05 -05:00
|
|
|
|
2020-08-11 15:00:31 -05:00
|
|
|
class AddFolderViewController: UITableViewController {
|
2019-04-15 15:03:05 -05:00
|
|
|
|
2020-08-11 15:00:31 -05:00
|
|
|
@IBOutlet private weak var addButton: UIBarButtonItem!
|
2019-05-19 17:48:03 -05:00
|
|
|
@IBOutlet private weak var nameTextField: UITextField!
|
|
|
|
@IBOutlet private weak var accountLabel: UILabel!
|
|
|
|
@IBOutlet private weak var accountPickerView: UIPickerView!
|
|
|
|
|
2020-08-11 15:00:31 -05:00
|
|
|
static let preferredContentSizeForFormSheetDisplay = CGSize(width: 460.0, height: 400.0)
|
|
|
|
|
2019-05-19 17:48:03 -05:00
|
|
|
private var shouldDisplayPicker: Bool {
|
|
|
|
return accounts.count > 1
|
|
|
|
}
|
2019-04-15 15:03:05 -05:00
|
|
|
|
2019-12-23 17:20:52 +02:00
|
|
|
private var accounts: [Account]! {
|
|
|
|
didSet {
|
2020-07-02 10:47:45 +08:00
|
|
|
if let predefinedAccount = accounts.first(where: { $0.accountID == AppDefaults.shared.addFolderAccountID }) {
|
2019-12-23 17:20:52 +02: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 15:03:05 -05:00
|
|
|
|
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
|
|
|
|
2020-05-07 10:03:04 -04:00
|
|
|
accounts = AccountManager.shared
|
|
|
|
.sortedActiveAccounts
|
|
|
|
.filter { !$0.behaviors.contains(.disallowFolderManagement) }
|
2019-05-19 17:48:03 -05:00
|
|
|
|
|
|
|
nameTextField.delegate = self
|
2019-09-05 15:07:35 -05:00
|
|
|
|
2019-05-19 17:48:03 -05:00
|
|
|
if shouldDisplayPicker {
|
|
|
|
accountPickerView.dataSource = self
|
|
|
|
accountPickerView.delegate = self
|
2019-11-16 15:28:57 -06:00
|
|
|
|
2019-12-23 17:20:52 +02:00
|
|
|
if let index = accounts.firstIndex(of: selectedAccount) {
|
2019-11-16 15:28:57 -06:00
|
|
|
accountPickerView.selectRow(index, inComponent: 0, animated: false)
|
|
|
|
}
|
|
|
|
|
2019-05-19 17:48:03 -05:00
|
|
|
} else {
|
|
|
|
accountPickerView.isHidden = true
|
|
|
|
}
|
2019-04-15 15:03:05 -05:00
|
|
|
|
2019-04-16 16:03:30 -05:00
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(textDidChange(_:)), name: UITextField.textDidChangeNotification, object: nameTextField)
|
|
|
|
|
2020-08-11 15:00:31 -05:00
|
|
|
nameTextField.becomeFirstResponder()
|
2019-04-15 15:03:05 -05:00
|
|
|
}
|
2019-12-23 17:20:52 +02:00
|
|
|
|
|
|
|
private func didSelect(_ account: Account) {
|
2020-07-02 10:47:45 +08:00
|
|
|
AppDefaults.shared.addFolderAccountID = account.accountID
|
2019-12-23 17:20:52 +02:00
|
|
|
selectedAccount = account
|
|
|
|
}
|
2019-04-15 15:03:05 -05:00
|
|
|
|
2020-08-11 15:00:31 -05:00
|
|
|
@IBAction func cancel(_ sender: Any) {
|
|
|
|
dismiss(animated: true)
|
2019-04-15 15:03:05 -05:00
|
|
|
}
|
|
|
|
|
2020-08-11 15:00:31 -05:00
|
|
|
@IBAction func add(_ sender: Any) {
|
2024-03-26 22:18:48 -07:00
|
|
|
|
2019-12-23 17:20:52 +02:00
|
|
|
guard let folderName = nameTextField.text else {
|
|
|
|
return
|
|
|
|
}
|
2024-03-26 22:18:48 -07:00
|
|
|
|
|
|
|
Task { @MainActor in
|
|
|
|
do {
|
|
|
|
_ = try await selectedAccount.addFolder(folderName)
|
2020-08-11 15:00:31 -05:00
|
|
|
self.dismiss(animated: true)
|
2024-03-26 22:18:48 -07:00
|
|
|
} catch {
|
2019-12-23 17:20:52 +02:00
|
|
|
self.presentError(error)
|
2020-08-11 15:00:31 -05:00
|
|
|
self.dismiss(animated: true)
|
2019-09-27 08:46:25 -05:00
|
|
|
}
|
2019-04-15 15:03:05 -05:00
|
|
|
}
|
2019-04-16 16:03:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@objc func textDidChange(_ note: Notification) {
|
2020-08-11 15:00:31 -05:00
|
|
|
addButton.isEnabled = !(nameTextField.text?.isEmpty ?? false)
|
2019-04-15 15:03:05 -05:00
|
|
|
}
|
|
|
|
|
2019-05-19 17:48:03 -05:00
|
|
|
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
2019-05-19 19:20:35 -05:00
|
|
|
let defaultNumberOfRows = super.tableView(tableView, numberOfRowsInSection: section)
|
2019-05-19 17:48:03 -05:00
|
|
|
if section == 1 && !shouldDisplayPicker {
|
2019-05-19 19:20:35 -05:00
|
|
|
return defaultNumberOfRows - 1
|
2019-05-19 17:48:03 -05:00
|
|
|
}
|
|
|
|
|
2019-05-19 19:20:35 -05:00
|
|
|
return defaultNumberOfRows
|
2019-05-19 17:48:03 -05:00
|
|
|
}
|
2019-04-15 15:03:05 -05: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 17:20:52 +02:00
|
|
|
didSelect(accounts[row])
|
2019-04-15 15:03:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-05-19 17:48:03 -05:00
|
|
|
|
|
|
|
extension AddFolderViewController: UITextFieldDelegate {
|
|
|
|
|
|
|
|
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
|
|
|
|
textField.resignFirstResponder()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|