Skip to content

ViewModels

Marek Fořt edited this page Aug 30, 2018 · 4 revisions

BaseViewModel

Same as our BaseViewController - we always use this base class. It's not used so often as BaseViewController because viewModels does not share so much logic but at least we have init/deinit logging for viewModels as well.

ExampleViewModel

Structure of the file is pretty much the same as service file structure (see section Models). Actions protocol for namespacing, public interface definition and implementation.

We are trying to move logic from VC to VM as much as possible, so there should be only simple binding in the VC without any data transformations or combining. In fact the main goal is to use UIViewController as a simple view. This is the only code you want in setupBindings()

imageView.reactive.image <~ viewModel.photo

and this is what you should avoid and move it to VM.

imageView.reactive.image <~ viewModel.photoURLString
            .filterMap { URL(string: $0) }
            .filterMap { try? Data(contentsOf: $0) }
            .map { UIImage(data: $0) }

Continue to Theme ➡️