NetNewsWire/Mac/Preferences/Accounts/AccountsAddViewController.s...

145 lines
4.5 KiB
Swift
Raw Normal View History

2019-05-01 17:28:13 +02:00
//
// AccountsAddViewController.swift
2019-05-01 17:28:13 +02:00
// NetNewsWire
//
// Created by Maurice Parker on 5/1/19.
// Copyright © 2019 Ranchero Software. All rights reserved.
//
2019-05-01 19:37:13 +02:00
import AppKit
import Account
2020-01-16 07:36:46 +01:00
import RSCore
2019-05-01 17:28:13 +02:00
class AccountsAddViewController: NSViewController {
2019-05-01 17:28:13 +02:00
@IBOutlet weak var tableView: NSTableView!
2019-05-01 19:37:13 +02:00
private var accountsAddWindowController: NSWindowController?
2019-11-21 18:28:08 +01:00
private let addableAccountTypes: [AccountType] = [.onMyMac, .feedbin, .feedly, .feedWrangler, .freshRSS]
2019-05-01 17:28:13 +02:00
init() {
super.init(nibName: "AccountsAdd", bundle: nil)
2019-05-01 17:28:13 +02:00
}
public required init?(coder: NSCoder) {
super.init(coder: coder)
}
override func viewDidLoad() {
2019-05-01 19:37:13 +02:00
2019-05-01 17:28:13 +02:00
super.viewDidLoad()
2019-05-01 19:37:13 +02:00
2019-05-01 17:28:13 +02:00
tableView.dataSource = self
tableView.delegate = self
2019-05-01 19:37:13 +02:00
}
2019-05-01 17:28:13 +02:00
}
// MARK: - NSTableViewDataSource
extension AccountsAddViewController: NSTableViewDataSource {
2019-05-01 17:28:13 +02:00
func numberOfRows(in tableView: NSTableView) -> Int {
return addableAccountTypes.count
2019-05-01 17:28:13 +02:00
}
func tableView(_ tableView: NSTableView, objectValueFor tableColumn: NSTableColumn?, row: Int) -> Any? {
return nil
}
}
// MARK: - NSTableViewDelegate
extension AccountsAddViewController: NSTableViewDelegate {
2019-05-01 17:28:13 +02:00
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? AccountsAddTableCellView {
switch addableAccountTypes[row] {
case .onMyMac:
cell.accountNameLabel?.stringValue = Account.defaultLocalAccountName
cell.accountImageView?.image = AppAssets.accountLocal
case .feedbin:
2019-05-01 17:28:13 +02:00
cell.accountNameLabel?.stringValue = NSLocalizedString("Feedbin", comment: "Feedbin")
cell.accountImageView?.image = AppAssets.accountFeedbin
2019-09-28 06:44:58 +02:00
case .feedWrangler:
cell.accountNameLabel?.stringValue = NSLocalizedString("Feed Wrangler", comment: "Feed Wrangler")
cell.accountImageView?.image = AppAssets.accountFeedWrangler
case .freshRSS:
2019-06-20 14:22:51 +02:00
cell.accountNameLabel?.stringValue = NSLocalizedString("FreshRSS", comment: "FreshRSS")
cell.accountImageView?.image = AppAssets.accountFreshRSS
case .feedly:
cell.accountNameLabel?.stringValue = NSLocalizedString("Feedly", comment: "Feedly")
2019-09-20 21:47:40 +02:00
cell.accountImageView?.image = AppAssets.accountFeedly
2019-05-01 17:28:13 +02:00
default:
break
}
return cell
}
return nil
}
func tableViewSelectionDidChange(_ notification: Notification) {
2019-05-01 19:37:13 +02:00
let selectedRow = tableView.selectedRow
guard selectedRow != -1 else {
return
}
switch addableAccountTypes[selectedRow] {
case .onMyMac:
2019-05-01 19:37:13 +02:00
let accountsAddLocalWindowController = AccountsAddLocalWindowController()
accountsAddLocalWindowController.runSheetOnWindow(self.view.window!)
accountsAddWindowController = accountsAddLocalWindowController
case .feedbin:
2019-05-04 23:10:58 +02:00
let accountsFeedbinWindowController = AccountsFeedbinWindowController()
accountsFeedbinWindowController.runSheetOnWindow(self.view.window!)
accountsAddWindowController = accountsFeedbinWindowController
2019-09-28 06:44:58 +02:00
case .feedWrangler:
let accountsFeedWranglerWindowController = AccountsFeedWranglerWindowController()
accountsFeedWranglerWindowController.runSheetOnWindow(self.view.window!)
accountsAddWindowController = accountsFeedWranglerWindowController
case .freshRSS:
let accountsReaderAPIWindowController = AccountsReaderAPIWindowController()
2019-06-20 14:22:51 +02:00
accountsReaderAPIWindowController.accountType = .freshRSS
accountsReaderAPIWindowController.runSheetOnWindow(self.view.window!)
accountsAddWindowController = accountsReaderAPIWindowController
case .feedly:
let addAccount = OAuthAccountAuthorizationOperation(accountType: .feedly)
addAccount.delegate = self
addAccount.presentationAnchor = self.view.window!
MainThreadOperationQueue.shared.add(addAccount)
2019-05-01 19:37:13 +02:00
default:
break
}
tableView.selectRowIndexes([], byExtendingSelection: false)
2019-05-01 17:28:13 +02:00
}
}
// MARK: OAuthAccountAuthorizationOperationDelegate
extension AccountsAddViewController: OAuthAccountAuthorizationOperationDelegate {
func oauthAccountAuthorizationOperation(_ operation: OAuthAccountAuthorizationOperation, didCreate account: Account) {
account.refreshAll { [weak self] result in
switch result {
case .success:
break
case .failure(let error):
self?.presentError(error)
}
}
}
func oauthAccountAuthorizationOperation(_ operation: OAuthAccountAuthorizationOperation, didFailWith error: Error) {
view.window?.presentError(error)
}
}