111 lines
2.7 KiB
Swift
111 lines
2.7 KiB
Swift
//
|
|
// 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!
|
|
|
|
private var sortedAccounts = [Account]()
|
|
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
NotificationCenter.default.addObserver(self, selector: #selector(displayNameDidChange(_:)), name: .DisplayNameDidChange, object: nil)
|
|
}
|
|
|
|
override func viewWillAppear() {
|
|
updateSortedAccounts()
|
|
tableView.reloadData()
|
|
}
|
|
|
|
override func viewWillDisappear() {
|
|
super.viewWillDisappear()
|
|
resetContainerView()
|
|
}
|
|
|
|
@objc func displayNameDidChange(_ note: Notification) {
|
|
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 {
|
|
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
|
|
}
|
|
return cell
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func tableViewSelectionDidChange(_ notification: Notification) {
|
|
|
|
let selectedRow = tableView.selectedRow
|
|
guard selectedRow != -1 else {
|
|
return
|
|
}
|
|
|
|
let account = sortedAccounts[selectedRow]
|
|
|
|
let controller = AccountDetailViewController(account: account)
|
|
addChild(controller)
|
|
controller.view.translatesAutoresizingMaskIntoConstraints = false
|
|
detailView.addSubview(controller.view)
|
|
detailView.rs_addFullSizeConstraints(forSubview: controller.view)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// MARK: - Private
|
|
|
|
private extension AccountsPreferencesViewController {
|
|
|
|
func updateSortedAccounts() {
|
|
sortedAccounts = AccountManager.shared.sortedAccounts
|
|
}
|
|
|
|
func resetContainerView() {
|
|
if let controller = children.first {
|
|
children.removeAll()
|
|
controller.view.removeFromSuperview()
|
|
}
|
|
}
|
|
|
|
}
|