passepartout-apple/Passepartout/App/iOS/Scenes/Organizer/WizardProviderViewControlle...

248 lines
8.2 KiB
Swift
Raw Normal View History

2018-10-11 07:13:19 +00:00
//
// WizardProviderViewController.swift
2021-01-01 16:53:28 +00:00
// Passepartout
2018-10-11 07:13:19 +00:00
//
// Created by Davide De Rosa on 9/4/18.
2020-12-27 16:30:25 +00:00
// Copyright (c) 2021 Davide De Rosa. All rights reserved.
2018-10-11 07:13:19 +00:00
//
2018-11-03 21:33:30 +00:00
// https://github.com/passepartoutvpn
2018-10-11 07:13:19 +00:00
//
// This file is part of Passepartout.
//
// Passepartout is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Passepartout is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Passepartout. If not, see <http://www.gnu.org/licenses/>.
//
import UIKit
import SwiftyBeaver
import PassepartoutConstants
import PassepartoutCore
import ConvenienceUI
private let log = SwiftyBeaver.self
2018-10-11 07:13:19 +00:00
class WizardProviderViewController: UITableViewController, StrongTableHost {
private var available: [Infrastructure.Metadata] = []
2018-10-11 07:13:19 +00:00
private var createdProfile: ProviderConnectionProfile?
2019-12-04 13:16:50 +00:00
private var selectedMetadata: Infrastructure.Metadata?
// MARK: StrongTableHost
let model = StrongTableModel<SectionType, RowType>()
func reloadModel() {
available = TransientStore.shared.service.availableProviders()
model.clear()
model.add(.availableProviders)
model.add(.listActions)
model.set(.provider, count: available.count, forSection: .availableProviders)
model.set([.updateList], forSection: .listActions)
}
// MARK: UIViewController
deinit {
NotificationCenter.default.removeObserver(self)
}
2018-10-11 07:13:19 +00:00
override func viewDidLoad() {
super.viewDidLoad()
let nc = NotificationCenter.default
nc.addObserver(self, selector: #selector(didReloadReceipt), name: ProductManager.didReloadReceipt, object: nil)
2018-10-11 07:13:19 +00:00
title = L10n.Organizer.Sections.Providers.header
reloadModel()
2018-10-11 07:13:19 +00:00
}
private func tryNext(withMetadata metadata: Infrastructure.Metadata, purchaseIfNecessary: Bool) {
2019-12-04 13:16:50 +00:00
selectedMetadata = metadata
2021-01-07 22:37:32 +00:00
do {
try ProductManager.shared.verifyEligible(forProvider: metadata)
} catch ProductError.beta {
presentBetaFeatureUnavailable("Providers")
return
} catch {
guard purchaseIfNecessary else {
return
}
presentPurchaseScreen(forProduct: metadata.product, delegate: self)
return
}
// make sure that infrastructure exists locally
guard let _ = InfrastructureFactory.shared.infrastructure(forName: metadata.name) else {
let hud = HUD(view: view)
_ = InfrastructureFactory.shared.update(metadata.name, notBeforeInterval: nil) { [weak self] in
hud.hide()
guard let _ = $0 else {
self?.alertMissingInfrastructure(forMetadata: metadata, error: $1)
return
}
self?.next(withMetadata: metadata)
}
return
}
next(withMetadata: metadata)
}
private func next(withMetadata metadata: Infrastructure.Metadata) {
let profile = ProviderConnectionProfile(name: metadata.name)
2018-10-11 07:13:19 +00:00
createdProfile = profile
let accountVC = StoryboardScene.Main.accountIdentifier.instantiate()
guard let infrastructure = InfrastructureFactory.shared.infrastructure(forName: metadata.name) else {
fatalError("Moving to credentials with nil infrastructure, not downloaded properly?")
}
2018-10-11 07:13:19 +00:00
accountVC.usernamePlaceholder = infrastructure.defaults.username
accountVC.infrastructureName = infrastructure.name
accountVC.delegate = self
navigationController?.pushViewController(accountVC, animated: true)
}
private func alertMissingInfrastructure(forMetadata metadata: Infrastructure.Metadata, error: Error?) {
var message = L10n.Wizards.Provider.Alerts.Unavailable.message
2019-11-30 11:27:30 +00:00
if let error = error {
log.error("Unable to download missing \(metadata.description) infrastructure (network error): \(error.localizedDescription)")
2019-11-30 11:27:30 +00:00
message.append(" \(error.localizedDescription)")
} else {
log.error("Unable to download missing \(metadata.description) infrastructure (API error)")
2019-11-30 11:27:30 +00:00
}
let alert = UIAlertController.asAlert(metadata.description, message)
alert.addCancelAction(L10n.Global.ok)
present(alert, animated: true, completion: nil)
2019-11-30 11:27:30 +00:00
if let ip = tableView.indexPathForSelectedRow {
tableView.deselectRow(at: ip, animated: true)
}
}
2018-10-11 07:13:19 +00:00
private func finish(withCredentials credentials: Credentials) {
guard let profile = createdProfile else {
fatalError("No profile created?")
}
let service = TransientStore.shared.service
2018-10-11 07:13:19 +00:00
dismiss(animated: true) {
service.addOrReplaceProfile(profile, credentials: credentials)
2018-10-11 07:13:19 +00:00
}
}
private func updateProvidersList() {
let hud = HUD(view: view)
InfrastructureFactory.shared.updateIndex { [weak self] in
hud.hide()
if let error = $0 {
log.error("Unable to update providers list: \(error)")
return
}
self?.reloadModel()
self?.tableView.reloadData()
}
}
2018-10-11 07:13:19 +00:00
@IBAction private func close() {
dismiss(animated: true, completion: nil)
}
}
// MARK: -
extension WizardProviderViewController {
enum SectionType {
case availableProviders
case listActions
}
enum RowType {
case provider
case updateList
}
override func numberOfSections(in tableView: UITableView) -> Int {
return model.numberOfSections
}
2018-10-11 07:13:19 +00:00
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return model.numberOfRows(forSection: section)
2018-10-11 07:13:19 +00:00
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let row = model.row(at: indexPath)
2018-10-11 07:13:19 +00:00
let cell = Cells.setting.dequeue(from: tableView, for: indexPath)
switch row {
case .provider:
let metadata = available[indexPath.row]
cell.apply(.current)
cell.imageView?.image = metadata.logo
cell.leftText = metadata.description
case .updateList:
cell.applyAction(.current)
cell.imageView?.image = nil
cell.leftText = L10n.Wizards.Provider.Cells.UpdateList.caption
}
2018-10-11 07:13:19 +00:00
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let row = model.row(at: indexPath)
switch row {
case .provider:
let metadata = available[indexPath.row]
tryNext(withMetadata: metadata, purchaseIfNecessary: true)
case .updateList:
tableView.deselectRow(at: indexPath, animated: true)
updateProvidersList()
}
2018-10-11 07:13:19 +00:00
}
}
// MARK: -
extension WizardProviderViewController: AccountViewControllerDelegate {
func accountController(_: AccountViewController, didEnterCredentials credentials: Credentials) {
}
func accountControllerDidComplete(_ vc: AccountViewController) {
finish(withCredentials: vc.credentials)
}
}
2019-12-04 13:16:50 +00:00
// MARK: -
extension WizardProviderViewController: PurchaseViewControllerDelegate {
func purchaseController(_ purchaseController: PurchaseViewController, didPurchase product: LocalProduct?) {
2019-12-04 13:16:50 +00:00
guard let metadata = selectedMetadata else {
return
}
tryNext(withMetadata: metadata, purchaseIfNecessary: false)
}
@objc private func didReloadReceipt() {
guard let metadata = selectedMetadata else {
return
}
tryNext(withMetadata: metadata, purchaseIfNecessary: false)
2019-12-04 13:16:50 +00:00
}
}