separate classes

This commit is contained in:
Max Sokolov 2015-11-12 19:17:42 +03:00
parent 668a40dbba
commit 9c3a1ee5c9
5 changed files with 206 additions and 162 deletions

View File

@ -0,0 +1,122 @@
//
// 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.
import UIKit
import Foundation
/**
Responsible for building cells of given type and passing items to them.
*/
public class TableRowBuilder<I, C where C: UITableViewCell> : RowBuilder {
public typealias ReturnValue = AnyObject
public typealias TableRowBuilderActionBlock = (data: ActionData<I, C>) -> Void
public typealias TableRowBuilderReturnValueActionBlock = (data: ActionData<I, C>) -> ReturnValue
private var actions = Dictionary<String, TableRowBuilderActionBlock>()
private var items = [I]()
public var reusableIdentifier: String
public var numberOfRows: Int {
get {
return items.count
}
}
public init(item: I, id: String) {
reusableIdentifier = id
items.append(item)
}
public init(items: [I]? = nil, id: String) {
reusableIdentifier = id
if items != nil {
self.items.appendContentsOf(items!)
}
}
public func appendItems(items: [I]) {
self.items.appendContentsOf(items)
}
public func clear() {
items.removeAll()
}
// MARK: Chaining actions
public func action(key: String, action: TableRowBuilderActionBlock) -> Self {
actions[key] = action
return self
}
public func action(key: ActionType, action: TableRowBuilderActionBlock) -> Self {
actions[key.rawValue] = action
return self
}
public func action(key: ActionType, action: TableRowBuilderReturnValueActionBlock) -> Self {
return self
}
// MARK: Triggers
public func triggerAction(key: String, cell: UITableViewCell, indexPath: NSIndexPath, itemIndex: Int) -> ActionResult {
let actionData = ActionData(cell: cell as! C, indexPath: indexPath, item: items[itemIndex], itemIndex: itemIndex)
if let block = actions[key] {
block(data: actionData)
return .Failure
}
return .Failure
}
}
/**
Responsible for building configurable cells of given type and passing items to them.
*/
public class TableConfigurableRowBuilder<I, C: ConfigurableCell where C.Item == I, C: UITableViewCell> : TableRowBuilder<I, C> {
public init(item: I) {
super.init(item: item, id: C.reusableIdentifier())
}
public init(items: [I]? = nil) {
super.init(items: items, id: C.reusableIdentifier())
}
public override func triggerAction(key: String, cell: UITableViewCell, indexPath: NSIndexPath, itemIndex: Int) -> ActionResult {
(cell as! C).configureWithItem(items[itemIndex])
return super.triggerAction(key, cell: cell, indexPath: indexPath, itemIndex: itemIndex)
}
}

View File

@ -0,0 +1,76 @@
//
// 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.
import UIKit
import Foundation
/**
Responsible for building a certain table view section.
*/
public class TableSectionBuilder {
private var builders = [RowBuilder]()
public var headerTitle: String?
public var footerTitle: String?
public var headerView: UIView?
public var headerHeight: CGFloat = UITableViewAutomaticDimension
public var footerView: UIView?
public var footerHeight: CGFloat = UITableViewAutomaticDimension
/// A total number of rows in section of each row builder.
public var numberOfRowsInSection: Int {
return builders.reduce(0) { $0 + $1.numberOfRows }
}
public init(headerTitle: String? = nil, footerTitle: String? = nil, rowBuilders: [RowBuilder]? = nil) {
self.headerTitle = headerTitle
self.footerTitle = footerTitle
if let initialRows = rowBuilders {
self.builders.appendContentsOf(initialRows)
}
}
public init(headerView: UIView? = nil, headerHeight: CGFloat = UITableViewAutomaticDimension, footerView: UIView? = nil, footerHeight: CGFloat = UITableViewAutomaticDimension) {
self.headerView = headerView
self.headerHeight = headerHeight
self.footerView = footerView
self.footerHeight = footerHeight
}
internal func builderAtIndex(var index: Int) -> (RowBuilder, Int)? {
for builder in builders {
if index < builder.numberOfRows {
return (builder, index)
}
index -= builder.numberOfRows
}
return nil
}
}

View File

@ -106,11 +106,6 @@ public class Action {
}
}
public class ActionObject<I, C> {
}
/**
If you want to delegate your cell configuration logic to cell itself (with your view model or even model) than
just provide an implementation of this protocol for your cell. Enjoy strong typisation.
@ -135,163 +130,6 @@ public protocol RowBuilder {
func triggerAction(key: String, cell: UITableViewCell, indexPath: NSIndexPath, itemIndex: Int) -> ActionResult
}
/**
A class that responsible for building cells of given type and passing items to them.
*/
public class TableRowBuilder<I, C where C: UITableViewCell> : RowBuilder {
public typealias ReturnValue = AnyObject
public typealias TableRowBuilderActionBlock = (data: ActionData<I, C>) -> Void
public typealias TableRowBuilderReturnValueActionBlock = (data: ActionData<I, C>) -> ReturnValue
private var actions = Dictionary<String, TableRowBuilderActionBlock>()
private var items = [I]()
public var reusableIdentifier: String
public var numberOfRows: Int {
get {
return items.count
}
}
public init(item: I, id: String) {
reusableIdentifier = id
items.append(item)
}
public init(items: [I]? = nil, id: String) {
reusableIdentifier = id
if items != nil {
self.items.appendContentsOf(items!)
}
}
public func appendItems(items: [I]) {
self.items.appendContentsOf(items)
}
public func clear() {
items.removeAll()
}
// MARK: Chaining actions
public func action(key: String, action: TableRowBuilderActionBlock) -> Self {
actions[key] = action
return self
}
public func action(key: ActionType, action: TableRowBuilderActionBlock) -> Self {
actions[key.rawValue] = action
return self
}
public func action(key: ActionType, action: TableRowBuilderReturnValueActionBlock) -> Self {
return self
}
// MARK: Triggers
public func triggerAction(key: String, cell: UITableViewCell, indexPath: NSIndexPath, itemIndex: Int) -> ActionResult {
let actionData = ActionData(cell: cell as! C, indexPath: indexPath, item: items[itemIndex], itemIndex: itemIndex)
if let block = actions[key] {
block(data: actionData)
return .Failure
}
return .Failure
}
}
/**
A class that responsible for building configurable cells of given type and passing items to them.
*/
public class TableConfigurableRowBuilder<I, C: ConfigurableCell where C.Item == I, C: UITableViewCell> : TableRowBuilder<I, C> {
public init(item: I) {
super.init(item: item, id: C.reusableIdentifier())
}
public init(items: [I]? = nil) {
super.init(items: items, id: C.reusableIdentifier())
}
public override func triggerAction(key: String, cell: UITableViewCell, indexPath: NSIndexPath, itemIndex: Int) -> ActionResult {
(cell as! C).configureWithItem(items[itemIndex])
return super.triggerAction(key, cell: cell, indexPath: indexPath, itemIndex: itemIndex)
}
}
/**
A class that responsible for building a certain table view section.
*/
public class TableSectionBuilder {
private var builders = [RowBuilder]()
public var headerTitle: String?
public var footerTitle: String?
public var headerView: UIView?
public var headerHeight: CGFloat = UITableViewAutomaticDimension
public var footerView: UIView?
public var footerHeight: CGFloat = UITableViewAutomaticDimension
/// A total number of rows in section of each row builder.
public var numberOfRowsInSection: Int {
var number = 0
for builder in builders {
number += builder.numberOfRows
}
return number
}
public init(headerTitle: String? = nil, footerTitle: String? = nil, rowBuilders: [RowBuilder]? = nil) {
self.headerTitle = headerTitle
self.footerTitle = footerTitle
if let initialRows = rowBuilders {
self.builders.appendContentsOf(initialRows)
}
}
public init(headerView: UIView? = nil, headerHeight: CGFloat = UITableViewAutomaticDimension, footerView: UIView? = nil, footerHeight: CGFloat = UITableViewAutomaticDimension) {
self.headerView = headerView
self.headerHeight = headerHeight
self.footerView = footerView
self.footerHeight = footerHeight
}
internal func builderAtIndex(var index: Int) -> (RowBuilder, Int)? {
for builder in builders {
if index < builder.numberOfRows {
return (builder, index)
}
index -= builder.numberOfRows
}
return nil
}
}
/**
Responsible for table view's datasource and delegate.

View File

@ -7,6 +7,8 @@
objects = {
/* Begin PBXBuildFile section */
508B71841BF48DD300272920 /* TableSectionBuilder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 508B71831BF48DD300272920 /* TableSectionBuilder.swift */; settings = {ASSET_TAGS = (); }; };
508B71861BF48E0D00272920 /* TableRowBuilder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 508B71851BF48E0D00272920 /* TableRowBuilder.swift */; settings = {ASSET_TAGS = (); }; };
DAB7EB2B1BEF787300D2AD5E /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = DAB7EB2A1BEF787300D2AD5E /* AppDelegate.swift */; };
DAB7EB2D1BEF787300D2AD5E /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = DAB7EB2C1BEF787300D2AD5E /* ViewController.swift */; };
DAB7EB301BEF787300D2AD5E /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = DAB7EB2E1BEF787300D2AD5E /* Main.storyboard */; };
@ -17,6 +19,8 @@
/* End PBXBuildFile section */
/* Begin PBXFileReference section */
508B71831BF48DD300272920 /* TableSectionBuilder.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TableSectionBuilder.swift; sourceTree = "<group>"; };
508B71851BF48E0D00272920 /* TableRowBuilder.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TableRowBuilder.swift; sourceTree = "<group>"; };
DAB7EB271BEF787300D2AD5E /* TabletDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = TabletDemo.app; sourceTree = BUILT_PRODUCTS_DIR; };
DAB7EB2A1BEF787300D2AD5E /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
DAB7EB2C1BEF787300D2AD5E /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; };
@ -74,6 +78,8 @@
isa = PBXGroup;
children = (
DAB7EB3D1BEF78A400D2AD5E /* Tablet.swift */,
508B71831BF48DD300272920 /* TableSectionBuilder.swift */,
508B71851BF48E0D00272920 /* TableRowBuilder.swift */,
);
name = Tablet;
path = ../Tablet;
@ -149,8 +155,10 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
508B71841BF48DD300272920 /* TableSectionBuilder.swift in Sources */,
DAB7EB2D1BEF787300D2AD5E /* ViewController.swift in Sources */,
DAB7EB3E1BEF78A400D2AD5E /* Tablet.swift in Sources */,
508B71861BF48E0D00272920 /* TableRowBuilder.swift in Sources */,
DAB7EB401BEFD07E00D2AD5E /* ConfigurableTableViewCell.swift in Sources */,
DAB7EB2B1BEF787300D2AD5E /* AppDelegate.swift in Sources */,
);