I'm new to SwiftUI and coding in general, so sorry if this has being covered before. I did search about it but I couldn't find it clear enough.
I'm trying to make an app for storing notes and tasks. (I'm not planning to put it on the store, I'm too newbie for that, but I do want to learn Swift and working on an actual app is more useful to me than reading about it.) I have an entity called "Base" with 8 attributes and automatic Codegen Class Definition selected. I prefer to keep it that way, no manual please.
I have three fetch requests. One gets me all data from Core Data. The other two filter one attribute called campoEstado. For now in campoEstado I only store one of two possible values, as strings: "Activas", and "Pospuestas". In the future I may add more so I can't use a boolean for this.
I get a List working with one fetch request. But I can't change that source when the app is running. I made a Picker with .pickerStyle(SegmentedPickerStyle()) that shows me: Todo, Activas, Pospuestas. When the user selects one of this tabs in the picker the list should change to:
- Tab 1: All tasks
- Tab 2: A filtered list containing only tasks with campoEstado = "Activas" (calls the fetch request filtroActivas)
- Tab 3: A filtered list containing only tasks with campoEstado = "Pospuestas" (calls the fetch request filtroPospuestas)
How it should look:
My code in ContentView:
struct ContentView: View {
@Environment(\.managedObjectContext) var moc
@FetchRequest(entity: Base.entity(), sortDescriptors: [
NSSortDescriptor(keyPath: \Base.campoNota, ascending: true),
NSSortDescriptor(keyPath: \Base.campoFechaCreacion, ascending: true)
], predicate: NSPredicate(format: "campoEstado == %@", "Activas")
) var filtroActivas: FetchedResults<Base>
@FetchRequest(entity: Base.entity(), sortDescriptors: [
NSSortDescriptor(keyPath: \Base.campoNota, ascending: true),
NSSortDescriptor(keyPath: \Base.campoFechaCreacion, ascending: true)
], predicate: NSPredicate(format: "campoEstado == %@", "Pospuestas")
) var filtroPospuestas: FetchedResults<Base>
@FetchRequest(entity: Base.entity(), sortDescriptors: [
NSSortDescriptor(keyPath: \Base.campoNota, ascending: true),
NSSortDescriptor(keyPath: \Base.campoFechaCreacion, ascending: true)
]
) var bases: FetchedResults<Base>
var body: some View {
NavigationView {
List {
Picker("Solapas", selection: $selectorIndex) {
//This should connect to ForEach source, currently "bases"
}
.pickerStyle(SegmentedPickerStyle())
ForEach(bases, id: \.self) { lista in
NavigationLink(destination: VistaEditar(base: lista)) {
Rectangle()
.foregroundColor(.gray)
.frame(width: 7, height: 50)
VStack(alignment: .leading) {
Text(lista.campoNota ?? "Unknown title")
.font(.headline)
Text(lista.campoEstado ?? "Activas")
}
}
}
I have two problems:
I don't know how to connect the selected tab in the picker with the source of ForEach inside List
I made the list work for one fetch request, the one that brings me every record in Core Data, but I don't know how to change the ForEach source when the app is running. I have tried an array of names of variables for every one of the fetch requests variables names (bases, filtroActivas and filtroPospuestas) putting them in [] but that didn't work.
I know this isn't elegant, I just need it to work first and then go for efficiency and elegance. I'm sure there is some stupid thing I'm not seeing but it's been a week and I'm getting desperate.
I hope I was clear, if you need more information please ask. If anyone can help me I would very much appreciate it. Thanks in advance!