Replies: 3 comments 9 replies
-
You can use If you want to create a model grouping multiple properties into one property, you can use a value instead of a reference type, and updates will work fine. import Adwaita
struct CounterModel: Codable {
var count = 0 {
didSet {
print(count)
}
}
}
struct CounterDemo: View {
@State("count", folder: "io.github.AparokshaUI.Demo/count")
private var count = CounterModel()
var view: Body {
VStack {
HStack {
CountButton(count: $count.count, icon: .goPrevious) { $0 -= 1 }
Text("\(count.count)")
.style("title-1")
.frame(minWidth: 100)
CountButton(count: $count.count, icon: .goNext) { $0 += 1 }
}
.halign(.center)
}
.valign(.center)
.padding()
}
private struct CountButton: View {
@Binding var count: Int
var icon: Icon.DefaultIcon
var action: (inout Int) -> Void
var view: Body {
Button(icon: .default(icon: icon)) {
action(&count)
}
.style("circular")
}
}
} |
Beta Was this translation helpful? Give feedback.
-
I added support for the observation framework on the 1.0.0 branch for the next major version as the implementation required some breaking changes. I decided to make import Adwaita
import Observation
@Observable
class CounterModel {
var count = 0 {
didSet {
print("Hi")
}
}
}
struct CounterDemo: View {
@State private var count = CounterModel()
var view: Body {
VStack {
HStack {
CountButton(icon: .goPrevious) { count.count -= 1 }
Text("\(count.count)")
.style("title-1")
.frame(minWidth: 100)
CountButton(icon: .goNext) { count.count += 1 }
}
.halign(.center)
}
.valign(.center)
.padding()
}
private struct CountButton: SimpleView {
var icon: Icon.DefaultIcon
var action: () -> Void
var view: Body {
Button(icon: .default(icon: icon)) {
action()
}
.style("circular")
}
}
} |
Beta Was this translation helpful? Give feedback.
-
Hello @s-k, I changed the plan a bit after my last answer. Instead of adding support for the observation framework (which makes tracking changes way more difficult than with value types), I'll use an own value type approach which integrates better with the state system. The only time when a reference type is sensible is when having to update a property from inside the reference type itself in a closure, right? Something like this: @Observable
class SomeModel {
var property: String
func updateProperty {
Task {
// Do something
Idle {
property = "Hello"
}
}
}
} I'll make adwaita-swift a backend based on Meta in the 1.0.0 release, where I solved this using the struct SomeModel: Model {
var property: String
var model: ModelData?
func updateProperty {
Task {
// Do something
Idle {
setModel { $0.property = "Hello" }
}
}
}
} The advantage is that changes to the model type from outside the model can be observed in the same way as any changes, and the changes inside of the model (such as in the example) are also simpler to track. If you see a problem with this approach, I'd be glad if you could share it! |
Beta Was this translation helpful? Give feedback.
-
Is there an equivalent to
@ObservedObject
from SwiftUI? If not, how would I update the UI when a property in the model changes?Beta Was this translation helpful? Give feedback.
All reactions