Connecting outlets is not interesting, let’s focus on ViewController.setupObserving. This method makes all UI controls “alive” by assigning properties / actions to them. See ViewController.swift.Let’s go for them one-by-one:
- We’re adding reactive extension weatherFeatures on WeatherView to bind an appropriate property from ViewModel. This gonna be implemented in ViewModel
- viewModel.isLoading should be implemented as SignalProducer<Bool, NoError>to bind to activity indicator
- isEnabled(for:) is a function returning SignalProducer. This is bound to isEnabled property of UISegmentedControl
- The same for title, but String, not Bool
- We’re creating a binding target for rightBarButtonItem and binding either the activity indicator or the refresh button, depending on the state
- We’re wrapping actions for left, right, reload, locate buttons into CocoaAction and assigning them to reactive.pressed property. This automatically handles click events and isEnabled state.
- We’re adding a custom handler for UISegmentedControl.controlEvents signal, because it has no built-in action expression
Implementations in ViewModel look like this.
Screen in IB