DTCollectionViewManager

11.0.0

Protocol-oriented UICollectionView management, powered by generics and associated types.
DenTelezhkin/DTCollectionViewManager

What's New

11.0.0

2022-10-06T12:46:17Z

Added

  • Support for UICollectionViewDelegate.collectionView(_:canPerformPrimaryActionForItemAt:) and UICollectionViewDelegate.collectionView(_:performPrimaryActionForItemAt:) delegate methods on iOS 16 and tvOS 16.
  • Support for UICollectionViewDelegate.collectionView(_:contextMenuConfigurationForItemsAt:point:), UICollectionViewDelegate.collectionView(_:contextMenuConfiguration:highlightPreviewForItemAt:) and UICollectionViewDelegate.collectionView(_:contextMenuConfiguration:dismissalPreviewForItemAt: methods on iOS 16.
  • Support for UIHostingConfiguration on iOS 16 / tvOS 16 / macCatalyst 16:
manager.registerHostingConfiguration(for: Post.self) { _, post, _ in
    UIHostingConfiguration {
        PostView(post: post)
    }
}

It's also possible to incorporate UIKit cell states by simply adding additional parameter to registration:

manager.registerHostingConfiguration(for: Post.self) { state, _, post, _ in
    UIHostingConfiguration {
        PostView(post: post, isSelected: state.isSelected)
    }
}

Additionally, it's possible to customize UICollectionViewCell being used to host SwiftUI view, for example for list cells:

manager.registerHostingConfiguration(for: Post.self, cell: UICollectionViewListCell.self) { _, post, _ in
    UIHostingConfiguration {
        PostView(post: post)
    }
}
  • Support for events, wrapping UICollectionViewDataSourcePrefetching protocol.
manager.register(PostCell.self) { mapping in
    mapping.prefetch { model, indexPath in }
    mapping.cancelPrefetch { model, indexPath in }
}

Please note, that while datasource methods are called once per array of indexPaths, events for models will be called individually, so single model (and indexPath) is passed to each event. Theoretically, this should make prefetching and cancellation easier, since you no longer need to walk through array and find all data models, you can operate on a single data model at a time.

Deprecated

  • Cell / View events, registered with DTCollectionViewManager are soft-deprecated. Please use events in mapping instead:

Deprecated:

    manager.register(PostCell.self)
    manager.didSelect(PostCell.self) { postCell, post, indexPath in }

Recommended:

    manager.register(PostCell.self) { mapping in
        mapping.didSelect { postCell, post, indexPath in }
    }

While previously main benefits for second syntax were mostly syntactic, now with support for SwiftUI it will be hard to actually specialize hosting cells (and might be impossible when iOS 16 hosting configuration is supported), so only second syntax will work for all kinds of cells, and first syntax can only work for non-SwiftUI cells.
New delegate methods for UICollectionView (starting with iOS 16 / tvO 16 SDK) will be added only as extension to mapping protocols, not DTCollectionViewManager itself.

CI codecov.io CocoaPod platform CocoaPod version Swift Package Manager compatible Packagist

DTCollectionViewManager

Features

  • Powerful mapping system between data models and cells, headers and footers
  • Automatic datasource and interface synchronization.
  • Flexible Memory/CoreData/Realm/diffable datasource storage options
  • Powerful compile-time safe events system, that covers all of UICollectionView delegate methods
  • Views created from code, XIB, or storyboard, automatic registration and dequeue
  • Can be used with UICollectionViewController, or UIViewController with UICollectionView
  • Built-in support for iOS 14 UICollectionView.CellRegistration and content configuration
  • Unified syntax with DTTableViewManager
  • Complete documentation
  • API Reference

Requirements

  • Xcode 12+
  • iOS 11.0+ / tvOS 11.0+ / macCatalyst 13.0+
  • Swift 5.3+

If you need Xcode 11 support or Swift 4...Swift 5.2, or iOS 8...iOS 10 support, you can use 7.x releases.

Installation

Swift Package Manager

Add package into Xcode Project settings -> Swift Packages

pod 'DTCollectionViewManager', '~> 11.0.0-beta.1'

Quick start

Let's say you have an array of Posts you want to display in UICollectionView. To quickly show them using DTCollectionViewManager, here's what you need to do:

  1. Create UICollectionViewCell subclass, let's say PostCell and adopt ModelTransfer protocol:
class PostCell : UICollectionViewCell, ModelTransfer {
    func update(with model: Post) {
        // Fill your cell with actual data
    }
}
  1. In your view controller:
class PostsViewController: UICollectionViewController, DTCollectionViewManageable {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Register PostCell to be used with this controller's collection view
        manager.register(PostCell.self)

        // Populate datasource
        manager.memoryStorage.setItems(posts)
    }
}    

Make sure your UICollectionView outlet is wired to your class (or use UICollectionViewController subclass). If you have a PostCell.xib file, it will be automatically used for dequeueing PostCell.

  1. That's it! It's that easy!

Of course, cool stuff does not stop there, framework supports all datasource and delegate methods as closures, conditional mappings and much much more! Choose what interests you in the next section of readme.

Burning questions

Starter pack
Advanced

Sample code and documentation

Thanks

  • Alexey Belkevich for providing initial implementation of CellFactory.
  • Michael Fey for providing insight into NSFetchedResultsController updates done right.
  • Nickolay Sheika for great feedback, that helped shaping 3.0 release and future direction of the library.
  • Artem Antihevich for great discussions about Swift generics and type capturing.

Description

  • Swift Tools 5.5.0
View More Packages from this Author

Dependencies

Last updated: Tue Mar 12 2024 01:56:47 GMT-0900 (Hawaii-Aleutian Daylight Time)