Add reset cell to revert to original configuration

When a backing .ovpn is available.
This commit is contained in:
Davide De Rosa 2018-10-22 11:16:05 +02:00
parent 346a9490ec
commit be17f57a5e
5 changed files with 69 additions and 4 deletions

View File

@ -25,6 +25,9 @@
import UIKit
import TunnelKit
import SwiftyBeaver
private let log = SwiftyBeaver.self
class ConfigurationViewController: UIViewController, TableModelHost {
@IBOutlet private weak var tableView: UITableView!
@ -37,15 +40,21 @@ class ConfigurationViewController: UIViewController, TableModelHost {
var isEditable = false
var originalConfigurationURL: URL?
weak var delegate: ConfigurationModificationDelegate?
// MARK: TableModelHost
lazy var model: TableModel<SectionType, RowType> = {
let model: TableModel<SectionType, RowType> = TableModel()
let hasConfigurationURL = isEditable && (originalConfigurationURL != nil)
// sections
model.add(.communication)
if hasConfigurationURL {
model.add(.reset)
}
model.add(.tls)
model.add(.other)
@ -55,12 +64,17 @@ class ConfigurationViewController: UIViewController, TableModelHost {
model.setHeader(L10n.Configuration.Sections.Other.header, for: .other)
// footers
if isEditable {
if hasConfigurationURL {
model.setFooter(L10n.Configuration.Sections.Reset.footer, for: .reset)
} else if isEditable {
model.setFooter(L10n.Configuration.Sections.Communication.Footer.editable, for: .communication)
}
// rows
model.set([.cipher, .digest, .compressionFrame], in: .communication)
if hasConfigurationURL {
model.set([.resetOriginal], in: .reset)
}
model.set([.client, .tlsWrapping], in: .tls)
model.set([.compressionAlgorithm, .keepAlive, .renegSeconds], in: .other)
@ -103,6 +117,14 @@ class ConfigurationViewController: UIViewController, TableModelHost {
// MARK: Actions
private func resetOriginalConfiguration() {
guard let url = originalConfigurationURL else {
log.warning("Resetting with no original configuration set? Bad table model?")
return
}
// TODO
}
@IBAction private func refresh() {
guard isEditable else {
return
@ -119,7 +141,9 @@ class ConfigurationViewController: UIViewController, TableModelHost {
extension ConfigurationViewController: UITableViewDataSource, UITableViewDelegate {
enum SectionType: Int {
case communication
case reset
case tls
case other
@ -132,6 +156,8 @@ extension ConfigurationViewController: UITableViewDataSource, UITableViewDelegat
case compressionFrame
case resetOriginal
case client
case tlsWrapping
@ -155,6 +181,16 @@ extension ConfigurationViewController: UITableViewDataSource, UITableViewDelegat
return model.footer(for: section)
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
guard let title = model.header(for: section) else {
return 1.0
}
guard !title.isEmpty else {
return 0.0
}
return UITableView.automaticDimension
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return model.count(for: section)
}
@ -186,6 +222,10 @@ extension ConfigurationViewController: UITableViewDataSource, UITableViewDelegat
cell.leftText = L10n.Configuration.Cells.CompressionFrame.caption
cell.rightText = configuration.compressionFraming.cellDescription
case .resetOriginal:
cell.leftText = L10n.Configuration.Cells.ResetOriginal.caption
cell.applyAction(Theme.current)
case .client:
cell.leftText = L10n.Configuration.Cells.Client.caption
cell.rightText = (configuration.clientCertificate != nil) ? L10n.Configuration.Cells.Client.Value.enabled : L10n.Configuration.Cells.Client.Value.disabled
@ -283,6 +323,10 @@ extension ConfigurationViewController: UITableViewDataSource, UITableViewDelegat
}
navigationController?.pushViewController(vc, animated: true)
case .resetOriginal:
tableView.deselectRow(at: indexPath, animated: true)
resetOriginalConfiguration()
default:
break
}

View File

@ -155,6 +155,7 @@ class ServiceViewController: UIViewController, TableModelHost {
vc?.title = L10n.Service.Cells.Host.Parameters.caption
vc?.initialConfiguration = uncheckedHostProfile.parameters
vc?.isEditable = true
vc?.originalConfigurationURL = ProfileConfigurationFactory.shared.configurationURL(for: uncheckedHostProfile)
vc?.delegate = self
case .debugLogSegueIdentifier:

View File

@ -126,6 +126,7 @@
"configuration.sections.communication.header" = "Communication";
"configuration.sections.communication.footer.editable" = "Make sure to match server communication parameters, otherwise you will end up with broken connectivity.";
"configuration.sections.reset.footer" = "If you ended up with broken connectivity after changing the communication parameters, tap to revert to the original configuration.";
"configuration.sections.tls.header" = "TLS";
"configuration.sections.other.header" = "Other";
"configuration.cells.cipher.caption" = "Cipher";
@ -135,6 +136,7 @@
"configuration.cells.compression_frame.value.disabled" = "None";
"configuration.cells.compression_frame.value.lzo" = "LZO";
"configuration.cells.compression_frame.value.compress" = "Compress";
"configuration.cells.reset_original.caption" = "Reset configuration";
"configuration.cells.client.caption" = "Client certificate";
"configuration.cells.client.value.enabled" = "Verified";
"configuration.cells.client.value.disabled" = "Not verified";

View File

@ -41,12 +41,20 @@ class ProfileConfigurationFactory {
}
func save(url: URL, for profile: ConnectionProfile) throws -> URL {
let savedUrl = configurationURL(for: profile)
let savedUrl = targetConfigurationURL(for: profile)
try FileManager.default.copyItem(at: url, to: savedUrl)
return savedUrl
}
func configurationURL(for profile: ConnectionProfile) -> URL {
func configurationURL(for profile: ConnectionProfile) -> URL? {
let url = targetConfigurationURL(for: profile)
guard FileManager.default.fileExists(atPath: url.path) else {
return nil
}
return url
}
private func targetConfigurationURL(for profile: ConnectionProfile) -> URL {
let filename = "\(profile.id).ovpn"
return configurationsPath.appendingPathComponent(filename)
}

View File

@ -172,6 +172,11 @@ internal enum L10n {
}
}
internal enum ResetOriginal {
/// Reset configuration
internal static let caption = L10n.tr("Localizable", "configuration.cells.reset_original.caption")
}
internal enum TlsWrapping {
/// Wrapping
internal static let caption = L10n.tr("Localizable", "configuration.cells.tls_wrapping.caption")
@ -204,6 +209,11 @@ internal enum L10n {
internal static let header = L10n.tr("Localizable", "configuration.sections.other.header")
}
internal enum Reset {
/// If you ended up with broken connectivity after changing the communication parameters, tap to revert to the original configuration.
internal static let footer = L10n.tr("Localizable", "configuration.sections.reset.footer")
}
internal enum Tls {
/// TLS
internal static let header = L10n.tr("Localizable", "configuration.sections.tls.header")