2019-10-21 18:51:33 +02:00
|
|
|
//
|
|
|
|
// AddAccountViewController.swift
|
|
|
|
// NetNewsWire-iOS
|
|
|
|
//
|
|
|
|
// Created by Maurice Parker on 5/16/19.
|
|
|
|
// Copyright © 2019 Ranchero Software. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Account
|
|
|
|
import UIKit
|
2020-01-16 07:10:06 +01:00
|
|
|
import RSCore
|
2019-10-21 18:51:33 +02:00
|
|
|
|
|
|
|
protocol AddAccountDismissDelegate: UIViewController {
|
|
|
|
func dismiss()
|
|
|
|
}
|
|
|
|
|
|
|
|
class AddAccountViewController: UITableViewController, AddAccountDismissDelegate {
|
|
|
|
|
2020-03-28 22:51:41 +01:00
|
|
|
#if DEBUG
|
2020-04-01 14:50:11 +02:00
|
|
|
private var addableAccountTypes: [AccountType] = [.onMyMac, .feedbin, .feedly, .feedWrangler, .cloudKit, .newsBlur]
|
2020-03-28 22:51:41 +01:00
|
|
|
#else
|
2020-04-06 22:37:26 +02:00
|
|
|
private var addableAccountTypes: [AccountType] = [.onMyMac, .feedbin, .feedly, .cloudKit, .newsBlur]
|
2020-03-28 22:51:41 +01:00
|
|
|
#endif
|
|
|
|
|
2019-10-21 18:51:33 +02:00
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
2020-03-28 22:51:41 +01:00
|
|
|
restrictAccounts()
|
|
|
|
}
|
|
|
|
|
|
|
|
override func numberOfSections(in tableView: UITableView) -> Int {
|
|
|
|
1
|
2019-10-21 18:51:33 +02:00
|
|
|
}
|
|
|
|
|
2019-11-25 16:30:14 +01:00
|
|
|
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
2020-03-28 22:51:41 +01:00
|
|
|
return addableAccountTypes.count
|
|
|
|
}
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
|
|
|
|
return 52.0
|
|
|
|
}
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
2020-04-16 15:25:40 +02:00
|
|
|
let cell = tableView.dequeueReusableCell(withIdentifier: "SettingsAccountTableViewCell", for: indexPath) as! SettingsComboTableViewCell
|
2020-03-28 22:51:41 +01:00
|
|
|
|
|
|
|
switch addableAccountTypes[indexPath.row] {
|
|
|
|
case .onMyMac:
|
2020-04-16 15:25:40 +02:00
|
|
|
cell.comboNameLabel?.text = Account.defaultLocalAccountName
|
|
|
|
cell.comboImage?.image = AppAssets.image(for: .onMyMac)
|
2020-03-28 23:51:14 +01:00
|
|
|
case .cloudKit:
|
2020-04-16 15:25:40 +02:00
|
|
|
cell.comboNameLabel?.text = NSLocalizedString("iCloud", comment: "iCloud")
|
|
|
|
cell.comboImage?.image = AppAssets.accountCloudKitImage
|
2020-03-28 22:51:41 +01:00
|
|
|
case .feedbin:
|
2020-04-16 15:25:40 +02:00
|
|
|
cell.comboNameLabel?.text = NSLocalizedString("Feedbin", comment: "Feedbin")
|
|
|
|
cell.comboImage?.image = AppAssets.accountFeedbinImage
|
2020-03-28 22:51:41 +01:00
|
|
|
case .feedWrangler:
|
2020-04-16 15:25:40 +02:00
|
|
|
cell.comboNameLabel?.text = NSLocalizedString("Feed Wrangler", comment: "Feed Wrangler")
|
|
|
|
cell.comboImage?.image = AppAssets.accountFeedWranglerImage
|
2020-03-28 22:51:41 +01:00
|
|
|
case .feedly:
|
2020-04-16 15:25:40 +02:00
|
|
|
cell.comboNameLabel?.text = NSLocalizedString("Feedly", comment: "Feedly")
|
|
|
|
cell.comboImage?.image = AppAssets.accountFeedlyImage
|
2020-03-28 22:51:41 +01:00
|
|
|
case .newsBlur:
|
2020-04-16 15:25:40 +02:00
|
|
|
cell.comboNameLabel?.text = NSLocalizedString("NewsBlur", comment: "NewsBlur")
|
|
|
|
cell.comboImage?.image = AppAssets.accountNewsBlurImage
|
2020-03-28 22:51:41 +01:00
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
return cell
|
2019-11-25 16:30:14 +01:00
|
|
|
}
|
|
|
|
|
2019-10-21 18:51:33 +02:00
|
|
|
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
2020-03-28 22:51:41 +01:00
|
|
|
switch addableAccountTypes[indexPath.row] {
|
|
|
|
case .onMyMac:
|
2020-03-28 23:51:14 +01:00
|
|
|
let navController = UIStoryboard.account.instantiateViewController(withIdentifier: "LocalAccountNavigationViewController") as! UINavigationController
|
2019-10-21 18:51:33 +02:00
|
|
|
navController.modalPresentationStyle = .currentContext
|
2019-10-24 02:58:18 +02:00
|
|
|
let addViewController = navController.topViewController as! LocalAccountViewController
|
2019-10-21 18:51:33 +02:00
|
|
|
addViewController.delegate = self
|
|
|
|
present(navController, animated: true)
|
2020-03-28 23:51:14 +01:00
|
|
|
case .cloudKit:
|
|
|
|
let navController = UIStoryboard.account.instantiateViewController(withIdentifier: "CloudKitAccountNavigationViewController") as! UINavigationController
|
|
|
|
navController.modalPresentationStyle = .currentContext
|
|
|
|
let addViewController = navController.topViewController as! CloudKitAccountViewController
|
|
|
|
addViewController.delegate = self
|
|
|
|
present(navController, animated: true)
|
2020-03-28 22:51:41 +01:00
|
|
|
case .feedbin:
|
2019-10-24 02:58:18 +02:00
|
|
|
let navController = UIStoryboard.account.instantiateViewController(withIdentifier: "FeedbinAccountNavigationViewController") as! UINavigationController
|
2019-10-21 18:51:33 +02:00
|
|
|
navController.modalPresentationStyle = .currentContext
|
|
|
|
let addViewController = navController.topViewController as! FeedbinAccountViewController
|
|
|
|
addViewController.delegate = self
|
|
|
|
present(navController, animated: true)
|
2020-03-28 22:51:41 +01:00
|
|
|
case .feedly:
|
2019-11-10 22:12:54 +01:00
|
|
|
let addAccount = OAuthAccountAuthorizationOperation(accountType: .feedly)
|
|
|
|
addAccount.delegate = self
|
|
|
|
addAccount.presentationAnchor = self.view.window!
|
2020-02-06 06:23:23 +01:00
|
|
|
MainThreadOperationQueue.shared.add(addAccount)
|
2020-03-28 22:51:41 +01:00
|
|
|
case .feedWrangler:
|
2019-11-25 06:26:03 +01:00
|
|
|
let navController = UIStoryboard.account.instantiateViewController(withIdentifier: "FeedWranglerAccountNavigationViewController") as! UINavigationController
|
|
|
|
navController.modalPresentationStyle = .currentContext
|
|
|
|
let addViewController = navController.topViewController as! FeedWranglerAccountViewController
|
|
|
|
addViewController.delegate = self
|
|
|
|
present(navController, animated: true)
|
2020-03-28 22:51:41 +01:00
|
|
|
case .newsBlur:
|
2020-03-09 23:39:58 +01:00
|
|
|
let navController = UIStoryboard.account.instantiateViewController(withIdentifier: "NewsBlurAccountNavigationViewController") as! UINavigationController
|
|
|
|
navController.modalPresentationStyle = .currentContext
|
|
|
|
let addViewController = navController.topViewController as! NewsBlurAccountViewController
|
|
|
|
addViewController.delegate = self
|
|
|
|
present(navController, animated: true)
|
2019-10-21 18:51:33 +02:00
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func dismiss() {
|
|
|
|
navigationController?.popViewController(animated: false)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-11-10 22:12:54 +01:00
|
|
|
|
|
|
|
extension AddAccountViewController: OAuthAccountAuthorizationOperationDelegate {
|
|
|
|
|
|
|
|
func oauthAccountAuthorizationOperation(_ operation: OAuthAccountAuthorizationOperation, didCreate account: Account) {
|
|
|
|
let rootViewController = view.window?.rootViewController
|
|
|
|
|
|
|
|
account.refreshAll { result in
|
|
|
|
switch result {
|
|
|
|
case .success:
|
|
|
|
break
|
|
|
|
case .failure(let error):
|
|
|
|
guard let viewController = rootViewController else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
viewController.presentError(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dismiss()
|
|
|
|
}
|
|
|
|
|
|
|
|
func oauthAccountAuthorizationOperation(_ operation: OAuthAccountAuthorizationOperation, didFailWith error: Error) {
|
|
|
|
presentError(error)
|
|
|
|
}
|
|
|
|
}
|
2020-03-28 22:51:41 +01:00
|
|
|
|
|
|
|
// MARK: Private
|
|
|
|
|
|
|
|
private extension AddAccountViewController {
|
|
|
|
|
|
|
|
func restrictAccounts() {
|
|
|
|
func removeAccountType(_ accountType: AccountType) {
|
|
|
|
if let index = addableAccountTypes.firstIndex(of: accountType) {
|
|
|
|
addableAccountTypes.remove(at: index)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-02 04:47:45 +02:00
|
|
|
if AppDefaults.shared.isDeveloperBuild {
|
2020-03-28 22:51:41 +01:00
|
|
|
removeAccountType(.cloudKit)
|
|
|
|
removeAccountType(.feedly)
|
|
|
|
removeAccountType(.feedWrangler)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if AccountManager.shared.activeAccounts.firstIndex(where: { $0.type == .cloudKit }) != nil {
|
|
|
|
removeAccountType(.cloudKit)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|