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: "")
}
}
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.
If value changed, notify changes like this.
viewModel.text = "Changed"
center.continuum.post(keyPath: \ViewModel.text)
print(label.text) // Changed
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.
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.
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
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.
You can try Continuum with Playground. Open Continuum.xcworkspace and run build. You can try like this.
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.
- Xcode 9.2 or later
- Swift 4.0.3 or later
- iOS 10.0 or later
Continuum is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'Continuum'
If you’re using Carthage, simply add Continuum to your Cartfile
:
github "marty-suzuki/Continuum"
marty-suzuki, [email protected]
Continuum is available under the MIT license. See the LICENSE file for more info.