-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserInfo.swift
147 lines (129 loc) · 5.5 KB
/
UserInfo.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
142
143
144
145
146
147
//
// UserInfo.swift
// CovidSymptomTracking
//
// Created by Soumya Duriseti on 2/14/21.
//
import SwiftUI
struct Location {
static let allLocations = [
"Palo Alto",
"Davis",
"New York",
"Chicago",
"San Francisco"
]
}
struct UserInfo: View {
@State private var firstname = ""
@State private var lastname = ""
@State private var location = ""
@State private var termsAccepted = false
@State private var age = 20
private let oldPasswordToConfirmAgainst = "12345"
@State private var oldPassword = ""
@State private var newPassword = ""
@State private var confirmedPassword = ""
@State private var keyboardOffset: CGFloat = 0
var body: some View {
VStack {
NavigationView {
Form {
Section(header: Text("User Details")) {
TextField("Firstname",
text: $firstname)
TextField("Lastname",
text: $lastname)
Picker(selection: $location,
label: Text("Location")) {
ForEach(Location.allLocations, id: \.self) { location in
Text(location).tag(location)
}
}
Toggle(isOn: $termsAccepted,
label: {
Text("Accept terms and conditions")
})
Stepper(value: $age,
in: 18...100,
label: {
Text("Current age: \(self.age)")
})
if self.isUserInformationValid() {
Button(action: {
print("Updated profile")
}, label: {
Text("Update Profile")
})
}
}
// Section(header: Text("Password")) {
// SecureField("Enter old password", text: $oldPassword)
// SecureField("New Password", text: $newPassword)
// SecureField("Confirm New Password", text: $confirmedPassword)
//
//// if self.isPasswordValid() {
//// Button(action: {
//// print("Updated password")
//// }, label: {
//// Text("Update password")
//// })
//// }
// }
}.navigationBarTitle(Text("Profile"))
}
.offset(y: -self.keyboardOffset)
.onAppear {
NotificationCenter.default.addObserver(forName: UIResponder.keyboardDidShowNotification,
object: nil,
queue: .main) { (notification) in
NotificationCenter.default.addObserver(
forName: UIResponder.keyboardDidShowNotification,
object: nil,
queue: .main) { (notification) in
guard let userInfo = notification.userInfo,
let keyboardRect = userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect else { return }
self.keyboardOffset = keyboardRect.height
}
NotificationCenter.default.addObserver(
forName: UIResponder.keyboardDidHideNotification,
object: nil,
queue: .main) { (notification) in
self.keyboardOffset = 0
}
}
}
}.background(Color(UIColor.systemGray6))
}
private func isUserInformationValid() -> Bool {
if firstname.isEmpty {
return false
}
if lastname.isEmpty {
return false
}
if !termsAccepted {
return false
}
if location.isEmpty {
return false
}
return true
}
// private func isPasswordValid() -> Bool {
// if oldPassword != oldPasswordToConfirmAgainst {
// return false
// }
//
// if !newPassword.isEmpty && newPassword == confirmedPassword {
// return true
// }
//
// return false
// }
}
struct UserInfo_Previews: PreviewProvider {
static var previews: some View {
UserInfo()
}
}