Last active
June 11, 2019 02:42
-
-
Save pteasima/5cf17a18bbd0800875c96b778d17200e to your computer and use it in GitHub Desktop.
Experiments with current state of SwiftUI beta. Second state property broke it so Ill probably end here.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
extension Binding { | |
init(global keyPath: KeyPath<Bindings, Binding>) { | |
self = bindings[keyPath: keyPath] | |
} | |
} | |
struct Bindings { | |
@Binding var anotherOne: Bool | |
@Binding var isOn: Bool | |
} | |
var bindings: Bindings! | |
var firstRun = true | |
struct Store { | |
var isOn: Bool = true | |
var anotherOne: Bool = false | |
} | |
struct ContentView : View { | |
@State var store: Store = Store() | |
var body: some View { | |
print("content") | |
if firstRun { | |
firstRun = false | |
bindings = Bindings( | |
anotherOne: $store.anotherOne, | |
isOn: $store.isOn | |
) | |
} | |
return Child() | |
} | |
} | |
struct Child: View { | |
var body: some View { | |
print("child") | |
return VStack { | |
// Child4() //uncomment to make 4 work, 2 break | |
Child2() | |
} | |
} | |
} | |
struct Child2: View { | |
//we still need this as a property, using global bindings.$isOn directly in body doesnt work | |
@Binding(global: \.$isOn) var isOn: Bool | |
var body: some View { | |
print("child2") | |
return VStack { | |
Toggle(isOn: $isOn) { | |
Text(String(describing: isOn)) | |
} | |
Child3() | |
} | |
} | |
} | |
struct Child3: View { | |
var body: some View { | |
//when child2 changes, it also recomputes child3, except first time. Layout may be the reason, but its still strange this isnt called on the first change | |
print("child3") | |
return Child4() | |
} | |
} | |
struct Child4: View { | |
@Binding(global: \.$anotherOne) var anotherOne: Bool | |
var body: some View { | |
print("child4") | |
//this binding can trigger statechange but this view never gets recomputed. Instead, child2 gets recomputed | |
return Toggle(isOn: $anotherOne) { | |
Text(String(describing: anotherOne)) | |
} | |
} | |
} | |
#if DEBUG | |
struct ContentView_Previews : PreviewProvider { | |
static var previews: some View { | |
ContentView() | |
} | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment