2018-10-11 07:13:19 +00:00
|
|
|
//
|
|
|
|
// WizardProviderViewController.swift
|
|
|
|
// Passepartout-iOS
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 9/4/18.
|
2019-03-09 10:44:44 +00:00
|
|
|
// Copyright (c) 2019 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-05-26 06:56:39 +00:00
|
|
|
import PassepartoutCore
|
2019-11-28 09:09:28 +00:00
|
|
|
import Convenience
|
2019-11-28 09:27:47 +00:00
|
|
|
import SwiftyBeaver
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
// 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
|
2018-10-11 07:13:19 +00:00
|
|
|
|
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
|
|
|
|
2019-06-21 21:15:43 +00:00
|
|
|
title = L10n.Core.Organizer.Sections.Providers.header
|
2019-11-28 09:09:28 +00:00
|
|
|
reloadModel()
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
|
2019-11-30 11:00:31 +00:00
|
|
|
private func tryNext(withMetadata metadata: Infrastructure.Metadata) {
|
2019-11-23 13:22:34 +00:00
|
|
|
guard ProductManager.shared.isEligible(forProvider: metadata.name) else {
|
|
|
|
presentPurchaseScreen(forProduct: metadata.product)
|
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 {
|
|
|
|
self?.alertMissingInfrastructure(forName: metadata.name, error: $1)
|
|
|
|
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
|
|
|
|
|
|
|
private func alertMissingInfrastructure(forName name: Infrastructure.Name, error: Error?) {
|
|
|
|
log.error("Unable to download missing \(name) infrastructure: \(error?.localizedDescription ?? "")")
|
|
|
|
|
|
|
|
let alert = UIAlertController.asAlert(title, "..........")
|
|
|
|
alert.addCancelAction(L10n.Core.Global.ok)
|
|
|
|
present(alert, animated: true, completion: nil)
|
|
|
|
}
|
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
|
|
|
|
if let error = $0 {
|
|
|
|
hud.hide()
|
|
|
|
log.error("Unable to update providers list: \(error)")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-30 10:42:21 +00:00
|
|
|
ProductManager.shared.listProducts { (products, error) in
|
|
|
|
hud.hide()
|
|
|
|
if let error = error {
|
|
|
|
log.error("Unable to list products: \(error)")
|
|
|
|
return
|
|
|
|
}
|
2019-11-28 09:27:47 +00:00
|
|
|
self?.reloadModel()
|
|
|
|
self?.tableView.reloadData()
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
cell.leftText = L10n.Core.Wizards.Provider.Cells.UpdateList.caption
|
|
|
|
}
|
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]
|
2019-11-30 11:00:31 +00:00
|
|
|
tryNext(withMetadata: metadata)
|
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)
|
|
|
|
}
|
|
|
|
}
|