Giter VIP home page Giter VIP logo

diffabledatasources's Introduction

DiffableDataSources

๐Ÿ’พ A library for backporting UITableView/UICollectionViewDiffableDataSource
powered by DifferenceKit.

Swift5 Release CocoaPods Carthage Swift Package Manager
Build Status Platform Lincense

Made with โค๏ธ by Ryo Aoyama


Introduction

Apple has announced a diffable data source at WWDC 2019.
It's a great API that easily updating our table view and collection view items using automatic diffing.
However, it's a little while before we can use it in a production service.
That because it requires the latest OS to use.
DiffableDataSources make it possible to introduce almost the same functionality from now on.

Uses a sophisticated open source DifferenceKit for the algorithm engine.
It's extremely fast and completely avoids synchronization bugs, exceptions, and crashes.



Difference from the Official

Spec

  • Supports iOS 9.0+ / macOS 10.11+ / tvOS 9.0+
  • Open sourced algorithm.
  • Duplicate sections or items are allowed.
  • Using performBatchUpdates for diffing updates.

Namings

DiffableDataSources have different class names to avoid conflicts with the official API.
Correspondence table is below.

Official Backported
NSDiffableDataSourceSnapshot DiffableDataSourceSnapshot
UITableViewDiffableDataSource TableViewDiffableDataSource
UICollectionViewDiffableDataSource CollectionViewDiffableDataSource
NSCollectionViewDiffableDataSource CocoaCollectionViewDiffableDataSource

Getting Started

Build Project

$ git clone https://github.com/ra1028/DiffableDataSources.git
$ cd DiffableDataSources/
$ make setup
$ open DiffableDataSources.xcworkspace

Basic Usage

First, define the type representing section.
It should conforms to Hashable for identifies from the all sections.
Type of enum can used conveniently because it conforms Hashable by default.

enum Section {
    case main
}

Then, define the item type conforms to Hashable.

struct User: Hashable {
    var name: String
}

Create a data source object, it will be set to table view automatically.
You should dequeue the non nil cells via closure.

final class UsersViewController: UIViewController {
    let tableView: UITableView = ...

    lazy var dataSource = TableViewDiffableDataSource<Section, User>(tableView: tableView) { tableView, indexPath, user in
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
        cell.textLabel?.text = user.name
        return cell
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
    }
}

Manages and updates the data sources intuitively by intermediating DiffableDataSourceSnapshot.
The UI isn't updated until you apply the edited snapshot object.
Update the UI with diffing animation automatically calculated by applying an edited snapshot.

let users = [
    User(name: "Steve Jobs"),
    User(name: "Stephen Wozniak"),
    User(name: "Tim Cook"),
    User(name: "Jonathan Ive")
]

let snapshot = DiffableDataSourceSnapshot<Section, User>()
snapshot.appendSections([.main])
snapshot.appendItems(users)

dataSource.apply(snapshot) {
    // completion
}

Check the documentation for more detailed API.


Requirements

  • Swift 5.0+
  • iOS 9.0+
  • macOS 10.11+
  • tvOS 9.0+

Installation

Add the following to your Podfile:

pod 'DiffableDataSources'

Add the following to your Cartfile:

github "ra1028/DiffableDataSources"

Add the following to the dependencies of your Package.swift:

.package(url: "https://github.com/ra1028/DiffableDataSources.git", from: "x.x.x")

Contributing

Pull requests, bug reports and feature requests are welcome ๐Ÿš€
Please see the CONTRIBUTING file for learn how to contribute to DiffableDataSources.


Relations

A fast and flexible O(n) difference algorithm framework for Swift collection.

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


License

DiffableDataSources is released under the Apache 2.0 License.

diffabledatasources's People

Contributors

bobek-balinek avatar dependabot[bot] avatar fossil12 avatar jdanthinne avatar kishikawakatsumi avatar ra1028 avatar sidepelican avatar simba909 avatar vincentisambart avatar

Stargazers

 avatar  avatar

Watchers

 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.