Bladeren bron

Added Registration Networking

Hector Carrion 4 jaren geleden
bovenliggende
commit
c0793e3b0c
3 gewijzigde bestanden met toevoegingen van 216 en 34 verwijderingen
  1. 12
    2
      Comedores Sociales/LoginView.swift
  2. 59
    0
      Comedores Sociales/NetworkingModel.swift
  3. 145
    32
      Comedores Sociales/RegisterView.swift

+ 12
- 2
Comedores Sociales/LoginView.swift Bestand weergeven

@@ -64,12 +64,12 @@ struct LoginView: View {
64 64
     @State var showingRegister: Bool = false
65 65
     
66 66
     @ObservedObject var loginVM = LoginViewModel()
67
+    @State private var showingAlert = false
67 68
     
68 69
     var body: some View {
69 70
         VStack {
70 71
             WelcomeImage()
71 72
             WelcomeText()
72
-            
73 73
             EmailField(email: $email)
74 74
             PasswordField(password: $password)
75 75
             
@@ -84,7 +84,13 @@ struct LoginView: View {
84 84
             }
85 85
             
86 86
             Button(action: {
87
-                    self.loginVM.login(data: Authentication(email: email, password: password))
87
+                let authData = Authentication(email: email, password: password)
88
+                if authData.isComplete() {
89
+                    self.loginVM.login(data: authData)
90
+                } else {
91
+                    // alert
92
+                    self.showingAlert = true
93
+                }
88 94
                 
89 95
             }) {
90 96
                 LoginText()
@@ -94,6 +100,10 @@ struct LoginView: View {
94 100
                 RegisterText()
95 101
             }
96 102
             .sheet(isPresented: $showingRegister, content: {RegisterView()})
103
+            .alert(isPresented: $showingAlert) {
104
+                Alert(title: Text("Fields incomplete"), message: Text("Please enter a valid email and password"), dismissButton: .default(Text("OK")))
105
+                
106
+            }
97 107
         }
98 108
         .padding()
99 109
     }

+ 59
- 0
Comedores Sociales/NetworkingModel.swift Bestand weergeven

@@ -17,4 +17,63 @@ class Authentication: Codable {
17 17
         self.username = email
18 18
         self.password = password
19 19
     }
20
+    
21
+    func isComplete() -> Bool {
22
+        if username != "" && password != "" {
23
+            return true
24
+        } else {
25
+            return false
26
+        }
27
+    }
28
+}
29
+
30
+class Registration: Codable {
31
+    var username: String
32
+    var password: String
33
+    var nombre: String
34
+    var urbanizacion: String
35
+    var calle: String
36
+    var pueblo: String
37
+    var cpostal: String
38
+    var telefono: String
39
+    var correo: String
40
+    var organizacion: String?
41
+    var puesto: String?
42
+    
43
+    init(email: String, password: String, name: String, urb: String, street: String, city: String, zip: String, phone: String, org: String? = nil, position: String? = nil) {
44
+        if org != "" {
45
+            self.username = email
46
+            self.password = password
47
+            self.nombre = name
48
+            self.urbanizacion = urb
49
+            self.calle = street
50
+            self.pueblo = city
51
+            self.cpostal = zip
52
+            self.telefono = phone
53
+            self.correo = email
54
+            self.organizacion = org
55
+            self.puesto = position
56
+            
57
+        } else {
58
+            self.username = email
59
+            self.password = password
60
+            self.nombre = name
61
+            self.urbanizacion = urb
62
+            self.calle = street
63
+            self.pueblo = city
64
+            self.cpostal = zip
65
+            self.telefono = phone
66
+            self.correo = email
67
+            self.organizacion = nil
68
+            self.puesto = nil
69
+        }
70
+    }
71
+    
72
+    func isComplete() -> Bool {
73
+        if username != "" && password != "" && nombre != "" && urbanizacion != "" && calle != "" && pueblo != "" && cpostal != "" && telefono != "" && correo != "" {
74
+            return true
75
+        } else {
76
+            return false
77
+        }
78
+    }
20 79
 }

+ 145
- 32
Comedores Sociales/RegisterView.swift Bestand weergeven

@@ -7,14 +7,71 @@
7 7
 
8 8
 import SwiftUI
9 9
 
10
+class RegisterViewModel: ObservableObject {
11
+    @Published var response: Bool?
12
+    
13
+    func changeResponse(response: Bool) {
14
+        self.response = response
15
+    }
16
+    
17
+    func register(data: Registration) {
18
+        
19
+        let service = "signup.php"
20
+        guard let encodedData = try? JSONEncoder().encode(data)
21
+        else {
22
+            print("Failed to encode register request")
23
+            return
24
+        }
25
+        
26
+        print("Reg JSON to send:", String(data: encodedData, encoding: .utf8)!)
27
+        
28
+        let url = URL(string: serverAddress + service)!
29
+        var request = URLRequest(url: url)
30
+        request.setValue("appregister/json", forHTTPHeaderField: "Content-Type")
31
+        request.httpMethod = "POST"
32
+        request.httpBody = encodedData
33
+        
34
+        URLSession.shared.dataTask(with: request) {data, response, error in
35
+            
36
+            //handle result here
37
+            guard let data = data
38
+            else {
39
+                print("No data in response: \(error?.localizedDescription ?? "Unknown error").")
40
+                return
41
+            }
42
+            print(response ?? "No response")
43
+            print("Recieved data:", data)
44
+            print(error ?? "No error")
45
+            
46
+            if let httpResponse = response as? HTTPURLResponse {
47
+                DispatchQueue.main.async {
48
+                    if httpResponse.statusCode == 200 {
49
+                        self.changeResponse(response: true)
50
+                    } else {
51
+                        self.changeResponse(response: false)
52
+                    }
53
+                }
54
+            }
55
+        }.resume()
56
+    }
57
+}
58
+
10 59
 struct RegisterView: View {
11
-    @State var firstName: String = ""
12
-    @State var lastName: String = ""
60
+    @State var name: String = ""
13 61
     @State var phoneNumber: String = ""
14 62
     @State var email: String = ""
15 63
     @State var password: String = ""
64
+    @State var urb: String = ""
65
+    @State var city: String = ""
66
+    @State var street: String = ""
67
+    @State var org: String = ""
68
+    @State var position: String = ""
69
+    @State var zip: String = ""
16 70
     @Environment(\.colorScheme) var colorScheme
17 71
     
72
+    @ObservedObject var registerVM = RegisterViewModel()
73
+    @State private var showingAlert = false
74
+    
18 75
     var body: some View {
19 76
         VStack {
20 77
             Text("Details")
@@ -23,36 +80,90 @@ struct RegisterView: View {
23 80
                 .frame(width: 350, height: 25, alignment: .topLeading)
24 81
                 .padding(.bottom, 20)
25 82
             
26
-            TextField("First Name", text: $firstName)
27
-                .padding()
28
-                .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
29
-                .cornerRadius(5.0)
30
-                .padding(.bottom, 5)
31
-            TextField("Last Name", text: $lastName)
32
-                .padding()
33
-                .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
34
-                .cornerRadius(5.0)
35
-                .padding(.bottom, 5)
36
-            TextField("Phone Number", text: $phoneNumber)
37
-                .keyboardType(.numberPad)
38
-                .padding()
39
-                .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
40
-                .cornerRadius(5.0)
41
-                .padding(.bottom, 5)
42
-            TextField("Email", text: $email)
43
-                .autocapitalization(.none)
44
-                .padding()
45
-                .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
46
-                .cornerRadius(5.0)
47
-                .padding(.bottom, 5)
48
-            SecureField("Password", text: $password)
49
-                .autocapitalization(.none)
50
-                .padding()
51
-                .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
52
-                .cornerRadius(5.0)
53
-                .padding(.bottom, 20)
83
+            ScrollView {
84
+                Group {
85
+                    TextField("Full Name", text: $name)
86
+                        .padding()
87
+                        .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
88
+                        .cornerRadius(5.0)
89
+                        .padding(.bottom, 5)
90
+                    TextField("Email", text: $email)
91
+                        .autocapitalization(.none)
92
+                        .padding()
93
+                        .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
94
+                        .cornerRadius(5.0)
95
+                        .padding(.bottom, 5)
96
+                    SecureField("Password", text: $password)
97
+                        .autocapitalization(.none)
98
+                        .padding()
99
+                        .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
100
+                        .cornerRadius(5.0)
101
+                        .padding(.bottom, 5)
102
+                    TextField("Phone Number", text: $phoneNumber)
103
+                        .keyboardType(.numberPad)
104
+                        .padding()
105
+                        .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
106
+                        .cornerRadius(5.0)
107
+                        .padding(.bottom, 5)
108
+                    TextField("Urbanization", text: $urb)
109
+                        .padding()
110
+                        .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
111
+                        .cornerRadius(5.0)
112
+                        .padding(.bottom, 5)
113
+                    TextField("Street", text: $street)
114
+                        .padding()
115
+                        .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
116
+                        .cornerRadius(5.0)
117
+                        .padding(.bottom, 5)
118
+                    TextField("City", text: $city)
119
+                        .padding()
120
+                        .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
121
+                        .cornerRadius(5.0)
122
+                        .padding(.bottom, 5)
123
+                    TextField("Zip Code", text: $zip)
124
+                        .keyboardType(.numberPad)
125
+                        .padding()
126
+                        .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
127
+                        .cornerRadius(5.0)
128
+                        .padding(.bottom, 5)
129
+                    TextField("Organization (optional)", text: $org)
130
+                        .padding()
131
+                        .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
132
+                        .cornerRadius(5.0)
133
+                        .padding(.bottom, 5)
134
+                    TextField("Position (optional)", text: $position)
135
+                        .padding()
136
+                        .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
137
+                        .cornerRadius(5.0)
138
+                        //.padding(.bottom, 5)
139
+                    
140
+                }.padding(5)
141
+            }
142
+            
143
+            if self.registerVM.response == false {
144
+                Text("Registration failed, please try again")
145
+                    //.offset(y: -5)
146
+                    .foregroundColor(.red)
147
+            } else if self.registerVM.response == true {
148
+                Text("Registration successful 🎉")
149
+                    //.offset(y: -5)
150
+                    .foregroundColor(.green)
151
+            }
152
+            
54 153
             
55
-            Button(action: {print("register tapped")}) {
154
+            Button(action: {
155
+                print("register tapped")
156
+                
157
+                let registrationData = Registration(email: email, password: password, name: name, urb: urb, street: street, city: city, zip: zip, phone: phoneNumber, org: org, position: position)
158
+                
159
+                if registrationData.isComplete() {
160
+                    self.registerVM.register(data: registrationData)
161
+                } else {
162
+                    // Alert here
163
+                    self.showingAlert = true
164
+                }
165
+                
166
+            }) {
56 167
                 Text("Register")
57 168
                     .font(.headline)
58 169
                     .foregroundColor(.white)
@@ -64,7 +175,9 @@ struct RegisterView: View {
64 175
             }
65 176
             
66 177
             
67
-        }.padding()
178
+        }.alert(isPresented: $showingAlert) {
179
+            Alert(title: Text("Fields incomplete"), message: Text("Please enter a all required information"), dismissButton: .default(Text("OK")))}
180
+        .padding()
68 181
     }
69 182
 }
70 183