Conv smart represent UICollectionView data structure more than UIKit.

Overview

Swift4 CocoaPods Carthage
Platform Lincense

Conv

Conv smart represent UICollectionView data structure more than UIKit.
Easy definition for UICollectionView DataSource and Delegate methods.

And Conv reload fast to use diffing algorithm based on the Paul Heckel's algorithm.

Insert and Delete Move item and section

Conv(called KONBU) means Seaweed in Japan.
This library is inspired by Shoyu. Thanks @yukiasai.

Usage

First, Conv need to prepare array of definition datastructure for section and item. And then it should conform Differenciable protocol for difference algorithm.

Section

enum SectionType: Int {
  case one
  case two
  case three

  static var allCases: [SectionType] {
    return [.one, .two, .three]
  }
}

extension SectionType: Differenciable {
  var differenceIdentifier: DifferenceIdentifier {
    return "\(self)"
  }
}
let sectionTypes = SectionType.allCases

Item

struct ItemModel {
    let index: Int
    let imageName: String
    var image: UIImage {
        return UIImage(named: imageName)!
    }
}

extension ItemModel: Differenciable {
    var differenceIdentifier: DifferenceIdentifier {
        return "\(index)" + imageName
    }
}
let itemModels = [
    ItemModel(index: 1, imageName: "forest"),
    ItemModel(index: 2, imageName: "moon"),
    ItemModel(index: 3, imageName: "pond"),
    ItemModel(index: 4, imageName: "river"),
]

Second, start to define data structure for section and item.
It use prepared Differenciable array.

collectionView
    .conv // #1
    .diffing() 
    .start() 
    .append(for: sectionTypes) { (sectionType, section) in // #2
        section.append(.header, headerOrFooter: { (header: SectionHeaderFooter<ListCollectionReusableView>) in // #3
            header.reusableIdentifier = "ListCollectionReusableView"
            header.size = CGSize(width: UIScreen.main.bounds.width, height: 50)
            header.configureView { view, _ in
                view.nameLabel.text = "\(sectionType)".uppercased()
                view.nameLabel.textColor = .white
                view.backgroundColor = sectionType.backgroundColor
            }
        })
        section.append(for: itemModels, items: { (itemModel, item: Item<ListCollectionViewCell>) in // #4
            item.reusableIdentifier = "ListCollectionViewCell"
            item.sizeFor({ _ -> CGSize in
                let gridCount: CGFloat = 3
                let edge = floor((UIScreen.main.bounds.width - (gridCount - 1)) / gridCount)
                let size = CGSize(width: edge, height: edge)
                return size
            })
            
            item.configureCell { (cell, info) in
                cell.setup(with: itemModel)
            }
            
            item.didSelect { [weak self] (item) in
                let viewController = DetailViewController(imageName: itemModel.imageName)
                self?.navigationController?.pushViewController(viewController, animated: true)
            }
        })
}

This swift code has the following meaning. It explain for # mark in code.

  1. Start to define UICollectionView data structure it using Conv.
  2. Append sections that number of sectionTypes. And start define about section.
  3. Append section header for each section. And start define about section header.
  4. Append items that number of itemModels for each section. And start define about item.

Last, If you want to render of collectionView, you call collectionView.update() your best timing.
update() calculate diff for minimum reloading data between before section and between before items.

collectionView.conv.update()

Or if you want to all realod cells, you can call reload(). It will be same behavior of collectionView.reloadData().

collectionView.conv.reload()

You can see more example to ConvExmaple

Algorithm

Conv to use diffing algorithm based on the Paul Heckel's algorithm.
And I also referred to other libraries below.

Install

CocoaPods

Conv is available through Cocoapods.
You can write it into target and exec pod install.

pod 'Conv'

Carthage

Conv is available through Carhtage.
You can write it into target and exec carthage update --platform iOS. And find conv framework and embed your project.

github 'bannzai/Conv'

Why Conv?

UIKit.UICollectionView has some problems.

  1. UICollectionView.dequeueXXX method not type safe. So, should convert to want class each cells.
  2. UICollectionViewDataSource and UICollectionViewDelegate(or DelegateFlowLayout) far away each configured functions. So, reading configuration flow for each indexPath very difficalt.
  3. Many case to use UICollectionView with Array. But extract element from array using indexPath many time.

Conv resolve these problem.

  1. Conv does not need to call UICollectionView.dequeueXXX. Because you can define configureCell method and get converted custom class cell.
section.append(for: itemModels, items: { (itemModel, item: Item<ListCollectionViewCell>) in // #4
    ...
    item.configureCell { (cell, info) in
        // cell is converted ListCollectionViewCell
        cell.setup(with: itemModel)
    }
})
  1. You can write to neary for each UICollectionView component. section,item,header and footer. So, this definition to be natural expression for UICollectionView data strcture, hierarchy, releation.

  2. When append section or item, you can passed allCases for configure UICollectionView. Next each element pass closure argument that define Conv.Section or Conv.Item. So, You can represent CollectionView data structure with extracted each element.

LICENSE

Conv is released under the MIT license. See LICENSE for details.

Header logo is released CC BY-NC 4.0 license. Original design by noainoue.

You might also like...
Collapse and expand UICollectionView sections with one method call.

This library provides a custom UICollectionView that allows to expand and collapse sections. Provides a simple API to manage collection view appearanc

🚴 A declarative library for building component-based user interfaces in UITableView and UICollectionView.
🚴 A declarative library for building component-based user interfaces in UITableView and UICollectionView.

A declarative library for building component-based user interfaces in UITableView and UICollectionView. Declarative Component-Based Non-Destructive Pr

ZHTCView - UITableview & UICollectionView

ZHTCView 这是一个使用Block替换代理的UITableview & UICollectionView。 使用方法如下: - (DSTableView *)tableView { if (!_tableView) { _tableView = DSTableView.

CollectionView - UICollectionView using UICollectionViewCompositionalLayout

CollectionView UICollectionView using UICollectionViewCompositionalLayout create

CollectionViewSegmentedControl - Scrollable UISegmentedControl built using a UICollectionView
CollectionViewSegmentedControl - Scrollable UISegmentedControl built using a UICollectionView

CollectionViewSegmentedControl Installation CocoaPods Download CocoaPods Run 'Po

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

DTCollectionViewManager Features Powerful mapping system between data models and cells, headers and footers Automatic datasource and interface synchro

A modest attempt to port UICollectionView to SwiftUI.
A modest attempt to port UICollectionView to SwiftUI.

LazyCollectionView A modest attempt to port UICollectionView to SwiftUI. Table of Contents Description Requirements Installation Usage Components Impr

A SwiftUI collection view with support for custom layouts, preloading, and more.
A SwiftUI collection view with support for custom layouts, preloading, and more.

ASCollectionView A SwiftUI implementation of UICollectionView & UITableView. Here's some of its useful features: supports preloading and onAppear/onDi

Generic collection view controller with external data processing

FlexibleCollectionViewController Swift library of generic collection view controller with external data processing of functionality, like determine ce

Comments
  • Fix/syntax/tagged

    Fix/syntax/tagged

    What

    • Change style of start to define UICollectionView Data Structures.
    • Swiching definition conv way for diffing reload and simple reload.
    • Add method for start to define conv to use other conv.
    • Add insert and delete methos for Conv.sections and Sections.items.

    Why

    I want to pass UICollectionView Data Structures between difference class(or structure).

    opened by bannzai 0
  • Fix/function/configure cell

    Fix/function/configure cell

    What

    • Add arguments of reuse identifier for configure cell

    Why

    d_date 's idea. It is required parameter for cellForItem at indexPath. So, it should setting forced and more safety.

    Other

    • Refactor property name from reusableIdentifier -> reuseIdentifier.

    Reason for missing property name.

    opened by bannzai 0
  • Fix/algorithm/item

    Fix/algorithm/item

    What

    • [x] Change algorithm from :https://github.com/bannzai/Conv/pull/27
      • reference altorithm:
        • https://github.com/mcudich/HeckelDiff
        • https://github.com/ra1028/DifferenceKit
        • https://github.com/Instagram/IGListKit/
    • [x] Add Algorithm test
    opened by bannzai 0
  • Add/test for item

    Add/test for item

    What

    • Add Test
      • Item
      • ~Section~
      • ~Conv~
      • ~SectionHeaderFooter~
      • Fix argument type for canFocusItem.

    TODO

    • Add Test
      • Section
      • Conv
      • SectionHeaderFooter
    opened by bannzai 0
Owner
bannzai
bannzai
Easier way to represent the structure of UITableView.

Shoyu Shoyu is a library written in Swift to represent UITableView data structures. Shoyu means Soy Sauce in Japanese. Usage Create single section and

yukiasai 278 Apr 14, 2022
Infinite paging, Smart auto layout, Interface of similar to UIKit.

Infinite paging, Smart auto layout, Interface of similar to UIKit. Appetize's Demo Requirements Swift 4.2 iOS 8.0 or later How to Install PagingView C

Kyohei Ito 314 Dec 6, 2022
A data-driven UICollectionView framework for building fast and flexible lists.

A data-driven UICollectionView framework for building fast and flexible lists. Main Features ?? Never call performBatchUpdates(_:, completion:) or rel

Instagram 12.5k Jan 1, 2023
A generic small reusable components for data source implementation for UITableView/UICollectionView in Swift.

GenericDataSource A generic small reusable components for data source implementation for UITableView/UICollectionView written in Swift. Features Basic

null 132 Sep 8, 2021
An iOS drop-in UITableView, UICollectionView and UIScrollView superclass category for showing a customizable floating button on top of it.

MEVFloatingButton An iOS drop-in UITableView, UICollectionView, UIScrollView superclass category for showing a customizable floating button on top of

Manuel Escrig 298 Jul 17, 2022
Automates prefetching of content in UITableView and UICollectionView

Automates preheating (prefetching) of content in UITableView and UICollectionView. Deprecated on iOS 10. This library is similar to UITableViewDataSou

Alexander Grebenyuk 633 Sep 16, 2022
UICollectionView layout for presenting of the overlapping cells.

StickyCollectionView UICollectionView layout for presenting of the overlapping cells. Objective-C version here Checkout demo Overview Installation Man

Bogdan Matveev 325 Oct 11, 2022
Netflix and App Store like UITableView with UICollectionView, written in pure Swift 4.2

GLTableCollectionView Branch Status master develop What it is GLTableCollectionView is a ready to use UITableViewController with a UICollectionView fo

Giulio 708 Nov 17, 2022
Incremental update tool to UITableView and UICollectionView

EditDistance is one of the incremental update tool for UITableView and UICollectionView. The followings show how this library update UI. They generate

Kazuhiro Hayashi 90 Jun 9, 2022
Reimagining UICollectionView

CollectionKit Reimagining UICollectionView A modern Swift framework for building composable data-driven collection view. Migration Guide v2.0 Features

SoySauceLab 4.3k Dec 27, 2022