From 42e2c83eb5b5ca0ec9a6f8d9fd6eb0a2e5a0d73d Mon Sep 17 00:00:00 2001 From: Davide De Rosa Date: Fri, 21 Jun 2019 14:39:20 +0200 Subject: [PATCH 1/2] Move platform-specific extensions around --- Passepartout-iOS/Global/Macros.swift | 37 ++++++++++++++++++++++++++++ Passepartout-iOS/Global/Theme.swift | 18 -------------- 2 files changed, 37 insertions(+), 18 deletions(-) diff --git a/Passepartout-iOS/Global/Macros.swift b/Passepartout-iOS/Global/Macros.swift index 9029d1fe..65ed21e2 100644 --- a/Passepartout-iOS/Global/Macros.swift +++ b/Passepartout-iOS/Global/Macros.swift @@ -73,3 +73,40 @@ extension UIAlertController { return action } } + +extension UIView { + static func get() -> T { + let name = String(describing: T.self) + let nib = UINib(nibName: name, bundle: nil) + let objects = nib.instantiate(withOwner: nil) + for o in objects { + if let view = o as? T { + return view + } + } + fatalError() + } +} + +extension UITableView { + func scrollToRowAsync(at indexPath: IndexPath) { + DispatchQueue.main.async { [weak self] in + self?.scrollToRow(at: indexPath, at: .middle, animated: false) + } + } + + func selectRowAsync(at indexPath: IndexPath) { + DispatchQueue.main.async { [weak self] in + self?.selectRow(at: indexPath, animated: false, scrollPosition: .middle) + } + } +} + +extension UIColor { + convenience init(rgb: UInt32, alpha: CGFloat) { + let r = CGFloat((rgb & 0xff0000) >> 16) / 255.0 + let g = CGFloat((rgb & 0xff00) >> 8) / 255.0 + let b = CGFloat(rgb & 0xff) / 255.0 + self.init(red: r, green: g, blue: b, alpha: alpha) + } +} diff --git a/Passepartout-iOS/Global/Theme.swift b/Passepartout-iOS/Global/Theme.swift index 4d647c45..b43f64dd 100644 --- a/Passepartout-iOS/Global/Theme.swift +++ b/Passepartout-iOS/Global/Theme.swift @@ -28,15 +28,6 @@ import MessageUI import StoreKit import PassepartoutCore -extension UIColor { - convenience init(rgb: UInt32, alpha: CGFloat) { - let r = CGFloat((rgb & 0xff0000) >> 16) / 255.0 - let g = CGFloat((rgb & 0xff00) >> 8) / 255.0 - let b = CGFloat(rgb & 0xff) / 255.0 - self.init(red: r, green: g, blue: b, alpha: alpha) - } -} - struct Theme { struct Palette { var primaryBackground = UIColor(rgb: 0x515d71, alpha: 1.0) @@ -174,12 +165,3 @@ extension PoolGroup { return ImageAsset(name: country.lowercased()).image } } - -extension SKProduct { - var localizedPrice: String? { - let fmt = NumberFormatter() - fmt.numberStyle = .currency - fmt.locale = priceLocale - return fmt.string(from: price) - } -} From 2e362bf5532479447b8c506bb0b49f2f4a0e7772 Mon Sep 17 00:00:00 2001 From: Davide De Rosa Date: Sat, 22 Jun 2019 09:34:43 +0200 Subject: [PATCH 2/2] Reuse Core extensions - NetworkChoice.choices() - OpenVPN.* available options - ConnectionProfile.clientNetworkSettings --- .../Scenes/ConfigurationViewController.swift | 8 ++++---- .../NetworkSettingsViewController.swift | 20 ++++--------------- Passepartout.xcodeproj/project.pbxproj | 4 ++++ 3 files changed, 12 insertions(+), 20 deletions(-) diff --git a/Passepartout-iOS/Scenes/ConfigurationViewController.swift b/Passepartout-iOS/Scenes/ConfigurationViewController.swift index 5559b4b4..5f08e86a 100644 --- a/Passepartout-iOS/Scenes/ConfigurationViewController.swift +++ b/Passepartout-iOS/Scenes/ConfigurationViewController.swift @@ -315,7 +315,7 @@ extension ConfigurationViewController: UITableViewDataSource, UITableViewDelegat case .cipher: let vc = OptionViewController() vc.title = settingCell?.leftText - vc.options = [.aes128cbc, .aes192cbc, .aes256cbc, .aes128gcm, .aes192gcm, .aes256gcm] + vc.options = OpenVPN.Cipher.available vc.selectedOption = configuration.cipher vc.descriptionBlock = { $0.description } vc.selectionBlock = { [weak self] in @@ -327,7 +327,7 @@ extension ConfigurationViewController: UITableViewDataSource, UITableViewDelegat case .digest: let vc = OptionViewController() vc.title = settingCell?.leftText - vc.options = [.sha1, .sha224, .sha256, .sha384, .sha512] + vc.options = OpenVPN.Digest.available vc.selectedOption = configuration.digest vc.descriptionBlock = { $0.description } vc.selectionBlock = { [weak self] in @@ -339,7 +339,7 @@ extension ConfigurationViewController: UITableViewDataSource, UITableViewDelegat case .compressionFraming: let vc = OptionViewController() vc.title = settingCell?.leftText - vc.options = [.disabled, .compLZO, .compress] + vc.options = OpenVPN.CompressionFraming.available vc.selectedOption = configuration.compressionFraming ?? .disabled vc.descriptionBlock = { $0.cellDescription } vc.selectionBlock = { [weak self] in @@ -358,7 +358,7 @@ extension ConfigurationViewController: UITableViewDataSource, UITableViewDelegat let vc = OptionViewController() vc.title = settingCell?.leftText - vc.options = [.disabled, .LZO] + vc.options = OpenVPN.CompressionAlgorithm.available vc.selectedOption = configuration.compressionAlgorithm ?? .disabled vc.descriptionBlock = { $0.cellDescription } vc.selectionBlock = { [weak self] in diff --git a/Passepartout-iOS/Scenes/NetworkSettingsViewController.swift b/Passepartout-iOS/Scenes/NetworkSettingsViewController.swift index 15961bce..30f18a8d 100644 --- a/Passepartout-iOS/Scenes/NetworkSettingsViewController.swift +++ b/Passepartout-iOS/Scenes/NetworkSettingsViewController.swift @@ -66,19 +66,7 @@ class NetworkSettingsViewController: UITableViewController { private let networkSettings = ProfileNetworkSettings() - private lazy var clientNetworkSettings: ProfileNetworkSettings? = { - guard let hostProfile = profile as? HostConnectionProfile else { - return nil - } - return ProfileNetworkSettings(from: hostProfile.parameters.sessionConfiguration) - }() - - private var choices: [NetworkChoice] { - guard let _ = clientNetworkSettings else { - return [.server, .manual] - } - return [.client, .server, .manual] - } + private lazy var clientNetworkSettings = profile?.clientNetworkSettings // MARK: TableModelHost @@ -430,7 +418,7 @@ extension NetworkSettingsViewController { case .gateway: let vc = OptionViewController() vc.title = (cell as? SettingTableViewCell)?.leftText - vc.options = choices + vc.options = NetworkChoice.choices(for: profile) vc.descriptionBlock = { $0.description } vc.selectedOption = networkChoices.gateway @@ -443,7 +431,7 @@ extension NetworkSettingsViewController { case .dns: let vc = OptionViewController() vc.title = (cell as? SettingTableViewCell)?.leftText - vc.options = choices + vc.options = NetworkChoice.choices(for: profile) vc.descriptionBlock = { $0.description } vc.selectedOption = networkChoices.dns @@ -456,7 +444,7 @@ extension NetworkSettingsViewController { case .proxy: let vc = OptionViewController() vc.title = (cell as? SettingTableViewCell)?.leftText - vc.options = choices + vc.options = NetworkChoice.choices(for: profile) vc.descriptionBlock = { $0.description } vc.selectedOption = networkChoices.proxy diff --git a/Passepartout.xcodeproj/project.pbxproj b/Passepartout.xcodeproj/project.pbxproj index 2bff6ba7..4e6db6d7 100644 --- a/Passepartout.xcodeproj/project.pbxproj +++ b/Passepartout.xcodeproj/project.pbxproj @@ -66,6 +66,7 @@ 0E45E6E022BD793800F19312 /* App.strings in Resources */ = {isa = PBXBuildFile; fileRef = 0E45E6E222BD793800F19312 /* App.strings */; }; 0E45E6E422BD799700F19312 /* SwiftGen+Strings.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E45E6E322BD799700F19312 /* SwiftGen+Strings.swift */; }; 0E45E6FA22BD8FC500F19312 /* Core.strings in Resources */ = {isa = PBXBuildFile; fileRef = 0E3CAF98229AAE760008E5C8 /* Core.strings */; }; + 0E45E71022BE108100F19312 /* OpenVPNOptions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E45E70F22BE108100F19312 /* OpenVPNOptions.swift */; }; 0E4C9CBB20DCF0D600A0C59C /* DestructiveTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E4C9CBA20DCF0D600A0C59C /* DestructiveTableViewCell.swift */; }; 0E4FD7F120D58618002221FF /* Macros.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E4FD7F020D58618002221FF /* Macros.swift */; }; 0E533B162258E03B00EF94FC /* PoolGroup.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E533B152258E03B00EF94FC /* PoolGroup.swift */; }; @@ -203,6 +204,7 @@ 0E45E6F722BD898800F19312 /* fr */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = fr; path = fr.lproj/App.strings; sourceTree = ""; }; 0E45E6F822BD898A00F19312 /* es */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = es; path = es.lproj/App.strings; sourceTree = ""; }; 0E45E6F922BD898B00F19312 /* pt */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = pt; path = pt.lproj/App.strings; sourceTree = ""; }; + 0E45E70F22BE108100F19312 /* OpenVPNOptions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OpenVPNOptions.swift; sourceTree = ""; }; 0E4C9CB820DB9BC600A0C59C /* TrustedNetworks.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TrustedNetworks.swift; sourceTree = ""; }; 0E4C9CBA20DCF0D600A0C59C /* DestructiveTableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DestructiveTableViewCell.swift; sourceTree = ""; }; 0E4FD7DD20D3E49A002221FF /* StandardVPNProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StandardVPNProvider.swift; sourceTree = ""; }; @@ -502,6 +504,7 @@ 0ECEB109224FECEA00E9E551 /* DataUnit.swift */, 0EC7F20420E24308004EA58E /* DebugLog.swift */, 0ED38AE621404F100004D387 /* EndpointDataSource.swift */, + 0E45E70F22BE108100F19312 /* OpenVPNOptions.swift */, 0E89DFC4213DF7AE00741BA1 /* Preferences.swift */, 0EFB901722764689006405E4 /* ProfileNetworkSettings.swift */, 0E89DFC7213E8FC500741BA1 /* SessionProxy+Communication.swift */, @@ -942,6 +945,7 @@ 0E3152C2223FA04800F61841 /* MockVPNProvider.swift in Sources */, 0E533B162258E03B00EF94FC /* PoolGroup.swift in Sources */, 0E3152D2223FA05400F61841 /* DebugLog.swift in Sources */, + 0E45E71022BE108100F19312 /* OpenVPNOptions.swift in Sources */, 0E3152C4223FA04800F61841 /* VPN.swift in Sources */, 0E3152C6223FA04800F61841 /* VPNProvider.swift in Sources */, 0E3152DA223FA05800F61841 /* PlaceholderConnectionProfile.swift in Sources */,