-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContentView.swift
159 lines (146 loc) · 7.12 KB
/
ContentView.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
148
149
150
151
152
153
154
155
156
157
158
159
import SwiftUI
import Firebase
import UserNotifications
struct ContentView: View {
@State private var email = ""
@State private var password = ""
@Binding var userIsLogged: Bool
@StateObject private var datamanager = DataManager()
init(userIsLogged: Binding<Bool>) {
_userIsLogged = userIsLogged
}
var body: some View {
if userIsLogged {
ListView(userIsLogged: $userIsLogged)
.environmentObject(datamanager)
} else {
ZStack {
Color.black.edgesIgnoringSafeArea(.all)
VStack(alignment: .center) {
GeometryReader { geometry in
ZStack(alignment: .top){
RoundedRectangle(cornerRadius: 25.0, style: .continuous)
.foregroundColor(.white)
Text("Dose Deck")
.fontWeight(.bold)
.foregroundColor(Color(red: 41.0/255.0, green: 51.0/255.0, blue: 120.0/255.0, opacity: 1.0))
.font(.largeTitle)
.padding(.top, 80.0)
Text("Create an Account or Login to Save Medical Records")
.multilineTextAlignment(.center)
.foregroundColor(.black)
.padding(.top, 150.0)
}
}
.frame(height: 0.3 * UIScreen.main.bounds.height)
GeometryReader { geometry in
ZStack(alignment: .top){
RoundedRectangle(cornerRadius: 25.0, style: .continuous)
.foregroundColor(Color(red:34.0/255.0, green:40.0/255.0, blue: 49.0/255.0, opacity: 1.0))
VStack(alignment: .center) {
Text("Welcome")
.font(.title)
.fontWeight(.bold)
.foregroundColor(.white)
.multilineTextAlignment(.leading)
.padding(.vertical, 20)
Text("Enter Your Email")
.fontWeight(.bold)
.foregroundColor(Color(red:1.0, green:1.0, blue: 1.0, opacity: 0.7))
.offset(x: -100)
TextField("Email", text: $email)
.padding(EdgeInsets(top: 20, leading: 20, bottom: 20, trailing: 20))
.foregroundColor(.white)
.background(
RoundedRectangle(cornerRadius: 25)
.stroke(Color(red: 1.0, green: 1.0, blue: 1.0, opacity: 0.4), lineWidth: 1.0)
)
.padding(.horizontal, 20)
.autocapitalization(.none)
.keyboardType(.emailAddress)
Text("Enter Your Password")
.fontWeight(.bold)
.foregroundColor(Color(red:1.0, green:1.0, blue: 1.0, opacity: 0.7))
.offset(x: -85)
.padding(.top, 20)
SecureField("Password", text: $password)
.padding(EdgeInsets(top: 20, leading: 20, bottom: 20, trailing: 20))
.foregroundColor(.white)
.background(
RoundedRectangle(cornerRadius: 25)
.stroke(Color(red: 1.0, green: 1.0, blue: 1.0, opacity: 0.4), lineWidth: 1.0)
)
.padding([.leading, .bottom, .trailing], 20)
Button(action: register) {
Text("Sign Up")
.frame(width: 100, height: 40)
.background(
RoundedRectangle(cornerRadius: 20, style: .continuous).fill(Color(.black))
)
.foregroundColor(.white)
.padding(.bottom, 10)
}
.alert(isPresented: $datamanager.showAlert) {
Alert(title: Text(datamanager.alertTitle), message: Text(datamanager.alertMessage), dismissButton: .default(Text("OK")))
}
Button(action: login) {
Text("Have an Account? Login here")
.foregroundColor(.white)
}
}
}
}
}
.onAppear {
requestNotificationPermission()
Auth.auth().addStateDidChangeListener { auth, user in
if user != nil {
userIsLogged.toggle()
}
}
}
}
.ignoresSafeArea()
}
}
func requestNotificationPermission() {
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in
if granted {
print("Notification permission granted")
} else if let error = error {
print("Error requesting notification permission: \(error.localizedDescription)")
}
}
}
func login() {
Auth.auth().signIn(withEmail: email, password: password) { result, error in
if error != nil {
print(error!.localizedDescription)
datamanager.showAlert(title: "Error", message: "Invalid email or password.")
} else {
if let userId = Auth.auth().currentUser?.uid {
datamanager.setUserId(userId)
}
userIsLogged = true
}
}
}
func register() {
Auth.auth().createUser(withEmail: email, password: password) { result, error in
if error != nil {
print(error!.localizedDescription)
datamanager.showAlert(title: "Error", message: "Unable to create an account. Please try again.")
} else {
if let userId = Auth.auth().currentUser?.uid {
datamanager.setUserId(userId)
}
userIsLogged = true
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView(userIsLogged: .constant(false))
}
}