-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMedicationView.swift
74 lines (66 loc) · 2.06 KB
/
MedicationView.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
//
// File.swift
// MediApp
//
// Created by Daniel Ramzani on 20/02/2025.
//
import SwiftUI
struct MedicationListView: View {
@State private var medications = [("Aspirin", "100 mg"), ("Ibuprofen", "200 mg"), ("Paracetamol", "500 mg")]
@State private var showAddMedicationSheet = false
var body: some View {
VStack {
Text("Medication List")
.font(.title)
.fontWeight(.bold)
.padding()
List(medications, id: \.0) { medication in
MedicationItemView(medicationName: medication.0, dosage: medication.1)
}
.listStyle(PlainListStyle())
Button(action: {
showAddMedicationSheet = true
}) {
Text("Add Medication")
.font(.headline)
.padding()
.background(Color.blue)
.foregroundColor(.white)
.cornerRadius(10)
}
.padding()
.sheet(isPresented: $showAddMedicationSheet) {
AddMedicationView { name, dosage in
medications.append((name, dosage))
showAddMedicationSheet = false
}
}
}
}
}
struct AddMedicationView: View {
@State private var name = ""
@State private var dosage = ""
var onAdd: (String, String) -> Void
var body: some View {
NavigationView {
Form {
Section(header: Text("Medication Details")) {
TextField("Name", text: $name)
TextField("Dosage", text: $dosage)
}
}
.navigationBarTitle("Add Medication", displayMode: .inline)
.navigationBarItems(leading: Button("Cancel") {
onAdd("", "")
}, trailing: Button("Add") {
onAdd(name, dosage)
})
}
}
}
struct MedicationListView_Previews: PreviewProvider {
static var previews: some View {
MedicationListView()
}
}