GithubHelp home page GithubHelp logo

tchigher / combinedatasources Goto Github PK

View Code? Open in Web Editor NEW

This project forked from combinecommunity/combinedatasources

0.0 0.0 0.0 812 KB

Table and collection view data sources for Combine

Home Page: https://github.com/combineopensource/CombineDataSources

License: MIT License

Swift 100.00%

combinedatasources's Introduction

Combine Data Sources

CombineDataSources provides custom Combine subscribers that act as table and collection view controllers and bind a stream of element collections to table or collection sections with cells.

⚠️⚠️⚠️ Note: The package is currently work in progress.

Table of Contents

  1. Usage

1.1 Bind a plain list of elements

1.2 Bind a list of Section models

1.2 Customize the table controller

1.3 Subscribing a completing publisher

  1. Installation

2.1 Swift Package Manager

  1. License

  2. Credits


Usage

The repo contains a demo app in the Example sub-folder that demonstrates visually different ways to use CombineDataSources.

Bind a plain list of elements

var data = PassthroughSubject<[Person], Never>()

data
  .receive(subscriber: tableView.rowsSubscriber(cellIdentifier: "Cell", cellType: PersonCell.self, cellConfig: { cell, indexPath, model in
    cell.nameLabel.text = model.name
  }))

Plain list updates with CombineDataSources

Bind a list of Section models

var data = PassthroughSubject<[Section<Person>], Never>()

data
  .receive(subscriber: tableView.sectionsSubscriber(cellIdentifier: "Cell", cellType: PersonCell.self, cellConfig: { cell, indexPath, model in
    cell.nameLabel.text = model.name
  }))

Sectioned list updates with CombineDataSources

Customize the table controller

var data = PassthroughSubject<[[Person]], Never>()

let controller = TableViewItemsController<[[Person]]>(cellIdentifier: "Cell", cellType: PersonCell.self) { cell, indexPath, person in
  cell.nameLabel.text = person.name
}
controller.animated = false

// More custom controller configuration ...

data
  .subscribe(subscriber: tableView.sectionsSubscriber(controller))

Subscribing a completing publisher

Sometimes you'll bind a publisher to your table or collection view and it will complete at a point. When you use subscribe(_) the completion event will release the CombineDataSource subscriber as well and that will likely render the table/collection empty.

In such case you can use the custom operator included in CombineDataSources subscribe(retaining:) that will give you an AnyCancellable to retain the subscriber, like so:

var subscriptions = [AnyCancellable]()
...
Just([Person(name: "test"])
  .subscribe(retaining: tableView.rowsSubscriber(cellIdentifier: "Cell", cellType: UITableViewCell.self, cellConfig: { (cell, ip, person) in
    cell.textLabel!.text = person.name
  }))
  .store(in: &subscriptions)

This will keep the subscriber and the data source alive until you cancel the subscription manually or it is released from memory.

Installation

Swift Package Manager

Add the following dependency to your Package.swift file:

.package(url: "https://github.com/combineopensource/CombineDataSources, from: "0.2")

License

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

Credits

Created by Marin Todorov for CombineOpenSource.

Inspired by RxDataSources and RxRealmDataSources.

combinedatasources's People

Contributors

icanzilb 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.