Code Monkey home page Code Monkey logo

conv's Introduction

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
    .create(for: sectionTypes) { (sectionType, section) in // #2
        section.create(.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.create(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. Create sections that number of sectionTypes. And start define about section.
  3. Create section header for each section. And start define about section header.
  4. Create 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.update()

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

collectionView.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.create(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 create 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.

conv's People

Contributors

bannzai avatar yutailang0119 avatar

Watchers

Carabineiro avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.