-
Notifications
You must be signed in to change notification settings - Fork 1
/
ProfileSettingsView.swift
132 lines (110 loc) · 4.84 KB
/
ProfileSettingsView.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
//
// ProfileSettingsView.swift
// Jointwise
//
// Created by Biagio Marra on 20/04/23.
//
import SwiftUI
import CareKitStore
struct ProfileSettingsView: View {
@Binding var name: String
@Binding var surname: String
@Binding var sex: String
@Binding var birthDay: Date
@EnvironmentObject var storeManagerWrapper: StoreManagerWrapper
@Binding var isPresented: Bool
@State private var tempNome: String
@State private var tempCognome: String
@State private var tempSesso: String
@State private var tempCompleanno: Date
init(Nome: Binding<String>, Cognome: Binding<String>, Sesso: Binding<String>, Compleanno: Binding<Date>, isPresented: Binding<Bool>) {
_name = Nome
_surname = Cognome
_sex = Sesso
_birthDay = Compleanno
_isPresented = isPresented
_tempNome = State(initialValue: Nome.wrappedValue)
_tempCognome = State(initialValue: Cognome.wrappedValue)
_tempSesso = State(initialValue: Sesso.wrappedValue)
_tempCompleanno = State(initialValue: Compleanno.wrappedValue)
}
var body: some View {
NavigationView {
Form {
SwiftUI.Section{
TextField("First Name", text: $name)
.padding(.vertical, 8)
TextField("Last Name", text: $surname)
.padding(.vertical, 8)
} header:{
Text("Name")
}
SwiftUI.Section {
Picker(selection: $sex, label: Text("Sex")) {
Text("Male").tag("M")
Text("Female").tag("F")
}
.padding(.vertical, 8)
DatePicker(selection: $birthDay, displayedComponents: .date) {
Text("Birth Date")
}
.padding(.vertical, 8)
} header: {
Text("Personal Info")
}
SwiftUI.Section {
Button(action: {
let storeManager = storeManagerWrapper.storeManager
let patientID = "patientId"
storeManager.store.fetchAnyPatient(withID: patientID, callbackQueue: .main) { result in
switch result {
case .success(let patiente):
var updatedPatient = OCKPatient(id: patiente.id, givenName: name, familyName: surname)
updatedPatient.birthday = birthDay
updatedPatient.sex = OCKBiologicalSex(rawValue: sex)
storeManager.store.updateAnyPatient(updatedPatient, callbackQueue: .main) { result in
switch result {
case .success(let updatedPatient):
print("Patient updated successfully: \(updatedPatient)")
print(updatedPatient)
case .failure(let error):
print("Failed to update patient: \(error)")
}
}
case .failure(let error):
print("Failed to fetch patient with ID \(patientID): \(error)")
}
}
self.isPresented = false
}) {
Text("Save")
.foregroundColor(.green)
.font(.headline)
}
Button(action: {
// Ripristina i valori originali
name=tempNome
surname=tempCognome
sex=tempSesso
birthDay=tempCompleanno
isPresented = false
}) {
Text("Cancel")
.foregroundColor(.red)
.font(.headline)
}
.padding(.top, 8)
}
}
.navigationBarTitle("Profile Settings")
}
.accentColor(.green)
.onAppear {
// Salva i valori originali nelle variabili temporanee quando la vista viene caricata
tempNome = name
tempCognome = surname
tempSesso = sex
tempCompleanno = birthDay
}
}
}