Skip to content

Commit

Permalink
Xcode 8.3, remove unused code, fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DenTelezhkin committed Apr 6, 2017
1 parent 20530e8 commit c36ea1b
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 104 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ matrix:
# env: ACTION=cocoapods

language: objective-c
osx_image: xcode8
osx_image: xcode8.3

script:
- git submodule update --init --recursive
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Change Log
All notable changes to this project will be documented in this file.

## Master

* Reworked `EventReaction` class to use `ViewModelMapping` to properly identify not only model and `ViewType`, but also `viewClass`. This allows event reactions to run for cases where two view subclasses, conforming to `ModelTransfer`, use the same model, and have similar events.

## [4.1.0](https://github.com/DenHeadless/DTModelStorage/releases/tag/4.1.0)

* Adds `setItems(_:)` method, that allows to set items for all sections in `MemoryStorage`.
Expand Down
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ SHELL := /bin/bash
# Install Tasks

install-iOS:
xcrun instruments -w "iPhone 6s (10.0)" || true
xcrun instruments -w "iPhone 7 (10.3)" || true

install-tvOS:
xcrun instruments -w "Apple TV 1080p (10.0)" || true
xcrun instruments -w "Apple TV 1080p (10.2)" || true

install-carthage:
brew remove carthage --force || true
Expand All @@ -17,7 +17,7 @@ install-cocoapods:
# Run Tasks

test-iOS:
set -o pipefail && xcodebuild -project DTModelStorage.xcodeproj -scheme DTModelStorage-iOS -destination "name=iPhone 6s" -enableCodeCoverage YES test -configuration "Release" | xcpretty -ct
set -o pipefail && xcodebuild -project DTModelStorage.xcodeproj -scheme DTModelStorage-iOS -destination "name=iPhone 7" -enableCodeCoverage YES test -configuration "Release" | xcpretty -ct
bash <(curl -s https://codecov.io/bash)

test-tvOS:
Expand Down
91 changes: 3 additions & 88 deletions Source/Core/EventReactions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ open class EventReaction {
viewModelMapping = ViewModelMapping(viewType: viewType, modelClass: T.self)
}

/// Updates reaction with cell type, type-erases T and U into Any types.
/// Creates reaction with type-erased T and T.ModelType into Any types.
open func makeReaction<T:ModelTransfer,U>(_ block: @escaping (T, T.ModelType, IndexPath) -> U) {
reaction = { cell, model, indexPath in
guard let model = model as? T.ModelType,
Expand All @@ -63,6 +63,7 @@ open class EventReaction {
}
}

/// Creates reaction with type-erased T into Any type.
open func makeReaction<T,U>(_ block: @escaping (T, IndexPath) -> U) {
reaction = { cell, model, indexPath in
guard let model = model as? T,
Expand All @@ -73,87 +74,6 @@ open class EventReaction {
}
}

// /// Updates reaction with cell type, type-erases T and U into Any types.
// open func makeCellReaction<T,U>(_ block: @escaping (T?, T.ModelType, IndexPath) -> U)
// where T: ModelTransfer
// {
// viewModelMapping = ViewModelMapping(viewType: .cell, viewClass: T.self)
// reaction = { cell, model, indexPath in
// guard let model = model as? T.ModelType,
// let indexPath = indexPath as? IndexPath else {
// return 0
// }
// return block(cell as? T, model, indexPath)
// }
// }
//
// /// Updates reaction with cell type, type-erases T and U into Any types.
// open func makeCellReaction<T,U>(_ block: @escaping (T, IndexPath) -> U) {
// viewModelMapping = ViewModelMapping(viewType: .cell, modelClass: T.self)
// reaction = { cell, model, indexPath in
// guard let model = model as? T,
// let indexPath = indexPath as? IndexPath else {
// return 0
// }
// return block(model, indexPath)
// }
// }
//
// /// Updates reaction with cell type, type-erases T and U into Any types.
// open func makeCellReaction<T,U>(_ block: @escaping (T, T.ModelType, IndexPath) -> U)
// where T: ModelTransfer
// {
// viewModelMapping = ViewModelMapping(viewType: .cell, viewClass: T.self)
// reaction = { cell, model, indexPath in
// guard let model = model as? T.ModelType,
// let indexPath = indexPath as? IndexPath,
// let cell = cell as? T
// else {
// return 0
// }
// return block(cell, model, indexPath)
// }
// }
//
// /// Updates reaction with supplementary type, type-erases T and U into Any types.
// open func makeSupplementaryReaction<T,U>(forKind kind: String, block: @escaping (T?, T.ModelType, IndexPath) -> U)
// where T: ModelTransfer
// {
// viewModelMapping = ViewModelMapping(viewType: .supplementaryView(kind: kind), viewClass: T.self)
// reaction = { supplementary, model, sectionIndex in
// guard let model = model as? T.ModelType,
// let index = sectionIndex as? IndexPath else {
// return ""
// }
// return block(supplementary as? T, model, index)
// }
// }
//
// /// Updates reaction with supplementary type, type-erases T and U into Any types.
// open func makeSupplementaryReaction<T,U>(for kind: String, block: @escaping (T, IndexPath) -> U) {
// viewModelMapping = ViewModelMapping(viewType: .supplementaryView(kind: kind), modelClass: T.self)
// reaction = { supplementary, model, sectionIndex in
// guard let model = model as? T, let index = sectionIndex as? IndexPath else { return 0 }
// return block(model,index)
// }
// }
//
// /// Updates reaction with supplementary type, type-erases T and U into Any types.
// open func makeSupplementaryReaction<T,U>(forKind kind: String, block: @escaping (T, T.ModelType, IndexPath) -> U)
// where T: ModelTransfer
// {
// viewModelMapping = ViewModelMapping(viewType: .supplementaryView(kind: kind), viewClass: T.self)
// reaction = { supplementary, model, sectionIndex in
// guard let model = model as? T.ModelType,
// let index = sectionIndex as? IndexPath,
// let supplementary = supplementary as? T else
// {
// return ""
// }
// return block(supplementary, model, index)
// }
// }

/// Performs reaction with `arguments`.
open func performWithArguments(_ arguments: (Any,Any,Any)) -> Any {
return reaction?(arguments.0,arguments.1,arguments.2) ?? 0
Expand Down Expand Up @@ -204,12 +124,7 @@ public extension RangeReplaceableCollection where Self.Iterator.Element: EventRe
/// Returns reaction of `type`, with `signature` and `model`. Returns nil, if reaction was not found.
@available(*, deprecated, message: "Please use reaction(of:signature:forModel:view:) method instead")
public func reaction(of type: ViewType, signature: String, forModel model: Any) -> EventReaction? {
return filter({ reaction in
guard let unwrappedModel = RuntimeHelper.recursivelyUnwrapAnyValue(model) else { return false}
return reaction.viewModelMapping.viewType == type &&
reaction.viewModelMapping.modelTypeCheckingBlock(unwrappedModel) &&
reaction.methodSignature == signature
}).first
return reaction(of: type, signature: signature, forModel: model, view: nil)
}

public func reaction(of type: ViewType,
Expand Down
24 changes: 12 additions & 12 deletions Tests/Specs/UIReactionsTestCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,22 @@ class UIReactionsTestCase: XCTestCase {
}

func testReactionsAreSearchable() {
let reaction = EventReaction(signature: "foo")
reaction.makeCellReaction(makeCellBlock({ } , cell: TableCell(), returnValue: 5))
let reaction = EventReaction(signature: "foo", viewType: .cell, modelType: Int.self)
reaction.makeReaction(makeCellBlock({ } , cell: TableCell(), returnValue: 5))
reactions.append(reaction)

let foundReaction = reactions.reaction(of: .cell, signature: "foo", forModel: 5)
let foundReaction = reactions.reaction(of: .cell, signature: "foo", forModel: 5, view: nil)
expect(foundReaction).toNot(beNil())
}

func testReactionsForOptionalModelsAreSearchable() {
let reaction = EventReaction(signature: "foo")
reaction.makeCellReaction(makeCellBlock({ } , cell: TableCell(), returnValue: 5))
let reaction = EventReaction(signature: "foo", viewType: .cell, modelType: Int.self)
reaction.makeReaction(makeCellBlock({ } , cell: TableCell(), returnValue: 5))
reactions.append(reaction)

let nilModel: Int? = 5

let foundReaction = reactions.reaction(of: .cell, signature: "foo", forModel: nilModel as Any)
let foundReaction = reactions.reaction(of: .cell, signature: "foo", forModel: nilModel as Any, view: nil)
expect(foundReaction).toNot(beNil())
}

Expand All @@ -66,9 +66,9 @@ class UIReactionsTestCase: XCTestCase {
}

func testCellReactionIsExecutable() {
let reaction = EventReaction(signature: "foo")
let reaction = EventReaction(signature: "foo", viewType: .cell, modelType: Int.self)
let exp = expectation(description: "executeCell")
reaction.makeCellReaction(makeCellBlock({
reaction.makeReaction(makeCellBlock({
exp.fulfill()
}, cell: TableCell(), returnValue: 3))
let result = reaction.performWithArguments((TableCell(),5,indexPath(0, 0)))
Expand All @@ -77,9 +77,9 @@ class UIReactionsTestCase: XCTestCase {
}

func testSupplementaryReactionIsExecutable() {
let reaction = EventReaction(signature: "foo")
let reaction = EventReaction(signature: "foo", viewType: .supplementaryView(kind: "Foo"), modelType: Int.self)
let exp = expectation(description: "executeCell")
reaction.makeSupplementaryReaction(forKind: "bar", block: makeSupplementaryBlock({
reaction.makeReaction(makeSupplementaryBlock({
exp.fulfill()
}, cell: TableCell(), returnValue: 3))
let result = reaction.performWithArguments((TableCell(),5,indexPath(0, 5)))
Expand All @@ -88,9 +88,9 @@ class UIReactionsTestCase: XCTestCase {
}

func testReactionOfTypeIsPerformable() {
let reaction = EventReaction(signature: "foo")
let reaction = EventReaction(signature: "foo", viewType: .cell, modelType: Int.self)
let exp = expectation(description: "executeCell")
reaction.makeCellReaction(makeCellBlock({
reaction.makeReaction(makeCellBlock({
exp.fulfill()
}, cell: TableCell(), returnValue: 3))
reactions.append(reaction)
Expand Down

0 comments on commit c36ea1b

Please sign in to comment.