2019-03-18 21:00:55 +00:00
|
|
|
//
|
|
|
|
// ShortcutsConnectToViewController.swift
|
2021-01-01 16:53:28 +00:00
|
|
|
// Passepartout
|
2019-03-18 21:00:55 +00:00
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 3/18/19.
|
2020-12-27 16:30:25 +00:00
|
|
|
// Copyright (c) 2021 Davide De Rosa. All rights reserved.
|
2019-03-18 21:00:55 +00:00
|
|
|
//
|
|
|
|
// https://github.com/passepartoutvpn
|
|
|
|
//
|
|
|
|
// 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 Intents
|
|
|
|
import IntentsUI
|
2019-05-26 06:56:39 +00:00
|
|
|
import PassepartoutCore
|
2019-10-11 08:56:23 +00:00
|
|
|
import Convenience
|
2019-03-18 21:00:55 +00:00
|
|
|
|
2019-03-27 19:36:20 +00:00
|
|
|
@available(iOS 12, *)
|
2019-10-11 08:56:23 +00:00
|
|
|
class ShortcutsConnectToViewController: UITableViewController, ProviderPoolViewControllerDelegate, StrongTableHost {
|
2019-03-18 21:00:55 +00:00
|
|
|
private let service = TransientStore.shared.service
|
|
|
|
|
2020-02-28 17:32:04 +00:00
|
|
|
private var providers: [Infrastructure.Name] = []
|
2019-03-18 21:00:55 +00:00
|
|
|
|
|
|
|
private var hosts: [String] = []
|
|
|
|
|
|
|
|
private var selectedProfile: ConnectionProfile?
|
|
|
|
|
2019-03-27 21:18:41 +00:00
|
|
|
weak var delegate: ShortcutsIntentDelegate?
|
2019-03-27 20:04:55 +00:00
|
|
|
|
2019-10-11 08:56:23 +00:00
|
|
|
// MARK: StrongTableHost
|
2019-03-18 21:00:55 +00:00
|
|
|
|
2019-10-11 08:56:23 +00:00
|
|
|
let model: StrongTableModel<SectionType, RowType> = {
|
|
|
|
let model: StrongTableModel<SectionType, RowType> = StrongTableModel()
|
|
|
|
model.setHeader(L10n.Core.Organizer.Sections.Providers.header, forSection: .providers)
|
|
|
|
model.setHeader(L10n.Core.Organizer.Sections.Hosts.header, forSection: .hosts)
|
2019-03-18 21:00:55 +00:00
|
|
|
return model
|
|
|
|
}()
|
|
|
|
|
|
|
|
func reloadModel() {
|
2020-02-28 17:32:04 +00:00
|
|
|
providers = service.sortedProviderNames()
|
|
|
|
hosts = service.sortedHostIds()
|
2019-03-18 21:00:55 +00:00
|
|
|
|
|
|
|
if !providers.isEmpty {
|
|
|
|
model.add(.providers)
|
2019-10-11 08:56:23 +00:00
|
|
|
model.set(.providerShortcut, count: providers.count, forSection: .providers)
|
2019-03-18 21:00:55 +00:00
|
|
|
}
|
|
|
|
if !hosts.isEmpty {
|
|
|
|
model.add(.hosts)
|
2019-10-11 08:56:23 +00:00
|
|
|
model.set(.hostShortcut, count: hosts.count, forSection: .hosts)
|
2019-03-18 21:00:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: UIViewController
|
|
|
|
|
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
|
|
|
|
2019-06-21 21:15:43 +00:00
|
|
|
title = L10n.Core.Shortcuts.Add.Cells.Connect.caption
|
2019-03-18 21:00:55 +00:00
|
|
|
reloadModel()
|
|
|
|
}
|
|
|
|
|
|
|
|
override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
|
|
|
|
guard identifier == StoryboardSegue.Shortcuts.pickLocationSegueIdentifier.rawValue else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
guard let _ = selectedProfile as? ProviderConnectionProfile else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
|
|
|
|
guard let vc = segue.destination as? ProviderPoolViewController else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
guard let provider = selectedProfile as? ProviderConnectionProfile else {
|
|
|
|
return
|
|
|
|
}
|
2019-04-25 18:40:50 +00:00
|
|
|
vc.setInfrastructure(provider.infrastructure, currentPoolId: nil)
|
2019-03-18 21:00:55 +00:00
|
|
|
vc.delegate = self
|
|
|
|
}
|
|
|
|
|
2019-03-27 19:36:20 +00:00
|
|
|
// MARK: UITableViewController
|
|
|
|
|
2019-03-18 21:00:55 +00:00
|
|
|
enum SectionType {
|
|
|
|
case providers
|
|
|
|
|
|
|
|
case hosts
|
|
|
|
}
|
|
|
|
|
|
|
|
enum RowType {
|
|
|
|
case providerShortcut
|
|
|
|
|
|
|
|
case hostShortcut
|
|
|
|
}
|
|
|
|
|
|
|
|
override func numberOfSections(in tableView: UITableView) -> Int {
|
2019-10-11 08:56:23 +00:00
|
|
|
return model.numberOfSections
|
2019-03-18 21:00:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
|
2019-10-11 08:56:23 +00:00
|
|
|
return model.header(forSection: section)
|
2019-03-18 21:00:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
2019-10-11 08:56:23 +00:00
|
|
|
return model.numberOfRows(forSection: section)
|
2019-03-18 21:00:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
|
|
|
let cell = Cells.setting.dequeue(from: tableView, for: indexPath)
|
|
|
|
switch model.row(at: indexPath) {
|
|
|
|
case .providerShortcut:
|
2020-02-28 17:32:04 +00:00
|
|
|
let name = providers[indexPath.row]
|
|
|
|
if let metadata = InfrastructureFactory.shared.metadata(forName: name) {
|
|
|
|
cell.leftText = metadata.description
|
|
|
|
} else {
|
|
|
|
cell.leftText = name
|
|
|
|
}
|
2019-03-18 21:00:55 +00:00
|
|
|
|
|
|
|
case .hostShortcut:
|
2020-02-28 17:32:04 +00:00
|
|
|
let id = hosts[indexPath.row]
|
|
|
|
cell.leftText = service.screenTitle(forHostId: id)
|
2019-03-18 21:00:55 +00:00
|
|
|
}
|
|
|
|
return cell
|
|
|
|
}
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
|
|
|
switch model.row(at: indexPath) {
|
|
|
|
case .providerShortcut:
|
2020-02-28 17:32:04 +00:00
|
|
|
selectedProfile = service.profile(withContext: .provider, id: providers[indexPath.row])
|
2019-03-18 21:00:55 +00:00
|
|
|
pickProviderLocation()
|
|
|
|
|
|
|
|
case .hostShortcut:
|
|
|
|
selectedProfile = service.profile(withContext: .host, id: hosts[indexPath.row])
|
|
|
|
addConnect()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-27 19:36:20 +00:00
|
|
|
// MARK: Actions
|
2019-03-18 21:00:55 +00:00
|
|
|
|
|
|
|
private func addConnect() {
|
|
|
|
guard let host = selectedProfile as? HostConnectionProfile else {
|
|
|
|
fatalError("Not a HostConnectionProfile")
|
|
|
|
}
|
2020-02-28 17:32:04 +00:00
|
|
|
let title = service.screenTitle(forHostId: host.id)
|
|
|
|
addShortcut(with: IntentDispatcher.intentConnect(profile: host, title: title))
|
2019-03-18 21:00:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private func addMoveToLocation(pool: Pool) {
|
|
|
|
guard let provider = selectedProfile as? ProviderConnectionProfile else {
|
|
|
|
fatalError("Not a ProviderConnectionProfile")
|
|
|
|
}
|
2019-03-27 22:47:35 +00:00
|
|
|
addShortcut(with: IntentDispatcher.intentMoveTo(profile: provider, pool: pool))
|
2019-03-18 21:00:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private func addShortcut(with intent: INIntent) {
|
2019-03-27 21:18:41 +00:00
|
|
|
delegate?.shortcutsDidSelectIntent(intent: intent)
|
2019-03-18 21:00:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private func pickProviderLocation() {
|
|
|
|
perform(segue: StoryboardSegue.Shortcuts.pickLocationSegueIdentifier)
|
|
|
|
}
|
2019-03-18 21:40:06 +00:00
|
|
|
|
2019-03-27 19:36:20 +00:00
|
|
|
// MARK: ProviderPoolViewControllerDelegate
|
|
|
|
|
2019-03-18 21:00:55 +00:00
|
|
|
func providerPoolController(_: ProviderPoolViewController, didSelectPool pool: Pool) {
|
|
|
|
addMoveToLocation(pool: pool)
|
|
|
|
}
|
2019-11-21 13:00:34 +00:00
|
|
|
|
|
|
|
func providerPoolController(_: ProviderPoolViewController, didUpdateFavoriteGroups favoriteGroupIds: [String]) {
|
|
|
|
}
|
2019-03-18 21:00:55 +00:00
|
|
|
}
|