Skip to content

Swift-Gurus/EZSource

Repository files navigation

EZSource

Build Status License Platform

Documentation

Usage

The core of EZSource for UITableView. Provides declarative API to work with UITableView All you need is to initialize the source, create rows, add them to section updates and call update function The rest is handled by EZSource, next time you have to update the UITableView, EZSource will find take care about inserting, deleting or reloading rows using animation provided in the section updates

Usage Example

  • Define a Cell Model
struct StringCellModel:  Hashable  {
  let uniqueID: String
  let text: String

  // Defines uniqueness of the model
  func hash(into hasher: inout Hasher) {
      hasher.combine(uniqueID)
  }

  // Defines dynamic context of the model
  static func == (lhs: Self, rhs: Self) -> Bool {
      lhs.text == rhs.text
  }
}
  • Define a Cell
final class StringCell: UITableViewCell, ReusableCell, Configurable {
  typealias Model = StringCellModel

  func configure(with model: StringCellModel) {
      textLabel?.text = "ID: \(model.uniqueID): \(model.text)"
      selectionStyle = .none
  }
}
  • Create Data Source
let config = DiffableTableViewDataSource.Config(tableView: tableView,
                                                cellTypes: [StringCell.self],
                                                headerFooters: [TestReusableView.self])
source = DiffableTableViewDataSource(config: config)
  • create a TableViewRow
let model = StringCellModel(uniqueID: ID, text: title)
let row = TableViewRow<StringCell>(model: model,
                                 onTap: { debugPrint("tapped with \($0)")})
// add rows if need
row.addRowLeadingActions(leadingActions)
row.addRowTrailingActions(trailingActions)
  • Add the row to the cell
let updates = TableViewDiffableSection(id: "SectionID here")
updates.addRows([row])
  • Create a ReusableView
final class TestReusableView: ReusableView, Configurable {
  
   let label: UILabel
   override init(reuseIdentifier: String?) {
       // Init and Configure UILabel

       super.init(reuseIdentifier: reuseIdentifier)
   }

   required init?(coder aDecoder: NSCoder) {
       super.init(coder: aDecoder)
   }
  
   func configure(with txt: String) {
       label.text = txt
   }
}
  • Create a header
let text = "My custom section"
let footer = ImmutableHeaderFooterProvider<TestReusableView>(model: text)
  • Attach the header to a TableViewDiffableSection
let updates = TableViewDiffableSection(id: "SectionID here")
updates.addHeader(header)
  • Call updates on the source
source.update(sections: [updates])

Documentaion

For more information check the documentaion page

Author

Swift Gurus., alexei.hmelevski@gmail.com

License

EZSource is available under the MIT license. See the LICENSE file for more info.