Browse Source

Added networking functionality for login

Hector Carrion 4 years ago
parent
commit
2798d44418

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

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

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

9
 import CoreData
9
 import CoreData
10
 
10
 
11
 let lightGreyColor = Color(red: 239.0/255.0, green: 243.0/255.0, blue: 244.0/255.0, opacity: 1.0)
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
 struct ContentView: View {
15
 struct ContentView: View {
14
     @Environment(\.managedObjectContext) private var viewContext
16
     @Environment(\.managedObjectContext) private var viewContext

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

7
 
7
 
8
 import SwiftUI
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
 struct LoginView: View {
61
 struct LoginView: View {
11
     @State var email: String = ""
62
     @State var email: String = ""
12
     @State var password: String = ""
63
     @State var password: String = ""
13
     @State var showingRegister: Bool = false
64
     @State var showingRegister: Bool = false
14
     
65
     
66
+    @ObservedObject var loginVM = LoginViewModel()
67
+    
15
     var body: some View {
68
     var body: some View {
16
         VStack {
69
         VStack {
17
             WelcomeImage()
70
             WelcomeImage()
18
             WelcomeText()
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
                 LoginText()
90
                 LoginText()
35
             }
91
             }
36
             
92
             
37
             Button(action: {self.showingRegister.toggle()}) {
93
             Button(action: {self.showingRegister.toggle()}) {
38
                 RegisterText()
94
                 RegisterText()
39
-            }.sheet(isPresented: $showingRegister, content: {
40
-                RegisterView()
41
-            })
42
-            
95
+            }
96
+            .sheet(isPresented: $showingRegister, content: {RegisterView()})
43
         }
97
         }
44
         .padding()
98
         .padding()
45
     }
99
     }
96
             .cornerRadius(5)
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

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