Comments (4)
Hey @Devepre !
I was also wondering about this problem. Since in ViewState MVVM there should only be a read-only state available to the view and all state changes should be initiated by an input being triggered. Therefore, I would suggest the following extension to create bindings. To make this API even easier to use, you can create extensions using keypaths as well, just as an example.
extension AnyViewModel {
func bind<Value>(_ value: @escaping (State) -> Value,
to input: @escaping (Value) -> Input?) -> Binding<Value> {
Binding(get: { value(self.state) },
set: { input($0).map { self.trigger($0) } })
}
}
This would allow you to create bindings like the following for a text property of your state and an input for that property:
viewModel.bind({ $0.text }, to: { .text($0) })
In your viewModel, you can then change the state depending on that input - that may be a bit of an overhead, but it would ensure that the viewModel state is read-only. Another option would be to make the state read-write and then ensuring that you are only writing to the state, whenever it would seriously impact code overhead.
from swiftui-architectures.
Hi @pauljohanneskraft ,
Sounds reasonable, thanks a lot!
I've tried both approaches a little bit in practice. For now, second approach with read-write state and KeyPaths "everywhere" is more enjoyable to me, because of cleaner API and less boilerplate code. It will look like this code snippet:
viewModel.bind(on: \.text)
Also it's critical to have read-only state, that's why I've used private(set) var state
but func forceUpdate(_ newState: State)
as ViewModel
protocol requirement while AnyViewModel's
implementation will assert failure.
from swiftui-architectures.
@Devepre
Could you be so kind to share how you realized this?
viewModel.bind(on: \.text)
from swiftui-architectures.
@savage7 it was tricky task, since I've moved to another approach a long time ago and didn't have commits pushed to the server. Anyway I've managed to find my old implementation. If it makes sense, I can make pull request to the original repo, anyway you can take a look implementation in forked version here https://github.com/Devepre/SwiftUI-Architectures/tree/develop commit c69e644e6dfac461e172adb1804723b50c4eba01
from swiftui-architectures.
Related Issues (5)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from swiftui-architectures.