Skip to content

Commit

Permalink
Merge branch 'develop' into feature/91-pretty-print-in-inspector
Browse files Browse the repository at this point in the history
# Conflicts:
#	CHANGELOG.md
#	Cauliframework.xcodeproj/project.pbxproj
#	Example/cauli-ios-example/Pods/Pods.xcodeproj/project.pbxproj
  • Loading branch information
brototyp committed Mar 3, 2019
2 parents 78ffcd3 + 63967b1 commit c6f29ad
Show file tree
Hide file tree
Showing 14 changed files with 791 additions and 784 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
* **improvement** Added some description to the `CauliViewController`s sections. [#157](https://github.com/cauliframework/cauli/issues/157) by @pstued
* **improvement** Added `preStorageRecordModifier` to `Configuration` and `Storage` to allow for records to be modified before they are stored. [#146](https://github.com/cauliframework/cauli/pull/146) by @shukuyen
* **improvement** Added a done button to dismiss the CauliViewController when the ViewController is displayed via the shake gesture. [#114](https://github.com/cauliframework/cauli/issues/114) by @pstued
* **improvement** Added a PlaintextPrettyPrinter and manual selection
[#91](https://github.com/cauliframework/cauli/issues/91) by @brototyp
* **improvement** Added a PlaintextPrettyPrinter and manual selection [#91](https://github.com/cauliframework/cauli/issues/91) by @brototyp
* **improvement** Splitted the Floret protocol into InterceptingFloret and DisplayingFloret protocols for a better separation of a Florets functionality and responsibility. [#155](https://github.com/cauliframework/cauli/issues/155) by @pstued
* **bugfix** Fixed an issue where records cells were cropped in the InspectorViewController. [#147](https://github.com/cauliframework/cauli/issues/147)
* **bugfix** Fixed a bug where Records would be duplicated in the InspectorViewController. [#148](https://github.com/cauliframework/cauli/issues/148) by @brototyp
* **bugfix** Fixed a bug where the searchbar could cover the first entry in the InspectorViewController. [#144](https://github.com/cauliframework/cauli/issues/144) by @brototyp
Expand Down
8 changes: 4 additions & 4 deletions Cauli/Cauli.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public class Cauli {
/// The Storage used by this instance to store all Records.
public let storage: Storage
internal let florets: [Floret]
private var enabledFlores: [Floret] {
return florets.filter { $0.enabled }
private var enabledFlorets: [InterceptingFloret] {
return florets.compactMap { $0 as? InterceptingFloret }.filter { $0.enabled }
}
private let configuration: Configuration
private var viewControllerManager: ViewControllerShakePresenter?
Expand Down Expand Up @@ -117,7 +117,7 @@ extension Cauli: CauliURLProtocolDelegate {
func willRequest(_ record: Record, modificationCompletionHandler completionHandler: @escaping (Record) -> Void) {
assert(!Thread.current.isMainThread, "should never be called on the MainThread")
guard enabled else { completionHandler(record); return }
enabledFlores.cauli_reduceAsync(record, transform: { record, floret, completion in
enabledFlorets.cauli_reduceAsync(record, transform: { record, floret, completion in
floret.willRequest(record) { record in
completion(record)
}
Expand All @@ -132,7 +132,7 @@ extension Cauli: CauliURLProtocolDelegate {
func didRespond(_ record: Record, modificationCompletionHandler completionHandler: @escaping (Record) -> Void) {
assert(!Thread.current.isMainThread, "should never be called on the MainThread")
guard enabled else { completionHandler(record); return }
enabledFlores.cauli_reduceAsync(record, transform: { record, floret, completion in
enabledFlorets.cauli_reduceAsync(record, transform: { record, floret, completion in
floret.didRespond(record) { record in
completion(record)
}
Expand Down
26 changes: 14 additions & 12 deletions Cauli/CauliViewController/CauliViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,16 @@ import UIKit

internal class CauliViewController: UITableViewController {

typealias DisplayableFloret = Floret & Displayable

private let cauli: Cauli
private var displayableFlorets: [DisplayableFloret]
private var displayingFlorets: [DisplayingFloret]
private var interceptingFlorets: [InterceptingFloret]

init(cauli: Cauli) {
self.cauli = cauli
displayableFlorets = cauli.florets.compactMap { $0 as? (DisplayableFloret) }

displayingFlorets = cauli.florets.compactMap { $0 as? DisplayingFloret }
interceptingFlorets = cauli.florets.compactMap { $0 as? InterceptingFloret }

super.init(style: .grouped)
title = "Cauli"
}
Expand All @@ -54,10 +56,10 @@ internal class CauliViewController: UITableViewController {

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
return displayableFlorets.count
return displayingFlorets.count
}

return cauli.florets.count
return interceptingFlorets.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
Expand All @@ -70,15 +72,15 @@ internal class CauliViewController: UITableViewController {

private func tableView(_ tableView: UITableView, detailCellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
cell.textLabel?.text = displayableFlorets[indexPath.row].name
cell.textLabel?.text = displayingFlorets[indexPath.row].name
cell.accessoryType = .disclosureIndicator
return cell
}

private func tableView(_ tableView: UITableView, switchCellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: SwitchTableViewCell.reuseIdentifier, for: indexPath) as? SwitchTableViewCell else { fatalError("we shouldn't reach this point") }

var floret = cauli.florets[indexPath.row]
var floret = interceptingFlorets[indexPath.row]
cell.set(title: floret.name, switchValue: floret.enabled)
cell.switchValueChanged = {
floret.enabled = $0
Expand All @@ -89,20 +91,20 @@ internal class CauliViewController: UITableViewController {

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard indexPath.section == 0 else { return }
navigationController?.pushViewController(displayableFlorets[indexPath.row].viewController(cauli), animated: true)
navigationController?.pushViewController(displayingFlorets[indexPath.row].viewController(cauli), animated: true)
}

override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
switch section {
case 0: return "Displayable Florets"
case 1: return "Interceptable Florets"
case 0: return "Displaying Florets"
case 1: return "Intercepting Florets"
default: return nil
}
}

override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
switch section {
case 1: return "If an InterceptableFloret is disabled it cannot intercept any requests or responses."
case 1: return "If an InterceptingFloret is disabled it cannot intercept any requests or responses."
default: return nil
}
}
Expand Down
5 changes: 2 additions & 3 deletions Cauli/Displayable.swift → Cauli/DisplayingFloret.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@

import UIKit

/// A Displayable provides a ViewController for settings or to display any information.
/// It usually is used in combination with a Floret
public protocol Displayable {
/// A DisplayingFloret provides a ViewController for settings or to display any information.
public protocol DisplayingFloret: Floret {
/// This function is called whenever the Cauli UI will be displayed.
/// If a Floret needs any UI for configuration or to display data you
/// can return a ViewController here.
Expand Down
31 changes: 0 additions & 31 deletions Cauli/Floret.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,37 +29,6 @@ public protocol Floret {
/// The name of the Floret. This will be used to identify the floret in the UI.
/// If not implemented, the type will be used per default.
var name: String { get }

/// If a Floret is disabled the both functions `willRequest` and `didRespond` will
/// not be called anymore. A Floret doesn't need to perform any specific action.
var enabled: Bool { get set }

/// This function will be called before a request is performed. The Florets will be
/// called in the order the Cauli instance got initialized with.
///
/// Using this function you can:
/// - inspect the request
/// - modify the request (update the `designatedRequest`)
/// - fail the request (set the `result` to `.error()`)
/// - return a cached or pre-calculated response (set the `result` to `.result()`)
///
/// - Parameters:
/// - record: The `Record` that represents the request before it was performed.
/// - completionHandler: Call this completion handler exactly once with the
/// original or modified `Record`.
func willRequest(_ record: Record, modificationCompletionHandler completionHandler: @escaping (Record) -> Void)

/// This function will be called after a request is performed and the response arrived.
/// The Florets will be called in the order the Cauli instance got initialized with.
///
/// Using this function you can:
/// - modify the request
///
/// - Parameters:
/// - record: The `Record` that represents the request after it was performed.
/// - completionHandler: Call this completion handler exactly once with the
/// original or modified `Record`.
func didRespond(_ record: Record, modificationCompletionHandler completionHandler: @escaping (Record) -> Void)
}

// swiftlint:disable missing_docs
Expand Down
2 changes: 1 addition & 1 deletion Cauli/Florets/FindReplace/FindReplaceFloret.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import Foundation
/// before sending a request and after receiving a response. Use multiple
/// instances of the `FindReplaceFloret`s to group certain RecordModifiers
/// under a given name.
public class FindReplaceFloret: Floret {
public class FindReplaceFloret: InterceptingFloret {

public var enabled: Bool = true
public let name: String
Expand Down
15 changes: 1 addition & 14 deletions Cauli/Florets/Inspector/InspectorFloret.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,10 @@

import UIKit

public class InspectorFloret: Floret {
public var enabled: Bool = true
public class InspectorFloret: DisplayingFloret {

public init() {}

public func willRequest(_ record: Record, modificationCompletionHandler completionHandler: @escaping (Record) -> Void) {
completionHandler(record)
}

public func didRespond(_ record: Record, modificationCompletionHandler completionHandler: @escaping (Record) -> Void) {
completionHandler(record)
}

}

extension InspectorFloret: Displayable {

public func viewController(_ cauli: Cauli) -> UIViewController {
return InspectorTableViewController(cauli)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ extension RecordTableViewController {
}
}

alertController.addAction(UIAlertAction(title: "Share", style: .default, handler: { [weak self] _ in
alertController.addAction(UIAlertAction(title: "Share", style: .default) { [weak self] _ in
let activityItem = item.value() ?? item.description
self?.presentShareSheet(for: [activityItem], from: cell)
}))
})

alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))

Expand Down
2 changes: 1 addition & 1 deletion Cauli/Florets/Mock/MockFloret.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ import Foundation
/// Result<Response>.notFound(for: request)
/// }
/// ```
public class MockFloret: Floret {
public class MockFloret: InterceptingFloret {

public var enabled: Bool = true

Expand Down
57 changes: 57 additions & 0 deletions Cauli/InterceptingFloret.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//
// Copyright (c) 2018 cauli.works
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//

import Foundation

protocol InterceptingFloret: Floret {

/// If an InterceptingFloret is disabled both functions `willRequest` and `didRespond` will
/// not be called anymore. A InterceptingFloret doesn't need to perform any specific action.
var enabled: Bool { get set }

/// This function will be called before a request is performed. The InterceptingFlorets will be
/// called in the order the Cauli instance got initialized with.
///
/// Using this function you can:
/// - inspect the request
/// - modify the request (update the `designatedRequest`)
/// - fail the request (set the `result` to `.error()`)
/// - return a cached or pre-calculated response (set the `result` to `.result()`)
///
/// - Parameters:
/// - record: The `Record` that represents the request before it was performed.
/// - completionHandler: Call this completion handler exactly once with the
/// original or modified `Record`.
func willRequest(_ record: Record, modificationCompletionHandler completionHandler: @escaping (Record) -> Void)

/// This function will be called after a request is performed and the response arrived.
/// The InterceptingFlorets will be called in the order the Cauli instance got initialized with.
///
/// Using this function you can:
/// - modify the request
///
/// - Parameters:
/// - record: The `Record` that represents the request after it was performed.
/// - completionHandler: Call this completion handler exactly once with the
/// original or modified `Record`.
func didRespond(_ record: Record, modificationCompletionHandler completionHandler: @escaping (Record) -> Void)
}
Loading

0 comments on commit c6f29ad

Please sign in to comment.