I'm trying out Swift Charts and I'm pretty messed up
After I deleted all of my items, I couldn't add items anymore
import SwiftUI
extension Array where Element == ChartsData {
mutating func delete(_ item: ChartsData) {
self.removeAll {
$0 == item
}
}
}
struct ContentView: View {
var ofType: ChartsType
@State var data: [ChartsData] = []
var body: some View {
List {
VStack {
HStack {
Spacer()
// MARK - add button
Button {
data.append(ChartsData(item: "item"))
} label: {
Image(systemName: "plus").imageScale(.large)
}.buttonStyle(.plain).padding()
}
List {
ForEach(data) { datum in
TableRow(data: datum).swipeActions {
Button(role: .destructive) {
data.delete(datum)
} label: {
Label("Delete", systemImage: "trash")
}
}
}
}
NavigationLink{
ChartsView(ofType: .BarChart, data: data)
} label: {
Text("Generate").font(.largeTitle).foregroundStyle(.linearGradient(colors: [.orange, .yellow, .blue, .purple], startPoint: .topTrailing, endPoint: .bottomLeading))
}.padding()
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView(ofType: .BarChart, data: [
ChartsData(item: "testItem1", value: 10),
ChartsData(item: "testItem2", value: 20)
])
}
}