Skip to content

Instantly share code, notes, and snippets.

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 {
class MainColorSectionDescriptor: CollectionSectionDescribable {
let backgroundInset = UIEdgeInsetsMake(10, 10, 10, 10)
}
class MyLayout: UICollectionViewFlowLayout {
//...
var backgroundAttributes = [IndexPath:UICollectionViewLayoutAttributes]()
override func prepare() {
@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
}
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)
struct TweetAdapter: CollectionAdapter, Diffable {
let label:NSAttributedString
let imageURL:URL
let backgroundColor:UIColor
init(tweet:Tweet) {
label = ...
imageURL = ...
backgroundColor = ...
final class RealTimeCollectionData : CollectionData {
var model:RealTimeModel?
func update(model:RealTimeModel) {
self.model = model
}
override func reloadData() {
super.reloadData()
fileprivate lazy var decorationViewHandler:DecorationViewsHandler = DecorationViewsHandler(collectionViewLayout: self)
// ...
decorationViewHandler.register(viewClass: SimpleDecorationView.self, for: sectionBackgroundKind)
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)
let decorationAttributes = decorationViewHandler.attributes(in:rect)
override func prepare(forCollectionViewUpdates updateItems: [UICollectionViewUpdateItem]) {
super.prepare(forCollectionViewUpdates: updateItems)
decorationViewHandler.prepare(forCollectionViewUpdates: updateItems)
}
override func indexPathsToInsertForDecorationView(ofKind elementKind: String) -> [IndexPath] {
return decorationViewHandler.inserted(for: elementKind)
}
override func indexPathsToDeleteForDecorationView(ofKind elementKind: String) -> [IndexPath] {