Skip to content
This repository has been archived by the owner on Apr 3, 2022. It is now read-only.

mejiagarcia/EasyBinding

Repository files navigation

So it was fun, but is time to switch to Combine. I created this library because I didn't like SwiftRX. This library saw production a cuple times and I'm proud of it, but I but I'm not going to reinvent the wheel.

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

Cocoapods

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

pod 'EasyBinding'

Swift Package Manager

EasyBinding is available through Swift Package Manager. To install it, simply add the following line to your Package.swift or your xcode project configuration.

dependencies: [
    .package(url: "https://github.com/mejiagarcia/EasyBinding.git", .upToNextMajor(from: "0.2.6"))
]

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)
}

License

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

About

We know that you only need to bind your ViewModels to your views with the simplest way possible, here is how.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published