Git Product home page Git Product logo

continuum's Introduction

Continuum

CI Status Version Carthage compatible License Platform

NotificationCenter based Lightweight UI / AnyObject binder.

final class ViewController: UIViewController {

    @IBOutlet weak var label: UILabel!

    private let viewModel: ViewModel = ViewModel()
    private let center = NotificationCenter()
    private let bag = ContinuumBag()

    override func viewDidLoad() {
        super.viewDidLoad()

        center.continuum
            .observe(viewModel.text, on: .main, bindTo: label, \.text)
            .disposed(by: bag)

        viewModel.text.value = "Binding this text to label.text!"
    }
}

final class ViewModel {
    let text: Variable<String>

    init() {
        self.text = Variable(value: "")
    }
}

Usage

1. Observe object KeyPath and bind it to target KeyPath

NotificationCenter's instance has continuum property. You can access Continuum functions from it.

let center = NotificationCenter()
let observer = center.continuum.observe(viewModel, \.text, on: .main, bindTo: label, \.text)

Above source code means observe viewModel's text propety and bind that value to label's text property on main thread. If property is observed, current value comes immediately.

Notify changes with func post(keyPath:)

If value changed, notify changes like this.

viewModel.text = "Changed"
center.continuum.post(keyPath: \ViewModel.text)
print(label.text) // Changed

2. Observe Constant / Variable and bind it to target KeyPath

Constant / Variable are value wrapper. Variable has getter / setter. Constant has only getter.

let center = NotificationCenter()
let text = Variable<String>(value: "")
let observer = center.continuum.observe(text, on: .main, bindTo: label, \.text)

If property is observed, current value comes immediately.

3. Observe Constant / Variable and bind it to closure

Constant / Variable are value wrapper. Variable has getter / setter. Constant has only getter.

let center = NotificationCenter()
let text = Variable<String>(value: "")
let observer = center.continuum.observe(text, on: .main, onValueChange: { value in
    // something to do
})

If property is observed, current value comes immediately.

Notify changes with setter of value at Variable

If Variable's value is changed, func post(name:object:) is automatically executed.

text.value = "Changed"
print(label.text) // Changed

In addition, if Variable's value is changed, related Constant value is automatically changed.

let center = NotificationCenter()
let variable = Variable<String>(value: "")
let constant = Constant<String>(variable: variable)
let observer = center.continuum.observe(constant, on: .main, bindTo: label, \.text)
variable.value = "Changed"
print(label.text) // Changed

Lifecycle of ContinuumObserver

func observe(_:,_:,on:,bindTo:,_:) returns ContinuumObserver. If func cancel() of ContinuumObserver called, observation is cancelled.

let observer = center.continuum.observe(viewModel, \.text, on: .main, bindTo: label, \.text)
observer.cancel()

If adding observer to ContinumeBag, observation is cancelled by lifecycle of ContinumeBag.

var bag = ContinumeBag()
center.continuum
    .observe(viewModel, \.text, on: .main, bindTo: label, \.text)
    .disposed(by: bag)

bag = ContinumeBag() // previous instance of ContinumeBag is released and observation is cancelled.

Example

Playground

You can try Continuum with Playground. Open Continuum.xcworkspace and run build. You can try like this.

Example Project

To run the example project, clone the repo, and run pod install from the Example directory first. Open ContinuumSample.xcworkspace and run build. You can try a simple counter app like this.

Requirements

  • Xcode 9.2 or later
  • Swift 4.0.3 or later
  • iOS 10.0 or later

Installation

CocoaPods

Continuum is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'Continuum'

Carthage

If you’re using Carthage, simply add Continuum to your Cartfile:

github "marty-suzuki/Continuum"

Author

marty-suzuki, [email protected]

License

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

continuum's People

Contributors

marty-suzuki avatar toshi0383 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.