nghialv / Hakuba

:cherry_blossom: Cellmodel-driven tableview manager
MIT License
474 stars 40 forks source link

Hakuba

Platform Language License Issues

I want to slim down view controllers.

I want to manage tableview without the code of UITableViewDelegate and UITableViewDataSource.

That is why I created Hakuba.

( Hakuba is one of the most famous ski resorts in Japan. )

Features

Quick example
// viewController swift file

hakuba = Hakuba(tableView: tableView)

let cellmodel = YourCellModel(title: "Title", des: "description") {
    println("Did select cell with title = \(title)")
}

hakuba[2]
    .append(cellmodel)  // append a new cell model into datasource
    .bump(.fade)        // show the cell of your cell model in the table view

hakuba[1]
    .remove(1...3)
    .bump(.Right)
// your cell swift file

class YourCellModel: CellModel {
    let title: String
    let des: String

    init(title: String, des: String, selectionHandler: @escaping (Cell) -> Void) {
        self.title = title
        self.des = des
        super.init(YourCell.self, selectionHandler: selectionHandler)
    }
}

class YourCell: Cell, CellType {
    typealias CellModel = YourCellModel

    @IBOutlet weak var titleLabel: UILabel!

    override func configure() {
        guard let cellmodel = cellmodel else {
            return
        }

        titleLabel.text = cellmodel.title
    }
}

Usage

private lazy var hakuba = Hakuba(tableView: tableView)   
let section = Section() // create a new section

// inserting
hakuba
    .insert(section, at: 1)
    .bump()

// removing
hakuba
    .remove(at: index)
    .bump(.left)

hakuba
    .remove(section)
    .bump()

hakuba
    .removeAll()
    .bump()

// handing section index by enum
enum YourSection: Int, SectionIndexType {
    case top
    case center
    case bottom

    static let count = 3
}

let topSection = hakuba[YourSection.top]
// 1. appending
hakuba[0]
    .append(cellmodel)  // append a cellmodel
    .bump(.fade)        // and bump with `Fade` animation

hakuba[1]
    .append(cellmodels) // append a list of cellmodes
    .bump(.left)                    

// by using section
let section = hakuba[YourSection.top]
section
    .append(cellmodel)
    .bump()

// 2. inserting
section
    .insert(cellmodels, at: 1)
    .bump(.middle)

// 3. reseting
section
    .reset(cellmodels)  // replace current data in section by the new data
    .bump()

section
    .reset()    // or remove all data in section
    .bump()

// 4. removing
section
    .remove(at: 1)
    .bump(.right)

section
    .remove(range: 2...5)
    .bump()

section
    .removeLast()
    .bump()
// updating cell data
let section = hakuba[YourSection.top]
section[1].property = newData
section[1]
    .bump()     
section.sort().bump()
section.shuffle().bump()
section.map
section.filter
section.reduce
section.mapFilter
section.each

section.first
section.last
section[1]
section.count
hakuba
    .registerCellByNib(CellClass.self)

hakuba
    .registerCell(CellClass.self)

hakuba
    .registerHeaderFooterByNib(HeaderOrFooterClass.self)

hakuba
    .registerHeaderFooter(HeaderOrFooterClass.self)

// register a list of cells by using variadic parameters
hakuba.registerCellByNibs(CellClass1.self, CellClass2.self, ..., CellClassN.self)
let header = HeaderFooterViewModel(view: CustomHeaderView) {
    println("Did select header view")
}
hakuba[Section.top].header = header
hakuba.loadmoreEnabled = true
hakuba.loadmoreHandler = {
    // request api
    // append new data
}
hakuba.commitEditingHandler = { [weak self] style, indexPath in
self?.hakuba[indexPath.section]
    .remove(indexPath.row)
}
hakuba.deselectAllCells(animated: true)
func willAppear(data: CellModel)
func didDisappear(data: CellModel)

Installation

pod 'Hakuba'

Requirements

License

Hakuba is released under the MIT license. See LICENSE for details.