wangshengjia / LeeGo

Declarative, configurable & highly reusable UI development as making Lego bricks.
MIT License
966 stars 45 forks source link

Fixing Issue 73 #72

Closed josephquigley closed 6 years ago

josephquigley commented 6 years ago

Fixing Issue 73 so that it's possible to access the child bricks of a Brick instance.

Renamed Bricks.bricks to Bricks.childBricks to convey what the developer will be accessing. Exposed UIView.lg_brickName so that you can bind views to bricks with children:

//Boilerplate for example
let name = "Example name"
let address = "Example address"

//BrickDataSource implemented
func update(_ targetView: UIView, with brick: Brick) {
    brick.childBricks?.forEach { brick in
        let viewsForBrick = ([targetView] + targetView.subviews)
            .filter({$0.lg_brickName == brick.name})

        for view in viewsForBrick {
            switch view {
            case let label as UILabel where brick == FooBrickBuilder.name:
                label.text = name
            case let label as UILabel where brick == FooBrickBuilder.address:
                label.text = address
            default: ()
            }
        }
    }
}

Now your UI binding code looks like the following:

let title = "title".build(UILabel.self).style([.numberOfLines(0), .text("Lorem Ipsum is simply dummy text of the printing industry")])
let description = "description".build(UILabel.self).style([.textColor(UIColor.lightGray), .numberOfLines(0), .font(UIFont.systemFont(ofSize: 14)), .text("Lorem Ipsum has been the industry's standard dummy text ever since the 1500s")])
let redBlock = "red".build().style(Style.redBlockStyle)
let greenBlock = "green".build().style(Style.greenBlockStyle)
let blueBlock = "blue".build(UIImageView.self).style(Style.blueBlockStyle + [Appearance.custom(["shadowColor": UIColor.brown, "shadowOpacity": Float(1.0)])])

let blocks = Brick.union("blocks", bricks: [
    redBlock.height(50),
    greenBlock.height(80),
    blueBlock.height(30)],
    axis: .horizontal, align: .top, distribution: .fillEqually, metrics: LayoutMetrics(10, 10, 10, 10, 10, 10)).style(Style.blocksStyle)

let brick = "details".build().bricks(title, description, blocks) {
    title, description, blocks in
    Layout(bricks: [title, description, blocks], axis: .vertical, align: .fill, distribution: .flow(3), metrics: LayoutMetrics(84, 20, 20, 20, 10, 10))
}

self.view.lg_configure(as: brick, updatingStrategy: .always)

// This example is assuming that ClassThatHandlesUpdates would
// change, say, the text of the UILabel created by the title brick
ClassThatHandlesUpdates().update(self.view, with: brick)
wangshengjia commented 6 years ago

Thanks for the PR, it makes sense.

Merged