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-10-27 04:42:28 +01:00
|
|
|
private enum AddAccountSections: Int, CaseIterable {
|
|
|
|
case local = 0
|
|
|
|
case icloud
|
|
|
|
case web
|
|
|
|
case selfhosted
|
|
|
|
|
|
|
|
var sectionHeader: String {
|
|
|
|
switch self {
|
|
|
|
case .local:
|
|
|
|
return NSLocalizedString("Local", comment: "Local Account")
|
|
|
|
case .icloud:
|
|
|
|
return NSLocalizedString("iCloud", comment: "iCloud Account")
|
|
|
|
case .web:
|
|
|
|
return NSLocalizedString("Web", comment: "Web Account")
|
|
|
|
case .selfhosted:
|
|
|
|
return NSLocalizedString("Self-hosted", comment: "Self hosted Account")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var sectionFooter: String {
|
|
|
|
switch self {
|
|
|
|
case .local:
|
|
|
|
return NSLocalizedString("Local accounts do not sync your subscriptions across devices.", comment: "Local Account")
|
|
|
|
case .icloud:
|
|
|
|
return NSLocalizedString("Use your iCloud account to sync your subscriptions across your iOS and macOS devices.", comment: "iCloud Account")
|
|
|
|
case .web:
|
|
|
|
return NSLocalizedString("Web accounts sync your subscriptions across all your devices.", comment: "Web Account")
|
|
|
|
case .selfhosted:
|
|
|
|
return NSLocalizedString("Self-hosted accounts sync your subscriptions across all your devices.", comment: "Self hosted Account")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var sectionContent: [AccountType] {
|
|
|
|
switch self {
|
|
|
|
case .local:
|
|
|
|
return [.onMyMac]
|
|
|
|
case .icloud:
|
|
|
|
return [.cloudKit]
|
|
|
|
case .web:
|
2020-11-12 22:48:25 +01:00
|
|
|
#if DEBUG
|
2020-10-27 04:42:28 +01:00
|
|
|
return [.bazQux, .feedbin, .feedly, .feedWrangler, .inoreader, .newsBlur, .theOldReader]
|
2020-11-12 22:48:25 +01:00
|
|
|
#else
|
|
|
|
return [.bazQux, .feedbin, .feedly, .inoreader, .newsBlur, .theOldReader]
|
|
|
|
#endif
|
2020-10-27 04:42:28 +01:00
|
|
|
case .selfhosted:
|
|
|
|
return [.freshRSS]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-21 18:51:33 +02:00
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
2020-03-28 22:51:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
override func numberOfSections(in tableView: UITableView) -> Int {
|
2020-10-27 04:42:28 +01:00
|
|
|
return AddAccountSections.allCases.count
|
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-10-27 04:42:28 +01:00
|
|
|
if section == AddAccountSections.local.rawValue {
|
|
|
|
return AddAccountSections.local.sectionContent.count
|
|
|
|
}
|
|
|
|
|
|
|
|
if section == AddAccountSections.icloud.rawValue {
|
|
|
|
return AddAccountSections.icloud.sectionContent.count
|
|
|
|
}
|
|
|
|
|
|
|
|
if section == AddAccountSections.web.rawValue {
|
|
|
|
return AddAccountSections.web.sectionContent.count
|
|
|
|
}
|
|
|
|
|
|
|
|
if section == AddAccountSections.selfhosted.rawValue {
|
|
|
|
return AddAccountSections.selfhosted.sectionContent.count
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
2020-03-28 22:51:41 +01:00
|
|
|
}
|
|
|
|
|
2020-10-27 04:42:28 +01:00
|
|
|
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
|
|
|
|
switch section {
|
|
|
|
case AddAccountSections.local.rawValue:
|
|
|
|
return AddAccountSections.local.sectionHeader
|
|
|
|
case AddAccountSections.icloud.rawValue:
|
|
|
|
return AddAccountSections.icloud.sectionHeader
|
|
|
|
case AddAccountSections.web.rawValue:
|
|
|
|
return AddAccountSections.web.sectionHeader
|
|
|
|
case AddAccountSections.selfhosted.rawValue:
|
|
|
|
return AddAccountSections.selfhosted.sectionHeader
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
|
|
|
|
switch section {
|
|
|
|
case AddAccountSections.local.rawValue:
|
|
|
|
return AddAccountSections.local.sectionFooter
|
|
|
|
case AddAccountSections.icloud.rawValue:
|
|
|
|
return AddAccountSections.icloud.sectionFooter
|
|
|
|
case AddAccountSections.web.rawValue:
|
|
|
|
return AddAccountSections.web.sectionFooter
|
|
|
|
case AddAccountSections.selfhosted.rawValue:
|
|
|
|
return AddAccountSections.selfhosted.sectionFooter
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
2020-03-28 22:51:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2020-10-27 04:42:28 +01:00
|
|
|
switch indexPath.section {
|
|
|
|
case AddAccountSections.local.rawValue:
|
|
|
|
cell.comboNameLabel?.text = AddAccountSections.local.sectionContent[indexPath.row].localizedAccountName()
|
2020-10-27 10:37:25 +01:00
|
|
|
cell.comboImage?.image = AppAssets.image(for: .onMyMac)
|
2020-10-27 04:42:28 +01:00
|
|
|
case AddAccountSections.icloud.rawValue:
|
|
|
|
cell.comboNameLabel?.text = AddAccountSections.icloud.sectionContent[indexPath.row].localizedAccountName()
|
2020-10-27 10:37:25 +01:00
|
|
|
cell.comboImage?.image = AppAssets.image(for: AddAccountSections.icloud.sectionContent[indexPath.row])
|
2020-10-27 21:08:34 +01:00
|
|
|
if AppDefaults.shared.isDeveloperBuild || AccountManager.shared.accounts.contains(where: { $0.type == .cloudKit }) {
|
2020-10-27 05:13:45 +01:00
|
|
|
cell.isUserInteractionEnabled = false
|
2020-10-27 21:08:34 +01:00
|
|
|
cell.comboNameLabel?.isEnabled = false
|
2020-10-27 05:13:45 +01:00
|
|
|
}
|
2020-10-27 04:42:28 +01:00
|
|
|
case AddAccountSections.web.rawValue:
|
|
|
|
cell.comboNameLabel?.text = AddAccountSections.web.sectionContent[indexPath.row].localizedAccountName()
|
2020-10-27 10:37:25 +01:00
|
|
|
cell.comboImage?.image = AppAssets.image(for: AddAccountSections.web.sectionContent[indexPath.row])
|
2020-10-27 05:13:45 +01:00
|
|
|
let type = AddAccountSections.web.sectionContent[indexPath.row]
|
2020-10-27 20:03:53 +01:00
|
|
|
if (type == .feedly || type == .feedWrangler || type == .inoreader) && AppDefaults.shared.isDeveloperBuild {
|
2020-10-27 05:13:45 +01:00
|
|
|
cell.isUserInteractionEnabled = false
|
2020-10-27 21:08:34 +01:00
|
|
|
cell.comboNameLabel?.isEnabled = false
|
2020-10-27 05:13:45 +01:00
|
|
|
}
|
2020-10-27 04:42:28 +01:00
|
|
|
case AddAccountSections.selfhosted.rawValue:
|
|
|
|
cell.comboNameLabel?.text = AddAccountSections.selfhosted.sectionContent[indexPath.row].localizedAccountName()
|
2020-10-27 10:37:25 +01:00
|
|
|
cell.comboImage?.image = AppAssets.image(for: AddAccountSections.selfhosted.sectionContent[indexPath.row])
|
2020-10-27 04:42:28 +01:00
|
|
|
|
|
|
|
default:
|
|
|
|
return cell
|
2020-03-28 22:51:41 +01:00
|
|
|
}
|
|
|
|
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-10-27 04:42:28 +01:00
|
|
|
|
|
|
|
switch indexPath.section {
|
|
|
|
case AddAccountSections.local.rawValue:
|
|
|
|
let type = AddAccountSections.local.sectionContent[indexPath.row]
|
|
|
|
presentController(for: type)
|
|
|
|
case AddAccountSections.icloud.rawValue:
|
|
|
|
let type = AddAccountSections.icloud.sectionContent[indexPath.row]
|
|
|
|
presentController(for: type)
|
|
|
|
case AddAccountSections.web.rawValue:
|
|
|
|
let type = AddAccountSections.web.sectionContent[indexPath.row]
|
|
|
|
presentController(for: type)
|
|
|
|
case AddAccountSections.selfhosted.rawValue:
|
|
|
|
let type = AddAccountSections.selfhosted.sectionContent[indexPath.row]
|
|
|
|
presentController(for: type)
|
|
|
|
default:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func presentController(for accountType: AccountType) {
|
|
|
|
switch accountType {
|
2020-03-28 22:51:41 +01:00
|
|
|
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)
|
2020-10-26 02:03:32 +01:00
|
|
|
case .bazQux, .inoreader, .freshRSS, .theOldReader:
|
|
|
|
let navController = UIStoryboard.account.instantiateViewController(withIdentifier: "ReaderAPIAccountNavigationViewController") as! UINavigationController
|
|
|
|
navController.modalPresentationStyle = .currentContext
|
|
|
|
let addViewController = navController.topViewController as! ReaderAPIAccountViewController
|
2020-10-27 04:42:28 +01:00
|
|
|
addViewController.accountType = accountType
|
2020-10-26 02:03:32 +01:00
|
|
|
addViewController.delegate = self
|
|
|
|
present(navController, animated: true)
|
2019-10-21 18:51:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|