Skip to content

Commit

Permalink
Updates after swiftlint warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
brototyp committed Mar 13, 2021
1 parent 1535da7 commit 4219b6f
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 33 deletions.
2 changes: 0 additions & 2 deletions Cauli/CauliURLProtocol/CauliURLProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Cauli/CauliViewController/CauliViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion Cauli/Floret.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public extension Floret {
}

var description: String? {
return nil
nil
}
}
// swiftlint:enable missing_docs
35 changes: 17 additions & 18 deletions Cauli/Florets/MapRemote/MapRemoteFloret.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,52 +34,53 @@ 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<String> {
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)
} else {
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)
Expand All @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions Cauli/Florets/MapRemote/Mapping.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
10 changes: 5 additions & 5 deletions Cauli/Florets/MapRemote/MappingsListDatasource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
8 changes: 4 additions & 4 deletions Cauli/Florets/MapRemote/MappingsListViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

}

0 comments on commit 4219b6f

Please sign in to comment.