EasyBinding 0.2.5

EasyBinding 0.2.5

Maintained by Carlos Mejía.



EasyBinding

CI Status Version License Platform

Simplest way to bind your ViewModels to your views :), for real, really simple.

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Installation

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

pod 'EasyBinding'

Usage

Var is the heart of the observable classes, you can use Var to listen the your variable changes, for example:

let isLoading: Var<Bool>
let title: Var<String>

Var receive a generic type to create the variable you need, you can find your variable in the value property.

let isLoading = Var<Bool>(false)
// Check your bool
if isLoading.value {
    ...
}

bindTo() is a method to bind your views to the variable changes.

// Example UI reference
@IBOutlet weak var activityIndicator: UIActivityIndicatorView!

// Example observable variable.
let isLoading = Var<Bool>(false)

// Example of binding.
isLoading.bindTo(activityIndicator, to: .state)

.state is a state designed to specify which attribute of the UI element should be changed.

public enum VisibilityAnimation {
    case fade(time: TimeInterval)
}

public enum BindedProperty {
    case visibility
    case visibilityAnimated(animation: VisibilityAnimation?)
    case text
    case state
    case title
    case image
    case value
    case progress
    case dataSource
}

listen is a method to listen the value changes of your Var.

func listen(triggerInitialValue: Bool = false, valueDidChange: @escaping (T) -> Void)

Usage

myVar.listen { newValue in
    print("The new value is: \(newValue)")
}

This method will trigger the call only if your variable change, if you want to catch the initial value, you can pass the parameter triggerInitialValue (default false) to true.

myVar.listen(triggerInitialValue: true) { newValue in
    print("The new value is: \(newValue)")
}

notify is a method to trigger manually all the variable listeners (listen method and UI binded objects).

Usage

myVar.notify()

Add supported views

If you want to support new UIViews or even your custom classes, you only have to conform the ObserverViewProtocol, this protocol contains a simple method named setValue that triggers when any observable variable binded changes, you can customize your behavior when this event happends.

public protocol ObserverViewProtocol {
    func setValue<T>(_ value: T, to property: BindedProperty)
}

Author

Carlos Mejía, [email protected]

https://twitter.com/carlosmejia083

License

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