-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Alex Usbergo
committed
May 18, 2016
1 parent
62957dd
commit 13f15d5
Showing
28 changed files
with
248 additions
and
4 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Binary file removed
BIN
-7.59 KB
.../playground.xcworkspace/xcuserdata/alexusbergo.xcuserdatad/UserInterfaceState.xcuserstate
Binary file not shown.
File renamed without changes.
File renamed without changes
File renamed without changes.
4 changes: 4 additions & 0 deletions
4
Playgrounds/02 Vanilla Components.playground/contents.xcplayground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | ||
<playground version='5.0' target-platform='ios' display-mode='rendered' executeOnSourceChanges='false'> | ||
<timeline fileName='timeline.xctimeline'/> | ||
</playground> |
File renamed without changes.
Binary file added
BIN
+8.69 KB
.../playground.xcworkspace/xcuserdata/alexusbergo.xcuserdatad/UserInterfaceState.xcuserstate
Binary file not shown.
File renamed without changes.
116 changes: 116 additions & 0 deletions
116
Playgrounds/03 Components embedded in Cells.playground/Contents.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import UIKit | ||
import XCPlayground | ||
import Render | ||
|
||
/*: | ||
![logo](logo_small.png) | ||
# Components embedded in cells | ||
|
||
You can wrap your components in `ComponentTableViewCell` or `ComponentCollectionViewCell` and use | ||
the classic dataSource/delegate pattern for you view controller. | ||
*/ | ||
|
||
class FooState: ComponentStateBase { | ||
let text: String = randomString(randomInt(0, max: 500)) | ||
} | ||
|
||
class FooComponentView: ComponentView { | ||
|
||
override class func initialize() { | ||
|
||
// if we intend to use this component inside of a cell | ||
// we have to register an instance as a prototype for the infra. | ||
// The 'initialize' function seems to be a convenient palce to do it. | ||
registerPrototype(component: FooComponentView()) | ||
} | ||
|
||
// we cast the state for convenience | ||
var fooState: FooState { | ||
return (self.state as? FooState) ?? FooState() | ||
} | ||
|
||
override func construct() -> ComponentNodeType { | ||
let margin: Float = 4.0 | ||
let insets: Inset = (margin, margin, margin, margin, margin, margin) | ||
return ComponentNode<UIView>().configure({ view in | ||
view.style.flexDirection = .Row | ||
view.style.margin = insets | ||
|
||
// that's how we can define the size in relation to the size of the parent view. | ||
let min = ~self.parentSize.width | ||
view.style.minDimensions.width = max(min, 96) | ||
|
||
view.backgroundColor = UIColor.A | ||
}).children([ | ||
ComponentNode<UIView>().configure({ view in | ||
view.style.dimensions = (32, 32) | ||
view.style.margin = insets | ||
view.backgroundColor = UIColor.B | ||
view.layer.cornerRadius = 16 | ||
}), | ||
ComponentNode<UILabel>().configure({ view in | ||
view.style.margin = insets | ||
view.style.alignSelf = .Center | ||
view.style.flex = Flex.Max | ||
view.text = self.fooState.text | ||
view.numberOfLines = 0 | ||
view.font = UIFont.systemFontOfSize(12.0, weight: UIFontWeightLight) | ||
}) | ||
]) | ||
} | ||
} | ||
|
||
/*: Now this is how we wrap the component inside a `ComponentTableViewCell` in our datasource */ | ||
|
||
class DataSource: NSObject, UITableViewDataSource { | ||
|
||
let items: [FooState] = { | ||
var items = [FooState]() | ||
for _ in 0..<6 { | ||
items.append(FooState()) | ||
} | ||
return items | ||
}() | ||
|
||
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | ||
return items.count | ||
} | ||
|
||
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { | ||
let reuseIdentifier = String(FooComponentView.self) | ||
let cell: ComponentTableViewCell! = | ||
//dequeue a cell with the given identifier | ||
//(remember to use different identifiers for different component classes) | ||
tableView.dequeueReusableCellWithIdentifier(reuseIdentifier) as? ComponentTableViewCell ?? | ||
|
||
//or create a new Cell wrapping the component | ||
ComponentTableViewCell(reuseIdentifier: reuseIdentifier, component: FooComponentView()) | ||
|
||
//set the state for the cell | ||
cell.state = items[indexPath.row] | ||
|
||
//and render the component | ||
cell.renderComponent(CGSize.sizeConstraintToWidth(tableView.bounds.size.width)) | ||
return cell | ||
} | ||
} | ||
|
||
/*: And finally create a `UITableView` */ | ||
|
||
let tableView = UITableView(frame: CGRect(origin: CGPoint.zero, size: CGSize(width: 320, height: 320))) | ||
|
||
//we want automatic dimensions for our cells | ||
tableView.rowHeight = UITableViewAutomaticDimension | ||
|
||
//this improves drastically the performance of reloadData when you have a large collection of items | ||
tableView.estimatedRowHeight = 100 | ||
|
||
let dataSource = DataSource() | ||
tableView.dataSource = dataSource | ||
tableView.reloadData() | ||
|
||
tableView | ||
|
||
|
||
|
||
|
Binary file added
BIN
+61.4 KB
Playgrounds/03 Components embedded in Cells.playground/Resources/logo_small.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
51 changes: 51 additions & 0 deletions
51
Playgrounds/03 Components embedded in Cells.playground/Sources/Shared.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
|
||
import UIKit | ||
|
||
public func snapshot(view: UIView) -> UIImage { | ||
view.layoutSubviews() | ||
UIGraphicsBeginImageContext(view.frame.size) | ||
view.layer.renderInContext(UIGraphicsGetCurrentContext()!) | ||
let image = UIGraphicsGetImageFromCurrentImageContext() | ||
UIGraphicsEndImageContext() | ||
return image | ||
} | ||
|
||
extension UIColor { | ||
class public var A: UIColor { | ||
return UIColor(red: 238/255, green: 238/255, blue: 238/255, alpha: 1) | ||
} | ||
class public var B: UIColor { | ||
return UIColor(red: 0/255, green: 188/255, blue: 212/255, alpha: 1) | ||
} | ||
class public var C: UIColor { | ||
return UIColor(red: 76/255, green: 175/255, blue: 80/255, alpha: 1) | ||
} | ||
class public var D: UIColor { | ||
return UIColor(red: 165/255, green: 214/255, blue: 167/255, alpha: 1) | ||
} | ||
class public var E: UIColor { | ||
return UIColor(red: 46/255, green: 125/255, blue: 50/255, alpha: 1) | ||
} | ||
class public var F: UIColor { | ||
return UIColor(red: 244/255, green: 67/255, blue: 54/255, alpha: 1) | ||
} | ||
class public var G: UIColor { | ||
return UIColor(red: 33/255, green: 150/255, blue: 243/255, alpha: 1) | ||
} | ||
} | ||
|
||
public func randomString(length: Int) -> String { | ||
let allowedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 " | ||
let allowedCharsCount = UInt32(allowedChars.characters.count) | ||
var randomString = "" | ||
for _ in (0..<length) { | ||
let randomNum = Int(arc4random_uniform(allowedCharsCount)) | ||
let newCharacter = allowedChars[allowedChars.startIndex.advancedBy(randomNum)] | ||
randomString += String(newCharacter) | ||
} | ||
return randomString | ||
} | ||
|
||
public func randomInt(min: Int, max:Int) -> Int { | ||
return min + Int(arc4random_uniform(UInt32(max - min + 1))) | ||
} |
2 changes: 1 addition & 1 deletion
2
...mponents.playground/contents.xcplayground → ...in Cells.playground/contents.xcplayground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | ||
<playground version='5.0' target-platform='ios' display-mode='raw' executeOnSourceChanges='false'> | ||
<playground version='5.0' target-platform='ios' display-mode='raw'> | ||
<timeline fileName='timeline.xctimeline'/> | ||
</playground> |
7 changes: 7 additions & 0 deletions
7
...3 Components embedded in Cells.playground/playground.xcworkspace/contents.xcworkspacedata
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file added
BIN
+7.86 KB
.../playground.xcworkspace/xcuserdata/alexusbergo.xcuserdatad/UserInterfaceState.xcuserstate
Binary file not shown.
58 changes: 58 additions & 0 deletions
58
Playgrounds/03 Components embedded in Cells.playground/timeline.xctimeline
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<Timeline | ||
version = "3.0"> | ||
<TimelineItems> | ||
<LoggerValueHistoryTimelineItem | ||
documentLocation = "#CharacterRangeLen=0&CharacterRangeLoc=3960&EndingColumnNumber=54&EndingLineNumber=62&StartingColumnNumber=1&StartingLineNumber=62&Timestamp=485261764.843199" | ||
selectedRepresentationIndex = "0" | ||
shouldTrackSuperviewWidth = "NO"> | ||
</LoggerValueHistoryTimelineItem> | ||
<LoggerValueHistoryTimelineItem | ||
documentLocation = "#CharacterRangeLen=0&CharacterRangeLoc=3960&EndingColumnNumber=23&EndingLineNumber=62&StartingColumnNumber=1&StartingLineNumber=62&Timestamp=485261764.843389" | ||
selectedRepresentationIndex = "0" | ||
shouldTrackSuperviewWidth = "NO"> | ||
</LoggerValueHistoryTimelineItem> | ||
<LoggerValueHistoryTimelineItem | ||
documentLocation = "#CharacterRangeLen=0&CharacterRangeLoc=3960&EndingColumnNumber=27&EndingLineNumber=62&StartingColumnNumber=1&StartingLineNumber=62&Timestamp=485261764.843548" | ||
selectedRepresentationIndex = "0" | ||
shouldTrackSuperviewWidth = "NO"> | ||
</LoggerValueHistoryTimelineItem> | ||
<LoggerValueHistoryTimelineItem | ||
documentLocation = "#CharacterRangeLen=0&CharacterRangeLoc=3960&EndingColumnNumber=27&EndingLineNumber=62&StartingColumnNumber=1&StartingLineNumber=62&Timestamp=485261764.843701" | ||
selectedRepresentationIndex = "0" | ||
shouldTrackSuperviewWidth = "NO"> | ||
</LoggerValueHistoryTimelineItem> | ||
<LoggerValueHistoryTimelineItem | ||
documentLocation = "#CharacterRangeLen=0&CharacterRangeLoc=3960&EndingColumnNumber=20&EndingLineNumber=61&StartingColumnNumber=1&StartingLineNumber=61&Timestamp=485261764.843851" | ||
lockedSize = "{175, 50}" | ||
selectedRepresentationIndex = "0" | ||
shouldTrackSuperviewWidth = "NO"> | ||
</LoggerValueHistoryTimelineItem> | ||
<LoggerValueHistoryTimelineItem | ||
documentLocation = "#CharacterRangeLen=0&CharacterRangeLoc=3960&EndingColumnNumber=20&EndingLineNumber=61&StartingColumnNumber=1&StartingLineNumber=61&Timestamp=485261764.844004" | ||
selectedRepresentationIndex = "0" | ||
shouldTrackSuperviewWidth = "NO"> | ||
</LoggerValueHistoryTimelineItem> | ||
<LoggerValueHistoryTimelineItem | ||
documentLocation = "#CharacterRangeLen=0&CharacterRangeLoc=281&EndingColumnNumber=31&EndingLineNumber=11&StartingColumnNumber=1&StartingLineNumber=11&Timestamp=485255669.173545" | ||
selectedRepresentationIndex = "0" | ||
shouldTrackSuperviewWidth = "NO"> | ||
</LoggerValueHistoryTimelineItem> | ||
<LoggerValueHistoryTimelineItem | ||
documentLocation = "#CharacterRangeLen=0&CharacterRangeLoc=3960&EndingColumnNumber=20&EndingLineNumber=62&StartingColumnNumber=1&StartingLineNumber=62&Timestamp=485261764.84429" | ||
selectedRepresentationIndex = "0" | ||
shouldTrackSuperviewWidth = "NO"> | ||
</LoggerValueHistoryTimelineItem> | ||
<LoggerValueHistoryTimelineItem | ||
documentLocation = "#CharacterRangeLen=0&CharacterRangeLoc=3960&EndingColumnNumber=20&EndingLineNumber=62&StartingColumnNumber=1&StartingLineNumber=62&Timestamp=485261764.844435" | ||
selectedRepresentationIndex = "0" | ||
shouldTrackSuperviewWidth = "NO"> | ||
</LoggerValueHistoryTimelineItem> | ||
<LoggerValueHistoryTimelineItem | ||
documentLocation = "#CharacterRangeLen=9&CharacterRangeLoc=3949&EndingColumnNumber=10&EndingLineNumber=111&StartingColumnNumber=1&StartingLineNumber=111&Timestamp=485261764.844579" | ||
lockedSize = "{328, 333}" | ||
selectedRepresentationIndex = "0" | ||
shouldTrackSuperviewWidth = "NO"> | ||
</LoggerValueHistoryTimelineItem> | ||
</TimelineItems> | ||
</Timeline> |
7 changes: 5 additions & 2 deletions
7
Playgrounds/RenderPlaygrounds.xcworkspace/contents.xcworkspacedata
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file modified
BIN
+5.95 KB
(130%)
...Playgrounds.xcworkspace/xcuserdata/alexusbergo.xcuserdatad/UserInterfaceState.xcuserstate
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file modified
BIN
+0 Bytes
(100%)
...roj/project.xcworkspace/xcuserdata/alexusbergo.xcuserdatad/UserInterfaceState.xcuserstate
Binary file not shown.