Hi! First of all, I'm sorry if it's a stupid question or if it has nothing to do with these package.
I'm trying to animate a List
where the order of elements depends on a variable in Defaults
.
If I'm using a @State
, it's working as expected.
If I'm using a @Default
, animation doesn't work.
These works:
enum OrderBy: String, Codable, Defaults.Serializable {
case index, name
}
struct Element: Identifiable {
let id: String
let index: Int
let name: String
}
let allElements: [Element] = [
.init(id: "1", index: 5, name: "Element 1"),
.init(id: "2", index: 4, name: "Element 2"),
.init(id: "3", index: 1, name: "Element 3"),
.init(id: "4", index: 3, name: "Element 4"),
.init(id: "5", index: 2, name: "Element 5"),
]
struct MyView: View {
@State var order: OrderBy = .index
func toggleOrder() {
withAnimation {
order = order == .index ? .name : .index
}
}
var elements: [Element] {
allElements.sorted(by: { order == .index ? $0.index < $1.index : $0.name < $1.name })
}
var body: some View {
Picker("Order by", selection: $order) {
Text("index").tag(OrderBy.index)
Text("name").tag(OrderBy.name)
}
List {
ForEach(elements, id: \.id) { element in
Text("Element \(element.name)")
}
}
}
}
These doesn't work:
extension Defaults.Keys {
static let order = Key<OrderBy>("orderBy", default: OrderBy.index)
}
struct MyView: View {
@Default(.order) var order
func toggleOrder() {
withAnimation {
listSortOption = listSortOption == .index ? .name : .index
}
}
...
}
Is these a limitation of the package or is it completely something else?
Thanks
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too