-
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 23, 2016
1 parent
29dc216
commit e536e45
Showing
14 changed files
with
230 additions
and
78 deletions.
There are no files selected for viewing
Binary file modified
BIN
+1.33 KB
(100%)
...roj/project.xcworkspace/xcuserdata/alexusbergo.xcuserdatad/UserInterfaceState.xcuserstate
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+1.11 KB
(110%)
.../playground.xcworkspace/xcuserdata/alexusbergo.xcuserdatad/UserInterfaceState.xcuserstate
Binary file not shown.
135 changes: 135 additions & 0 deletions
135
Playgrounds/05 List Component.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,135 @@ | ||
import UIKit | ||
import XCPlayground | ||
import Render | ||
|
||
/*: | ||
![logo](logo_small.png) | ||
# ComponentTableView/CollectionView | ||
|
||
Although the approach shown above works perfectly, it does clashes with the React-like component pattern. | ||
`ComponentTableView` and `ComponentCollectionView` expose the same interface and work with a simple array of `ListComponentItemType` (see also `ListComponentItem<ComponentViewType, ComponentStateType>: ListComponentItemType`). | ||
ComponentTableView/CollectionView takes care of cell reuse for you and apply a diff algorithm when the `items` property is set (so that proper insertions/deletions are performed rather than `reloadData() `). | ||
|
||
*/ | ||
|
||
func ==(lhs: Album, rhs: Album) -> Bool { | ||
return lhs.id == rhs.id | ||
} | ||
class Album: ComponentStateBase { | ||
let id = NSUUID().UUIDString | ||
let title: String = "Foo" | ||
let artist: String = "Bar" | ||
let cover: UIImage = UIImage(named: "logo_rect")! | ||
var featured: Bool | ||
|
||
init(featured: Bool = false) { | ||
self.featured = featured | ||
} | ||
} | ||
|
||
// This will just improve the performance in list diffs. | ||
extension Album: ComponentStateTypeUniquing { | ||
var stateUniqueIdentifier: String { | ||
return self.id | ||
} | ||
} | ||
|
||
class AlbumComponentView: ComponentView { | ||
|
||
// If the component is used as list item it should be registered | ||
// as prototype for the infra. | ||
override class func initialize() { | ||
registerPrototype(component: AlbumComponentView()) | ||
} | ||
|
||
/// The component state. | ||
var album: Album? { return self.state as? Album } | ||
var featured: Bool { return self.album?.featured ?? false } | ||
|
||
/// Constructs the component tree. | ||
override func construct() -> ComponentNodeType { | ||
return ComponentNode<UIView>().configure({ view in | ||
view.style.flexDirection = self.featured ? .Column : .Row | ||
view.backgroundColor = UIColor.blackColor() | ||
view.style.dimensions.width = self.featured ? ~self.parentSize.width/2 : ~self.parentSize.width | ||
view.style.dimensions.height = self.featured ? Undefined : 64 | ||
}).children([ | ||
ComponentNode<UIImageView>().configure({ view in | ||
view.image = self.album?.cover | ||
view.style.alignSelf = .Center | ||
view.style.dimensions.width = self.featured ? ~self.parentSize.width/2 : 48 | ||
view.style.dimensions.height = self.featured ? view.style.dimensions.width : 48 | ||
}), | ||
ComponentNode<UIView>().configure({ view in | ||
view.style.flexDirection = .Column | ||
view.style.margin = (0.0, 4.0, 0.0, 4.0, 4.0, 4.0) | ||
view.style.alignSelf = .Center | ||
|
||
}).children([ | ||
ComponentNode<UILabel>().configure({ view in | ||
view.style.margin = (0.0, 4.0, 0.0, 4.0, 4.0, 4.0) | ||
view.text = self.album?.title ?? "None" | ||
view.textColor = UIColor.whiteColor() | ||
}) | ||
]) | ||
]) | ||
} | ||
} | ||
|
||
class ListDemoViewController: UIViewController { | ||
|
||
// The item list. | ||
var albums: [ListComponentItemType] = [ListComponentItem<AlbumComponentView, Album>]() { | ||
didSet { | ||
self.listComponentView.renderComponent(self.view.bounds.size) | ||
} | ||
} | ||
|
||
/// The collection view component. | ||
lazy var listComponentView: ComponentCollectionView = { | ||
return ComponentCollectionView() | ||
}() | ||
|
||
/// Called after the controller's view is loaded into memory. | ||
override func viewDidLoad() { | ||
super.viewDidLoad() | ||
|
||
// generate some fake data | ||
self.prepareDummyData() | ||
|
||
// configure the list component. | ||
self.listComponentView.configure() { | ||
guard let view = $0 as? ComponentCollectionView else { return } | ||
view.frame.size = view.parentSize | ||
view.backgroundColor = UIColor.blackColor() | ||
view.items = self.albums | ||
} | ||
self.view.addSubview(self.listComponentView) | ||
} | ||
|
||
/// Called to notify the view controller that its view has just laid out its subviews. | ||
override func viewDidLayoutSubviews() { | ||
self.listComponentView.renderComponent(self.view.bounds.size) | ||
} | ||
} | ||
|
||
extension ListDemoViewController { | ||
|
||
//creates some dummy models. | ||
func prepareDummyData() { | ||
var albums = [ListComponentItemType]() | ||
for idx in 0..<4 { | ||
let item = ListComponentItem<AlbumComponentView, Album>(state: Album(featured: idx < 2)) | ||
albums.append(item) | ||
} | ||
self.albums = albums | ||
} | ||
} | ||
|
||
let vc = ListDemoViewController() | ||
vc.view.frame = CGRect(origin: CGPoint.zero, size:CGSize(width: 500, height: 500)) | ||
|
||
vc.view | ||
|
||
|
||
|
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions
11
Playgrounds/05 List Component.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,11 @@ | ||
|
||
import UIKit | ||
|
||
public func snapshot(view: UIView) -> UIImage { | ||
view.layoutSubviews() | ||
UIGraphicsBeginImageContext(view.frame.size) | ||
view.layer.renderInContext(UIGraphicsGetCurrentContext()!) | ||
let image = UIGraphicsGetImageFromCurrentImageContext() | ||
UIGraphicsEndImageContext() | ||
return image | ||
} |
4 changes: 4 additions & 0 deletions
4
Playgrounds/05 List Component.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'> | ||
<timeline fileName='timeline.xctimeline'/> | ||
</playground> |
7 changes: 7 additions & 0 deletions
7
Playgrounds/05 List Component.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
+8.5 KB
.../playground.xcworkspace/xcuserdata/alexusbergo.xcuserdatad/UserInterfaceState.xcuserstate
Binary file not shown.
64 changes: 64 additions & 0 deletions
64
Playgrounds/05 List Component.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,64 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<Timeline | ||
version = "3.0"> | ||
<TimelineItems> | ||
<LoggerValueHistoryTimelineItem | ||
documentLocation = "#CharacterRangeLen=0&CharacterRangeLoc=4747&EndingColumnNumber=54&EndingLineNumber=78&StartingColumnNumber=1&StartingLineNumber=78&Timestamp=485699974.355752" | ||
selectedRepresentationIndex = "0" | ||
shouldTrackSuperviewWidth = "NO"> | ||
</LoggerValueHistoryTimelineItem> | ||
<LoggerValueHistoryTimelineItem | ||
documentLocation = "#CharacterRangeLen=0&CharacterRangeLoc=4747&EndingColumnNumber=23&EndingLineNumber=78&StartingColumnNumber=1&StartingLineNumber=78&Timestamp=485699974.355964" | ||
selectedRepresentationIndex = "0" | ||
shouldTrackSuperviewWidth = "NO"> | ||
</LoggerValueHistoryTimelineItem> | ||
<LoggerValueHistoryTimelineItem | ||
documentLocation = "#CharacterRangeLen=0&CharacterRangeLoc=4747&EndingColumnNumber=27&EndingLineNumber=78&StartingColumnNumber=1&StartingLineNumber=78&Timestamp=485699974.356142" | ||
selectedRepresentationIndex = "0" | ||
shouldTrackSuperviewWidth = "NO"> | ||
</LoggerValueHistoryTimelineItem> | ||
<LoggerValueHistoryTimelineItem | ||
documentLocation = "#CharacterRangeLen=0&CharacterRangeLoc=4747&EndingColumnNumber=27&EndingLineNumber=78&StartingColumnNumber=1&StartingLineNumber=78&Timestamp=485699974.356316" | ||
selectedRepresentationIndex = "0" | ||
shouldTrackSuperviewWidth = "NO"> | ||
</LoggerValueHistoryTimelineItem> | ||
<LoggerValueHistoryTimelineItem | ||
documentLocation = "#CharacterRangeLen=0&CharacterRangeLoc=4747&EndingColumnNumber=20&EndingLineNumber=78&StartingColumnNumber=1&StartingLineNumber=78&Timestamp=485699974.356484" | ||
lockedSize = "{175, 50}" | ||
selectedRepresentationIndex = "0" | ||
shouldTrackSuperviewWidth = "NO"> | ||
</LoggerValueHistoryTimelineItem> | ||
<LoggerValueHistoryTimelineItem | ||
documentLocation = "#CharacterRangeLen=0&CharacterRangeLoc=4747&EndingColumnNumber=20&EndingLineNumber=78&StartingColumnNumber=1&StartingLineNumber=78&Timestamp=485699974.356659" | ||
selectedRepresentationIndex = "0" | ||
shouldTrackSuperviewWidth = "NO"> | ||
</LoggerValueHistoryTimelineItem> | ||
<LoggerValueHistoryTimelineItem | ||
documentLocation = "#CharacterRangeLen=0&CharacterRangeLoc=4747&EndingColumnNumber=31&EndingLineNumber=78&StartingColumnNumber=1&StartingLineNumber=78&Timestamp=485699974.356824" | ||
selectedRepresentationIndex = "0" | ||
shouldTrackSuperviewWidth = "NO"> | ||
</LoggerValueHistoryTimelineItem> | ||
<LoggerValueHistoryTimelineItem | ||
documentLocation = "#CharacterRangeLen=0&CharacterRangeLoc=4747&EndingColumnNumber=20&EndingLineNumber=78&StartingColumnNumber=1&StartingLineNumber=78&Timestamp=485699974.356992" | ||
selectedRepresentationIndex = "0" | ||
shouldTrackSuperviewWidth = "NO"> | ||
</LoggerValueHistoryTimelineItem> | ||
<LoggerValueHistoryTimelineItem | ||
documentLocation = "#CharacterRangeLen=0&CharacterRangeLoc=4747&EndingColumnNumber=20&EndingLineNumber=78&StartingColumnNumber=1&StartingLineNumber=78&Timestamp=485699974.357161" | ||
selectedRepresentationIndex = "0" | ||
shouldTrackSuperviewWidth = "NO"> | ||
</LoggerValueHistoryTimelineItem> | ||
<LoggerValueHistoryTimelineItem | ||
documentLocation = "#CharacterRangeLen=0&CharacterRangeLoc=4747&EndingColumnNumber=10&EndingLineNumber=78&StartingColumnNumber=1&StartingLineNumber=78&Timestamp=485699974.357336" | ||
lockedSize = "{338, 345}" | ||
selectedRepresentationIndex = "0" | ||
shouldTrackSuperviewWidth = "NO"> | ||
</LoggerValueHistoryTimelineItem> | ||
<LoggerValueHistoryTimelineItem | ||
documentLocation = "#CharacterRangeLen=7&CharacterRangeLoc=4737&EndingColumnNumber=8&EndingLineNumber=131&StartingColumnNumber=1&StartingLineNumber=131&Timestamp=485699974.357484" | ||
lockedSize = "{205, 226}" | ||
selectedRepresentationIndex = "0" | ||
shouldTrackSuperviewWidth = "NO"> | ||
</LoggerValueHistoryTimelineItem> | ||
</TimelineItems> | ||
</Timeline> |
3 changes: 3 additions & 0 deletions
3
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
+4.17 KB
(110%)
...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