-
Notifications
You must be signed in to change notification settings - Fork 0
/
IE.swift
141 lines (128 loc) · 4.74 KB
/
IE.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
//
// ContentView.swift
// Industrial_APP
//
// Created by User on 2023/2/21.
//
import SwiftUI
import FirebaseFirestore
struct ProcessData: Identifiable, Codable {
var id: String
var name: String
var quantity: String
var machine: String
var progress: String
}
struct IE: View {
@State private var processes: [ProcessData] = []
@State private var showAlert = false
@State private var deleteProcess: ProcessData?
private let collectionReference = Firestore.firestore().collection("processes")
var body: some View {
NavigationView {
VStack {
List {
HeaderRow()
ForEach($processes) { $process in
HStack {
TextField("Producto", text: $process.name, onCommit: {
saveProcess(process)
})
TextField("Cantidad", text: $process.quantity, onCommit: {
saveProcess(process)
})
TextField("Máquina", text: $process.machine, onCommit: {
saveProcess(process)
})
TextField("Progreso", text: $process.progress, onCommit: {
saveProcess(process)
})
Button(action: {
deleteProcess = process
showAlert = true
}) {
Image(systemName: "trash")
.foregroundColor(.red)
}
}
}
}
}
.navigationBarTitle("Proceso", displayMode: .inline)
.navigationBarItems(trailing: Button(action: {
addProcess()
}) {
Image(systemName: "plus")
})
.onAppear {
fetchProcesses()
}
.alert(isPresented: $showAlert) {
Alert(
title: Text("Confirm Delete"),
message: Text("Are you sure you want to delete this row?"),
primaryButton: .destructive(Text("Delete")) {
if let processToDelete = deleteProcess {
deleteProcess(processToDelete)
}
},
secondaryButton: .cancel()
)
}
}
}
private func HeaderRow() -> some View {
HStack {
Text("Producto").font(.footnote).frame(maxWidth: .infinity, alignment: .center)
Text("Cantidad").font(.footnote).frame(maxWidth: .infinity, alignment: .center)
Text("Máquina").font(.footnote).frame(maxWidth: .infinity, alignment: .center)
Text("Progreso").font(.footnote).frame(maxWidth: .infinity, alignment: .center)
}
}
private func fetchProcesses() {
collectionReference.getDocuments { snapshot, error in
if let error = error {
print("Error fetching documents: \(error)")
return
}
guard let documents = snapshot?.documents else { return }
processes = documents.compactMap { document in
do {
let processData = try document.data(as: ProcessData.self)
return processData
} catch {
print("Error decoding document: \(error)")
return nil
}
}
}
}
private func saveProcess(_ process: ProcessData) {
do {
try collectionReference.document(process.id).setData(from: process)
} catch {
print("Error saving document: \(error)")
}
}
private func deleteProcess(_ process: ProcessData) {
collectionReference.document(process.id).delete { error in
if let error = error {
print("Error deleting document: \(error)")
} else {
if let index = processes.firstIndex(where: { $0.id == process.id }) {
processes.remove(at: index)
}
}
}
}
private func addProcess() {
let newProcess = ProcessData(id: UUID().uuidString, name: "", quantity: "", machine: "", progress: "")
processes.insert(newProcess, at: 0)
saveProcess(newProcess)
}
}
struct IE_Previews: PreviewProvider {
static var previews: some View {
IE()
}
}