2018-10-11 07:13:19 +00:00
|
|
|
//
|
|
|
|
// ProviderPresetViewController.swift
|
|
|
|
// Passepartout-iOS
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 9/2/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-05-26 06:56:39 +00:00
|
|
|
import PassepartoutCore
|
2018-10-11 07:13:19 +00:00
|
|
|
|
|
|
|
protocol ProviderPresetViewControllerDelegate: class {
|
|
|
|
func providerPresetController(_: ProviderPresetViewController, didSelectPreset preset: InfrastructurePreset)
|
|
|
|
}
|
|
|
|
|
|
|
|
class ProviderPresetViewController: UIViewController {
|
|
|
|
@IBOutlet private weak var tableView: UITableView!
|
|
|
|
|
|
|
|
var presets: [InfrastructurePreset] = []
|
|
|
|
|
|
|
|
var currentPresetId: String?
|
|
|
|
|
|
|
|
weak var delegate: ProviderPresetViewControllerDelegate?
|
|
|
|
|
|
|
|
// MARK: Table
|
|
|
|
|
|
|
|
private let rows: [RowType] = [.presetDescription, .techDetails]
|
|
|
|
|
|
|
|
// MARK: UIViewController
|
|
|
|
|
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
|
|
|
|
2019-06-21 21:15:43 +00:00
|
|
|
title = L10n.Core.Service.Cells.Provider.Preset.caption
|
2018-10-11 07:13:19 +00:00
|
|
|
tableView.reloadData()
|
|
|
|
if let ip = selectedIndexPath {
|
2019-04-05 10:46:50 +00:00
|
|
|
tableView.scrollToRowAsync(at: ip)
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override func viewWillAppear(_ animated: Bool) {
|
|
|
|
super.viewWillAppear(animated)
|
|
|
|
|
|
|
|
if let ip = tableView.indexPathForSelectedRow {
|
|
|
|
tableView.deselectRow(at: ip, animated: true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension ProviderPresetViewController: UITableViewDataSource, UITableViewDelegate {
|
|
|
|
enum RowType: Int {
|
|
|
|
case presetDescription
|
|
|
|
|
|
|
|
case techDetails
|
|
|
|
}
|
|
|
|
|
|
|
|
private var selectedIndexPath: IndexPath? {
|
2019-04-11 14:51:03 +00:00
|
|
|
guard let i = presets.firstIndex(where: { $0.id == currentPresetId }) else {
|
2018-10-11 07:13:19 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return IndexPath(row: 0, section: i)
|
|
|
|
}
|
|
|
|
|
|
|
|
func numberOfSections(in tableView: UITableView) -> Int {
|
|
|
|
return presets.count
|
|
|
|
}
|
|
|
|
|
|
|
|
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
|
|
|
|
let preset = presets[section]
|
|
|
|
return preset.name
|
|
|
|
}
|
|
|
|
|
|
|
|
// func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
|
2019-06-21 21:15:43 +00:00
|
|
|
// return L10n.Core.Provider.Preset.Sections.Main.footer
|
2018-10-11 07:13:19 +00:00
|
|
|
// }
|
|
|
|
|
|
|
|
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
|
|
|
return rows.count
|
|
|
|
}
|
|
|
|
|
|
|
|
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
|
|
|
let preset = presets[indexPath.section]
|
|
|
|
|
|
|
|
let cell = Cells.setting.dequeue(from: tableView, for: indexPath)
|
|
|
|
cell.isTappable = true
|
|
|
|
switch rows[indexPath.row] {
|
|
|
|
case .presetDescription:
|
|
|
|
cell.leftText = preset.comment
|
2019-10-25 17:30:21 +00:00
|
|
|
cell.applyChecked(preset.id == currentPresetId, .current)
|
2018-10-11 07:13:19 +00:00
|
|
|
|
|
|
|
case .techDetails:
|
2019-10-25 17:30:21 +00:00
|
|
|
cell.applyAction(.current)
|
2019-06-21 21:15:43 +00:00
|
|
|
cell.leftText = L10n.App.Provider.Preset.Cells.TechDetails.caption
|
2018-10-11 07:13:19 +00:00
|
|
|
cell.accessoryType = .none
|
|
|
|
}
|
|
|
|
return cell
|
|
|
|
}
|
|
|
|
|
|
|
|
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
|
|
|
let preset = presets[indexPath.section]
|
|
|
|
|
|
|
|
switch rows[indexPath.row] {
|
|
|
|
case .presetDescription:
|
|
|
|
currentPresetId = preset.id
|
|
|
|
delegate?.providerPresetController(self, didSelectPreset: preset)
|
|
|
|
|
|
|
|
case .techDetails:
|
|
|
|
let vc = StoryboardScene.Main.configurationIdentifier.instantiate()
|
|
|
|
vc.title = preset.name
|
2018-10-25 18:08:22 +00:00
|
|
|
vc.initialConfiguration = preset.configuration.sessionConfiguration
|
2018-10-11 07:13:19 +00:00
|
|
|
navigationController?.pushViewController(vc, animated: true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|