2018-10-11 07:13:19 +00:00
|
|
|
//
|
|
|
|
// OrganizerViewController.swift
|
|
|
|
// Passepartout-iOS
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 9/2/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-03-18 10:52:19 +00:00
|
|
|
import Passepartout_Core
|
2018-10-11 07:13:19 +00:00
|
|
|
|
|
|
|
// XXX: convoluted due to the separation of provider/host profiles
|
|
|
|
|
|
|
|
class OrganizerViewController: UITableViewController, TableModelHost {
|
|
|
|
private let service = TransientStore.shared.service
|
|
|
|
|
2018-10-26 08:00:28 +00:00
|
|
|
private var providers: [String] = []
|
2018-10-11 07:13:19 +00:00
|
|
|
|
2018-10-26 08:00:28 +00:00
|
|
|
private var hosts: [String] = []
|
2018-10-11 07:13:19 +00:00
|
|
|
|
|
|
|
private var availableProviderNames: [Infrastructure.Name]?
|
2018-10-18 07:52:23 +00:00
|
|
|
|
|
|
|
private var didShowSubreddit = false
|
|
|
|
|
2018-10-11 07:13:19 +00:00
|
|
|
// MARK: TableModelHost
|
|
|
|
|
|
|
|
let model: TableModel<SectionType, RowType> = {
|
|
|
|
let model: TableModel<SectionType, RowType> = TableModel()
|
|
|
|
model.add(.providers)
|
|
|
|
model.add(.hosts)
|
2019-03-18 19:14:59 +00:00
|
|
|
if #available(iOS 12, *) {
|
|
|
|
model.add(.siri)
|
|
|
|
}
|
2018-10-11 07:13:19 +00:00
|
|
|
model.add(.about)
|
|
|
|
model.add(.destruction)
|
|
|
|
model.setHeader(L10n.Organizer.Sections.Providers.header, for: .providers)
|
|
|
|
model.setHeader(L10n.Organizer.Sections.Hosts.header, for: .hosts)
|
|
|
|
model.setFooter(L10n.Organizer.Sections.Providers.footer, for: .providers)
|
|
|
|
model.setFooter(L10n.Organizer.Sections.Hosts.footer, for: .hosts)
|
2019-03-18 19:14:59 +00:00
|
|
|
if #available(iOS 12, *) {
|
|
|
|
model.setHeader(L10n.Organizer.Sections.Siri.header, for: .siri)
|
2019-03-19 16:31:07 +00:00
|
|
|
model.setFooter(L10n.Organizer.Sections.Siri.footer, for: .siri)
|
2019-03-18 19:14:59 +00:00
|
|
|
model.set([.siriShortcuts], in: .siri)
|
|
|
|
}
|
2018-10-11 07:13:19 +00:00
|
|
|
model.set([.openAbout], in: .about)
|
|
|
|
model.set([.uninstall], in: .destruction)
|
2019-03-06 16:44:27 +00:00
|
|
|
if AppConstants.Flags.isBeta {
|
|
|
|
model.add(.test)
|
|
|
|
model.setHeader("Beta", for: .test)
|
2019-03-07 21:59:46 +00:00
|
|
|
model.set([.testDisplayLog, .testTermination], in: .test)
|
2019-03-06 16:44:27 +00:00
|
|
|
}
|
2018-10-11 07:13:19 +00:00
|
|
|
return model
|
|
|
|
}()
|
|
|
|
|
|
|
|
func reloadModel() {
|
2018-10-29 16:44:34 +00:00
|
|
|
providers = service.ids(forContext: .provider).sorted()
|
2018-10-27 14:48:38 +00:00
|
|
|
hosts = service.ids(forContext: .host).sortedCaseInsensitive()
|
2018-10-11 07:13:19 +00:00
|
|
|
|
2018-10-26 08:00:28 +00:00
|
|
|
var providerRows = [RowType](repeating: .profile, count: providers.count)
|
|
|
|
var hostRows = [RowType](repeating: .profile, count: hosts.count)
|
|
|
|
providerRows.append(.addProvider)
|
|
|
|
hostRows.append(.addHost)
|
2018-10-11 07:13:19 +00:00
|
|
|
|
2018-10-26 08:00:28 +00:00
|
|
|
model.set(providerRows, in: .providers)
|
|
|
|
model.set(hostRows, in: .hosts)
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: UIViewController
|
2018-10-27 18:10:59 +00:00
|
|
|
|
2018-10-11 07:13:19 +00:00
|
|
|
override func awakeFromNib() {
|
|
|
|
super.awakeFromNib()
|
|
|
|
|
|
|
|
applyMasterTitle(Theme.current)
|
|
|
|
}
|
|
|
|
|
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
|
|
|
|
|
|
|
title = GroupConstants.App.title
|
|
|
|
navigationItem.rightBarButtonItem = editButtonItem
|
|
|
|
Cells.destructive.register(with: tableView)
|
|
|
|
reloadModel()
|
|
|
|
|
|
|
|
tableView.reloadData()
|
|
|
|
if let ip = selectedIndexPath {
|
|
|
|
tableView.scrollToRow(at: ip, at: .middle, animated: false)
|
|
|
|
}
|
|
|
|
|
|
|
|
service.delegate = self
|
|
|
|
}
|
|
|
|
|
2018-10-18 07:52:23 +00:00
|
|
|
override func viewDidAppear(_ animated: Bool) {
|
|
|
|
super.viewDidAppear(animated)
|
|
|
|
|
|
|
|
if !didShowSubreddit && !TransientStore.shared.didHandleSubreddit {
|
|
|
|
didShowSubreddit = true
|
|
|
|
|
2018-10-18 13:57:59 +00:00
|
|
|
let alert = Macros.alert(L10n.Reddit.title, L10n.Reddit.message)
|
2018-10-18 07:52:23 +00:00
|
|
|
alert.addDefaultAction(L10n.Reddit.Buttons.subscribe) {
|
|
|
|
TransientStore.shared.didHandleSubreddit = true
|
|
|
|
self.subscribeSubreddit()
|
|
|
|
}
|
|
|
|
alert.addAction(L10n.Reddit.Buttons.never) {
|
|
|
|
TransientStore.shared.didHandleSubreddit = true
|
|
|
|
}
|
|
|
|
alert.addCancelAction(L10n.Reddit.Buttons.remind)
|
|
|
|
present(alert, animated: true, completion: nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-11 07:13:19 +00:00
|
|
|
override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
|
|
|
|
if let cell = sender as? UITableViewCell, let indexPath = tableView.indexPath(for: cell) {
|
|
|
|
return model.row(at: indexPath) == .profile
|
|
|
|
}
|
|
|
|
|
|
|
|
// fall back to active profile if no selection
|
|
|
|
return service.hasActiveProfile()
|
|
|
|
}
|
|
|
|
|
|
|
|
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
|
|
|
|
let destination = (segue.destination as? UINavigationController)?.topViewController
|
|
|
|
|
|
|
|
if let vc = destination as? ServiceViewController {
|
|
|
|
var selectedProfile: ConnectionProfile?
|
|
|
|
|
|
|
|
// XXX: sender can be a cell or a profile
|
|
|
|
selectedProfile = sender as? ConnectionProfile
|
|
|
|
if selectedProfile == nil, let cell = sender as? UITableViewCell, let indexPath = tableView.indexPath(for: cell) {
|
|
|
|
selectedProfile = profile(at: indexPath)
|
|
|
|
}
|
|
|
|
assert(selectedProfile != nil, "No selected profile")
|
|
|
|
|
2019-03-08 11:50:34 +00:00
|
|
|
vc.setProfile(selectedProfile)
|
2018-10-27 18:10:59 +00:00
|
|
|
} else if let providerVC = destination as? WizardProviderViewController {
|
|
|
|
providerVC.availableNames = availableProviderNames ?? []
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Actions
|
|
|
|
|
|
|
|
@IBAction private func about() {
|
|
|
|
perform(segue: StoryboardSegue.Organizer.aboutSegueIdentifier, sender: nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
private func addNewProvider() {
|
|
|
|
var names = Set(InfrastructureFactory.shared.allNames)
|
2018-10-26 08:00:28 +00:00
|
|
|
var createdNames: [Infrastructure.Name] = []
|
|
|
|
providers.forEach {
|
|
|
|
guard let name = Infrastructure.Name(rawValue: $0) else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
createdNames.append(name)
|
|
|
|
}
|
2018-10-11 07:13:19 +00:00
|
|
|
names.formSymmetricDifference(createdNames)
|
|
|
|
|
|
|
|
guard !names.isEmpty else {
|
|
|
|
let alert = Macros.alert(
|
|
|
|
L10n.Organizer.Sections.Providers.header,
|
|
|
|
L10n.Organizer.Alerts.ExhaustedProviders.message
|
|
|
|
)
|
|
|
|
alert.addCancelAction(L10n.Global.ok)
|
|
|
|
present(alert, animated: true, completion: nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-10-29 16:44:34 +00:00
|
|
|
availableProviderNames = names.sorted()
|
2018-10-11 07:13:19 +00:00
|
|
|
perform(segue: StoryboardSegue.Organizer.addProviderSegueIdentifier)
|
|
|
|
}
|
|
|
|
|
|
|
|
private func addNewHost() {
|
2018-10-27 12:40:52 +00:00
|
|
|
perform(segue: StoryboardSegue.Organizer.showImportedHostsSegueIdentifier)
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
2019-03-18 19:14:59 +00:00
|
|
|
|
|
|
|
private func addShortcuts() {
|
|
|
|
perform(segue: StoryboardSegue.Organizer.siriShortcutsSegueIdentifier)
|
|
|
|
}
|
2018-10-11 07:13:19 +00:00
|
|
|
|
|
|
|
private func removeProfile(at indexPath: IndexPath) {
|
|
|
|
let sectionObject = model.section(for: indexPath.section)
|
2018-10-26 08:00:28 +00:00
|
|
|
let rowProfile = profileKey(at: indexPath)
|
2018-10-11 07:13:19 +00:00
|
|
|
switch sectionObject {
|
|
|
|
case .providers:
|
2018-10-26 08:00:28 +00:00
|
|
|
providers.remove(at: indexPath.row)
|
2018-10-11 07:13:19 +00:00
|
|
|
|
|
|
|
case .hosts:
|
2018-10-26 08:00:28 +00:00
|
|
|
hosts.remove(at: indexPath.row)
|
2018-10-11 07:13:19 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// var fallbackSection: SectionType?
|
|
|
|
|
2018-10-26 08:00:28 +00:00
|
|
|
let total = providers.count + hosts.count
|
2018-10-11 07:13:19 +00:00
|
|
|
|
|
|
|
// removed all profiles
|
|
|
|
if total == 0 {
|
|
|
|
VPN.shared.disconnect(completionHandler: nil)
|
|
|
|
}
|
|
|
|
// removed active profile
|
|
|
|
else if service.isActiveProfile(rowProfile) {
|
|
|
|
// let anyProvider = providerProfiles.first
|
|
|
|
// let anyHost = hostProfiles.first
|
|
|
|
// guard let anyProfile: ConnectionProfile = firstProvider ?? firstHost else {
|
|
|
|
// fatalError("There must be one profile somewhere")
|
|
|
|
// }
|
|
|
|
// fallbackSection = (anyProvider != nil) ? .providers : .hosts
|
|
|
|
// store.service.activateProfile(only)
|
|
|
|
VPN.shared.disconnect(completionHandler: nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
tableView.beginUpdates()
|
|
|
|
model.deleteRow(in: sectionObject, at: indexPath.row)
|
|
|
|
tableView.deleteRows(at: [indexPath], with: .automatic)
|
|
|
|
// if let fallbackSection = fallbackSection {
|
|
|
|
// let section = model.index(ofSection: fallbackSection)
|
|
|
|
// tableView.reloadRows(at: [IndexPath(row: 0, section: section)], with: .none)
|
|
|
|
// }
|
|
|
|
tableView.endUpdates()
|
|
|
|
|
2018-11-03 22:40:07 +00:00
|
|
|
service.removeProfile(rowProfile)
|
2019-03-09 07:42:58 +00:00
|
|
|
if #available(iOS 12, *) {
|
2019-03-18 16:06:19 +00:00
|
|
|
IntentDispatcher.forgetProfile(withKey: rowProfile)
|
2019-03-09 07:42:58 +00:00
|
|
|
}
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private func confirmVpnProfileDeletion() {
|
|
|
|
let alert = Macros.alert(
|
|
|
|
L10n.Organizer.Cells.Uninstall.caption,
|
|
|
|
L10n.Organizer.Alerts.DeleteVpnProfile.message
|
|
|
|
)
|
|
|
|
alert.addDefaultAction(L10n.Global.ok) {
|
|
|
|
VPN.shared.uninstall(completionHandler: nil)
|
|
|
|
}
|
|
|
|
alert.addCancelAction(L10n.Global.cancel)
|
|
|
|
present(alert, animated: true, completion: nil)
|
|
|
|
}
|
2018-10-18 07:52:23 +00:00
|
|
|
|
|
|
|
private func subscribeSubreddit() {
|
|
|
|
UIApplication.shared.open(AppConstants.URLs.subreddit, options: [:], completionHandler: nil)
|
|
|
|
}
|
2019-03-06 16:44:27 +00:00
|
|
|
|
2019-03-07 21:59:46 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
private func testDisplayLog() {
|
|
|
|
guard let log = try? String(contentsOf: AppConstants.Log.fileURL) else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
let alert = Macros.alert("Debug log", log)
|
|
|
|
alert.addCancelAction(L10n.Global.ok)
|
|
|
|
present(alert, animated: true, completion: nil)
|
|
|
|
}
|
|
|
|
|
2019-03-06 16:44:27 +00:00
|
|
|
private func testTermination() {
|
|
|
|
exit(0)
|
|
|
|
}
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: -
|
|
|
|
|
|
|
|
extension OrganizerViewController {
|
|
|
|
enum SectionType: Int {
|
|
|
|
case providers
|
|
|
|
|
|
|
|
case hosts
|
|
|
|
|
2019-03-18 19:14:59 +00:00
|
|
|
case siri
|
|
|
|
|
2018-10-11 07:13:19 +00:00
|
|
|
case about
|
|
|
|
|
|
|
|
case destruction
|
2019-03-06 16:44:27 +00:00
|
|
|
|
|
|
|
case test
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
enum RowType: Int {
|
|
|
|
case profile
|
|
|
|
|
|
|
|
case addProvider
|
|
|
|
|
|
|
|
case addHost
|
|
|
|
|
2019-03-18 19:14:59 +00:00
|
|
|
case siriShortcuts
|
|
|
|
|
2018-10-11 07:13:19 +00:00
|
|
|
case openAbout
|
|
|
|
|
|
|
|
case uninstall
|
2019-03-07 21:59:46 +00:00
|
|
|
|
|
|
|
case testDisplayLog
|
2019-03-06 16:44:27 +00:00
|
|
|
|
|
|
|
case testTermination
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private var selectedIndexPath: IndexPath? {
|
2018-10-26 08:00:28 +00:00
|
|
|
guard let active = service.activeProfileKey else {
|
2018-10-11 07:13:19 +00:00
|
|
|
return nil
|
|
|
|
}
|
2018-10-26 08:00:28 +00:00
|
|
|
switch active.context {
|
|
|
|
case .provider:
|
|
|
|
if let row = providers.index(where: { $0 == active.id }) {
|
|
|
|
return IndexPath(row: row, section: 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
case .host:
|
|
|
|
if let row = hosts.index(where: { $0 == active.id }) {
|
|
|
|
return IndexPath(row: row, section: 1)
|
|
|
|
}
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
override func numberOfSections(in tableView: UITableView) -> Int {
|
|
|
|
return model.count
|
|
|
|
}
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
|
|
|
|
return model.header(for: section)
|
|
|
|
}
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
|
|
|
|
return model.footer(for: section)
|
|
|
|
}
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
|
|
|
return model.count(for: section)
|
|
|
|
}
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
|
|
|
switch model.row(at: indexPath) {
|
|
|
|
case .profile:
|
|
|
|
let cell = Cells.setting.dequeue(from: tableView, for: indexPath)
|
2018-10-26 08:00:28 +00:00
|
|
|
let rowProfile = profileKey(at: indexPath)
|
2018-10-25 21:08:43 +00:00
|
|
|
cell.leftText = rowProfile.id
|
2018-10-17 13:30:40 +00:00
|
|
|
cell.rightText = service.isActiveProfile(rowProfile) ? L10n.Organizer.Cells.Profile.Value.current : nil
|
2018-10-11 07:13:19 +00:00
|
|
|
return cell
|
|
|
|
|
|
|
|
case .addProvider:
|
|
|
|
let cell = Cells.setting.dequeue(from: tableView, for: indexPath)
|
|
|
|
cell.applyAction(Theme.current)
|
|
|
|
cell.leftText = L10n.Organizer.Cells.AddProvider.caption
|
|
|
|
return cell
|
|
|
|
|
|
|
|
case .addHost:
|
|
|
|
let cell = Cells.setting.dequeue(from: tableView, for: indexPath)
|
|
|
|
cell.applyAction(Theme.current)
|
|
|
|
cell.leftText = L10n.Organizer.Cells.AddHost.caption
|
|
|
|
return cell
|
|
|
|
|
2019-03-18 19:14:59 +00:00
|
|
|
case .siriShortcuts:
|
|
|
|
let cell = Cells.setting.dequeue(from: tableView, for: indexPath)
|
|
|
|
cell.applyAction(Theme.current)
|
|
|
|
cell.leftText = L10n.Organizer.Cells.SiriShortcuts.caption
|
|
|
|
return cell
|
|
|
|
|
2018-10-11 07:13:19 +00:00
|
|
|
case .openAbout:
|
|
|
|
let cell = Cells.setting.dequeue(from: tableView, for: indexPath)
|
|
|
|
cell.leftText = L10n.Organizer.Cells.About.caption(GroupConstants.App.name)
|
|
|
|
return cell
|
|
|
|
|
|
|
|
case .uninstall:
|
|
|
|
let cell = Cells.destructive.dequeue(from: tableView, for: indexPath)
|
|
|
|
cell.caption = L10n.Organizer.Cells.Uninstall.caption
|
|
|
|
return cell
|
2019-03-06 16:44:27 +00:00
|
|
|
|
2019-03-07 21:59:46 +00:00
|
|
|
case .testDisplayLog:
|
|
|
|
let cell = Cells.setting.dequeue(from: tableView, for: indexPath)
|
|
|
|
cell.leftText = "Display current log"
|
|
|
|
return cell
|
|
|
|
|
2019-03-06 16:44:27 +00:00
|
|
|
case .testTermination:
|
2019-03-07 21:59:46 +00:00
|
|
|
let cell = Cells.setting.dequeue(from: tableView, for: indexPath)
|
|
|
|
cell.leftText = "Terminate app"
|
2019-03-06 16:44:27 +00:00
|
|
|
return cell
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
|
|
|
switch model.row(at: indexPath) {
|
|
|
|
case .profile:
|
|
|
|
// selectedProfileId = profile(at: indexPath).id
|
|
|
|
break
|
|
|
|
|
|
|
|
case .addProvider:
|
|
|
|
addNewProvider()
|
|
|
|
|
|
|
|
case .addHost:
|
|
|
|
addNewHost()
|
|
|
|
|
2019-03-18 19:14:59 +00:00
|
|
|
case .siriShortcuts:
|
|
|
|
addShortcuts()
|
|
|
|
|
2018-10-11 07:13:19 +00:00
|
|
|
case .openAbout:
|
|
|
|
about()
|
|
|
|
|
|
|
|
case .uninstall:
|
|
|
|
confirmVpnProfileDeletion()
|
2019-03-06 16:44:27 +00:00
|
|
|
|
2019-03-07 21:59:46 +00:00
|
|
|
case .testDisplayLog:
|
|
|
|
testDisplayLog()
|
|
|
|
|
2019-03-06 16:44:27 +00:00
|
|
|
case .testTermination:
|
|
|
|
testTermination()
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
tableView.deselectRow(at: indexPath, animated: true)
|
|
|
|
}
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
|
|
|
|
guard tableView.isEditing else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return model.row(at: indexPath) == .profile
|
|
|
|
}
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
|
|
|
|
removeProfile(at: indexPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Helpers
|
|
|
|
|
2018-10-26 08:00:28 +00:00
|
|
|
private func sectionProfiles(at indexPath: IndexPath) -> [String] {
|
|
|
|
let ids: [String]
|
2018-10-11 07:13:19 +00:00
|
|
|
let sectionObject = model.section(for: indexPath.section)
|
|
|
|
switch sectionObject {
|
|
|
|
case .providers:
|
2018-10-26 08:00:28 +00:00
|
|
|
ids = providers
|
2018-10-11 07:13:19 +00:00
|
|
|
|
|
|
|
case .hosts:
|
2018-10-26 08:00:28 +00:00
|
|
|
ids = hosts
|
2018-10-11 07:13:19 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
fatalError("Unexpected section: \(sectionObject)")
|
|
|
|
}
|
2018-10-26 08:00:28 +00:00
|
|
|
guard indexPath.row < ids.count else {
|
2018-10-11 07:13:19 +00:00
|
|
|
fatalError("No profile found at \(indexPath), is it an add cell?")
|
|
|
|
}
|
2018-10-26 08:00:28 +00:00
|
|
|
return ids
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
|
2018-11-06 10:08:15 +00:00
|
|
|
private func profileKey(at indexPath: IndexPath) -> ProfileKey {
|
2018-10-26 08:00:28 +00:00
|
|
|
let section = model.section(for: indexPath.section)
|
|
|
|
switch section {
|
|
|
|
case .providers:
|
2018-11-06 10:08:15 +00:00
|
|
|
return ProfileKey(.provider, providers[indexPath.row])
|
2018-10-26 08:00:28 +00:00
|
|
|
|
|
|
|
case .hosts:
|
2018-11-06 10:08:15 +00:00
|
|
|
return ProfileKey(.host, hosts[indexPath.row])
|
2018-10-26 08:00:28 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
fatalError("Profile found in unexpected section: \(section)")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-11 07:13:19 +00:00
|
|
|
private func profile(at indexPath: IndexPath) -> ConnectionProfile {
|
2018-10-26 08:00:28 +00:00
|
|
|
let id = sectionProfiles(at: indexPath)[indexPath.row]
|
|
|
|
let section = model.section(for: indexPath.section)
|
2018-10-26 16:36:41 +00:00
|
|
|
let context: Context
|
2018-10-26 08:00:28 +00:00
|
|
|
switch section {
|
|
|
|
case .providers:
|
|
|
|
context = .provider
|
|
|
|
|
|
|
|
case .hosts:
|
|
|
|
context = .host
|
|
|
|
|
|
|
|
default:
|
|
|
|
fatalError("Profile found in unexpected section: \(section)")
|
|
|
|
}
|
|
|
|
guard let found = service.profile(withContext: context, id: id) else {
|
|
|
|
fatalError("Profile (\(context), \(id)) could not be found, why was it returned?")
|
|
|
|
}
|
|
|
|
return found
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: -
|
|
|
|
|
|
|
|
extension OrganizerViewController: ConnectionServiceDelegate {
|
2018-11-02 13:20:40 +00:00
|
|
|
func connectionService(didAdd profile: ConnectionProfile) {
|
2018-11-04 13:51:01 +00:00
|
|
|
TransientStore.shared.serialize(withProfiles: false) // add
|
2018-11-02 13:20:40 +00:00
|
|
|
|
2018-10-11 07:13:19 +00:00
|
|
|
reloadModel()
|
|
|
|
tableView.reloadData()
|
|
|
|
|
2019-03-09 08:53:48 +00:00
|
|
|
if #available(iOS 12, *) {
|
2019-03-19 16:12:24 +00:00
|
|
|
IntentDispatcher.donateEnableVPN()
|
2019-03-09 08:53:48 +00:00
|
|
|
}
|
|
|
|
|
2018-10-27 19:13:21 +00:00
|
|
|
// XXX: hack around bad replace when detail presented in compact view
|
|
|
|
if let detailNav = navigationController?.viewControllers.last as? UINavigationController {
|
|
|
|
var existingServiceVC: ServiceViewController?
|
|
|
|
for vc in detailNav.viewControllers {
|
|
|
|
if let found = vc as? ServiceViewController {
|
|
|
|
existingServiceVC = found
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let serviceVC = existingServiceVC ?? (StoryboardScene.Main.serviceIdentifier.instantiate().topViewController as! ServiceViewController)
|
2019-03-08 11:50:34 +00:00
|
|
|
serviceVC.setProfile(profile)
|
2018-10-27 19:13:21 +00:00
|
|
|
detailNav.setViewControllers([serviceVC], animated: true)
|
|
|
|
return
|
|
|
|
}
|
2018-10-11 07:13:19 +00:00
|
|
|
perform(segue: StoryboardSegue.Organizer.selectProfileSegueIdentifier, sender: profile)
|
|
|
|
}
|
2018-11-02 13:20:40 +00:00
|
|
|
|
2018-11-02 10:19:59 +00:00
|
|
|
func connectionService(didRename oldProfile: ConnectionProfile, to newProfile: ConnectionProfile) {
|
2018-11-04 13:51:01 +00:00
|
|
|
TransientStore.shared.serialize(withProfiles: false) // rename
|
2018-11-02 13:20:40 +00:00
|
|
|
|
|
|
|
reloadModel()
|
|
|
|
tableView.reloadData()
|
|
|
|
}
|
|
|
|
|
2018-11-06 10:08:15 +00:00
|
|
|
func connectionService(didRemoveProfileWithKey key: ProfileKey) {
|
2018-11-04 13:51:01 +00:00
|
|
|
TransientStore.shared.serialize(withProfiles: false) // delete
|
2018-11-04 08:18:16 +00:00
|
|
|
|
|
|
|
splitViewController?.serviceViewController?.hideProfileIfDeleted()
|
2018-11-02 13:20:40 +00:00
|
|
|
}
|
|
|
|
|
2018-11-03 22:40:07 +00:00
|
|
|
// XXX: deactivate + activate leads to a redundant serialization
|
|
|
|
|
|
|
|
func connectionService(willDeactivate profile: ConnectionProfile) {
|
2018-11-04 13:35:12 +00:00
|
|
|
TransientStore.shared.serialize(withProfiles: false) // deactivate
|
2018-11-03 22:40:07 +00:00
|
|
|
|
2018-11-02 13:20:40 +00:00
|
|
|
tableView.reloadData()
|
|
|
|
}
|
|
|
|
|
|
|
|
func connectionService(didActivate profile: ConnectionProfile) {
|
2018-11-04 13:35:12 +00:00
|
|
|
TransientStore.shared.serialize(withProfiles: false) // activate
|
2018-11-03 22:40:07 +00:00
|
|
|
|
2018-11-02 13:20:40 +00:00
|
|
|
tableView.reloadData()
|
2019-03-09 08:53:48 +00:00
|
|
|
|
|
|
|
if #available(iOS 12, *) {
|
2019-03-19 16:12:24 +00:00
|
|
|
IntentDispatcher.donateEnableVPN()
|
2019-03-09 08:53:48 +00:00
|
|
|
}
|
2018-11-02 13:20:40 +00:00
|
|
|
}
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|