I have a model:
class Glyph {
...
@Published var manager: CoordinatesManager
var currentState: Whatever {
return whateverDepends(on: manager.currentCoordinates)
}
}
class CoordinatesManager {
...
@Published var currentCoordinates: [Coordinate<Axis>]
...
}
And now I try to build SwiftUI view hierarchy. In one view I need to show Glyph
object, in another as many sliders as CoordinatesManager.currentCoordinates.count
to change manager.currentCoordinates
. Of course any change should be reflected in first view because it changes glyph.manager.currentCoordinates
.
View for Coordinate:
public struct CoordinateView : View {
@Binding var coordinate: Coordinate<Axis>
public var body : some View {
VStack {
HStack {
Text(coordinate.axis.name).font(.system(size: 8))
Text("\(coordinate.at)").font(.system(size: 8))
}
Slider(value: $coordinate.at, in: coordinate.axis.bounds).controlSize(.mini)
}.lineSpacing(0.1)
}
}
View for Coordinates:
public struct CoordinatesView : View
{
@Binding var coordinates: [Coordinate<Axis>]
public var body: some View {
VStack {
List ((0..<coordinates.count), id:\.self) { coordinateIndex in
CoordinateView(coordinate: self.$coordinates[coordinateIndex])
}
Text("Test \(coordinates.description)")
}
}
}
View for Glyph. Should react for changes in glyph.manager.currentCoordinates
but it doesn't and I don't know how to do it.
public struct NavigatorView: View
{
@ObservedObject var glyph: Glyph
public var body: some View {
ZStack {
// This is called only when i change View size
// GlyphShape generates some Shape based on Glyph.currentState
GlyphShape(glyph: glyph).stroke()
Text("Overlay for test")
}
}
}
Every time when I move cursor above Navigator view func path
in GlyphShape is fired and prints actual manager.currentCoordinates
. Path is redefined, but view isn't refreshed.
struct GlyphShape: Shape
{
@ObservedObject var glyph: Glyph
func path(in rect: CGRect) -> Path {
....
print ("MAKING SHAPE ,glyph.manager.currentCoordinates)
return resultPath
}
}
And on the bottom:
struct ContentView: View
{
@ObservedObject var glyph: Glyph
@ObservedObject var manager: CoordinatesManager
var body: some View {
NavigationView {
NavigatorView(glyph: glyph)
VStack {
CoordinatesView(coordinates: $manager.currentCoordinates)
Text ("Manager Coordinates\(manager.currentCoordinates.description)")
}.frame(minWidth: 100, idealWidth: .none, maxWidth: 200, minHeight: 300, idealHeight: .none, maxHeight: .infinity, alignment: .top)
}
}
}
How I should tell Glyph that manager.coordinates are changed and reflect it in NavigatorView?