NetNewsWire/Mac/Preferences/Accounts/AccountsPreferencesViewController.swift

163 lines
4.6 KiB
Swift
Raw Normal View History

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.
//
import AppKit
import Account
final class AccountsPreferencesViewController: NSViewController {
@IBOutlet weak var tableView: NSTableView!
@IBOutlet weak var detailView: NSView!
2019-05-02 01:26:23 +02:00
@IBOutlet weak var deleteButton: NSButton!
private var sortedAccounts = [Account]()
override func viewDidLoad() {
super.viewDidLoad()
2019-05-01 17:28:13 +02:00
updateSortedAccounts()
tableView.delegate = self
tableView.dataSource = self
NotificationCenter.default.addObserver(self, selector: #selector(displayNameDidChange(_:)), name: .DisplayNameDidChange, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(accountsDidChangeNotification(_:)), name: .AccountsDidChange, object: nil)
2019-05-01 19:37:13 +02:00
showController(AccountsAddViewController())
2019-05-01 17:28:13 +02:00
// Fix tableView frame  for some reason IB wants it 1pt wider than the clip view. This leads to unwanted horizontal scrolling.
var rTable = tableView.frame
rTable.size.width = tableView.superview!.frame.size.width
tableView.frame = rTable
}
2019-05-01 17:28:13 +02:00
@IBAction func addAccount(_ sender: Any) {
tableView.selectRowIndexes([], byExtendingSelection: false)
showController(AccountsAddViewController())
}
2019-05-01 17:28:13 +02:00
@IBAction func removeAccount(_ sender: Any) {
2019-05-02 13:57:45 +02:00
2019-05-02 01:26:23 +02:00
guard tableView.selectedRow != -1 else {
return
}
2019-05-02 13:57:45 +02:00
let acctName = sortedAccounts[tableView.selectedRow].nameForDisplay
let alert = NSAlert()
alert.alertStyle = .warning
let deletePrompt = NSLocalizedString("Delete", comment: "Delete")
alert.messageText = "\(deletePrompt) \"\(acctName)\"?"
alert.informativeText = NSLocalizedString("Are you sure you want to delete the account \"\(acctName)\"? This can not be undone.", comment: "Delete text")
alert.addButton(withTitle: NSLocalizedString("Delete", comment: "Delete Account"))
alert.addButton(withTitle: NSLocalizedString("Cancel", comment: "Cancel Delete Account"))
alert.beginSheetModal(for: view.window!) { [weak self] result in
if result == NSApplication.ModalResponse.alertFirstButtonReturn {
guard let self = self else { return }
AccountManager.shared.deleteAccount(self.sortedAccounts[self.tableView.selectedRow])
self.showController(AccountsAddViewController())
2019-05-02 13:57:45 +02:00
}
}
}
@objc func displayNameDidChange(_ note: Notification) {
2019-05-01 17:28:13 +02:00
updateSortedAccounts()
tableView.reloadData()
}
2019-05-01 19:37:13 +02:00
@objc func accountsDidChangeNotification(_ note: Notification) {
updateSortedAccounts()
tableView.reloadData()
}
}
// 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? {
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 = AppAssets.accountLocal
2019-05-01 13:49:50 +02:00
case .feedbin:
cell.imageView?.image = NSImage(named: "accountFeedbin")
default:
break
}
return cell
}
return nil
}
func tableViewSelectionDidChange(_ notification: Notification) {
let selectedRow = tableView.selectedRow
2019-05-02 01:26:23 +02:00
if tableView.selectedRow == -1 {
deleteButton.isEnabled = false
return
2019-05-02 01:26:23 +02:00
} else {
deleteButton.isEnabled = true
}
2019-05-02 01:26:23 +02:00
let account = sortedAccounts[selectedRow]
2019-05-02 01:26:23 +02:00
if AccountManager.shared.defaultAccount == account {
deleteButton.isEnabled = false
}
let controller = AccountsDetailViewController(account: account)
2019-05-01 17:28:13 +02:00
showController(controller)
}
}
// MARK: - Private
private extension AccountsPreferencesViewController {
func updateSortedAccounts() {
sortedAccounts = AccountManager.shared.sortedAccounts
}
2019-05-01 17:28:13 +02:00
func showController(_ controller: NSViewController) {
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)
}
}