-
Notifications
You must be signed in to change notification settings - Fork 1
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
Two way data binding #10
Comments
Hey @gsipic that depends. A binding to the underlying import Processed
import SwiftUI
final class ViewModel: ObservableObject, LoadableSupport {
@Published var loadable: LoadableState<Int> = .absent
}
struct OuterView: View {
@StateObject private var viewModel = ViewModel()
var body: some View {
InnerView(loadable: $viewModel.loadable)
}
}
struct InnerView: View {
@Binding var loadable: LoadableState<Int>
var body: some View {
Text("")
}
} However, if you need/want to pass down the When used in classes, these loadables and processes can't be complex objects themselves because value observation in SwiftUI would not work anymore (among other problems, like storing and handling the internal With iOS 17 and the new In retrospect, I should have probably been more explicit about this current limitation in the README. Sorry about that! |
Ok, thank you for response. Can you make data property in loadableState struct to have setter and getter. Because in current situation you need to pass all Loadable state in InnerView but a want only pass data part of loadableState. Current data don't have setter
Don't want
a want this
This is how a make workaround a have added setter and getter on data. And with that inner view can only get part of wanted data. I don't need to pass all Loadable to InnerView.
|
The problem is that there is no way to do this correctly, because there is not a possible, single definition for correct in such a case. @Binding var loadable: Int Using a non-optional type here makes the assumption that the loadable state can only ever be Secondly, what would it mean to set the That said, you might be facing a specific use case where your This is a use case that would have to rely on proper documentation (because its behavior is not exactly intuitive). There's still no single, correct way of doing this. Would the following work for you? extension Loadable.Binding {
var loadedBinding: Binding<Value>? {
guard let data = wrappedValue.data else {
return nil
}
return Binding<Value> {
data
} set: { newValue in
guard case .loaded = wrappedValue else {
return // no-op? Not clear what should happen in this case.
}
wrappedValue = .loaded(newValue)
}
}
}
struct OuterView: View {
@Loadable<Int> private var loadable
var body: some View {
if let binding = $loadable.loadedBinding {
InnerView(loadable: binding)
}
}
}
struct InnerView: View {
@Binding var loadable: Int
var body: some View {
Text(loadable.formatted())
}
} To be honest, I'm not entirely sure this would be a good addition to the package. I think it's just not clear enough in its behavior for people to easily understand and use this. But I need to think about this a bit more, or maybe you have some ideas or arguments on how to make it clearer 🙂 In any case, for now you can just use your existing workaround or copy my extension from above into your project and you will have support for this. |
Ok, thank you for support about this issue. This is edge case in my opinion. I need also to do some thinking 🙂. P.S The library is very good thank you for open sourcing it. |
Thank you 🙂 What might be doable is adding a second Would you be fine with that? Like, I could add a |
Hey, That is a good approach 🙂. I'm fine with that design. Making clean separation between core and extensions. Thank you! |
Awesome! |
I might need a few more days, sorry 😅 Got lost in some other projects over the weekend |
@SwiftedMind Yes that better naming convention. |
Hello,
How can you use Binding(two way data communication) with this library from view model.
The text was updated successfully, but these errors were encountered: