Browse Source

Added networking functionality for login

Hector Carrion 3 years ago
parent
commit
2798d44418

+ 4
- 0
Comedores Sociales.xcodeproj/project.pbxproj View File

@@ -17,6 +17,7 @@
17 17
 		356533A525449C3900F2F0DE /* Comedores_SocialesUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 356533A425449C3900F2F0DE /* Comedores_SocialesUITests.swift */; };
18 18
 		356533B92544A1AF00F2F0DE /* LoginView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 356533B82544A1AF00F2F0DE /* LoginView.swift */; };
19 19
 		356533C42544B1FF00F2F0DE /* RegisterView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 356533C32544B1FF00F2F0DE /* RegisterView.swift */; };
20
+		358E94702558AC130074E928 /* NetworkingModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 358E946F2558AC130074E928 /* NetworkingModel.swift */; };
20 21
 /* End PBXBuildFile section */
21 22
 
22 23
 /* Begin PBXContainerItemProxy section */
@@ -53,6 +54,7 @@
53 54
 		356533A625449C3900F2F0DE /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
54 55
 		356533B82544A1AF00F2F0DE /* LoginView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoginView.swift; sourceTree = "<group>"; };
55 56
 		356533C32544B1FF00F2F0DE /* RegisterView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RegisterView.swift; sourceTree = "<group>"; };
57
+		358E946F2558AC130074E928 /* NetworkingModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkingModel.swift; sourceTree = "<group>"; };
56 58
 /* End PBXFileReference section */
57 59
 
58 60
 /* Begin PBXFrameworksBuildPhase section */
@@ -107,6 +109,7 @@
107 109
 				3565338425449C3600F2F0DE /* ContentView.swift */,
108 110
 				356533B82544A1AF00F2F0DE /* LoginView.swift */,
109 111
 				356533C32544B1FF00F2F0DE /* RegisterView.swift */,
112
+				358E946F2558AC130074E928 /* NetworkingModel.swift */,
110 113
 				3565338625449C3800F2F0DE /* Assets.xcassets */,
111 114
 				3565338B25449C3800F2F0DE /* Persistence.swift */,
112 115
 				3565339025449C3800F2F0DE /* Info.plist */,
@@ -271,6 +274,7 @@
271 274
 			isa = PBXSourcesBuildPhase;
272 275
 			buildActionMask = 2147483647;
273 276
 			files = (
277
+				358E94702558AC130074E928 /* NetworkingModel.swift in Sources */,
274 278
 				356533B92544A1AF00F2F0DE /* LoginView.swift in Sources */,
275 279
 				3565338C25449C3800F2F0DE /* Persistence.swift in Sources */,
276 280
 				3565338525449C3600F2F0DE /* ContentView.swift in Sources */,

+ 2
- 0
Comedores Sociales/ContentView.swift View File

@@ -9,6 +9,8 @@ import SwiftUI
9 9
 import CoreData
10 10
 
11 11
 let lightGreyColor = Color(red: 239.0/255.0, green: 243.0/255.0, blue: 244.0/255.0, opacity: 1.0)
12
+let darkGreyColor = Color(red: 41.0/255.0, green: 42.0/255.0, blue: 47.0/255.0, opacity: 1.0)
13
+
12 14
 
13 15
 struct ContentView: View {
14 16
     @Environment(\.managedObjectContext) private var viewContext

+ 97
- 17
Comedores Sociales/LoginView.swift View File

@@ -7,39 +7,93 @@
7 7
 
8 8
 import SwiftUI
9 9
 
10
+class LoginViewModel: ObservableObject {
11
+    @Published var response: Bool?
12
+    
13
+    func changeResponse(response: Bool) {
14
+        self.response = response
15
+    }
16
+    
17
+    func login(data: Authentication) {
18
+        
19
+        let service = "login.php"
20
+        guard let encodedData = try? JSONEncoder().encode(data)
21
+        else {
22
+            print("Failed to encode login request")
23
+            return
24
+        }
25
+        
26
+        //print("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("applogin/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
+            }
56
+            
57
+        }.resume()
58
+    }
59
+}
60
+
10 61
 struct LoginView: View {
11 62
     @State var email: String = ""
12 63
     @State var password: String = ""
13 64
     @State var showingRegister: Bool = false
14 65
     
66
+    @ObservedObject var loginVM = LoginViewModel()
67
+    
15 68
     var body: some View {
16 69
         VStack {
17 70
             WelcomeImage()
18 71
             WelcomeText()
19 72
             
20
-            TextField("Email", text: $email)
21
-                .autocapitalization(.none)
22
-                .padding()
23
-                .background(lightGreyColor)
24
-                .cornerRadius(5.0)
25
-                .padding(.bottom, 5)
26
-            SecureField("Password", text: $password)
27
-                .autocapitalization(.none)
28
-                .padding()
29
-                .background(lightGreyColor)
30
-                .cornerRadius(5.0)
31
-                .padding(.bottom, 20)
73
+            EmailField(email: $email)
74
+            PasswordField(password: $password)
75
+            
76
+            if self.loginVM.response == false {
77
+                Text("Information incorrect, please try again")
78
+                    .offset(y: -10)
79
+                    .foregroundColor(.red)
80
+            } else if self.loginVM.response == true {
81
+                Text("Login successful 🎉")
82
+                    .offset(y: -10)
83
+                    .foregroundColor(.green)
84
+            }
32 85
             
33
-            Button(action: {print("login tapped")}) {
86
+            Button(action: {
87
+                    self.loginVM.login(data: Authentication(email: email, password: password))
88
+                
89
+            }) {
34 90
                 LoginText()
35 91
             }
36 92
             
37 93
             Button(action: {self.showingRegister.toggle()}) {
38 94
                 RegisterText()
39
-            }.sheet(isPresented: $showingRegister, content: {
40
-                RegisterView()
41
-            })
42
-            
95
+            }
96
+            .sheet(isPresented: $showingRegister, content: {RegisterView()})
43 97
         }
44 98
         .padding()
45 99
     }
@@ -96,3 +150,29 @@ struct RegisterText: View {
96 150
             .cornerRadius(5)
97 151
     }
98 152
 }
153
+
154
+struct EmailField: View {
155
+    @Binding var email: String
156
+    @Environment(\.colorScheme) var colorScheme
157
+    var body: some View {
158
+        TextField("Email", text: $email)
159
+            .autocapitalization(.none)
160
+            .padding()
161
+            .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
162
+            .cornerRadius(5.0)
163
+            .padding(.bottom, 5)
164
+    }
165
+}
166
+
167
+struct PasswordField: View {
168
+    @Binding var password: String
169
+    @Environment(\.colorScheme) var colorScheme
170
+    var body: some View {
171
+        SecureField("Password", text: $password)
172
+            .autocapitalization(.none)
173
+            .padding()
174
+            .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
175
+            .cornerRadius(5.0)
176
+            .padding(.bottom, 20)
177
+    }
178
+}

+ 20
- 0
Comedores Sociales/NetworkingModel.swift View File

@@ -0,0 +1,20 @@
1
+//
2
+//  DataModel.swift
3
+//  Comedores Sociales
4
+//
5
+//  Created by Hector Carrion on 11/8/20.
6
+//
7
+
8
+import Foundation
9
+
10
+let serverAddress: String = "http://161.35.60.201/"
11
+
12
+class Authentication: Codable {
13
+    var username: String
14
+    var password: String
15
+    
16
+    init(email: String, password: String) {
17
+        self.username = email
18
+        self.password = password
19
+    }
20
+}

+ 6
- 5
Comedores Sociales/RegisterView.swift View File

@@ -13,6 +13,7 @@ struct RegisterView: View {
13 13
     @State var phoneNumber: String = ""
14 14
     @State var email: String = ""
15 15
     @State var password: String = ""
16
+    @Environment(\.colorScheme) var colorScheme
16 17
     
17 18
     var body: some View {
18 19
         VStack {
@@ -24,30 +25,30 @@ struct RegisterView: View {
24 25
             
25 26
             TextField("First Name", text: $firstName)
26 27
                 .padding()
27
-                .background(lightGreyColor)
28
+                .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
28 29
                 .cornerRadius(5.0)
29 30
                 .padding(.bottom, 5)
30 31
             TextField("Last Name", text: $lastName)
31 32
                 .padding()
32
-                .background(lightGreyColor)
33
+                .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
33 34
                 .cornerRadius(5.0)
34 35
                 .padding(.bottom, 5)
35 36
             TextField("Phone Number", text: $phoneNumber)
36 37
                 .keyboardType(.numberPad)
37 38
                 .padding()
38
-                .background(lightGreyColor)
39
+                .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
39 40
                 .cornerRadius(5.0)
40 41
                 .padding(.bottom, 5)
41 42
             TextField("Email", text: $email)
42 43
                 .autocapitalization(.none)
43 44
                 .padding()
44
-                .background(lightGreyColor)
45
+                .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
45 46
                 .cornerRadius(5.0)
46 47
                 .padding(.bottom, 5)
47 48
             SecureField("Password", text: $password)
48 49
                 .autocapitalization(.none)
49 50
                 .padding()
50
-                .background(lightGreyColor)
51
+                .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
51 52
                 .cornerRadius(5.0)
52 53
                 .padding(.bottom, 20)
53 54