Skip to content

Commit

Permalink
Merge pull request #30 from nghialv/refactoring
Browse files Browse the repository at this point in the history
Use protocol extension to implement intValue in SectionIndexType
  • Loading branch information
nghialv committed Mar 16, 2016
2 parents 1c52a0d + 92e899f commit 7cb32b5
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
17 changes: 12 additions & 5 deletions Example/Example/CellTestViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@

import UIKit

enum SectionIndex: Int, SectionIndexType {
case Top
case Center

static let count = 2
}

class CellTestViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
private lazy var hakuba: Hakuba = Hakuba(tableView: self.tableView)
Expand All @@ -30,14 +37,14 @@ class CellTestViewController: UIViewController {
self?.pushChildViewController()
}
}

let topSection = Section()
let centerSection = Section()


hakuba
.reset([topSection, centerSection])
.reset(SectionIndex)
.bump()

let topSection = hakuba[SectionIndex.Top]
let centerSection = hakuba[SectionIndex.Center]

topSection
.reset(topCellmodels)
.bump()
Expand Down
9 changes: 8 additions & 1 deletion Hakuba/Common.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,15 @@ public typealias SelectionHandler = (Cell) -> ()

public typealias Animation = UITableViewRowAnimation

public protocol SectionIndex {
public protocol SectionIndexType {
var intValue: Int { get }
static var count: Int { get }
}

public extension SectionIndexType where Self: RawRepresentable, Self.RawValue == Int {
var intValue: Int {
return rawValue
}
}

public enum HakubaBumpType {
Expand Down
9 changes: 7 additions & 2 deletions Hakuba/Hakuba.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ final public class Hakuba: NSObject {
public var cellEditable = false
public var commitEditingHandler: ((UITableViewCellEditingStyle, NSIndexPath) -> ())?

public subscript(index: SectionIndex) -> Section {
public subscript(index: SectionIndexType) -> Section {
get {
return self[index.intValue]
}
Expand Down Expand Up @@ -73,7 +73,7 @@ final public class Hakuba: NSObject {
return sections.get(index)
}

public func getSection(index: SectionIndex) -> Section? {
public func getSection(index: SectionIndexType) -> Section? {
return getSection(index.intValue)
}

Expand Down Expand Up @@ -148,6 +148,11 @@ public extension Hakuba {
public extension Hakuba {
// MARK - Reset

func reset(listType: SectionIndexType.Type) -> Self {
let sections = (0...listType.count).map { _ in Section() }
return reset(sections)
}

func reset() -> Self {
return reset([])
}
Expand Down

0 comments on commit 7cb32b5

Please sign in to comment.