Skip to content

Commit 22891a0

Browse files
committed
ui changes
2 parents a5c5761 + d02d5f7 commit 22891a0

4 files changed

Lines changed: 72 additions & 7 deletions

File tree

AnimalsApp/AnimalsApp.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
/* Begin PBXBuildFile section */
1010
432FDDFDC9779A196A20F261 /* Pods_AnimalsApp.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9D1DC7AE05F709A3F0E450DA /* Pods_AnimalsApp.framework */; };
1111
56CADB59A535B3ED60380013 /* Pods_AnimalsApp_AnimalsAppUITests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A1E75A013A1B19FF9D2B114B /* Pods_AnimalsApp_AnimalsAppUITests.framework */; };
12+
5CA996F8285A98FB00FF5D79 /* RegisterViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5CA996F7285A98FB00FF5D79 /* RegisterViewModel.swift */; };
1213
A2A6AE83CC0E1DAC784999B1 /* Pods_AnimalsAppTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 290D574622CE042567539136 /* Pods_AnimalsAppTests.framework */; };
1314
A463D0172858EBBB00929A3C /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = A463D0162858EBBB00929A3C /* AppDelegate.swift */; };
1415
A463D0212858EBBB00929A3C /* AnimalsApp.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = A463D01F2858EBBB00929A3C /* AnimalsApp.xcdatamodeld */; };
@@ -59,6 +60,7 @@
5960
2DCF7719AB858998955D44F5 /* Pods-AnimalsApp-AnimalsAppUITests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-AnimalsApp-AnimalsAppUITests.release.xcconfig"; path = "Target Support Files/Pods-AnimalsApp-AnimalsAppUITests/Pods-AnimalsApp-AnimalsAppUITests.release.xcconfig"; sourceTree = "<group>"; };
6061
4DF5AC84080C5CAD4EFD22DD /* Pods-AnimalsApp-AnimalsAppUITests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-AnimalsApp-AnimalsAppUITests.debug.xcconfig"; path = "Target Support Files/Pods-AnimalsApp-AnimalsAppUITests/Pods-AnimalsApp-AnimalsAppUITests.debug.xcconfig"; sourceTree = "<group>"; };
6162
5955E4FFC7C4BE50ACCA87B4 /* Pods-AnimalsApp.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-AnimalsApp.debug.xcconfig"; path = "Target Support Files/Pods-AnimalsApp/Pods-AnimalsApp.debug.xcconfig"; sourceTree = "<group>"; };
63+
5CA996F7285A98FB00FF5D79 /* RegisterViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RegisterViewModel.swift; sourceTree = "<group>"; };
6264
7B390F83513FC642B7BEDAF0 /* Pods-AnimalsAppTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-AnimalsAppTests.release.xcconfig"; path = "Target Support Files/Pods-AnimalsAppTests/Pods-AnimalsAppTests.release.xcconfig"; sourceTree = "<group>"; };
6365
7B6C2810AB19D2F9FE7AD5CF /* Pods-AnimalsApp.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-AnimalsApp.release.xcconfig"; path = "Target Support Files/Pods-AnimalsApp/Pods-AnimalsApp.release.xcconfig"; sourceTree = "<group>"; };
6466
81C9DABDE310C2E15910D06C /* Pods-AnimalsAppTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-AnimalsAppTests.debug.xcconfig"; path = "Target Support Files/Pods-AnimalsAppTests/Pods-AnimalsAppTests.debug.xcconfig"; sourceTree = "<group>"; };
@@ -280,6 +282,7 @@
280282
isa = PBXGroup;
281283
children = (
282284
A463D073285A398900929A3C /* HomeViewModel.swift */,
285+
5CA996F7285A98FB00FF5D79 /* RegisterViewModel.swift */,
283286
);
284287
path = "View Models";
285288
sourceTree = "<group>";
@@ -565,6 +568,7 @@
565568
A463D04B2858F39000929A3C /* MainTabBarController.swift in Sources */,
566569
A463D074285A398900929A3C /* HomeViewModel.swift in Sources */,
567570
A463D07A285B8D8E00929A3C /* Colors.swift in Sources */,
571+
5CA996F8285A98FB00FF5D79 /* RegisterViewModel.swift in Sources */,
568572
A463D05D2858F64B00929A3C /* FavoritesViewController.swift in Sources */,
569573
A463D06D285A22D200929A3C /* DetailViewController.swift in Sources */,
570574
A463D069285A1A5A00929A3C /* WebServices.swift in Sources */,

AnimalsApp/AnimalsApp/Services/WebServices.swift

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import Alamofire
1111
protocol WebServicesContract: AnyObject {
1212
var endpoint: String { get }
1313
func fetchAnimals(completion: @escaping (Result<[Animal], Error>) -> ())
14-
func registerAnimal()
14+
func registerAnimal(with parameters: [String: Any])
1515
}
1616

1717
class WebServices: WebServicesContract {
@@ -31,7 +31,32 @@ class WebServices: WebServicesContract {
3131
}
3232
}
3333

34-
func registerAnimal() {
35-
34+
func registerAnimal(with parameters: [String: Any]) {
35+
AF.request(urlString, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: nil).validate(statusCode: 200 ..< 299).responseData { response in
36+
switch response.result {
37+
case .success(let data):
38+
do {
39+
guard let jsonObject = try JSONSerialization.jsonObject(with: data) as? [String: Any] else {
40+
print("Error: Cannot convert data to JSON object")
41+
return
42+
}
43+
guard let prettyJsonData = try? JSONSerialization.data(withJSONObject: jsonObject, options: .prettyPrinted) else {
44+
print("Error: Cannot convert JSON object to Pretty JSON data")
45+
return
46+
}
47+
guard let prettyPrintedJson = String(data: prettyJsonData, encoding: .utf8) else {
48+
print("Error: Could print JSON in String")
49+
return
50+
}
51+
52+
print(prettyPrintedJson)
53+
} catch {
54+
print("Error: Trying to convert JSON data to string")
55+
return
56+
}
57+
case .failure(let error):
58+
print(error)
59+
}
60+
}
3661
}
3762
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//
2+
// RegisterViewModel.swift
3+
// AnimalsApp
4+
//
5+
// Created by Fran Martins on 15/06/22.
6+
//
7+
8+
import Foundation
9+
10+
struct RegisterViewModel {
11+
private var webServices: WebServicesContract
12+
13+
init(webServices: WebServicesContract = WebServices()) {
14+
self.webServices = webServices
15+
}
16+
17+
func registerAnimal(with parameters: [String: Any]) {
18+
webServices.registerAnimal(with: parameters)
19+
}
20+
}

AnimalsApp/AnimalsApp/Views/RegisterViewController/RegisterViewController.swift

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,18 @@
66
//
77

88
import UIKit
9+
import Alamofire
910

1011
class RegisterViewController: UIViewController {
1112
// MARK: Properties
13+
var registerVM = RegisterViewModel()
14+
var parameters: [String: Any] = [
15+
"name": "",
16+
"description": "",
17+
"age": 0,
18+
"specie": "",
19+
"image": ""
20+
]
1221

1322
// MARK: Outlets
1423
@IBOutlet weak var textFieldName: UITextField!
@@ -31,7 +40,8 @@ class RegisterViewController: UIViewController {
3140

3241
// MARK: Actions
3342
@IBAction func handlerButtonRegister(_ sender: Any) {
34-
43+
setParameters()
44+
registerVM.registerAnimal(with: parameters)
3545
}
3646

3747
// MARK: Methods
@@ -48,14 +58,20 @@ class RegisterViewController: UIViewController {
4858
textField?.layer.masksToBounds = true
4959
}
5060

51-
let attributes = [
52-
NSAttributedString.Key.foregroundColor: UIColor.systemGray
53-
]
61+
let attributes = [NSAttributedString.Key.foregroundColor: UIColor.systemGray]
5462
textFieldName.attributedPlaceholder = NSAttributedString(string: "Nome", attributes:attributes)
5563
textFieldImageLink.attributedPlaceholder = NSAttributedString(string: "Link da imagem", attributes:attributes)
5664
textFieldDescription.attributedPlaceholder = NSAttributedString(string: "Descrição", attributes:attributes)
5765
textFieldSpecie.attributedPlaceholder = NSAttributedString(string: "Espécie", attributes:attributes)
5866
textFieldAge.attributedPlaceholder = NSAttributedString(string: "Idade", attributes:attributes)
5967
buttonRegister.layer.cornerRadius = 10
6068
}
69+
70+
private func setParameters() {
71+
parameters["name"] = textFieldName.text
72+
parameters["description"] = textFieldDescription.text
73+
parameters["age"] = Int(textFieldAge.text ?? "0")
74+
parameters["specie"] = textFieldSpecie.text
75+
parameters["image"] = textFieldImageLink.text
76+
}
6177
}

0 commit comments

Comments
 (0)