2019-03-18 19:14:59 +00:00
|
|
|
//
|
2019-03-27 19:23:53 +00:00
|
|
|
// ShortcutsAddViewController.swift
|
2021-01-01 16:53:28 +00:00
|
|
|
// Passepartout
|
2019-03-18 19:14:59 +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 19:14:59 +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
|
2019-03-27 21:18:41 +00:00
|
|
|
import Intents
|
2019-05-26 06:56:39 +00:00
|
|
|
import PassepartoutCore
|
2019-10-11 08:56:23 +00:00
|
|
|
import Convenience
|
2019-03-18 19:14:59 +00:00
|
|
|
|
2019-03-27 19:52:58 +00:00
|
|
|
@available(iOS 12, *)
|
2019-10-11 08:56:23 +00:00
|
|
|
class ShortcutsAddViewController: UITableViewController, StrongTableHost {
|
2019-03-27 21:18:41 +00:00
|
|
|
weak var delegate: ShortcutsIntentDelegate?
|
2019-03-18 19:14:59 +00:00
|
|
|
|
2019-10-11 08:56:23 +00:00
|
|
|
// MARK: StrongTableModel
|
2019-03-18 19:53:46 +00:00
|
|
|
|
2019-10-11 08:56:23 +00:00
|
|
|
let model: StrongTableModel<SectionType, RowType> = {
|
|
|
|
let model: StrongTableModel<SectionType, RowType> = StrongTableModel()
|
2019-03-18 19:59:28 +00:00
|
|
|
model.add(.vpn)
|
2019-03-19 16:35:27 +00:00
|
|
|
model.add(.wifi)
|
|
|
|
model.add(.cellular)
|
2019-10-11 08:56:23 +00:00
|
|
|
model.set([.connect, .enableVPN, .disableVPN], forSection: .vpn)
|
|
|
|
model.set([.trustCurrentWiFi, .untrustCurrentWiFi], forSection: .wifi)
|
|
|
|
model.set([.trustCellular, .untrustCellular], forSection: .cellular)
|
|
|
|
model.setHeader(L10n.Core.Shortcuts.Add.Sections.Vpn.header, forSection: .vpn)
|
|
|
|
model.setHeader(L10n.Core.Shortcuts.Add.Sections.Wifi.header, forSection: .wifi)
|
|
|
|
model.setHeader(L10n.Core.Shortcuts.Add.Sections.Cellular.header, forSection: .cellular)
|
2019-03-18 19:53:46 +00:00
|
|
|
return model
|
|
|
|
}()
|
|
|
|
|
|
|
|
func reloadModel() {
|
|
|
|
}
|
|
|
|
|
2019-03-18 19:14:59 +00:00
|
|
|
// MARK: UIViewController
|
|
|
|
|
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
|
|
|
|
2019-06-21 21:15:43 +00:00
|
|
|
title = L10n.App.Shortcuts.Add.title
|
2019-03-18 19:14:59 +00:00
|
|
|
}
|
2019-03-18 19:53:46 +00:00
|
|
|
|
2019-03-27 19:36:20 +00:00
|
|
|
// MARK: UITableViewController
|
|
|
|
|
2019-03-18 19:53:46 +00:00
|
|
|
enum SectionType {
|
2019-03-18 19:59:28 +00:00
|
|
|
case vpn
|
|
|
|
|
2019-03-19 16:35:27 +00:00
|
|
|
case wifi
|
|
|
|
|
|
|
|
case cellular
|
2019-03-18 19:53:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
enum RowType {
|
|
|
|
case connect // host or provider+location
|
|
|
|
|
|
|
|
case enableVPN
|
|
|
|
|
|
|
|
case disableVPN
|
|
|
|
|
2019-03-19 16:35:27 +00:00
|
|
|
case trustCurrentWiFi
|
2019-03-18 19:53:46 +00:00
|
|
|
|
2019-03-19 16:35:27 +00:00
|
|
|
case untrustCurrentWiFi
|
2019-03-18 19:53:46 +00:00
|
|
|
|
|
|
|
case trustCellular
|
|
|
|
|
|
|
|
case untrustCellular
|
|
|
|
}
|
|
|
|
|
2019-03-18 19:59:28 +00:00
|
|
|
override func numberOfSections(in tableView: UITableView) -> Int {
|
2019-10-11 08:56:23 +00:00
|
|
|
return model.numberOfSections
|
2019-03-18 19:59:28 +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 19:59:28 +00:00
|
|
|
}
|
|
|
|
|
2019-03-18 19:53:46 +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 19:53:46 +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 .connect:
|
2019-06-21 21:15:43 +00:00
|
|
|
cell.leftText = L10n.Core.Shortcuts.Add.Cells.Connect.caption
|
2019-03-18 19:53:46 +00:00
|
|
|
|
|
|
|
case .enableVPN:
|
2019-06-21 21:15:43 +00:00
|
|
|
cell.leftText = L10n.Core.Shortcuts.Add.Cells.EnableVpn.caption
|
2019-03-18 19:53:46 +00:00
|
|
|
|
|
|
|
case .disableVPN:
|
2019-06-21 21:15:43 +00:00
|
|
|
cell.leftText = L10n.Core.Shortcuts.Add.Cells.DisableVpn.caption
|
2019-03-18 19:53:46 +00:00
|
|
|
|
2019-03-19 16:35:27 +00:00
|
|
|
case .trustCurrentWiFi:
|
2019-06-21 21:15:43 +00:00
|
|
|
cell.leftText = L10n.Core.Shortcuts.Add.Cells.TrustCurrentWifi.caption
|
2019-03-18 19:53:46 +00:00
|
|
|
|
2019-03-19 16:35:27 +00:00
|
|
|
case .untrustCurrentWiFi:
|
2019-06-21 21:15:43 +00:00
|
|
|
cell.leftText = L10n.Core.Shortcuts.Add.Cells.UntrustCurrentWifi.caption
|
2019-03-18 19:53:46 +00:00
|
|
|
|
|
|
|
case .trustCellular:
|
2019-06-21 21:15:43 +00:00
|
|
|
cell.leftText = L10n.Core.Shortcuts.Add.Cells.TrustCellular.caption
|
2019-03-18 19:53:46 +00:00
|
|
|
|
|
|
|
case .untrustCellular:
|
2019-06-21 21:15:43 +00:00
|
|
|
cell.leftText = L10n.Core.Shortcuts.Add.Cells.UntrustCellular.caption
|
2019-03-18 19:53:46 +00:00
|
|
|
}
|
|
|
|
return cell
|
|
|
|
}
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
|
|
|
switch model.row(at: indexPath) {
|
|
|
|
case .connect:
|
|
|
|
addConnect()
|
|
|
|
|
|
|
|
case .enableVPN:
|
|
|
|
addEnable()
|
|
|
|
|
|
|
|
case .disableVPN:
|
|
|
|
addDisable()
|
|
|
|
|
2019-03-19 16:35:27 +00:00
|
|
|
case .trustCurrentWiFi:
|
2019-03-18 19:53:46 +00:00
|
|
|
addTrustWiFi()
|
|
|
|
|
2019-03-19 16:35:27 +00:00
|
|
|
case .untrustCurrentWiFi:
|
2019-03-18 19:53:46 +00:00
|
|
|
addUntrustWiFi()
|
|
|
|
|
|
|
|
case .trustCellular:
|
|
|
|
addTrustCellular()
|
|
|
|
|
|
|
|
case .untrustCellular:
|
|
|
|
addUntrustCellular()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-27 19:36:20 +00:00
|
|
|
// MARK: Actions
|
2019-03-27 20:04:55 +00:00
|
|
|
|
|
|
|
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
|
|
|
|
if let vc = segue.destination as? ShortcutsConnectToViewController {
|
|
|
|
vc.delegate = delegate
|
|
|
|
}
|
|
|
|
}
|
2019-03-18 19:53:46 +00:00
|
|
|
|
|
|
|
private func addConnect() {
|
2019-03-18 21:00:55 +00:00
|
|
|
guard TransientStore.shared.service.hasProfiles() else {
|
2019-10-11 08:56:23 +00:00
|
|
|
let alert = UIAlertController.asAlert(
|
2019-06-21 21:15:43 +00:00
|
|
|
L10n.Core.Shortcuts.Add.Cells.Connect.caption,
|
|
|
|
L10n.Core.Shortcuts.Add.Alerts.NoProfiles.message
|
2019-03-18 21:00:55 +00:00
|
|
|
)
|
2019-06-21 21:15:43 +00:00
|
|
|
alert.addAction(L10n.Core.Global.ok) {
|
2019-03-18 21:00:55 +00:00
|
|
|
if let ip = self.tableView.indexPathForSelectedRow {
|
|
|
|
self.tableView.deselectRow(at: ip, animated: true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
present(alert, animated: true, completion: nil)
|
2019-03-18 19:53:46 +00:00
|
|
|
return
|
|
|
|
}
|
2019-03-18 21:00:55 +00:00
|
|
|
perform(segue: StoryboardSegue.Shortcuts.connectToSegueIdentifier)
|
2019-03-18 19:53:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private func addEnable() {
|
2019-03-27 22:47:35 +00:00
|
|
|
addShortcut(with: IntentDispatcher.intentEnable())
|
2019-03-18 19:53:46 +00:00
|
|
|
}
|
2019-03-18 19:14:59 +00:00
|
|
|
|
2019-03-18 19:53:46 +00:00
|
|
|
private func addDisable() {
|
2019-03-27 22:47:35 +00:00
|
|
|
addShortcut(with: IntentDispatcher.intentDisable())
|
2019-03-18 19:53:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private func addTrustWiFi() {
|
2019-03-27 22:47:35 +00:00
|
|
|
addShortcut(with: IntentDispatcher.intentTrustWiFi())
|
2019-03-18 19:53:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private func addUntrustWiFi() {
|
2019-03-27 22:47:35 +00:00
|
|
|
addShortcut(with: IntentDispatcher.intentUntrustWiFi())
|
2019-03-18 19:53:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private func addTrustCellular() {
|
2019-03-27 22:47:35 +00:00
|
|
|
addShortcut(with: IntentDispatcher.intentTrustCellular())
|
2019-03-18 19:53:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private func addUntrustCellular() {
|
2019-03-27 22:47:35 +00:00
|
|
|
addShortcut(with: IntentDispatcher.intentUntrustCellular())
|
2019-03-18 19:53:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private func addShortcut(with intent: INIntent) {
|
2019-03-27 21:18:41 +00:00
|
|
|
delegate?.shortcutsDidSelectIntent(intent: intent)
|
2019-03-18 19:14:59 +00:00
|
|
|
}
|
2019-03-28 07:24:39 +00:00
|
|
|
|
|
|
|
@IBAction private func close() {
|
|
|
|
dismiss(animated: true, completion: nil)
|
|
|
|
}
|
2019-03-18 19:14:59 +00:00
|
|
|
}
|