• Stars
    star
    349
  • Rank 121,528 (Top 3 %)
  • Language
    Swift
  • License
    Other
  • Created over 8 years ago
  • Updated about 5 years ago

Reviews

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

Repository Details

Swift μ-framework for efficient array diffs and datasource adapters.

Buffer Swift Platform License

Buffer

Swift μ-framework for efficient array diffs, collection observation and data source implementation.

C++11 port here

Installation

cd {PROJECT_ROOT_DIRECTORY}
curl "https://raw.githubusercontent.com/alexdrone/Buffer/master/bin/dist.zip" > dist.zip && unzip dist.zip && rm dist.zip;

Drag Buffer.framework in your project and add it as an embedded binary.

If you use xcodegen add the framework to your project.yml like so:

targets:
  YOUR_APP_TARGET:
    ...
    dependencies:
      - framework: PATH/TO/YOUR/DEPS/Buffer.framework

Installation with CocoaPods/Carthage (deprecated)

If you are using CocoaPods:

Add the following to your Podfile:

pod 'Buffer'

If you are using Carthage:

To install Carthage, run (using Homebrew):

$ brew update
$ brew install carthage

Then add the following line to your Cartfile:

github "alexdrone/Buffer" "master"    

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(didMoveElement buffer: BufferType, from: UInt, to: UInt) {
  }

  public func buffer(didChangeAllContent buffer: BufferType) {
  }
}

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: [ListItem<FooModel>] = {
    var elements = [ListItem<FooModel>]()
    for _ in 0...100 {
      // AnyListItem wraps the data and the configuration for every row in the tableview.
      let item = ListItem(type: UITableViewCell.self,
                          container: self.tableView,
                          model: FooModel(text: "Foo"))) {
        cell, model in
        cell.textLabel?.text = model.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

More Repositories

1

Render

UIKit a-là SwiftUI.framework [min deployment target iOS10]
Swift
2,157
star
2

ios-fontawesome

NSString+FontAwesome
Objective-C
1,751
star
3

ios-realtimeblur

Multipurpose real-time blur view for iOS6+
Objective-C
956
star
4

Store

Unidirectional, transactional, operation-based Store implementation.
Swift
502
star
5

flexboxobjc_deprecated

A simple UIKit extension to wrap Flexbox layouts.
Objective-C
398
star
6

flexboxswfit_deprecated

(Deprecated) Port of Facebook's css-layout to Swift
Swift
307
star
7

DataStructures

A collection of Data Structures implemented in Swift.
Swift
48
star
8

Primer

Assign/Partial/ReadOnly/Proxy/Locks in Swift
Swift
32
star
9

CoreRender

Moved to https://github.com/alexdrone/Render
Objective-C++
26
star
10

S.swift

Get strong typed, autocompleted resources, color swatches and font styles in Swift projects from a simple YAML stylesheet.
Swift
25
star
11

YAS

Yet Another Stylesheet (YAML-based Stylesheet Engine)
Swift
23
star
12

Surface

A set of utilities and extensions to create depth in your views by using complex shadows.
Swift
18
star
13

PushID

Robust and thread-safe generator for uuids with guaranteed chronological ordering in Swift.
Swift
16
star
14

Emit

Event propagation and object observation library (Pre-Combine.framework).
Swift
10
star
15

ObjCTrait

Trait support for Objective-C
Objective-C
9
star
16

PatchComposer

A Quartz-composer like UI for patch editing in SwiftUI
Swift
8
star
17

SwiftUI_UIKit_Bridge

Share custom typography and color swatches between UIKit and SwiftUI.
Swift
6
star
18

Pbtxt

Schemaless protobuf text-format parser (with Codable support).
Swift
5
star
19

itu_pku_eventregexp

Java
5
star
20

GuruMeditation

Nostalgic error screen for iOS.
Swift
4
star
21

DesignSystemTemplate

A customizable design system in Swift.
Swift
4
star
22

Multipeer

Forked from: https://github.com/insidegui/MultipeerKit
Swift
3
star
23

ui_dsl_deprecated

(Deprecated) A lightweight native stylesheet engine for iOS
Objective-C
3
star
24

cxx_tutorials

A bunch of tutorials used to teach modern C++ and OpenGL to iOS devs
C++
3
star
25

Multi.kt

Kotlin Multiplatform Lib Template
Kotlin
3
star
26

swift1_rune_game

An iOS game inspired by 'Dots' based on the nordic runes
Objective-C
2
star
27

firebase_utils_deprecated

Firebase-backed store for Dispatch
Swift
2
star
28

trn_modelmapper

A ORM inspired by Ruby's DataMapper and ActiveRecord
Java
2
star
29

yaHN-android

Yet another HackerNews Android client
Java
2
star
30

FontCustom

fontcustom template for iOS
Objective-C
2
star
31

itu_algorithms

Some algorithms back from Uni time
Ruby
2
star
32

objc_latte_uikit_deprecated

(deprecated) An iOS markup language, framework and tool to help the design and the implementation of the presentation layer.
1
star