Skip to content

Commit 8410648

Browse files
committed
add get methot alamofire
1 parent 25d64e7 commit 8410648

14 files changed

Lines changed: 423 additions & 54 deletions

File tree

AnimalsApp/AnimalsApp.xcodeproj/project.pbxproj

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
A463D069285A1A5A00929A3C /* WebServices.swift in Sources */ = {isa = PBXBuildFile; fileRef = A463D068285A1A5A00929A3C /* WebServices.swift */; };
3232
A463D06D285A22D200929A3C /* DetailViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A463D06B285A22D200929A3C /* DetailViewController.swift */; };
3333
A463D06E285A22D300929A3C /* DetailViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = A463D06C285A22D200929A3C /* DetailViewController.xib */; };
34+
A463D071285A373800929A3C /* Animals.swift in Sources */ = {isa = PBXBuildFile; fileRef = A463D070285A373700929A3C /* Animals.swift */; };
35+
A463D074285A398900929A3C /* HomeViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = A463D073285A398900929A3C /* HomeViewModel.swift */; };
36+
A463D078285A5A8000929A3C /* OpenSans-Regular.ttf in Resources */ = {isa = PBXBuildFile; fileRef = A463D077285A5A8000929A3C /* OpenSans-Regular.ttf */; };
3437
/* End PBXBuildFile section */
3538

3639
/* Begin PBXContainerItemProxy section */
@@ -85,6 +88,9 @@
8588
A463D068285A1A5A00929A3C /* WebServices.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WebServices.swift; sourceTree = "<group>"; };
8689
A463D06B285A22D200929A3C /* DetailViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DetailViewController.swift; sourceTree = "<group>"; };
8790
A463D06C285A22D200929A3C /* DetailViewController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = DetailViewController.xib; sourceTree = "<group>"; };
91+
A463D070285A373700929A3C /* Animals.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Animals.swift; sourceTree = "<group>"; };
92+
A463D073285A398900929A3C /* HomeViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HomeViewModel.swift; sourceTree = "<group>"; };
93+
A463D077285A5A8000929A3C /* OpenSans-Regular.ttf */ = {isa = PBXFileReference; lastKnownFileType = file; path = "OpenSans-Regular.ttf"; sourceTree = "<group>"; };
8894
/* End PBXFileReference section */
8995

9096
/* Begin PBXFrameworksBuildPhase section */
@@ -153,8 +159,11 @@
153159
A463D0152858EBBB00929A3C /* AnimalsApp */ = {
154160
isa = PBXGroup;
155161
children = (
162+
A463D076285A59B500929A3C /* Resources */,
156163
A463D067285A1A4300929A3C /* Services */,
157164
A463D06028590CB700929A3C /* DesignSystem */,
165+
A463D06F285A371F00929A3C /* Models */,
166+
A463D072285A396C00929A3C /* View Models */,
158167
A463D05F2858F66B00929A3C /* Views */,
159168
A463D0162858EBBB00929A3C /* AppDelegate.swift */,
160169
A463D0222858EBBD00929A3C /* Assets.xcassets */,
@@ -194,10 +203,9 @@
194203
A463D04E2858F47C00929A3C /* HomeViewController */ = {
195204
isa = PBXGroup;
196205
children = (
206+
A463D075285A580400929A3C /* Components */,
197207
A463D0512858F60C00929A3C /* HomeViewController.swift */,
198208
A463D0522858F60C00929A3C /* HomeViewController.xib */,
199-
A463D063285953F900929A3C /* AnimalTableViewCell.swift */,
200-
A463D064285953F900929A3C /* AnimalTableViewCell.xib */,
201209
);
202210
path = HomeViewController;
203211
sourceTree = "<group>";
@@ -257,6 +265,39 @@
257265
path = DetailViewController;
258266
sourceTree = "<group>";
259267
};
268+
A463D06F285A371F00929A3C /* Models */ = {
269+
isa = PBXGroup;
270+
children = (
271+
A463D070285A373700929A3C /* Animals.swift */,
272+
);
273+
path = Models;
274+
sourceTree = "<group>";
275+
};
276+
A463D072285A396C00929A3C /* View Models */ = {
277+
isa = PBXGroup;
278+
children = (
279+
A463D073285A398900929A3C /* HomeViewModel.swift */,
280+
);
281+
path = "View Models";
282+
sourceTree = "<group>";
283+
};
284+
A463D075285A580400929A3C /* Components */ = {
285+
isa = PBXGroup;
286+
children = (
287+
A463D063285953F900929A3C /* AnimalTableViewCell.swift */,
288+
A463D064285953F900929A3C /* AnimalTableViewCell.xib */,
289+
);
290+
path = Components;
291+
sourceTree = "<group>";
292+
};
293+
A463D076285A59B500929A3C /* Resources */ = {
294+
isa = PBXGroup;
295+
children = (
296+
A463D077285A5A8000929A3C /* OpenSans-Regular.ttf */,
297+
);
298+
path = Resources;
299+
sourceTree = "<group>";
300+
};
260301
A9C6C33CA0A11D6B033AAEA1 /* Frameworks */ = {
261302
isa = PBXGroup;
262303
children = (
@@ -383,6 +424,7 @@
383424
A463D0262858EBBD00929A3C /* LaunchScreen.storyboard in Resources */,
384425
A463D0232858EBBD00929A3C /* Assets.xcassets in Resources */,
385426
A463D05E2858F64B00929A3C /* FavoritesViewController.xib in Resources */,
427+
A463D078285A5A8000929A3C /* OpenSans-Regular.ttf in Resources */,
386428
A463D0542858F60C00929A3C /* HomeViewController.xib in Resources */,
387429
);
388430
runOnlyForDeploymentPostprocessing = 0;
@@ -516,7 +558,9 @@
516558
A463D0212858EBBB00929A3C /* AnimalsApp.xcdatamodeld in Sources */,
517559
A463D0572858F62600929A3C /* RegisterViewController.swift in Sources */,
518560
A463D0172858EBBB00929A3C /* AppDelegate.swift in Sources */,
561+
A463D071285A373800929A3C /* Animals.swift in Sources */,
519562
A463D04B2858F39000929A3C /* MainTabBarController.swift in Sources */,
563+
A463D074285A398900929A3C /* HomeViewModel.swift in Sources */,
520564
A463D05D2858F64B00929A3C /* FavoritesViewController.swift in Sources */,
521565
A463D06D285A22D200929A3C /* DetailViewController.swift in Sources */,
522566
A463D069285A1A5A00929A3C /* WebServices.swift in Sources */,
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"images" : [
3+
{
4+
"filename" : "imagePlaceholder.png",
5+
"idiom" : "universal",
6+
"scale" : "1x"
7+
},
8+
{
9+
"idiom" : "universal",
10+
"scale" : "2x"
11+
},
12+
{
13+
"idiom" : "universal",
14+
"scale" : "3x"
15+
}
16+
],
17+
"info" : {
18+
"author" : "xcode",
19+
"version" : 1
20+
}
21+
}
1.72 KB
Loading

AnimalsApp/AnimalsApp/DesignSystem/Images.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,15 @@ import UIKit
1010
extension UIImage {
1111
static let registerTabBar: UIImage? = UIImage(named: "registerBlack")
1212
static let registerSelectedTabBar: UIImage? = UIImage(named: "registerWhite")
13+
1314
static let favoritesTabBar: UIImage? = UIImage(named: "favoritesBlack")
1415
static let favoritesSelectedTabBar: UIImage? = UIImage(named: "favoritesWhite")
16+
1517
static let homeTabBar: UIImage? = UIImage(named: "homeBlack")
1618
static let homeSelectedTabBar: UIImage? = UIImage(named: "homeWhite")
19+
20+
static let favorite: UIImage? = UIImage(named: "favorited")
21+
static let notFavorite: UIImage? = UIImage(named: "addFavorite")
22+
23+
static let imagePlaceHolder: UIImage? = UIImage(named: "imagePlaceholder")
1724
}

AnimalsApp/AnimalsApp/Info.plist

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
33
<plist version="1.0">
4-
<dict/>
4+
<dict>
5+
<key>UIAppFonts</key>
6+
<array>
7+
<string></string>
8+
</array>
9+
<key>Item 0</key>
10+
<string>OpenSans-Regular.ttf</string>
11+
</dict>
512
</plist>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//
2+
// Items.swift
3+
// AnimalsApp
4+
//
5+
// Created by Laura Pinheiro Marson on 15/06/22.
6+
//
7+
8+
import Foundation
9+
10+
struct Animals: Codable {
11+
var items: [Animal]
12+
}
13+
14+
struct Animal: Codable {
15+
let id: String
16+
let name: String?
17+
let description: String?
18+
let age: Int?
19+
let species: String?
20+
let image: String?
21+
let created_at: String
22+
var updated_at: String
23+
var isFavorite: Bool? = false
24+
}
25+
26+
extension Animal {
27+
28+
var imageURL: URL {
29+
guard let imageURL = URL(string: self.image ?? "") else {
30+
return URL(fileURLWithPath: "")
31+
}
32+
return imageURL
33+
}
34+
}
212 KB
Binary file not shown.

AnimalsApp/AnimalsApp/Services/WebServices.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,30 @@
77

88
import Foundation
99
import Alamofire
10+
11+
protocol WebServicesContract: AnyObject {
12+
func fetchAnimals(completion: @escaping (Result<[Animal], Error>) -> ())
13+
func registerAnimal()
14+
}
15+
16+
class WebServices: WebServicesContract {
17+
let urlString = "https://bootcamp-ios-api.herokuapp.com/api/v1/animals"
18+
19+
func fetchAnimals(completion: @escaping (Result<[Animal], Error>) -> ()) {
20+
21+
AF.request(urlString)
22+
.validate()
23+
.responseDecodable(of: Animals.self) { (response) in
24+
switch response.result {
25+
case .success(let data):
26+
completion(.success(data.items))
27+
case .failure(let error):
28+
completion(.failure(error))
29+
}
30+
}
31+
}
32+
33+
func registerAnimal() {
34+
35+
}
36+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
//
2+
// HomeViewModel.swift
3+
// AnimalsApp
4+
//
5+
// Created by Laura Pinheiro Marson on 15/06/22.
6+
//
7+
8+
import Foundation
9+
import Combine
10+
11+
class HomeViewModel {
12+
private var webServices: WebServicesContract
13+
var animals = [Animal]()
14+
15+
init(webServices: WebServicesContract = WebServices()) {
16+
self.webServices = webServices
17+
}
18+
19+
func numberOfRows() -> Int {
20+
return animals.count
21+
}
22+
23+
func modelAt(_ index: Int) -> Animal {
24+
return animals[index]
25+
}
26+
27+
func getAllAnimals(completion: @escaping () -> ()) {
28+
webServices.fetchAnimals() { [weak self] (result) in
29+
switch result {
30+
case .success(let animals):
31+
self?.handleAnimalResponse(with: animals)
32+
case .failure(let error):
33+
//obs criar alerta
34+
print(error.localizedDescription)
35+
}
36+
37+
completion()
38+
}
39+
}
40+
41+
private func handleAnimalResponse(with animals: [Animal]) {
42+
self.animals = animals.filter { animal in
43+
if let name = animal.name {
44+
return !name.isEmpty
45+
}
46+
return false
47+
}
48+
}
49+
}
50+
51+
class HomeViewModel2 {
52+
private var webServices: WebServicesContract
53+
var animals = [Animal]()
54+
55+
enum ViewState {
56+
case idle
57+
case loading
58+
case error(Error)
59+
case loaded([Animal])
60+
}
61+
62+
@Published
63+
var viewState: ViewState = .idle
64+
65+
init(webServices: WebServicesContract = WebServices()) {
66+
self.webServices = webServices
67+
}
68+
69+
func numberOfRows() -> Int {
70+
return animals.count
71+
}
72+
73+
func modelAt(_ index: Int) -> Animal {
74+
return animals[index]
75+
}
76+
77+
func getAllAnimals(completion: @escaping () -> ()) {
78+
webServices.fetchAnimals() { [weak self] (result) in
79+
switch result {
80+
case .success(let animals):
81+
self?.handleAnimalResponse(with: animals)
82+
case .failure(let error):
83+
//obs criar alerta
84+
print(error.localizedDescription)
85+
self?.viewState = .error(error)
86+
}
87+
88+
completion()
89+
}
90+
}
91+
92+
private func handleAnimalResponse(with animals: [Animal]) {
93+
self.animals = animals.filter { animal in
94+
if let name = animal.name {
95+
return !name.isEmpty
96+
}
97+
return false
98+
}
99+
viewState = .loaded(animals)
100+
}
101+
}

AnimalsApp/AnimalsApp/Views/HomeViewController/AnimalTableViewCell.swift

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)