2019-03-18 03:01:28 +01:00
|
|
|
//
|
|
|
|
// AccountsPreferencesViewController.swift
|
|
|
|
// NetNewsWire
|
|
|
|
//
|
|
|
|
// Created by Brent Simmons on 3/17/19.
|
|
|
|
// Copyright © 2019 Ranchero Software. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2019-03-20 06:41:09 +01:00
|
|
|
import AppKit
|
2019-03-24 20:33:43 +01:00
|
|
|
import Account
|
2019-03-20 06:41:09 +01:00
|
|
|
|
|
|
|
final class AccountsPreferencesViewController: NSViewController {
|
|
|
|
|
2019-04-30 13:38:18 +02:00
|
|
|
@IBOutlet weak var tableView: NSTableView!
|
|
|
|
@IBOutlet weak var detailView: NSView!
|
2019-05-02 01:26:23 +02:00
|
|
|
@IBOutlet weak var deleteButton: NSButton!
|
2019-04-30 13:38:18 +02:00
|
|
|
|
2019-05-02 01:26:23 +02:00
|
|
|
private var deleteController: AccountsDeleteWindowController?
|
2019-03-24 20:33:43 +01:00
|
|
|
private var sortedAccounts = [Account]()
|
|
|
|
|
2019-04-30 13:38:18 +02:00
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
2019-05-01 17:28:13 +02:00
|
|
|
|
|
|
|
updateSortedAccounts()
|
|
|
|
tableView.delegate = self
|
|
|
|
tableView.dataSource = self
|
|
|
|
|
2019-04-30 13:38:18 +02:00
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(displayNameDidChange(_:)), name: .DisplayNameDidChange, object: nil)
|
2019-05-01 19:37:13 +02:00
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(accountsDidChangeNotification(_:)), name: .AccountsDidChangeNotification, object: nil)
|
|
|
|
|
2019-05-01 18:05:55 +02:00
|
|
|
showController(AccountsAddViewController())
|
2019-05-01 17:28:13 +02:00
|
|
|
|
2019-04-30 13:38:18 +02:00
|
|
|
}
|
|
|
|
|
2019-05-01 17:28:13 +02:00
|
|
|
@IBAction func addAccount(_ sender: Any) {
|
2019-05-01 18:05:55 +02:00
|
|
|
tableView.selectRowIndexes([], byExtendingSelection: false)
|
|
|
|
showController(AccountsAddViewController())
|
2019-04-30 13:38:18 +02:00
|
|
|
}
|
|
|
|
|
2019-05-01 17:28:13 +02:00
|
|
|
@IBAction func removeAccount(_ sender: Any) {
|
2019-05-02 01:26:23 +02:00
|
|
|
guard tableView.selectedRow != -1 else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
let account = sortedAccounts[tableView.selectedRow]
|
|
|
|
deleteController = AccountsDeleteWindowController(account: account)
|
|
|
|
deleteController!.runSheetOnWindow(view.window!)
|
2019-04-30 13:38:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@objc func displayNameDidChange(_ note: Notification) {
|
2019-05-01 17:28:13 +02:00
|
|
|
updateSortedAccounts()
|
2019-04-30 13:38:18 +02:00
|
|
|
tableView.reloadData()
|
2019-03-24 20:33:43 +01:00
|
|
|
}
|
2019-04-30 13:38:18 +02:00
|
|
|
|
2019-05-01 19:37:13 +02:00
|
|
|
@objc func accountsDidChangeNotification(_ note: Notification) {
|
|
|
|
updateSortedAccounts()
|
|
|
|
tableView.reloadData()
|
|
|
|
}
|
|
|
|
|
2019-03-24 20:33:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - NSTableViewDataSource
|
|
|
|
|
|
|
|
extension AccountsPreferencesViewController: NSTableViewDataSource {
|
|
|
|
|
|
|
|
func numberOfRows(in tableView: NSTableView) -> Int {
|
|
|
|
return sortedAccounts.count
|
|
|
|
}
|
|
|
|
|
|
|
|
func tableView(_ tableView: NSTableView, objectValueFor tableColumn: NSTableColumn?, row: Int) -> Any? {
|
|
|
|
return sortedAccounts[row]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - NSTableViewDelegate
|
|
|
|
|
|
|
|
extension AccountsPreferencesViewController: NSTableViewDelegate {
|
|
|
|
|
|
|
|
private static let cellIdentifier = NSUserInterfaceItemIdentifier(rawValue: "AccountCell")
|
|
|
|
|
|
|
|
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
|
2019-04-30 13:38:18 +02:00
|
|
|
if let cell = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "Cell"), owner: nil) as? NSTableCellView {
|
2019-05-01 13:49:50 +02:00
|
|
|
let account = sortedAccounts[row]
|
|
|
|
cell.textField?.stringValue = account.nameForDisplay
|
|
|
|
switch account.type {
|
|
|
|
case .onMyMac:
|
|
|
|
cell.imageView?.image = AppImages.accountLocal
|
|
|
|
case .feedbin:
|
|
|
|
cell.imageView?.image = NSImage(named: "accountFeedbin")
|
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
2019-03-24 20:33:43 +01:00
|
|
|
return cell
|
|
|
|
}
|
2019-04-30 13:38:18 +02:00
|
|
|
return nil
|
2019-03-24 20:33:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func tableViewSelectionDidChange(_ notification: Notification) {
|
2019-04-30 13:38:18 +02:00
|
|
|
|
|
|
|
let selectedRow = tableView.selectedRow
|
2019-05-02 01:26:23 +02:00
|
|
|
if tableView.selectedRow == -1 {
|
|
|
|
deleteButton.isEnabled = false
|
2019-04-30 13:38:18 +02:00
|
|
|
return
|
2019-05-02 01:26:23 +02:00
|
|
|
} else {
|
|
|
|
deleteButton.isEnabled = true
|
2019-04-30 13:38:18 +02:00
|
|
|
}
|
2019-05-02 01:26:23 +02:00
|
|
|
|
2019-04-30 13:38:18 +02:00
|
|
|
let account = sortedAccounts[selectedRow]
|
2019-05-02 01:26:23 +02:00
|
|
|
if AccountManager.shared.defaultAccount == account {
|
|
|
|
deleteButton.isEnabled = false
|
|
|
|
}
|
2019-04-30 13:38:18 +02:00
|
|
|
|
2019-05-01 18:05:55 +02:00
|
|
|
let controller = AccountsDetailViewController(account: account)
|
2019-05-01 17:28:13 +02:00
|
|
|
showController(controller)
|
|
|
|
|
2019-03-24 20:33:43 +01:00
|
|
|
}
|
2019-04-30 13:38:18 +02:00
|
|
|
|
2019-03-24 20:33:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Private
|
|
|
|
|
|
|
|
private extension AccountsPreferencesViewController {
|
|
|
|
|
|
|
|
func updateSortedAccounts() {
|
|
|
|
sortedAccounts = AccountManager.shared.sortedAccounts
|
2019-03-20 06:41:09 +01:00
|
|
|
}
|
2019-04-30 13:38:18 +02:00
|
|
|
|
2019-05-01 17:28:13 +02:00
|
|
|
func showController(_ controller: NSViewController) {
|
|
|
|
|
2019-04-30 13:38:18 +02:00
|
|
|
if let controller = children.first {
|
|
|
|
children.removeAll()
|
|
|
|
controller.view.removeFromSuperview()
|
|
|
|
}
|
2019-05-01 17:28:13 +02:00
|
|
|
|
|
|
|
addChild(controller)
|
|
|
|
controller.view.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
detailView.addSubview(controller.view)
|
|
|
|
detailView.rs_addFullSizeConstraints(forSubview: controller.view)
|
|
|
|
|
2019-04-30 13:38:18 +02:00
|
|
|
}
|
|
|
|
|
2019-03-20 06:41:09 +01:00
|
|
|
}
|