passepartout-apple/Passepartout/App/iOS/Scenes/Organizer/ImportedHostsViewController...

92 lines
3.1 KiB
Swift
Raw Normal View History

2018-10-27 08:25:21 +00:00
//
// ImportedHostsViewController.swift
2021-01-01 16:53:28 +00:00
// Passepartout
2018-10-27 08:25:21 +00:00
//
// Created by Davide De Rosa on 10/27/18.
2020-12-27 16:30:25 +00:00
// Copyright (c) 2021 Davide De Rosa. All rights reserved.
2018-10-27 08:25:21 +00:00
//
2018-11-03 21:33:30 +00:00
// https://github.com/passepartoutvpn
2018-10-27 08:25:21 +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/>.
//
2018-10-27 08:25:21 +00:00
import UIKit
import TunnelKitCore
import SwiftyBeaver
import PassepartoutConstants
import PassepartoutCore
private let log = SwiftyBeaver.self
2018-10-27 08:25:21 +00:00
2021-06-26 07:49:48 +00:00
protocol ImportedHostsViewControllerDelegate: AnyObject {
func importedHostsController(_: ImportedHostsViewController, didImport url: URL)
}
2018-10-27 08:25:21 +00:00
class ImportedHostsViewController: UITableViewController {
2018-10-27 14:48:38 +00:00
private lazy var pendingConfigurationURLs = TransientStore.shared.service.pendingConfigurationURLs().sortedCaseInsensitive()
weak var delegate: ImportedHostsViewControllerDelegate?
2018-10-27 08:25:21 +00:00
override func viewDidLoad() {
super.viewDidLoad()
title = L10n.ImportedHosts.title
2018-10-27 08:25:21 +00:00
}
private func selectHost(withUrl url: URL) {
delegate?.importedHostsController(self, didImport: url)
}
2018-10-27 08:25:21 +00:00
@IBAction private func close() {
dismiss(animated: true, completion: nil)
}
private func deselectSelectedRow() {
if let selectedIP = tableView.indexPathForSelectedRow {
tableView.deselectRow(at: selectedIP, animated: true)
}
}
2018-10-27 08:25:21 +00:00
}
extension ImportedHostsViewController {
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return pendingConfigurationURLs.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let url = pendingConfigurationURLs[indexPath.row]
let cell = Cells.setting.dequeue(from: tableView, for: indexPath)
cell.leftText = url.normalizedFilename
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let url = pendingConfigurationURLs[indexPath.row]
selectHost(withUrl: url)
}
2018-10-27 10:28:40 +00:00
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
let url = pendingConfigurationURLs[indexPath.row]
try? FileManager.default.removeItem(at: url)
pendingConfigurationURLs.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .top)
}
}