From 4219b6f82c26f53b6f2492e3eace5719f49375df Mon Sep 17 00:00:00 2001 From: Cornelius Horstmann Date: Sat, 13 Mar 2021 15:26:18 +0100 Subject: [PATCH] Updates after swiftlint warnings --- Cauli/CauliURLProtocol/CauliURLProtocol.swift | 2 -- .../CauliViewController.swift | 2 +- Cauli/Floret.swift | 2 +- Cauli/Florets/MapRemote/MapRemoteFloret.swift | 35 +++++++++---------- Cauli/Florets/MapRemote/Mapping.swift | 4 +-- .../MapRemote/MappingsListDatasource.swift | 10 +++--- .../MappingsListViewController.swift | 8 ++--- 7 files changed, 30 insertions(+), 33 deletions(-) diff --git a/Cauli/CauliURLProtocol/CauliURLProtocol.swift b/Cauli/CauliURLProtocol/CauliURLProtocol.swift index 0feefdf..2a53b37 100644 --- a/Cauli/CauliURLProtocol/CauliURLProtocol.swift +++ b/Cauli/CauliURLProtocol/CauliURLProtocol.swift @@ -159,8 +159,6 @@ private extension CauliURLProtocol { class func handles(_ record: Record) -> Bool { delegates.contains { $0.handles(record) } } - - func willRequest(_ record: Record, modificationCompletionHandler completionHandler: @escaping (Record) -> Void) { CauliURLProtocol.delegates.cauli_reduceAsync(record, transform: { record, delegate, completion in diff --git a/Cauli/CauliViewController/CauliViewController.swift b/Cauli/CauliViewController/CauliViewController.swift index 029fd75..033670b 100644 --- a/Cauli/CauliViewController/CauliViewController.swift +++ b/Cauli/CauliViewController/CauliViewController.swift @@ -60,7 +60,7 @@ internal class CauliViewController: UITableViewController { let bundle = Bundle(for: SwitchTableViewCell.self) tableView.register(UINib(nibName: SwitchTableViewCell.nibName, bundle: bundle), forCellReuseIdentifier: SwitchTableViewCell.reuseIdentifier) } - + override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) tableView.reloadData() diff --git a/Cauli/Floret.swift b/Cauli/Floret.swift index d83840c..05a7c4a 100644 --- a/Cauli/Floret.swift +++ b/Cauli/Floret.swift @@ -43,7 +43,7 @@ public extension Floret { } var description: String? { - return nil + nil } } // swiftlint:enable missing_docs diff --git a/Cauli/Florets/MapRemote/MapRemoteFloret.swift b/Cauli/Florets/MapRemote/MapRemoteFloret.swift index f1815d9..e802497 100644 --- a/Cauli/Florets/MapRemote/MapRemoteFloret.swift +++ b/Cauli/Florets/MapRemote/MapRemoteFloret.swift @@ -34,40 +34,43 @@ import UIKit /// let floret = MapRemoteFloret(mappings: [httpsifyMapping, mapLocal]) /// Cauli([floret]) /// ``` -public class MapRemoteFloret { - +public class MapRemoteFloret: InterceptingFloret { + internal var userDefaults = UserDefaults() public var enabled: Bool { - set { - userDefaults.setValue(newValue, forKey: "Cauli.MapRemoteFloret.enabled") - } get { userDefaults.bool(forKey: "Cauli.MapRemoteFloret.enabled") } + set { + userDefaults.setValue(newValue, forKey: "Cauli.MapRemoteFloret.enabled") + } } + public var description: String? { "The MapRemoteFloret can modify the url of a request before performed. \n Currently \(enabledMappings.count) mappings are enabled." } - + private let mappings: [Mapping] private var enabledMappings: Set { - set { - userDefaults.setValue(Array(newValue), forKey: "Cauli.MapRemoteFloret.enabledMappings") - } get { guard let mappings = userDefaults.array(forKey: "Cauli.MapRemoteFloret.enabledMappings") as? [String] else { return [] } return Set(mappings) } + set { + userDefaults.setValue(Array(newValue), forKey: "Cauli.MapRemoteFloret.enabledMappings") + } } - + + /// Instantiates a new MapRemoteFloret instance with an array of mappings. + /// - Parameter mappings: An array of mappings. Mappings will be evaluated in the order of this array. public init(mappings: [Mapping]) { self.mappings = mappings } - + func isMappingEnabled(_ mapping: Mapping) -> Bool { enabledMappings.contains(mapping.name) } - + func setMapping(_ mapping: Mapping, enabled: Bool) { if enabled { enabledMappings.insert(mapping.name) @@ -75,11 +78,9 @@ public class MapRemoteFloret { enabledMappings.remove(mapping.name) } } -} -extension MapRemoteFloret: InterceptingFloret { public func willRequest(_ record: Record, modificationCompletionHandler completionHandler: @escaping (Record) -> Void) { - let mappedRecord = mappings.filter { enabledMappings.contains($0.name) }.reduce(record) { (record, mapping) -> Record in + let mappedRecord = mappings.filter { enabledMappings.contains($0.name) }.reduce(record) { record, mapping -> Record in guard let requestUrl = record.designatedRequest.url, mapping.sourceLocation.matches(url: requestUrl) else { return record } var record = record let updatedUrl = mapping.destinationLocation.updating(url: requestUrl) @@ -88,13 +89,11 @@ extension MapRemoteFloret: InterceptingFloret { } completionHandler(mappedRecord) } - + public func didRespond(_ record: Record, modificationCompletionHandler completionHandler: @escaping (Record) -> Void) { completionHandler(record) } -} -extension MapRemoteFloret: DisplayingFloret { public func viewController(_ cauli: Cauli) -> UIViewController { MappingsListViewController(mapRemoteFloret: self, mappings: mappings) } diff --git a/Cauli/Florets/MapRemote/Mapping.swift b/Cauli/Florets/MapRemote/Mapping.swift index f78e8e4..12dca34 100644 --- a/Cauli/Florets/MapRemote/Mapping.swift +++ b/Cauli/Florets/MapRemote/Mapping.swift @@ -42,7 +42,7 @@ public struct Mapping { let name: String let sourceLocation: MappingLocation let destinationLocation: MappingLocation - + /// Initializes a new Mapping. /// - Parameters: /// - name: The name of the Mapping. This is used to uniquely identify this mapping. @@ -61,7 +61,7 @@ public struct MappingLocation { let port: Int? let path: String? let query: String? - + public init(`protocol`: Protocol? = nil, host: String? = nil, port: Int? = nil, path: String? = nil, query: String? = nil) { self.`protocol` = `protocol` self.host = host diff --git a/Cauli/Florets/MapRemote/MappingsListDatasource.swift b/Cauli/Florets/MapRemote/MappingsListDatasource.swift index d848148..43d2ef3 100644 --- a/Cauli/Florets/MapRemote/MappingsListDatasource.swift +++ b/Cauli/Florets/MapRemote/MappingsListDatasource.swift @@ -23,24 +23,24 @@ import UIKit class MappingsListDatasource: NSObject, UITableViewDataSource { - + private let mapRemoteFloret: MapRemoteFloret private let mappings: [Mapping] - + init(mapRemoteFloret: MapRemoteFloret, mappings: [Mapping]) { self.mapRemoteFloret = mapRemoteFloret self.mappings = mappings } - + func setup(_ tableView: UITableView) { let bundle = Bundle(for: SwitchTableViewCell.self) tableView.register(UINib(nibName: SwitchTableViewCell.nibName, bundle: bundle), forCellReuseIdentifier: SwitchTableViewCell.reuseIdentifier) } - + func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { mappings.count } - + func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: SwitchTableViewCell.reuseIdentifier, for: indexPath) as! SwitchTableViewCell let mapping = mappings[indexPath.row] diff --git a/Cauli/Florets/MapRemote/MappingsListViewController.swift b/Cauli/Florets/MapRemote/MappingsListViewController.swift index cf1526d..b4541d8 100644 --- a/Cauli/Florets/MapRemote/MappingsListViewController.swift +++ b/Cauli/Florets/MapRemote/MappingsListViewController.swift @@ -23,19 +23,19 @@ import UIKit class MappingsListViewController: UITableViewController { - + private let dataSource: MappingsListDatasource - + init(mapRemoteFloret: MapRemoteFloret, mappings: [Mapping]) { self.dataSource = MappingsListDatasource(mapRemoteFloret: mapRemoteFloret, mappings: mappings) super.init(nibName: nil, bundle: nil) dataSource.setup(tableView) tableView.dataSource = dataSource } - + @available(*, unavailable) required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } - + }