skiptools / skip-ui

SwiftUI for Android
https://skip.tools
GNU Lesser General Public License v3.0
128 stars 14 forks source link

Observable with array value not updating views #22

Closed pnewell closed 10 months ago

pnewell commented 11 months ago

This example properly updates the view every time the Replace Item button is clicked in iOS but does not update the view in Android. If you swap the array for a struct var item: Item it works fine, so I believe this has to do with the var being an array.

Screenshot 2023-12-28 at 10 56 13 AM
struct Item: Identifiable {
    var name: String

    var id: String { name }
}

@Observable
class Repository {
    var items: [Item] = []

    func set() {
        self.items = [Item(name: Date().description)]
    }
}

public struct ContentView: View {
    @State var repo: Repository = Repository()

    public var body: some View {
        VStack {
            List {
                ForEach(repo.items) { item in
                    Text(item.name)
                }
            }
            Button(action: { repo.set() }) {
                Text("Replace Item")
            }
        }
    }
}
pnewell commented 11 months ago

In case it is helpful, I ran the same test using the old ObservableObject and it also works fine in iOS but fails in Android.

struct Item: Identifiable {
    var name: String

    var id: String { name }
}

class Repository: ObservableObject {
    @Published var items: [Item] = []

    func set() {
        self.items = [Item(name: Date().description)]
    }
}

public struct ContentView: View {
    @StateObject var repo: Repository = Repository()

    public var body: some View {
        VStack {
            List {
                ForEach(repo.items) { item in
                    Text(item.name)
                }
            }
            Button(action: { repo.set() }) {
                Text("Replace Item")
            }
        }
    }
}
aabewhite commented 10 months ago

Fixed in skip-ui 0.3.36. Thanks, and please let us know if you see any issues with the update.