Skip to content

Instantly share code, notes, and snippets.

override func prepare() {
super.prepare()
decorationViewHandler.prepare()
//...
let backgroundAttributes = SimpleDecorationViewLayoutAttributes(forDecorationViewOfKind: sectionBackgroundKind, with: firstCellIndexPath)
decorationViewHandler.add(attributes: backgroundAttributes)
}
override func layoutAttributesForDecorationView(ofKind elementKind: String, at atIndexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
return decorationViewHandler.attributes(for: elementKind, at: atIndexPath)
fileprivate lazy var decorationViewHandler:DecorationViewsHandler = DecorationViewsHandler(collectionViewLayout: self)
// ...
decorationViewHandler.register(viewClass: SimpleDecorationView.self, for: sectionBackgroundKind)
final class RealTimeCollectionData : CollectionData {
var model:RealTimeModel?
func update(model:RealTimeModel) {
self.model = model
}
override func reloadData() {
super.reloadData()
struct TweetAdapter: CollectionAdapter, Diffable {
let label:NSAttributedString
let imageURL:URL
let backgroundColor:UIColor
init(tweet:Tweet) {
label = ...
imageURL = ...
backgroundColor = ...
func fetch() {
realTimeService.getRecentTweets { [weak self] response in
switch response {
case .success(let data):
self?.collectionData.update(model: data)
let result = self?.collectionData.update { updater in
updater.diff()
}
if let result = result {
self?.collectionView.performUpdates(with: result)
@gwennguihal
gwennguihal / Style.swift
Last active September 8, 2017 08:58
Elegant method to transform String into NSAttributedString
import Foundation
struct Style {
let styling : (String) -> NSMutableAttributedString
}
extension Style {
static let myStyle = Style { (string) -> NSMutableAttributedString in
return NSMutableAttributedString(string: string) // add your attributes here
}
class MainColorSectionDescriptor: CollectionSectionDescribable {
let backgroundInset = UIEdgeInsetsMake(10, 10, 10, 10)
}
class MyLayout: UICollectionViewFlowLayout {
//...
var backgroundAttributes = [IndexPath:UICollectionViewLayoutAttributes]()
override func prepare() {
protocol BulletAdapter: CollectionAdapter {
var identifier: String
var className: String
var height: Float
var bullet: UIImage { get }
var label: NSAttributedString { get }
}
// Descriptor
final class BulletDescriptor: CollectionCellDescribable {
// 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")
extension TitleCollectionViewCell : CollectionCellAdaptable {
func update(with adapter: CollectionAdapter) {
guard let adapter = adapter as? TitleAdapter else {
fatalError("TitleAdapter required")
}
titleLabel.attributedText = adapter.title
}
}