Skip to content

Instantly share code, notes, and snippets.

@gwennguihal
Last active August 7, 2017 14:54
Show Gist options
  • Save gwennguihal/8584e2e4d3c3777e9a4f4f95ceaaa941 to your computer and use it in GitHub Desktop.
Save gwennguihal/8584e2e4d3c3777e9a4f4f95ceaaa941 to your computer and use it in GitHub Desktop.
// Adapter Protocol
protocol VSCollectionLabelAdapter: CollectionAdapter {
var label: NSAttributedString { get } // required property
}
// VSLabelCollectionViewCell implements CollectionCellAdaptable
extension VSLabelCollectionViewCell: CollectionCellAdaptable {
func update(with adapter: CollectionAdapter) {
guard let adapter = adapter as? VSCollectionLabelAdapter else {
fatalError("VSCollectionLabelAdapter required")
}
label.attributedText = adapter.label
}
}
// Descriptor
final class VSLabelDescriptor: CollectionCellDescribable {
let identifier: String = "VSLabelCollectionViewCell"
let className: String = "VSLabelCollectionViewCell"
//...
let adapter: VSCollectionLabelAdapter
init(adapter:VSCollectionLabelAdapter) {
self.adapter = adapter
}
//...
}
// An Example of an adapter
struct VSBasketHeaderAdapter: VSCollectionLabelAdapter {
var label: NSAttributedString
init() {
let paragrapheStyle = NSMutableParagraphStyle()
paragrapheStyle.alignment = .left
label = NSAttributedString(string: "Basket Title".uppercased(), attributes: [
NSParagraphStyleAttributeName : paragrapheStyle,
NSForegroundColorAttributeName: UIColor.black(),
NSFontAttributeName: UIFont.systemSemiBoldSmall()
])
}
}
// Implementation in the data
//...
let headerSection = VSHeaderSectionDescriptor().reloadSection { cells in
let basketHeaderAdapter = VSBasketHeaderAdapter()
let basketHeaderDescriptor = VSLabelDescriptor(basketHeaderAdapter)
cells.append(basketHeaderDescriptor)
}
sections.append(headerSection)
//...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment