Skip to content

Instantly share code, notes, and snippets.

@0xLeif
Created September 9, 2020 21:15
Show Gist options
  • Save 0xLeif/655d5d538617f114cd6e23ac79c35dbf to your computer and use it in GitHub Desktop.
Save 0xLeif/655d5d538617f114cd6e23ac79c35dbf to your computer and use it in GitHub Desktop.
SUIObject Example
import SwiftUI
import SUIObject
extension View {
var object: SUIObject {
SUIObject(self)
}
func navigate(to: Self) -> SUIObject {
to.object.configure {
$0.add(variable: "push", value: self)
}
}
}
struct IntView: View {
var value: Int?
var body: some View {
value.map { Text("Int: (\($0))") } ?? Text("No Value Yet!")
}
}
struct ContentView: View {
@ObservedObject var object = SUIObject("init") { obj in
obj.add(variable: "someView", value: Button("Change") {
obj.run(function: "change")
})
}
var body: some View {
VStack {
Text(object.stringValue() ?? "-1")
Text(object.otherText.stringValue() ?? "-2")
IntView(value: object.someRandomIntValueThatDoesNotExistYet.value())
object.someView.value(as: Button<Text>.self)
}
.onAppear {
self.object.configure { obj in
obj.add(value: "Hello World")
obj.add(variable: "otherText", value: "Another one")
obj.add(function: "change", value: { _ in
obj.variables["otherText"] = "\(obj.otherText.stringValue() ?? "")+"
obj.add(variable: "someRandomIntValueThatDoesNotExistYet", value: Int.random(in: 1 ... 10))
return nil
})
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment