Thanks Claude31
Below is the whole code of the two views I have so far
I tried your suggestion but i didn't get what i need, so i commented it out
when i changed the code i couldn't select any row
My objective is to be able to create instances of the DRUG struct in addView that are added to my favorites list in favView, I managed that so far. My next goal is to be able to select a row from my favorite list and be able to use that specific instance of the struct in another place
import SwiftUI
struct Drug: Identifiable, Codable, Hashable {
var id = UUID()
var name: String
var amount: String
var unitamount : String
var bag: String
var isRead: Bool = false
}
class Favoritos: ObservableObject {
@Published var favs = [Drug]() {
didSet {
if let encoded = try? JSONEncoder().encode(favs) {
UserDefaults.standard.set(encoded, forKey: "Favs")
}
}
}
init() {
if let savedItems = UserDefaults.standard.data(forKey: "Favs") {
if let decodedItems = try? JSONDecoder().decode([Drug].self, from: savedItems) {
favs = decodedItems
return
}
}
favs = []
}
}
struct FavListView: View {
@StateObject var favoritos = Favoritos()
// @State var newdrug = Drug(name: "", amount: "", bag: "")
@State private var showingAddView = false
func removeItems(at offsets: IndexSet) {
favoritos.favs.remove(atOffsets: offsets)
}
@State private var selection: String?
var body: some View {
NavigationView {
VStack{
// List {
// TextField ("Droga", text: $newdrug.name)
// TextField ("Cantidad", text: $newdrug.amount)
// TextField ("Volumen", text: $newdrug.bag)
// }
// List(favoritos.favs, id: \.self, selection: $selection) { drug in
// Text("\(drug.name) \(drug.amount) \(drug.unitamount) en \(drug.bag)")
// }
// .toolbar {
// EditButton()
// }
List
{ ForEach(favoritos.favs) { drug in
Text("\(drug.name) \(drug.amount) \(drug.unitamount) en \(drug.bag)")
}.onDelete(perform: removeItems)
}.navigationTitle("Infusiones favoritas")
.navigationBarTitleDisplayMode(.inline)
.accentColor(.black)
.toolbar {
EditButton()
}
Button ("agregar nuevo favorito") {showingAddView = true }
// Button ("Add") {
// favoritos.favs.insert(Drug(name: newdrug.name, amount: newdrug.amount, bag: newdrug.bag), at: 0)
//
//
// }
}.sheet(isPresented: $showingAddView) {
AddView(favoritos: favoritos)
}
}
}
struct FavListView_Previews: PreviewProvider {
static var previews: some View {
FavListView()
}
}
}
import SwiftUI
struct AddView: View {
@ObservedObject var favoritos : Favoritos
@State var newdrug = Drug(name: "", amount: "", unitamount: "", bag: "")
var body: some View {
VStack{
List {
TextField ("Droga", text: $newdrug.name)
.disableAutocorrection(/*@START_MENU_TOKEN@*/true/*@END_MENU_TOKEN@*/)
TextField ("Cantidad", text: $newdrug.amount)
TextField ("Unidad", text: $newdrug.unitamount)
.autocapitalization(/*@START_MENU_TOKEN@*/.none/*@END_MENU_TOKEN@*/)
TextField ("Volumen", text: $newdrug.bag)
}
}
Button ("Add") {
favoritos.favs.insert(Drug(name: newdrug.name, amount: newdrug.amount, unitamount: newdrug.unitamount, bag: newdrug.bag), at: 0)
}
}
}
struct AddView_Previews: PreviewProvider {
static var previews: some View {
AddView(favoritos : Favoritos())
}
}