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
|
2019-11-28 09:27:47 +00:00
|
|
|
import SwiftyBeaver
|
2021-10-27 16:47:08 +00:00
|
|
|
import PassepartoutConstants
|
|
|
|
import PassepartoutCore
|
|
|
|
import ConvenienceUI
|
2019-11-28 09:27:47 +00:00
|
|
|
|
|
|
|
private let log = SwiftyBeaver.self
|
2018-10-11 07:13:19 +00:00
|
|
|
|
2019-11-28 09:09:28 +00:00
|
|
|
class WizardProviderViewController: UITableViewController, StrongTableHost {
|
2019-11-28 09:27:47 +00:00
|
|
|
private var available: [Infrastructure.Metadata] = []
|
2018-10-11 07:13:19 +00:00
|
|
|
|
|
|
|
private var createdProfile: ProviderConnectionProfile?
|
2019-11-28 09:09:28 +00:00
|
|
|
|
2019-12-04 13:16:50 +00:00
|
|
|
private var selectedMetadata: Infrastructure.Metadata?
|
|
|
|
|
2019-11-28 09:09:28 +00:00
|
|
|
// MARK: StrongTableHost
|
|
|
|
|
|
|
|
let model = StrongTableModel<SectionType, RowType>()
|
|
|
|
|
|
|
|
func reloadModel() {
|
2019-11-28 09:27:47 +00:00
|
|
|
available = TransientStore.shared.service.availableProviders()
|
|
|
|
|
2019-11-28 09:09:28 +00:00
|
|
|
model.clear()
|
|
|
|
model.add(.availableProviders)
|
|
|
|
model.add(.listActions)
|
|
|
|
model.set(.provider, count: available.count, forSection: .availableProviders)
|
|
|
|
model.set([.updateList], forSection: .listActions)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: UIViewController
|
2020-04-11 13:37:44 +00:00
|
|
|
|
|
|
|
deinit {
|
|
|
|
NotificationCenter.default.removeObserver(self)
|
|
|
|
}
|
2018-10-11 07:13:19 +00:00
|
|
|
|
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
2020-04-11 13:37:44 +00:00
|
|
|
|
|
|
|
let nc = NotificationCenter.default
|
|
|
|
nc.addObserver(self, selector: #selector(didReloadReceipt), name: ProductManager.didReloadReceipt, object: nil)
|
2018-10-11 07:13:19 +00:00
|
|
|
|
2021-08-07 11:57:22 +00:00
|
|
|
title = L10n.Organizer.Sections.Providers.header
|
2019-11-28 09:09:28 +00:00
|
|
|
reloadModel()
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
|
2020-04-11 13:37:44 +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 {
|
2021-02-02 16:51:09 +00:00
|
|
|
try ProductManager.shared.verifyEligible(forProvider: metadata)
|
|
|
|
} catch ProductError.beta {
|
|
|
|
presentBetaFeatureUnavailable("Providers")
|
|
|
|
return
|
|
|
|
} catch {
|
|
|
|
guard purchaseIfNecessary else {
|
2020-04-11 13:37:44 +00:00
|
|
|
return
|
|
|
|
}
|
2021-02-02 16:51:09 +00:00
|
|
|
presentPurchaseScreen(forProduct: metadata.product, delegate: self)
|
2019-10-28 09:14:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-30 11:00:31 +00:00
|
|
|
// 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 {
|
2019-12-04 12:59:10 +00:00
|
|
|
self?.alertMissingInfrastructure(forMetadata: metadata, error: $1)
|
2019-11-30 11:00:31 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
self?.next(withMetadata: metadata)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
next(withMetadata: metadata)
|
|
|
|
}
|
|
|
|
|
|
|
|
private func next(withMetadata metadata: Infrastructure.Metadata) {
|
2019-11-23 13:22:34 +00:00
|
|
|
let profile = ProviderConnectionProfile(name: metadata.name)
|
2018-10-11 07:13:19 +00:00
|
|
|
createdProfile = profile
|
|
|
|
|
|
|
|
let accountVC = StoryboardScene.Main.accountIdentifier.instantiate()
|
2019-11-30 11:00:31 +00:00
|
|
|
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)
|
|
|
|
}
|
2019-11-30 11:00:31 +00:00
|
|
|
|
2019-12-04 12:59:10 +00:00
|
|
|
private func alertMissingInfrastructure(forMetadata metadata: Infrastructure.Metadata, error: Error?) {
|
2021-08-07 11:57:22 +00:00
|
|
|
var message = L10n.Wizards.Provider.Alerts.Unavailable.message
|
2019-11-30 11:27:30 +00:00
|
|
|
if let error = error {
|
2019-12-04 12:59:10 +00:00
|
|
|
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 {
|
2019-12-04 12:59:10 +00:00
|
|
|
log.error("Unable to download missing \(metadata.description) infrastructure (API error)")
|
2019-11-30 11:27:30 +00:00
|
|
|
}
|
|
|
|
|
2019-12-04 12:59:10 +00:00
|
|
|
let alert = UIAlertController.asAlert(metadata.description, message)
|
2021-08-07 11:57:22 +00:00
|
|
|
alert.addCancelAction(L10n.Global.ok)
|
2019-11-30 11:00:31 +00:00
|
|
|
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)
|
|
|
|
}
|
2019-11-30 11:00:31 +00:00
|
|
|
}
|
2018-10-11 07:13:19 +00:00
|
|
|
|
|
|
|
private func finish(withCredentials credentials: Credentials) {
|
|
|
|
guard let profile = createdProfile else {
|
|
|
|
fatalError("No profile created?")
|
|
|
|
}
|
2018-11-02 13:20:40 +00:00
|
|
|
let service = TransientStore.shared.service
|
2018-10-11 07:13:19 +00:00
|
|
|
dismiss(animated: true) {
|
2018-11-02 13:20:40 +00:00
|
|
|
service.addOrReplaceProfile(profile, credentials: credentials)
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-28 09:09:28 +00:00
|
|
|
|
|
|
|
private func updateProvidersList() {
|
2019-11-28 09:27:47 +00:00
|
|
|
let hud = HUD(view: view)
|
|
|
|
InfrastructureFactory.shared.updateIndex { [weak self] in
|
2021-02-04 17:44:36 +00:00
|
|
|
hud.hide()
|
2019-11-28 09:27:47 +00:00
|
|
|
if let error = $0 {
|
|
|
|
log.error("Unable to update providers list: \(error)")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-02-04 17:44:36 +00:00
|
|
|
self?.reloadModel()
|
|
|
|
self?.tableView.reloadData()
|
2019-11-28 09:27:47 +00:00
|
|
|
}
|
2019-11-28 09:09:28 +00:00
|
|
|
}
|
2018-10-11 07:13:19 +00:00
|
|
|
|
|
|
|
@IBAction private func close() {
|
|
|
|
dismiss(animated: true, completion: nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: -
|
|
|
|
|
|
|
|
extension WizardProviderViewController {
|
2019-11-28 09:09:28 +00:00
|
|
|
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 {
|
2019-11-28 09:09:28 +00:00
|
|
|
return model.numberOfRows(forSection: section)
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
2019-11-28 09:09:28 +00:00
|
|
|
let row = model.row(at: indexPath)
|
2018-10-11 07:13:19 +00:00
|
|
|
let cell = Cells.setting.dequeue(from: tableView, for: indexPath)
|
2019-11-28 09:09:28 +00:00
|
|
|
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
|
2021-08-07 11:57:22 +00:00
|
|
|
cell.leftText = L10n.Wizards.Provider.Cells.UpdateList.caption
|
2019-11-28 09:09:28 +00:00
|
|
|
}
|
2018-10-11 07:13:19 +00:00
|
|
|
return cell
|
|
|
|
}
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
2019-11-28 09:09:28 +00:00
|
|
|
let row = model.row(at: indexPath)
|
|
|
|
switch row {
|
|
|
|
case .provider:
|
|
|
|
let metadata = available[indexPath.row]
|
2020-04-11 13:37:44 +00:00
|
|
|
tryNext(withMetadata: metadata, purchaseIfNecessary: true)
|
2019-11-28 09:09:28 +00:00
|
|
|
|
|
|
|
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 {
|
2021-09-27 15:59:01 +00:00
|
|
|
func purchaseController(_ purchaseController: PurchaseViewController, didPurchase product: LocalProduct?) {
|
2019-12-04 13:16:50 +00:00
|
|
|
guard let metadata = selectedMetadata else {
|
|
|
|
return
|
|
|
|
}
|
2020-04-11 13:37:44 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|