Skip to content

Commit 13f15d5

Browse files
author
Alex Usbergo
committed
more playgrounds
1 parent 62957dd commit 13f15d5

File tree

28 files changed

+248
-4
lines changed

28 files changed

+248
-4
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios' display-mode='rendered' executeOnSourceChanges='false'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import UIKit
2+
import XCPlayground
3+
import Render
4+
5+
/*:
6+
![logo](logo_small.png)
7+
# Components embedded in cells
8+
9+
You can wrap your components in `ComponentTableViewCell` or `ComponentCollectionViewCell` and use
10+
the classic dataSource/delegate pattern for you view controller.
11+
*/
12+
13+
class FooState: ComponentStateBase {
14+
let text: String = randomString(randomInt(0, max: 500))
15+
}
16+
17+
class FooComponentView: ComponentView {
18+
19+
override class func initialize() {
20+
21+
// if we intend to use this component inside of a cell
22+
// we have to register an instance as a prototype for the infra.
23+
// The 'initialize' function seems to be a convenient palce to do it.
24+
registerPrototype(component: FooComponentView())
25+
}
26+
27+
// we cast the state for convenience
28+
var fooState: FooState {
29+
return (self.state as? FooState) ?? FooState()
30+
}
31+
32+
override func construct() -> ComponentNodeType {
33+
let margin: Float = 4.0
34+
let insets: Inset = (margin, margin, margin, margin, margin, margin)
35+
return ComponentNode<UIView>().configure({ view in
36+
view.style.flexDirection = .Row
37+
view.style.margin = insets
38+
39+
// that's how we can define the size in relation to the size of the parent view.
40+
let min = ~self.parentSize.width
41+
view.style.minDimensions.width = max(min, 96)
42+
43+
view.backgroundColor = UIColor.A
44+
}).children([
45+
ComponentNode<UIView>().configure({ view in
46+
view.style.dimensions = (32, 32)
47+
view.style.margin = insets
48+
view.backgroundColor = UIColor.B
49+
view.layer.cornerRadius = 16
50+
}),
51+
ComponentNode<UILabel>().configure({ view in
52+
view.style.margin = insets
53+
view.style.alignSelf = .Center
54+
view.style.flex = Flex.Max
55+
view.text = self.fooState.text
56+
view.numberOfLines = 0
57+
view.font = UIFont.systemFontOfSize(12.0, weight: UIFontWeightLight)
58+
})
59+
])
60+
}
61+
}
62+
63+
/*: Now this is how we wrap the component inside a `ComponentTableViewCell` in our datasource */
64+
65+
class DataSource: NSObject, UITableViewDataSource {
66+
67+
let items: [FooState] = {
68+
var items = [FooState]()
69+
for _ in 0..<6 {
70+
items.append(FooState())
71+
}
72+
return items
73+
}()
74+
75+
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
76+
return items.count
77+
}
78+
79+
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
80+
let reuseIdentifier = String(FooComponentView.self)
81+
let cell: ComponentTableViewCell! =
82+
//dequeue a cell with the given identifier
83+
//(remember to use different identifiers for different component classes)
84+
tableView.dequeueReusableCellWithIdentifier(reuseIdentifier) as? ComponentTableViewCell ??
85+
86+
//or create a new Cell wrapping the component
87+
ComponentTableViewCell(reuseIdentifier: reuseIdentifier, component: FooComponentView())
88+
89+
//set the state for the cell
90+
cell.state = items[indexPath.row]
91+
92+
//and render the component
93+
cell.renderComponent(CGSize.sizeConstraintToWidth(tableView.bounds.size.width))
94+
return cell
95+
}
96+
}
97+
98+
/*: And finally create a `UITableView` */
99+
100+
let tableView = UITableView(frame: CGRect(origin: CGPoint.zero, size: CGSize(width: 320, height: 320)))
101+
102+
//we want automatic dimensions for our cells
103+
tableView.rowHeight = UITableViewAutomaticDimension
104+
105+
//this improves drastically the performance of reloadData when you have a large collection of items
106+
tableView.estimatedRowHeight = 100
107+
108+
let dataSource = DataSource()
109+
tableView.dataSource = dataSource
110+
tableView.reloadData()
111+
112+
tableView
113+
114+
115+
116+
Loading
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
2+
import UIKit
3+
4+
public func snapshot(view: UIView) -> UIImage {
5+
view.layoutSubviews()
6+
UIGraphicsBeginImageContext(view.frame.size)
7+
view.layer.renderInContext(UIGraphicsGetCurrentContext()!)
8+
let image = UIGraphicsGetImageFromCurrentImageContext()
9+
UIGraphicsEndImageContext()
10+
return image
11+
}
12+
13+
extension UIColor {
14+
class public var A: UIColor {
15+
return UIColor(red: 238/255, green: 238/255, blue: 238/255, alpha: 1)
16+
}
17+
class public var B: UIColor {
18+
return UIColor(red: 0/255, green: 188/255, blue: 212/255, alpha: 1)
19+
}
20+
class public var C: UIColor {
21+
return UIColor(red: 76/255, green: 175/255, blue: 80/255, alpha: 1)
22+
}
23+
class public var D: UIColor {
24+
return UIColor(red: 165/255, green: 214/255, blue: 167/255, alpha: 1)
25+
}
26+
class public var E: UIColor {
27+
return UIColor(red: 46/255, green: 125/255, blue: 50/255, alpha: 1)
28+
}
29+
class public var F: UIColor {
30+
return UIColor(red: 244/255, green: 67/255, blue: 54/255, alpha: 1)
31+
}
32+
class public var G: UIColor {
33+
return UIColor(red: 33/255, green: 150/255, blue: 243/255, alpha: 1)
34+
}
35+
}
36+
37+
public func randomString(length: Int) -> String {
38+
let allowedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 "
39+
let allowedCharsCount = UInt32(allowedChars.characters.count)
40+
var randomString = ""
41+
for _ in (0..<length) {
42+
let randomNum = Int(arc4random_uniform(allowedCharsCount))
43+
let newCharacter = allowedChars[allowedChars.startIndex.advancedBy(randomNum)]
44+
randomString += String(newCharacter)
45+
}
46+
return randomString
47+
}
48+
49+
public func randomInt(min: Int, max:Int) -> Int {
50+
return min + Int(arc4random_uniform(UInt32(max - min + 1)))
51+
}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2-
<playground version='5.0' target-platform='ios' display-mode='raw' executeOnSourceChanges='false'>
2+
<playground version='5.0' target-platform='ios' display-mode='raw'>
33
<timeline fileName='timeline.xctimeline'/>
44
</playground>

Playgrounds/03 Components embedded in Cells.playground/playground.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Timeline
3+
version = "3.0">
4+
<TimelineItems>
5+
<LoggerValueHistoryTimelineItem
6+
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=3960&amp;EndingColumnNumber=54&amp;EndingLineNumber=62&amp;StartingColumnNumber=1&amp;StartingLineNumber=62&amp;Timestamp=485261764.843199"
7+
selectedRepresentationIndex = "0"
8+
shouldTrackSuperviewWidth = "NO">
9+
</LoggerValueHistoryTimelineItem>
10+
<LoggerValueHistoryTimelineItem
11+
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=3960&amp;EndingColumnNumber=23&amp;EndingLineNumber=62&amp;StartingColumnNumber=1&amp;StartingLineNumber=62&amp;Timestamp=485261764.843389"
12+
selectedRepresentationIndex = "0"
13+
shouldTrackSuperviewWidth = "NO">
14+
</LoggerValueHistoryTimelineItem>
15+
<LoggerValueHistoryTimelineItem
16+
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=3960&amp;EndingColumnNumber=27&amp;EndingLineNumber=62&amp;StartingColumnNumber=1&amp;StartingLineNumber=62&amp;Timestamp=485261764.843548"
17+
selectedRepresentationIndex = "0"
18+
shouldTrackSuperviewWidth = "NO">
19+
</LoggerValueHistoryTimelineItem>
20+
<LoggerValueHistoryTimelineItem
21+
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=3960&amp;EndingColumnNumber=27&amp;EndingLineNumber=62&amp;StartingColumnNumber=1&amp;StartingLineNumber=62&amp;Timestamp=485261764.843701"
22+
selectedRepresentationIndex = "0"
23+
shouldTrackSuperviewWidth = "NO">
24+
</LoggerValueHistoryTimelineItem>
25+
<LoggerValueHistoryTimelineItem
26+
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=3960&amp;EndingColumnNumber=20&amp;EndingLineNumber=61&amp;StartingColumnNumber=1&amp;StartingLineNumber=61&amp;Timestamp=485261764.843851"
27+
lockedSize = "{175, 50}"
28+
selectedRepresentationIndex = "0"
29+
shouldTrackSuperviewWidth = "NO">
30+
</LoggerValueHistoryTimelineItem>
31+
<LoggerValueHistoryTimelineItem
32+
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=3960&amp;EndingColumnNumber=20&amp;EndingLineNumber=61&amp;StartingColumnNumber=1&amp;StartingLineNumber=61&amp;Timestamp=485261764.844004"
33+
selectedRepresentationIndex = "0"
34+
shouldTrackSuperviewWidth = "NO">
35+
</LoggerValueHistoryTimelineItem>
36+
<LoggerValueHistoryTimelineItem
37+
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=281&amp;EndingColumnNumber=31&amp;EndingLineNumber=11&amp;StartingColumnNumber=1&amp;StartingLineNumber=11&amp;Timestamp=485255669.173545"
38+
selectedRepresentationIndex = "0"
39+
shouldTrackSuperviewWidth = "NO">
40+
</LoggerValueHistoryTimelineItem>
41+
<LoggerValueHistoryTimelineItem
42+
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=3960&amp;EndingColumnNumber=20&amp;EndingLineNumber=62&amp;StartingColumnNumber=1&amp;StartingLineNumber=62&amp;Timestamp=485261764.84429"
43+
selectedRepresentationIndex = "0"
44+
shouldTrackSuperviewWidth = "NO">
45+
</LoggerValueHistoryTimelineItem>
46+
<LoggerValueHistoryTimelineItem
47+
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=3960&amp;EndingColumnNumber=20&amp;EndingLineNumber=62&amp;StartingColumnNumber=1&amp;StartingLineNumber=62&amp;Timestamp=485261764.844435"
48+
selectedRepresentationIndex = "0"
49+
shouldTrackSuperviewWidth = "NO">
50+
</LoggerValueHistoryTimelineItem>
51+
<LoggerValueHistoryTimelineItem
52+
documentLocation = "#CharacterRangeLen=9&amp;CharacterRangeLoc=3949&amp;EndingColumnNumber=10&amp;EndingLineNumber=111&amp;StartingColumnNumber=1&amp;StartingLineNumber=111&amp;Timestamp=485261764.844579"
53+
lockedSize = "{328, 333}"
54+
selectedRepresentationIndex = "0"
55+
shouldTrackSuperviewWidth = "NO">
56+
</LoggerValueHistoryTimelineItem>
57+
</TimelineItems>
58+
</Timeline>

Playgrounds/RenderPlaygrounds.xcworkspace/contents.xcworkspacedata

Lines changed: 5 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Playgrounds/RenderPlaygrounds.xcworkspace/xcuserdata/alexusbergo.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<BreakpointProxy
1717
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
1818
<BreakpointContent
19-
shouldBeEnabled = "Yes"
19+
shouldBeEnabled = "No"
2020
ignoreCount = "0"
2121
continueAfterRunningActions = "No"
2222
filePath = "../Render/BaseComponentView.swift"

Render/ComponentView.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ public class FlexboxComponentView: BaseComponentView {
122122
return _root!
123123
}
124124

125+
125126
/// 'true' is the root node has been constructed already, 'false' otherwise
126127
public var isRootInitialized: Bool {
127128
guard let _ = self._root else { return false}

Render/ComponentViewType.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,7 @@ public protocol ComponentViewType: class {
6464
/// - parameter state: The (optional) state for this component.
6565
func renderComponent(size: CGSize)
6666
}
67+
68+
/// Used mostyle as base class for internal tests.
69+
public class ComponentStateBase: NSObject, ComponentStateType {
70+
}

0 commit comments

Comments
 (0)