CollectionModelAnimation 0.0.2

CollectionModelAnimation 0.0.2

Maintained by Denis Poifol.



 
Depends on:
CollectionModelCore~> 0
DifferenceKit= 1.1.5
 

CollectionModel

CI Version License Platform

The goal of this pod is to provide a data structure to represent what a collectionView/tableView is displaying. It enables to extract any business logic from the dataSource of your view to the part of your code that is responsible for generating its viewModel. Which help to enforce a better separation of conrcerns within your application, and keep your dataSources easily maintanable.

Example Application

To run the example project, clone the repo, and run bundle install & bundle exec pod install from the Example directory first.

Usage

Everything is documented within Xcode.

TableViewModel

TableViewModel is a struct representing as closely as possible the structure of a UITableView. It is generic over two parameters HeaderFooterViewModel and CellViewModel both parameters are self explanatory, they should represent the model used to configure the cells of the tableView or the headers and footers

This struct is mainly a wrapper for an array of TableViewSectionViewModel which is a struct representing a section of UITableView

TableViewSectionViewModel relies on an array of cellViewModels and two nullable header and footer properties.

Implementing a tableViewDataSource relying on tableViewModel is pretty straight forward if you only use one type of cell :

class SimpleTableViewDataSource: NSObject,
    UITableViewDataSource {

    typealias ViewModel = TableViewModel<Never, ATableViewCellModel>

    var viewModel = ViewModel()

    func numberOfSections(in tableView: UITableView) -> Int {
        viewModel.sections.count
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        viewModel[section].cells.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell: ATableViewCell = tableView.dequeueCell(at: indexPath)
        cell.configure(with: viewModel[indexPath])
        return cell
    }
}

If you have to use multiple types of cells you should create an enum for your cells type :

enum MultipleCellTypesTableViewCellModel {
    case a(ATableViewCellModel)
    case b(BTableViewCellModel)
}

This will require only to change your implementation of tableView(_:cellForRowAt:) in order to manage both cell type cases.

For more detail you can look at the example application.

CollectionViewModel

CollectionViewModel is a struct representing as closely as possible the structure of a UICollectionView. It is generic over two parameters SupplementaryViewModel and CellViewModel both parameters are self explanatory they should represent the models used to configure the cells of the collectionView or the supplementary views which could be a header a footer or a custom suplementary view

This struct is mainly a wrapper for an array of CollectionSectionViewModel which is a struct representing a section of UICollectionView.

CollectionSectionViewModel relies on an array of cellViewModels and a dictionary storing supplementaryViewModels where a key is SupplementaryViewSectionKey a couple of an index and kind for the suplementary view. Header and footer for the section are stored in their own variable appart from the dictionnary.

Implementing a collectionViewDataSource relying on CollectionViewModel is pretty straight forward if you only use one type of cell :

class SimpleCollectionViewDataSource: NSObject,
    UICollectionViewDataSource {
	
	typealias ViewModel = CollectionViewModel<Never, CollectionViewCellModel>

    var viewModel = ViewModel()

    func numberOfSections(in collectionView: UICollectionView) -> Int {
        viewModel.sections.count
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        viewModel[section].cells.count
    }

    func collectionView(_ collectionView: UICollectionView,
                        cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell: ACollectionViewCell = collectionView.dequeueCell(at: indexPath)
        cell.configure(with: viewModel.cellViewModel(at: indexPath))
        return cell
    }
}

If you have to use multiple types of cells you should create an enum for your cells type :

enum MultipleCellTypesCollectionViewCellModel {
    case a(ACollectionViewCellModel)
    case b(BCollectionViewCellModel)
}

This will require only to change your implementation of collectionView(_:cellForItemAt:) in order to manage both cell type cases.

Requirements

There is no technical requirement to use this pod other than using swift.

Installation

CollectionModel is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'CollectionModel'

Author

Denis Poifol, [email protected]

License

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