From 6dd8124f769cbe71761e162c9fddbf7d16abea58 Mon Sep 17 00:00:00 2001 From: Christoffer Winterkvist Date: Sun, 4 Oct 2015 20:21:42 +0200 Subject: [PATCH 1/3] Move reuseIdentifier into a static string on controller --- Source/ComponentController.swift | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Source/ComponentController.swift b/Source/ComponentController.swift index 1e22ee6f..bd11063d 100644 --- a/Source/ComponentController.swift +++ b/Source/ComponentController.swift @@ -3,6 +3,7 @@ import UIKit class ComponentsController: UIViewController { private let componentViews: [ComponentView] + static let reuseIdentifier = "ComponentCell" lazy var collectionView: UICollectionView = { [unowned self] in let layout = UICollectionViewFlowLayout() @@ -10,7 +11,7 @@ class ComponentsController: UIViewController { let collectionView = UICollectionView(frame: UIScreen.mainScreen().bounds, collectionViewLayout: layout) collectionView.dataSource = self - collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "ComponentCell") + collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier) return collectionView }() @@ -34,7 +35,7 @@ extension ComponentsController: UICollectionViewDataSource { func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { let view = componentViews[indexPath.item] - let cell = collectionView.dequeueReusableCellWithReuseIdentifier("ComponentCell", forIndexPath: indexPath) + let cell = collectionView.dequeueReusableCellWithReuseIdentifier(ComponentsController.reuseIdentifier, forIndexPath: indexPath) cell.contentView.addSubview(view.render()) return cell } From 9b158b4cd465433c3475785e88e7b1d2d5d00c90 Mon Sep 17 00:00:00 2001 From: Christoffer Winterkvist Date: Sun, 4 Oct 2015 20:28:21 +0200 Subject: [PATCH 2/3] Refactor Parser --- Source/Library/Parser.swift | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/Source/Library/Parser.swift b/Source/Library/Parser.swift index c89f0b75..a2e61630 100644 --- a/Source/Library/Parser.swift +++ b/Source/Library/Parser.swift @@ -8,18 +8,16 @@ struct Parser { var views = [ComponentView]() for component in components { if let type = component["type"] as? String, - items = component["items"] as? [AnyObject] where type == "list" { + items = component["items"] as? [JSONDictionary] where type == "list" { var componentItems = [ListItem]() - for item in items { - guard let json = item as? JSONDictionary else { continue } + for json in items { componentItems.append(ListItem(json: json)) } views.append(ListComponent(items: componentItems)) } } - + return views } - } From 3c76f98808dbda25866f96d1219e2bcd5dabfdcb Mon Sep 17 00:00:00 2001 From: Christoffer Winterkvist Date: Sun, 4 Oct 2015 20:31:34 +0200 Subject: [PATCH 3/3] Rename ComponentView to Component --- Source/ComponentController.swift | 10 +++++----- Source/Components/ListComponent.swift | 4 ++-- Source/Library/Parser.swift | 6 +++--- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/Source/ComponentController.swift b/Source/ComponentController.swift index bd11063d..b7ea1cda 100644 --- a/Source/ComponentController.swift +++ b/Source/ComponentController.swift @@ -2,7 +2,7 @@ import UIKit class ComponentsController: UIViewController { - private let componentViews: [ComponentView] + private let Components: [Component] static let reuseIdentifier = "ComponentCell" lazy var collectionView: UICollectionView = { [unowned self] in @@ -16,8 +16,8 @@ class ComponentsController: UIViewController { return collectionView }() - required init(views: [ComponentView]) { - self.componentViews = views + required init(views: [Component]) { + self.Components = views super.init(nibName: nil, bundle: nil) self.view.addSubview(collectionView) } @@ -30,11 +30,11 @@ class ComponentsController: UIViewController { extension ComponentsController: UICollectionViewDataSource { func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { - return componentViews.count + return Components.count } func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { - let view = componentViews[indexPath.item] + let view = Components[indexPath.item] let cell = collectionView.dequeueReusableCellWithReuseIdentifier(ComponentsController.reuseIdentifier, forIndexPath: indexPath) cell.contentView.addSubview(view.render()) return cell diff --git a/Source/Components/ListComponent.swift b/Source/Components/ListComponent.swift index eda0d79a..3b7622e8 100644 --- a/Source/Components/ListComponent.swift +++ b/Source/Components/ListComponent.swift @@ -2,11 +2,11 @@ import UIKit import Tailor import Sugar -protocol ComponentView { +protocol Component { func render() -> UIView } -class ListComponent: NSObject, ComponentView { +class ListComponent: NSObject, Component { lazy var tableView: UITableView = { [unowned self] in let tableView = UITableView() diff --git a/Source/Library/Parser.swift b/Source/Library/Parser.swift index a2e61630..b1eae161 100644 --- a/Source/Library/Parser.swift +++ b/Source/Library/Parser.swift @@ -3,9 +3,9 @@ import Sugar struct Parser { - static func parse(json: JSONDictionary) -> [ComponentView] { - guard let components = json["components"] as? JSONArray else { return [ComponentView]() } - var views = [ComponentView]() + static func parse(json: JSONDictionary) -> [Component] { + guard let components = json["components"] as? JSONArray else { return [Component]() } + var views = [Component]() for component in components { if let type = component["type"] as? String, items = component["items"] as? [JSONDictionary] where type == "list" {