Skip to content

Update view model properties #55

@PaMicht

Description

@PaMicht

I was wondering if there is no cleaner way of updating immutable ViewModel properties than doing that inside a StatefulViewController:

var actualModel = self.model
actualModel.isLoading = true
self.model = actualModel

Solution 1

A suggestion would be to have an extension for StatefulViewController containing this:

import JamitFoundation

extension StatefulViewController {
    func updateModel(modifier: (inout Model) -> Void) {
        var actualModel = model
        modifier(&actualModel)
        self.model = actualModel
    }
}

Extension in action:

viewController.updateModel { $0.isLoading = true }

Solution 2

Another approach could be to allow var properties in the ViewModel. Setting a var property would automatically trigger the update mechanism of the StatefulViewController .
Code example:

self.model.isLoading = true // isLoading is a var

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions