From 533c6e6eb8ab68f02464ccb511967aedf810ef68 Mon Sep 17 00:00:00 2001 From: Max Sokolov Date: Sun, 12 Jun 2016 23:42:39 +0300 Subject: [PATCH] add row builder --- TableKit/TableKit.xcodeproj/project.pbxproj | 4 ++ TableKit/TableRow.swift | 2 +- TableKit/TableRowBuilder.swift | 52 +++++++++++++++++++ TableKit/TableSection.swift | 4 +- .../Controllers/MainController.swift | 27 +++++----- 5 files changed, 73 insertions(+), 16 deletions(-) create mode 100644 TableKit/TableRowBuilder.swift diff --git a/TableKit/TableKit.xcodeproj/project.pbxproj b/TableKit/TableKit.xcodeproj/project.pbxproj index 449e067..085b763 100644 --- a/TableKit/TableKit.xcodeproj/project.pbxproj +++ b/TableKit/TableKit.xcodeproj/project.pbxproj @@ -16,6 +16,7 @@ DA9EA7781D0B68460021F650 /* TableSection.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA9EA76F1D0B68460021F650 /* TableSection.swift */; }; DA9EA7791D0B68460021F650 /* TableCellAction.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA9EA7701D0B68460021F650 /* TableCellAction.swift */; }; DA9EA7801D0B689C0021F650 /* TableKit.h in Headers */ = {isa = PBXBuildFile; fileRef = DA9EA77E1D0B689C0021F650 /* TableKit.h */; settings = {ATTRIBUTES = (Public, ); }; }; + DA9EA7861D0DFAD60021F650 /* TableRowBuilder.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA9EA7851D0DFAD60021F650 /* TableRowBuilder.swift */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -30,6 +31,7 @@ DA9EA7701D0B68460021F650 /* TableCellAction.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TableCellAction.swift; sourceTree = ""; }; DA9EA77D1D0B689C0021F650 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; DA9EA77E1D0B689C0021F650 /* TableKit.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TableKit.h; sourceTree = ""; }; + DA9EA7851D0DFAD60021F650 /* TableRowBuilder.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TableRowBuilder.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -66,6 +68,7 @@ DA9EA76B1D0B68460021F650 /* TableDirector.swift */, DA9EA76F1D0B68460021F650 /* TableSection.swift */, DA9EA76C1D0B68460021F650 /* TableRow.swift */, + DA9EA7851D0DFAD60021F650 /* TableRowBuilder.swift */, DA9EA76D1D0B68460021F650 /* TableRowAction.swift */, DA9EA7701D0B68460021F650 /* TableCellAction.swift */, DA9EA7681D0B68460021F650 /* ConfigurableCell.swift */, @@ -168,6 +171,7 @@ DA9EA7751D0B68460021F650 /* TableRow.swift in Sources */, DA9EA7761D0B68460021F650 /* TableRowAction.swift in Sources */, DA9EA7741D0B68460021F650 /* TableDirector.swift in Sources */, + DA9EA7861D0DFAD60021F650 /* TableRowBuilder.swift in Sources */, DA9EA7791D0B68460021F650 /* TableCellAction.swift in Sources */, DA9EA7731D0B68460021F650 /* Operators.swift in Sources */, ); diff --git a/TableKit/TableRow.swift b/TableKit/TableRow.swift index 770c686..2b06615 100644 --- a/TableKit/TableRow.swift +++ b/TableKit/TableRow.swift @@ -79,7 +79,7 @@ public class TableRow) -> Self { + public func addAction(action: TableRowAction) -> Self { actions[action.type.key] = action return self diff --git a/TableKit/TableRowBuilder.swift b/TableKit/TableRowBuilder.swift new file mode 100644 index 0000000..6351159 --- /dev/null +++ b/TableKit/TableRowBuilder.swift @@ -0,0 +1,52 @@ +// +// Copyright (c) 2015 Max Sokolov https://twitter.com/max_sokolov +// +// Permission is hereby granted, free of charge, to any person obtaining a copy of +// this software and associated documentation files (the "Software"), to deal in +// the Software without restriction, including without limitation the rights to +// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +// the Software, and to permit persons to whom the Software is furnished to do so, +// subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +public protocol RowBuilder { + + func rowItems() -> [Row]? +} + +public class TableRowBuilder: RowBuilder { + + public typealias BuilderHandler = (TableRowBuilder) -> () + + public var items: [ItemType]? + public var actions: [TableRowAction]? + + public init(handler: BuilderHandler) { + handler(self) + } + + // MARK: - RowBuilder - + + public func rowItems() -> [Row]? { + return items?.map { TableRow(item: $0, actions: actions) } + } +} + +public extension TableSection { + + public func append(builder builder: RowBuilder) { + + if let rows = builder.rowItems() { + append(rows: rows) + } + } +} \ No newline at end of file diff --git a/TableKit/TableSection.swift b/TableKit/TableSection.swift index 63b6deb..51453df 100644 --- a/TableKit/TableSection.swift +++ b/TableKit/TableSection.swift @@ -48,14 +48,14 @@ public class TableSection { } } - public convenience init(headerTitle: String?, footerTitle: String?, rows: [Row]?) { + public convenience init(headerTitle: String?, footerTitle: String?, rows: [Row]? = nil) { self.init(rows: rows) self.headerTitle = headerTitle self.footerTitle = footerTitle } - public convenience init(headerView: UIView?, footerView: UIView?, rows: [Row]?) { + public convenience init(headerView: UIView?, footerView: UIView?, rows: [Row]? = nil) { self.init(rows: rows) self.headerView = headerView diff --git a/TableKitDemo/Classes/Presentation/Controllers/MainController.swift b/TableKitDemo/Classes/Presentation/Controllers/MainController.swift index 228e894..d3e33eb 100644 --- a/TableKitDemo/Classes/Presentation/Controllers/MainController.swift +++ b/TableKitDemo/Classes/Presentation/Controllers/MainController.swift @@ -21,22 +21,20 @@ class MainController: UIViewController { override func viewDidLoad() { super.viewDidLoad() - /*let rowBuilder = TableRowBuilder(items: ["1", "1", "1", "1"]) - .action(.click) { [unowned self] data in - - self.performSegueWithIdentifier("headerfooter", sender: nil) - }*/ - - - let a = TableRowAction(.click) { - (row) in + (data) in - print("3") + print("3", data.item) } + let b = TableRowBuilder { + + $0.items = ["1", "2", "3"] + $0.actions = [a] + } + let row1 = TableRow(item: "1") let row2 = TableRow(item: "2") @@ -45,13 +43,13 @@ class MainController: UIViewController { row1 - .action(TableRowAction(.shouldHighlight) { (data) -> Bool in + .addAction(TableRowAction(.shouldHighlight) { (data) -> Bool in print("1") return false }) - .action(TableRowAction(.click) { (data) in + .addAction(TableRowAction(.click) { (data) in print("2") @@ -59,7 +57,10 @@ class MainController: UIViewController { }) - let section = TableSection(headerTitle: "", footerTitle: "", rows: [row1, row2, row3]) + let section = TableSection() + section.append(builder: b) + + //let section = TableSection(headerTitle: "", footerTitle: "", rows: [row1, row2, row3]) tableDirector += [section]