• Stars
    star
    836
  • Rank 52,304 (Top 2 %)
  • Language
    Swift
  • License
    Apache License 2.0
  • Created almost 5 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

πŸ’Ύ A library for backporting UITableView/UICollectionViewDiffableDataSource.

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.

More Repositories

1

DifferenceKit

πŸ’» A fast and flexible O(n) difference algorithm framework for Swift collection.
Swift
3,421
star
2

RACollectionViewReorderableTripletLayout

The custom collectionView layout that can perform reordering of cells by dragging it.
Objective-C
1,481
star
3

Former

Former is a fully customizable Swift library for easy creating UITableView based form.
Swift
1,307
star
4

Carbon

🚴 A declarative library for building component-based user interfaces in UITableView and UICollectionView.
Swift
1,291
star
5

RAReorderableLayout

A UICollectionView layout whitch can move item with drag and drop.
Swift
867
star
6

swiftui-hooks

πŸͺ A SwiftUI implementation of React Hooks. Enhances reusability of stateful logic and gives state and lifecycle to function view.
Swift
479
star
7

SwiftUI-Combine

This is an example project of SwiftUI and Combine using GitHub API.
Swift
452
star
8

VueFlux

♻️ Unidirectional State Management Architecture for Swift - Inspired by Vuex and Flux
Swift
331
star
9

PathDynamicModal

A modal view using UIDynamicAnimator, like the Path for iOS.
Swift
286
star
10

swiftui-atom-properties

βš›οΈ Atomic approach state management and dependency injection for SwiftUI
Swift
232
star
11

FloatingActionSheetController

FloatingActionSheetController is a cool design ActionSheetController library written in Swift2.
Swift
140
star
12

DelegateProxy

Proxy for receive delegate events more practically
Swift
129
star
13

RASlideInViewController

RASlideInViewController has an transition effect expressing the depth, and you can dismiss it by draging
Objective-C
127
star
14

Alembic

βš—οΈ Functional JSON Parser - Linux Ready 🐧
Swift
116
star
15

swift-mod

A tool for Swift code modification intermediating between code generation and formatting.
Swift
100
star
16

SwiftUI-Flux

πŸš€ This is a tiny experimental application using SwiftUI with Flux architecture.
Swift
54
star
17

monkey-lang-swift

The Monkey Programming Language written in Swift -- Writing An Interpreter In Go
Swift
17
star
18

LiveStreamingApp

A sample app repository that broadcasting and player of HLS for iOS.
Swift
16
star
19

KenBurnsSlideshowView

Slideshow with Ken Burns effect for iOS.
Swift
11
star
20

VueFluxExample-GitHub

VueFlux VueFluxReactive example project
Swift
8
star
21

OwnKit

My own utility toolkit for ios
Swift
7
star
22

ra1028

2
star
23

Dribbble_client_sample

Swift
1
star