this below is the view which doesn't remove the columns after I click the delete button and it shows the data even after it is removed from the firestore unless relaunched the app.
import SwiftUI
import Firebase
struct user:Identifiable{
@State var id:String=""
@State var productnm:String=""
@State var quan:String=""
}
class mainviewmodel:ObservableObject{
@Published var use = [user]()
init(){
fetchCurrentUser()
}
func fetchCurrentUser(){
guard let uid = Firebase.Auth.auth().currentUser?.uid else {
return
}
FirebaseManager.shared.firestore.collection("users").document(uid).collection("product").getDocuments{snapshot,error in
if error==nil{
//no errors
if let snapshot = snapshot{
//update the user property in the main thread
DispatchQueue.main.async {
// //get all the documents and create user
self.use=snapshot.documents.map{ d in //map will iterate over array and perform code on each item
//create an new user for each document returned
return user(id: d.documentID,productnm: d["product name"] as? String ?? "",
quan: d["quantity"] as? String ?? "")
}
}
}
print("no data found")
}
else{
print("no data found")
}
}
}
}
struct cart: View {
@ObservedObject var model=mainviewmodel()
@ObservedObject var vm = mainviewmodel()
@State var productnm:String=""
@State var quantity:String=""
@State var address:String=""
@State var payment:String=""
var body: some View{
VStack{
HStack{
Text("Cart")
navcustom(content:
Image(systemName: "cart"),col: .white)
Button(action: {vm.fetchCurrentUser()
}, label: {navcustom(content:
Image(systemName: "repeat.circle.fill"),col: .blue)})
}
List(model.use) {item in
HStack(spacing: 16) {
Image(systemName: "person.fill")
.font(.system(size: 32))
Text("\(item.productnm)")
Text("\(item.quan)")
}
Button(action: {deleteuser(productnm: item.productnm, quan: item.quan, id: item.id)}, label: {Text("Delete")})
Button(action: {if(address==""){Text("Enter the delivery address!").foregroundColor(.red)}else{buying(productnm: item.productnm, quantity: item.quan, address: address)}
deleteuser(productnm: item.productnm, quan: item.quan, id: item.id)
}, label: {Text("BUY")})
}
Text("Delivery Address:")
TextField("", text:$address)
.foregroundColor(.white)
.frame(width: 295)
.textFieldStyle(.roundedBorder)
}
}
func deleteuser(productnm:String,quan:String,id:String){
guard let uid = Firebase.Auth.auth().currentUser?.uid else {
return
}
FirebaseManager.shared.firestore.collection("users").document(uid).collection("product").document(id).delete(){ err in
if let err = err {
print("Error removing document: \(err)")
}
else {
print("Document successfully removed!")
}
}
}
func buying(productnm:String,quantity:String,address:String){
guard let uid = FirebaseManager.shared.auth.currentUser?.uid else { return }
let userData = ["user":uid,"product name":productnm,"quantity":quantity,"address":address]
FirebaseManager.shared.firestore.collection("purchase").document(uid).collection("product").addDocument(data: userData)
{ err in
if let err = err{
print(err)
return
}
}
}
}
struct cart_Previews: PreviewProvider {
static var previews: some View {
Group {
cart()
.environment(\.colorScheme, .dark)
}
}
}