@ObservedObject
Equivalent
#22
-
Is there an equivalent to |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 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.
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
State
handle both value types and observable reference types: