ViewController

open class ViewController<V: ViewControllerModellableView & UIView>: UIViewController, AnyViewController

Manages a screen of your app, it keeps the UI updated and listens for user interactions.

Overview

In Tempura, a Screen is composed by three different elements that interoperate in order to get the actual pixels on the screen and to keep them updated when the state changes. These are ViewController, ViewModelWithState and ViewControllerModellableView. The ViewController is a subclass of UIViewController that is responsible to manage the set of views that are shown in each screen of your UI.

   struct CounterState: State {
     var counter: Int = 0
   }
   struct IncrementCounter: Action {
     func updatedState(inout currentState: CounterState) {
       currentState.counter += 1
     }
   }
   struct DecrementCounter: Action {
     func updatedState(inout currentState: CounterState) {
       currentState.counter -= 1
     }
   }
   struct CounterViewModel: ViewModelWithState {
     var countDescription: String

     init(state: CounterState) {
       self.countDescription = "the counter is at \(state.counter)"
     }
   }
   class CounterView: UIView, ViewControllerModellableView {

     // subviews
     var counterLabel = UILabel()
     var addButton = UIButton(type: .custom)
     var subButton = UIButton(type: .custom)

     // interactions
     var didTapAdd: Interaction?
     var didTapSub: Interaction?

     // setup
     func setup() {
       self.addButton.on(.touchUpInside) { [unowned self] button in
         self.didTapAdd?()
       }
       self.subButton.on(.touchUpInside) { [unowned self] button in
         self.didTapSub?()
       }
       self.addSubview(self.counterLabel)
       self.addSubview(self.subButton)
       self.addSubview(self.addButton)
     }

     // style
     func style() {
       self.backgroundColor = .white
       self.addButton.setTitle("Add", for: .normal)
       self.subButton.setTitle("Sub", for: .normal)
     }

     // update
     func update(oldModel: CounterViewModel?) {
       self.counterLabel.text = self.model?.countDescription
       self.setNeedsLayout()
     }

     // layout
     override func layoutSubviews() {
       self.counterLabel.sizeToFit()
       self.addButton.frame = CGRect(x: 0, y: 100, width: 100, height: 44)
       self.subButton.frame = CGRect(x: 100, y: 100, width: 100, height: 44)
     }
   }
   class CounterViewController: ViewController<CounterView> {

   override func setupInteraction() {
     self.rootView.didTapAdd = { [unowned self] in
       self.dispatch(IncrementCounter())
     }
     self.rootView.didTapSub = { [unowned self] in
       self.dispatch(DecrementCounter())
     }
   }

Lifecycle of a ViewController

In order to instantiate a ViewController’s subclass you need to provide a Katana Store instance. This instance will be used by the ViewController to listen for state updates.

   let vc = CounterViewController(store: appStore)

When a ViewController is created it will start receiving state updates as soon as the connected property will become true.

When the ViewController becomes visible, the UIKit UIViewController.viewWillAppear() will be called and Tempura will set connected to true and the ViewController will start receiving the updates from the state. If you don’t want this to happen automatically every time the ViewController will become visible, set shouldConnectWhenVisible to false.

As soon a new state is available from the Katana store, the ViewController will instantiate a new ViewModel out of that state and feed the rootView with that, calling ModellableView.update(oldModel:)

When something happens inside the ViewControllerModellableView (or its subviews) the ViewController is responsible to listen for these Interaction callbacks and react accordingly dispatching actions in order to change the state.

When a ViewController is removed from the hierarchy or hidden by some other ViewController, UIKit will call UIViewController.viewWillDisappear() and Tempura will set connected to false, detaching the ViewController from the state updates. If you don’t want this to happen automatically every time the ViewControllet will become invisible, set sholdDisconnectWhenInvisible to false

  • true if the ViewController is connected to the store, false otherwise. A connected ViewController will receive all the updates from the store. Tempura will set this property to true when the ViewController is about to be displayed on screen, if you want to change this behaviour look at the shouldConnectWhenVisible property. Tempura will set this property to false when the ViewController is about to be hidden, if you want to change this behaviour look at the shouldDisconnectWhenVisible property.

    Declaration

    Swift

    open var connected: Bool
  • The store the ViewController will use to receive state updates.

    Declaration

    Swift

    public var store: PartialStore<V.VM.S>
  • The state of this ViewController

    Declaration

    Swift

    public var state: V.VM.S
  • When true, the ViewController will be set to connected = true as soon as it becomes visible.

    Declaration

    Swift

    public var shouldConnectWhenVisible = true
  • When true the ViewController will be set to connected = false as soon as it becomes invisible.

    Declaration

    Swift

    public var shouldDisconnectWhenInvisible = true
  • The latest ViewModel received by this ViewController from the state.

    Declaration

    Swift

    public var viewModel: V.VM?
  • Use the rootView to access the main view managed by this viewController.

    Declaration

    Swift

    open var rootView: V
  • Used internally to load the specific main view managed by this view controller.

    Declaration

    Swift

    open override func loadView()
  • Returns a newly initialized ViewController object.

    Declaration

    Swift

    public init(store: PartialStore<V.VM.S>, connected: Bool = false)
  • Override to setup something after init.

    Declaration

    Swift

    open func setup()
  • Shortcut to the dispatch function.

    Declaration

    Swift

    open func dispatch(action: Action)
  • Shortcut to the dispatch function.

    Declaration

    Swift

    open func dispatch(_ dispatchable: Dispatchable) -> Promise<Void>
  • Required init.

    Declaration

    Swift

    public required init?(coder aDecoder: NSCoder)
  • The ViewController is about to be displayed.

    Declaration

    Swift

    open override func viewWillAppear(_ animated: Bool)
  • The ViewController is about to be removed from the view hierarchy.

    Declaration

    Swift

    open override func viewWillDisappear(_ animated: Bool)
  • Called after the controller'€™s view is loaded into memory.

    Declaration

    Swift

    open override func viewDidLoad()
  • Called just before the update, override point for subclasses.

    Declaration

    Swift

    open func willUpdate(new: V.VM?)
  • Called right after the update, override point for subclasses.

    Declaration

    Swift

    open func didUpdate(old: V.VM?)
  • Asks to setup the interaction with the managed view, override point for subclasses.

    Declaration

    Swift

    open func setupInteraction()
  • Called just before the unsubscribe, override point for subclasses.

    Declaration

    Swift

    open func willUnsubscribe()
  • Undocumented

    Declaration

    Swift

    deinit