Skip to content

Commit c168420

Browse files
committed
create error alerts
1 parent d61e735 commit c168420

6 files changed

Lines changed: 70 additions & 16 deletions

File tree

AnimalsApp/AnimalsApp.xcodeproj/project.pbxproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
5CA996F8285A98FB00FF5D79 /* RegisterViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5CA996F7285A98FB00FF5D79 /* RegisterViewModel.swift */; };
1313
A2A6AE83CC0E1DAC784999B1 /* Pods_AnimalsAppTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 290D574622CE042567539136 /* Pods_AnimalsAppTests.framework */; };
1414
A458A086285E58CC0057BC34 /* FavoritesViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = A458A085285E58CC0057BC34 /* FavoritesViewModel.swift */; };
15+
A458A08C2860BD2D0057BC34 /* Alert+UIViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A458A08B2860BD2D0057BC34 /* Alert+UIViewController.swift */; };
1516
A463D0172858EBBB00929A3C /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = A463D0162858EBBB00929A3C /* AppDelegate.swift */; };
1617
A463D0212858EBBB00929A3C /* AnimalsApp.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = A463D01F2858EBBB00929A3C /* AnimalsApp.xcdatamodeld */; };
1718
A463D0232858EBBD00929A3C /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = A463D0222858EBBD00929A3C /* Assets.xcassets */; };
@@ -70,6 +71,7 @@
7071
9D1DC7AE05F709A3F0E450DA /* Pods_AnimalsApp.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_AnimalsApp.framework; sourceTree = BUILT_PRODUCTS_DIR; };
7172
A1E75A013A1B19FF9D2B114B /* Pods_AnimalsApp_AnimalsAppUITests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_AnimalsApp_AnimalsAppUITests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
7273
A458A085285E58CC0057BC34 /* FavoritesViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FavoritesViewModel.swift; sourceTree = "<group>"; };
74+
A458A08B2860BD2D0057BC34 /* Alert+UIViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Alert+UIViewController.swift"; sourceTree = "<group>"; };
7375
A463D0132858EBBB00929A3C /* AnimalsApp.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = AnimalsApp.app; sourceTree = BUILT_PRODUCTS_DIR; };
7476
A463D0162858EBBB00929A3C /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
7577
A463D0202858EBBB00929A3C /* AnimalsApp.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = AnimalsApp.xcdatamodel; sourceTree = "<group>"; };
@@ -144,6 +146,14 @@
144146
path = Pods;
145147
sourceTree = "<group>";
146148
};
149+
A458A08A2860BD1A0057BC34 /* Extensions */ = {
150+
isa = PBXGroup;
151+
children = (
152+
A458A08B2860BD2D0057BC34 /* Alert+UIViewController.swift */,
153+
);
154+
path = Extensions;
155+
sourceTree = "<group>";
156+
};
147157
A463D00A2858EBBA00929A3C = {
148158
isa = PBXGroup;
149159
children = (
@@ -169,6 +179,7 @@
169179
A463D0152858EBBB00929A3C /* AnimalsApp */ = {
170180
isa = PBXGroup;
171181
children = (
182+
A458A08A2860BD1A0057BC34 /* Extensions */,
172183
A463D076285A59B500929A3C /* Resources */,
173184
A463D067285A1A4300929A3C /* Services */,
174185
A463D06028590CB700929A3C /* DesignSystem */,
@@ -569,6 +580,7 @@
569580
buildActionMask = 2147483647;
570581
files = (
571582
A463D06228590D0300929A3C /* Images.swift in Sources */,
583+
A458A08C2860BD2D0057BC34 /* Alert+UIViewController.swift in Sources */,
572584
A463D07C285B9B6300929A3C /* CoreData.swift in Sources */,
573585
A463D065285953F900929A3C /* AnimalTableViewCell.swift in Sources */,
574586
A463D0212858EBBB00929A3C /* AnimalsApp.xcdatamodeld in Sources */,
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//
2+
// Alert+UIViewController.swift
3+
// AnimalsApp
4+
//
5+
// Created by Laura Pinheiro Marson on 20/06/22.
6+
//
7+
8+
import UIKit
9+
10+
protocol ShowAlertProtocol: AnyObject {
11+
func alert(_ alert: UIAlertController)
12+
}
13+
14+
extension UIViewController {
15+
16+
func fetchAlert(title: String, message: String) -> UIAlertController {
17+
let alert = UIAlertController(title: title,
18+
message: message,
19+
preferredStyle: .alert)
20+
alert.addAction(UIAlertAction(title: "Ok", style: .default))
21+
return alert
22+
}
23+
}

AnimalsApp/AnimalsApp/View Models/HomeViewModel.swift

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,16 @@ class HomeViewModel {
2626
return animals[index]
2727
}
2828

29-
func getAllAnimals(completion: @escaping () -> ()) {
29+
func getAllAnimals(completion: @escaping (Result<Void, Error>) -> ()) {
3030
webServices.fetchAnimals() { [weak self] (result) in
3131
switch result {
3232
case .success(let animals):
3333
self?.handleAnimalResponse(with: animals)
3434
self?.setFavorite()
35+
completion(.success(()))
3536
case .failure(let error):
36-
//obs criar alerta
37-
print(error.localizedDescription)
37+
completion(.failure(error))
3838
}
39-
40-
completion()
4139
}
4240
}
4341

@@ -84,6 +82,6 @@ class HomeViewModel {
8482

8583
extension HomeViewModel: UpdateDelegateProtocol {
8684
func updateFavoriteAnimals() {
87-
// setFavorite()
85+
setFavorite()
8886
}
8987
}

AnimalsApp/AnimalsApp/View Models/RegisterViewModel.swift

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,16 @@ struct RegisterViewModel {
1414
self.webServices = webServices
1515
}
1616

17-
func registerAnimal(name: String, description: String, age: Int, species: String, image: String, completion: @escaping (() -> Void)) {
17+
func registerAnimal(name: String, description: String, age: Int, species: String, image: String, completion: @escaping ((Result<Void, Error>) -> Void)) {
1818

1919
let newAnimal = Animal(name: name, description: description, age: age, species: species, image: image)
2020

2121
webServices.registerAnimal(with: newAnimal) { (result) in
2222
switch result {
2323
case .success:
24-
completion()
24+
completion(.success(()))
2525
case .failure(let error):
26-
//obs criar alerta
27-
print(error.localizedDescription)
28-
completion()
26+
completion(.failure(error))
2927
}
3028
}
3129
}

AnimalsApp/AnimalsApp/Views/HomeViewController/HomeViewController.swift

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,20 @@ class HomeViewController: UIViewController {
6565
}
6666

6767
private func populateTableView() {
68-
homeVM.getAllAnimals { [weak self] in
69-
DispatchQueue.main.async {
70-
self?.tableView.reloadData()
68+
homeVM.getAllAnimals { [weak self] (result) in
69+
70+
switch result {
71+
case .success():
72+
DispatchQueue.main.async {
73+
self?.tableView.reloadData()
74+
self?.loadingView.stopAnimating()
75+
}
76+
77+
case .failure(let error):
78+
print(error.localizedDescription)
7179
self?.loadingView.stopAnimating()
80+
guard let alert = self?.fetchAlert(title: "Oops...", message: "Não foi possível carregar os animais") else { return }
81+
self?.present(alert, animated: true)
7282
}
7383
}
7484
}

AnimalsApp/AnimalsApp/Views/RegisterViewController/RegisterViewController.swift

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,21 @@ class RegisterViewController: UIViewController {
4646
showAlerts(alertTitle: "Erro", alertMessage: "Preencha todos os campos")
4747
return }
4848

49-
registerVM.registerAnimal(name: name, description: description, age: age, species: species, image: image) { [weak self] in
50-
self?.registerSucceeded()
49+
registerVM.registerAnimal(name: name, description: description, age: age, species: species, image: image) { [weak self] (result) in
50+
51+
switch result {
52+
case .success:
53+
self?.registerSucceeded()
54+
55+
case .failure(let error):
56+
print(error.localizedDescription)
57+
guard let alert = self?.fetchAlert(title: "Oops...", message: "Não foi possível cadastrar o novo animal") else { return }
58+
self?.present(alert, animated: true) {
59+
self?.activityIndicator.stopAnimating()
60+
self?.buttonRegister.isUserInteractionEnabled = true
61+
self?.buttonRegister.backgroundColor = .purpleButtonColor
62+
}
63+
}
5164
}
5265
}
5366

0 commit comments

Comments
 (0)