Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle animations in SwiftUI with @Default #193

Open
Vincz opened this issue Nov 29, 2024 · 2 comments
Open

Handle animations in SwiftUI with @Default #193

Vincz opened this issue Nov 29, 2024 · 2 comments

Comments

@Vincz
Copy link

Vincz commented Nov 29, 2024

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

@sindresorhus
Copy link
Owner

Most likely a SwiftUI bug. The issue was introduced by 2dad0e4, but that was done to work around another SwiftUI bug, so we cannot just revert that.

@Vincz
Copy link
Author

Vincz commented Dec 11, 2024

Is there any way we can bypass the issue without an intermediate variables somewhere?
Will it be fixed in the package or when the SwiftUI bug will be fixed?
Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants