Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Experimental] Add SwiftUI.Binding support #256

Merged
merged 7 commits into from Apr 4, 2021

Conversation

k-kohey
Copy link
Contributor

@k-kohey k-kohey commented Apr 4, 2021

Motivation and Context

Most views that simply receive the state and reflect it in its appearance only need to get the state through a StateReader.
However, some views that update their state, such as a TextField, need to generate a SwiftUI.Binding.
I want to hide the generation process.

// current
TextField("hoge", text: .init(get: {
    return store.state.inputingText
}, set: { value in
    store.commit {
        $0.inputingText = value
    }
}))

// ideal
TextField("hoge", text: store.binding(\.inputingText))

Description

StoreType provides a function that returns the instance of SwiftUI.Binding.

Copy link
Collaborator

@muukii muukii left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your contibutions.
Since the current Verge's SwiftUI support is not enough, your contributions and help would be a big support.

/// - mutation: A closure to update the state.
/// If the closure is nil, state will be automatically updated.
/// - Returns: The result of binding
public func binding<T>(_ keypath: WritableKeyPath<State, T>, with mutation: ((T) -> Void)? = nil) -> Binding<T> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to add the same method in StoreComponentType which just calls directly StoreType's one.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or if you don't use StoreComponentType, we don't need to do this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

6a55ea3

@k-kohey
Copy link
Contributor Author

k-kohey commented Apr 4, 2021

Thank you for the review and the approval!

@muukii muukii changed the title Add SwiftUI.Binding support [Experimental] Add SwiftUI.Binding support Apr 4, 2021
@muukii muukii merged commit b9ca3d0 into VergeGroup:main Apr 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants