Skip to content

Commit

Permalink
Update to UpdateFrameListener to pass the cell. (#200)
Browse files Browse the repository at this point in the history
This allows the function to access _brick.index, for use by the dataSource, as well as cell.genericContentView to do updates later in the layout cycle (ie. setting images)
  • Loading branch information
ablokker authored and jay18001 committed Nov 7, 2017
1 parent 90ab1ee commit 0b8607f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
14 changes: 7 additions & 7 deletions Source/Bricks/Generic/GenericBrick.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ protocol ViewGenerator {
func configure(view: UIView, cell: GenericBrickCell)
}

public protocol UpdateFramesListener {
func didUpdateFrames()
public protocol FrameLayoutListener: class {
func didLayoutFrames(cell: GenericBrickCell)
}

public typealias HeightProviderHandler = ((_ width: CGFloat) -> CGFloat)
Expand All @@ -23,6 +23,7 @@ open class GenericBrick<T: UIView>: Brick, ViewGenerator {
public typealias ConfigureView = (_ view: T, _ cell: GenericBrickCell) -> Void

open var configureView: ConfigureView
open weak var frameLayoutListener: FrameLayoutListener?

public convenience init(_ identifier: String = "", width: BrickDimension = .ratio(ratio: 1), height: BrickDimension = .auto(estimate: .fixed(size: 50)), backgroundColor: UIColor = UIColor.clear, backgroundView: UIView? = nil, configureView: @escaping ConfigureView) {
self.init(identifier, size: BrickSize(width: width, height: height), backgroundColor: backgroundColor, backgroundView: backgroundView, configureView: configureView)
Expand Down Expand Up @@ -52,6 +53,7 @@ open class GenericBrick<T: UIView>: Brick, ViewGenerator {
}

func configure(view: UIView, cell: GenericBrickCell) {
cell.frameLayoutListener = (view as? FrameLayoutListener) ?? self.frameLayoutListener
self.configureView(view as! T, cell)
}

Expand All @@ -76,6 +78,7 @@ open class GenericBrickCell: BrickCell {
}

open var customHeightProvider: HeightProviderHandler?
open weak var frameLayoutListener: FrameLayoutListener?

internal private(set) var fromNib: Bool = false

Expand Down Expand Up @@ -186,12 +189,9 @@ open class GenericBrickCell: BrickCell {

open override func framesDidLayout() {
super.framesDidLayout()

if let genericContentView = genericContentView as? UpdateFramesListener {
genericContentView.didUpdateFrames()
}
frameLayoutListener?.didLayoutFrames(cell: self)
}

open override func heightForBrickView(withWidth width: CGFloat) -> CGFloat {
if let heightProvider = customHeightProvider {
let height = heightProvider(width)
Expand Down
32 changes: 29 additions & 3 deletions Tests/Cells/FrameCalculationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,40 @@ class FrameCalculationTests: XCTestCase {
waitForExpectations(timeout: 1.0, handler: nil)
}

func testGenericBrickUIImageViewFrameListener() {
let expect = expectation(description:"Expect framesDidLayout to get called")
let mockFrameChangeListener = MockFrameListener()

let genericBrick = GenericBrick<UIImageView>("FrameInfo", size: BrickSize(width: .fixed(size: 50), height: .fixed(size: 50))) { (view, cell) in
}

genericBrick.frameLayoutListener = mockFrameChangeListener

brickView.setupSingleBrickAndLayout(
genericBrick
)
let indexPath = brickView.indexPathsForBricksWithIdentifier("FrameInfo").first!
let cell = brickView.cellForItem(at: indexPath) as! GenericBrickCell
cell.layoutIfNeeded()

DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
XCTAssertTrue(mockFrameChangeListener.didUpdateFramesCalled)
expect.fulfill()
}
waitForExpectations(timeout: 1.0, handler: nil)
}
}

class TestView: UIView {
class TestView: UIView, FrameLayoutListener {
var didUpdateFramesCalled: Bool = false
func didLayoutFrames(cell: GenericBrickCell) {
didUpdateFramesCalled = true
}
}

extension TestView: UpdateFramesListener {
func didUpdateFrames() {
class MockFrameListener: FrameLayoutListener {
var didUpdateFramesCalled: Bool = false
func didLayoutFrames(cell: GenericBrickCell) {
didUpdateFramesCalled = true
}
}

0 comments on commit 0b8607f

Please sign in to comment.