2019-05-01 17:28:13 +02:00
|
|
|
//
|
2019-05-01 18:05:55 +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
|
2019-05-19 23:52:21 +02:00
|
|
|
import Account
|
2020-01-16 07:36:46 +01:00
|
|
|
import RSCore
|
2019-05-01 17:28:13 +02:00
|
|
|
|
2019-05-01 18:05:55 +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?
|
|
|
|
|
2020-03-18 21:48:44 +01:00
|
|
|
#if DEBUG
|
2020-04-01 14:50:11 +02:00
|
|
|
private var addableAccountTypes: [AccountType] = [.onMyMac, .feedbin, .feedly, .feedWrangler, .freshRSS, .cloudKit, .newsBlur]
|
2020-03-18 21:48:44 +01:00
|
|
|
#else
|
2020-07-10 23:13:42 +02:00
|
|
|
private var addableAccountTypes: [AccountType] = [.onMyMac, .feedbin, .feedly]
|
2020-03-18 21:48:44 +01:00
|
|
|
#endif
|
2019-08-30 07:17:25 +02:00
|
|
|
|
2019-05-01 17:28:13 +02:00
|
|
|
init() {
|
2019-05-01 18:05:55 +02:00
|
|
|
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() {
|
|
|
|
super.viewDidLoad()
|
|
|
|
tableView.dataSource = self
|
|
|
|
tableView.delegate = self
|
2020-03-28 22:51:41 +01:00
|
|
|
restrictAccounts()
|
2019-05-01 19:37:13 +02:00
|
|
|
}
|
|
|
|
|
2019-05-01 17:28:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - NSTableViewDataSource
|
|
|
|
|
2019-05-01 18:05:55 +02:00
|
|
|
extension AccountsAddViewController: NSTableViewDataSource {
|
2019-05-01 17:28:13 +02:00
|
|
|
|
|
|
|
func numberOfRows(in tableView: NSTableView) -> Int {
|
2019-08-30 07:17:25 +02:00
|
|
|
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
|
|
|
|
|
2019-05-01 18:05:55 +02:00
|
|
|
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? {
|
|
|
|
|
2019-05-01 18:05:55 +02:00
|
|
|
if let cell = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "Cell"), owner: nil) as? AccountsAddTableCellView {
|
2020-09-08 20:57:31 +02:00
|
|
|
|
|
|
|
cell.accountType = addableAccountTypes[row]
|
|
|
|
cell.delegate = self
|
|
|
|
|
2019-08-30 07:17:25 +02:00
|
|
|
switch addableAccountTypes[row] {
|
|
|
|
case .onMyMac:
|
2019-05-19 23:52:21 +02:00
|
|
|
cell.accountNameLabel?.stringValue = Account.defaultLocalAccountName
|
2019-05-21 12:42:40 +02:00
|
|
|
cell.accountImageView?.image = AppAssets.accountLocal
|
2020-03-18 21:48:44 +01:00
|
|
|
case .cloudKit:
|
|
|
|
cell.accountNameLabel?.stringValue = NSLocalizedString("iCloud", comment: "iCloud")
|
|
|
|
cell.accountImageView?.image = AppAssets.accountCloudKit
|
2019-08-30 07:17:25 +02:00
|
|
|
case .feedbin:
|
2019-05-01 17:28:13 +02:00
|
|
|
cell.accountNameLabel?.stringValue = NSLocalizedString("Feedbin", comment: "Feedbin")
|
2019-05-21 12:42:40 +02:00
|
|
|
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
|
2019-08-30 07:17:25 +02:00
|
|
|
case .freshRSS:
|
2019-06-20 14:22:51 +02:00
|
|
|
cell.accountNameLabel?.stringValue = NSLocalizedString("FreshRSS", comment: "FreshRSS")
|
|
|
|
cell.accountImageView?.image = AppAssets.accountFreshRSS
|
2019-09-18 01:18:06 +02:00
|
|
|
case .feedly:
|
|
|
|
cell.accountNameLabel?.stringValue = NSLocalizedString("Feedly", comment: "Feedly")
|
2019-09-20 21:47:40 +02:00
|
|
|
cell.accountImageView?.image = AppAssets.accountFeedly
|
2020-03-23 00:52:48 +01:00
|
|
|
case .newsBlur:
|
|
|
|
cell.accountNameLabel?.stringValue = NSLocalizedString("NewsBlur", comment: "NewsBlur")
|
|
|
|
cell.accountImageView?.image = AppAssets.accountNewsBlur
|
2019-05-01 17:28:13 +02:00
|
|
|
}
|
|
|
|
return cell
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-08 20:57:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: AccountsAddTableCellViewDelegate
|
2019-05-01 19:37:13 +02:00
|
|
|
|
2020-09-08 20:57:31 +02:00
|
|
|
extension AccountsAddViewController: AccountsAddTableCellViewDelegate {
|
|
|
|
|
|
|
|
func addAccount(_ accountType: AccountType) {
|
|
|
|
|
|
|
|
switch accountType {
|
2019-08-30 07:17:25 +02:00
|
|
|
case .onMyMac:
|
2019-05-01 19:37:13 +02:00
|
|
|
let accountsAddLocalWindowController = AccountsAddLocalWindowController()
|
|
|
|
accountsAddLocalWindowController.runSheetOnWindow(self.view.window!)
|
|
|
|
accountsAddWindowController = accountsAddLocalWindowController
|
2020-09-23 06:20:53 +02:00
|
|
|
|
2020-03-18 21:48:44 +01:00
|
|
|
case .cloudKit:
|
|
|
|
let accountsAddCloudKitWindowController = AccountsAddCloudKitWindowController()
|
|
|
|
accountsAddCloudKitWindowController.runSheetOnWindow(self.view.window!) { response in
|
|
|
|
if response == NSApplication.ModalResponse.OK {
|
2020-03-28 22:51:41 +01:00
|
|
|
self.restrictAccounts()
|
2020-03-18 21:48:44 +01:00
|
|
|
self.tableView.reloadData()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
accountsAddWindowController = accountsAddCloudKitWindowController
|
2020-09-23 06:20:53 +02:00
|
|
|
|
2019-08-30 07:17:25 +02:00
|
|
|
case .feedbin:
|
2019-05-04 23:10:58 +02:00
|
|
|
let accountsFeedbinWindowController = AccountsFeedbinWindowController()
|
|
|
|
accountsFeedbinWindowController.runSheetOnWindow(self.view.window!)
|
|
|
|
accountsAddWindowController = accountsFeedbinWindowController
|
2020-09-23 06:20:53 +02:00
|
|
|
|
2019-09-28 06:44:58 +02:00
|
|
|
case .feedWrangler:
|
|
|
|
let accountsFeedWranglerWindowController = AccountsFeedWranglerWindowController()
|
|
|
|
accountsFeedWranglerWindowController.runSheetOnWindow(self.view.window!)
|
|
|
|
accountsAddWindowController = accountsFeedWranglerWindowController
|
2020-09-23 06:20:53 +02:00
|
|
|
|
2019-08-30 07:17:25 +02:00
|
|
|
case .freshRSS:
|
2019-06-19 18:25:37 +02:00
|
|
|
let accountsReaderAPIWindowController = AccountsReaderAPIWindowController()
|
2019-06-20 14:22:51 +02:00
|
|
|
accountsReaderAPIWindowController.accountType = .freshRSS
|
2019-06-19 18:25:37 +02:00
|
|
|
accountsReaderAPIWindowController.runSheetOnWindow(self.view.window!)
|
|
|
|
accountsAddWindowController = accountsReaderAPIWindowController
|
2020-09-23 06:20:53 +02:00
|
|
|
|
2019-09-18 01:18:06 +02:00
|
|
|
case .feedly:
|
2019-11-08 08:35:22 +01:00
|
|
|
let addAccount = OAuthAccountAuthorizationOperation(accountType: .feedly)
|
2019-11-07 23:51:59 +01:00
|
|
|
addAccount.delegate = self
|
|
|
|
addAccount.presentationAnchor = self.view.window!
|
2020-09-23 06:20:53 +02:00
|
|
|
|
|
|
|
runAwaitingFeedlyLoginAlertModal(forLifetimeOf: addAccount)
|
|
|
|
|
2020-02-06 07:30:05 +01:00
|
|
|
MainThreadOperationQueue.shared.add(addAccount)
|
2020-09-23 06:20:53 +02:00
|
|
|
|
2020-03-23 00:52:48 +01:00
|
|
|
case .newsBlur:
|
|
|
|
let accountsNewsBlurWindowController = AccountsNewsBlurWindowController()
|
|
|
|
accountsNewsBlurWindowController.runSheetOnWindow(self.view.window!)
|
|
|
|
accountsAddWindowController = accountsNewsBlurWindowController
|
2019-05-01 19:37:13 +02:00
|
|
|
}
|
|
|
|
|
2019-05-01 17:28:13 +02:00
|
|
|
}
|
|
|
|
|
2020-09-23 06:20:53 +02:00
|
|
|
private func runAwaitingFeedlyLoginAlertModal(forLifetimeOf operation: OAuthAccountAuthorizationOperation) {
|
|
|
|
let alert = NSAlert()
|
|
|
|
alert.alertStyle = .informational
|
|
|
|
alert.messageText = NSLocalizedString("Waiting for access to Feedly",
|
|
|
|
comment: "Alert title when adding a Feedly account and waiting for authorization from the user.")
|
|
|
|
|
|
|
|
alert.informativeText = NSLocalizedString("Your default web browser will open the Feedly login for you to authorize access.",
|
|
|
|
comment: "Alert informative text when adding a Feedly account and waiting for authorization from the user.")
|
|
|
|
|
|
|
|
alert.addButton(withTitle: NSLocalizedString("Cancel", comment: "Cancel"))
|
|
|
|
|
|
|
|
let attachedWindow = self.view.window!
|
|
|
|
|
|
|
|
alert.beginSheetModal(for: attachedWindow) { response in
|
|
|
|
if response == .alertFirstButtonReturn {
|
|
|
|
operation.cancel()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
operation.completionBlock = { _ in
|
|
|
|
guard alert.window.isVisible else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
attachedWindow.endSheet(alert.window)
|
|
|
|
}
|
|
|
|
}
|
2019-05-01 17:28:13 +02:00
|
|
|
}
|
2019-11-07 23:51:59 +01:00
|
|
|
|
|
|
|
// MARK: OAuthAccountAuthorizationOperationDelegate
|
|
|
|
|
|
|
|
extension AccountsAddViewController: OAuthAccountAuthorizationOperationDelegate {
|
|
|
|
|
2019-11-10 22:10:39 +01:00
|
|
|
func oauthAccountAuthorizationOperation(_ operation: OAuthAccountAuthorizationOperation, didCreate account: Account) {
|
2020-09-22 01:49:59 +02:00
|
|
|
// `OAuthAccountAuthorizationOperation` is using `ASWebAuthenticationSession` which bounces the user
|
|
|
|
// to their browser on macOS for authorizing NetNewsWire to access the user's Feedly account.
|
|
|
|
// When this authorization is granted, the browser remains the foreground app which is unfortunate
|
|
|
|
// because the user probably wants to see the result of authorizing NetNewsWire to act on their behalf.
|
|
|
|
NSApp.activate(ignoringOtherApps: true)
|
|
|
|
|
2019-11-10 22:10:39 +01:00
|
|
|
account.refreshAll { [weak self] result in
|
|
|
|
switch result {
|
|
|
|
case .success:
|
|
|
|
break
|
|
|
|
case .failure(let error):
|
|
|
|
self?.presentError(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-07 23:51:59 +01:00
|
|
|
func oauthAccountAuthorizationOperation(_ operation: OAuthAccountAuthorizationOperation, didFailWith error: Error) {
|
2020-09-22 01:49:59 +02:00
|
|
|
// `OAuthAccountAuthorizationOperation` is using `ASWebAuthenticationSession` which bounces the user
|
|
|
|
// to their browser on macOS for authorizing NetNewsWire to access the user's Feedly account.
|
|
|
|
NSApp.activate(ignoringOtherApps: true)
|
|
|
|
|
2019-11-07 23:51:59 +01:00
|
|
|
view.window?.presentError(error)
|
|
|
|
}
|
|
|
|
}
|
2020-03-18 21:48:44 +01:00
|
|
|
|
|
|
|
// MARK: Private
|
|
|
|
|
|
|
|
private extension AccountsAddViewController {
|
2020-03-25 22:09:01 +01:00
|
|
|
|
2020-03-28 22:51:41 +01:00
|
|
|
func restrictAccounts() {
|
|
|
|
func removeAccountType(_ accountType: AccountType) {
|
|
|
|
if let index = addableAccountTypes.firstIndex(of: accountType) {
|
|
|
|
addableAccountTypes.remove(at: index)
|
2020-03-25 22:09:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-02 05:17:38 +02:00
|
|
|
if AppDefaults.shared.isDeveloperBuild {
|
2020-03-28 22:51:41 +01:00
|
|
|
removeAccountType(.cloudKit)
|
|
|
|
removeAccountType(.feedly)
|
|
|
|
removeAccountType(.feedWrangler)
|
2020-03-25 22:09:01 +01:00
|
|
|
return
|
|
|
|
}
|
2020-03-28 22:51:41 +01:00
|
|
|
|
|
|
|
if AccountManager.shared.activeAccounts.firstIndex(where: { $0.type == .cloudKit }) != nil {
|
|
|
|
removeAccountType(.cloudKit)
|
2020-03-18 21:48:44 +01:00
|
|
|
}
|
|
|
|
}
|
2020-03-25 22:09:01 +01:00
|
|
|
|
2020-03-18 21:48:44 +01:00
|
|
|
}
|