Git Product home page Git Product logo

buffer's Introduction

Buffer

Swift Carthage compatible ![Platform](https://img.shields.io/badge/platform-ios | macos | tvos | watchos-lightgrey.svg?style=flat) License

Swift μ-framework for efficient array diffs, collection observation and data source implementation. (Swift 2.3 branch here)

Installation

Carthage

To install Carthage, run (using Homebrew):

$ brew update
$ brew install carthage	

Then add the following line to your Cartfile:

github "alexdrone/Buffer" "master"

CocoaPods

TODO

Manually

Download and drop /Buffer folder in your project.

import Buffer

#Getting started

Buffer is designed to be very granular and has APIs with very different degrees of abstraction.

###Managing a collection with Buffer

You can initialize and use Buffer in the following way.

import Buffer

class MyClass: BufferDelegate {

	lazy var buffer: Buffer<Foo> = {
		// The `sort` and the `filter` closure are optional - they are a convenient way to map the src array.
		let buffer = Buffer(initialArray: self.elements, sort: { $0.bar > $1.bar }, filter: { $0.isBaz })
		buffer.delegate = self
	}()	
	
	var elements: [Foo] = [Foo]() {
		didSet {
			// When the elements are changed the buffer object will compute the difference and trigger
			// the invocation of the delegate methods.
			// The `synchronous` and `completion` arguments are optional.
			self.buffer.update(with: newValues, synchronous: false, completion: nil)
		}
	}
	
	
	//These methods will be called when the buffer has changedd.

	public func buffer(willChangeContent buffer: BufferType) {
		//e.g. self.tableView?.beginUpdates()

	}
	
	public func buffer(didDeleteElementAtIndices buffer: BufferType, indices: [UInt]) {
		//e.g. Remove rows from a tableview
	}
	
	public func buffer(didInsertElementsAtIndices buffer: BufferType, indices: [UInt]) {
	}
	
	public func buffer(didChangeContent buffer: BufferType) {
	}
	
	public func buffer(didChangeElementAtIndex buffer: BufferType, index: UInt) {
	}
	
	public func buffer(didChangeAllContent buffer: BufferType) {
	}
}

###Tracking Keypaths

If your model is KVO-compliant, you can pass an array of keypaths to your buffer object. When one of the observed keypath changes for one of the items managed by your buffer object, the sort and the filter closures are re-applied (on a background thread), the diff is computed and the delegate methods called.

buffer.trackKeyPaths(["foo", "bar.baz"])

###Built-in UITableView and UICollectionView adapter

One of the main use cases for Buffer is probably to apply changes to a TableView or a CollectionView. Buffer provides 2 adapter classes that implement the BufferDelegate protocol and automatically perform the required changes on the target tableview/collectionview when required.

import Buffer

class MyClass: UITableViewController {

	lazy var buffer: Buffer<Foo> = {
		// The `sort` and the `filter` closure are optional - they are convenient way to map the src array.
		let buffer = Buffer(initialArray: self.elements, sort: { $0.bar > $1.bar }, filter: { $0.isBaz })
		buffer.delegate = self
}()	

	var elements: [Foo] = [Foo]() {
	didSet {
		// When the elements are changed the buffer object will compute the difference and trigger
		// the invocation of the delegate methods.
		// The `synchronous` and `completion` arguments are optional.
		self.buffer.update(with: newValues, synchronous: false, completion: nil)
	}
}

let adapter: TableViewDiffAdapter<Foo>!
  		
	init() {
		super.init()
		self.adapter = TableViewDiffAdapter(buffer: self.buffer, view: self.tableView)
		
		// Additionaly you can let the adapter be the datasource for your table view by passing a cell
		// configuration closure to the adpater.
		adapter.useAsDataSource { (tableView, object, indexPath) -> UITableViewCell in
	 			let cell = tableView.dequeueReusableCellWithIdentifier("MyCell")
	  			cell?.textLabel?.text = object.foo
	  			return cell
		}
	}

}

###Component-Oriented TableView

Another convenient way to use Buffer is through the Buffer.TableView class. This abstraction allows for the tableView to reconfigure itself when its state (the elements) change.

import Buffer

class ViewController: UIViewController {

    lazy var tableView: TableView<FooModel> = {
        let tableView = TableView<FooModel>()
        return tableView
    }()

    lazy var elements: [AnyListItem<FooModel>] = {
        var elements = [AnyListItem<FooModel>]()
        for _ in 0...100 {
			// AnyListItem wraps the data and the configuration for every row in the tableview.
			let item = AnyListItem(type: UITableViewCell.self,
			                       referenceView: self.tableView,
			                       state: FooModel(text: "Foo"))) {
			    cell, state in
			    guard let cell = cell as? UITableViewCell else { return }
			    cell.textLabel?.text = state.text
			}
			elements.append(item)
        }
        return elements
    }()

    override func viewDidLayoutSubviews() {
        self.tableView.frame = self.view.bounds
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.addSubview(self.tableView)
        self.tableView.elements = self.elements
    }
}

Check the demo out to learn more about Buffer.

###Credits

  • The LCS algorithm implementation is forked from Dwifft by Jack Flintermann.

buffer's People

Contributors

alexdrone avatar t-pham avatar

Watchers

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