Skip to content

Commit 2c08c80

Browse files
committed
final changes
1 parent a2b6680 commit 2c08c80

7 files changed

Lines changed: 31 additions & 22 deletions

File tree

AnimalsApp/AnimalsApp/View Models/FavoritesViewModel.swift

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,28 @@
88
import Foundation
99

1010
class FavoritesViewModel {
11+
//MARK: Properties
1112
let coreData: CoreDataContract
1213

1314
var favoriteAnimals = [Animal]()
1415
private var newCoreDataChanges = true
1516

17+
//MARK: Initialization
1618
init(coreData: CoreDataContract = CoreData.shared) {
1719
self.coreData = coreData
1820
coreData.delegate.append(self)
1921
}
2022

23+
//MARK: Methods
24+
func numberOfRows() -> Int {
25+
return favoriteAnimals.count
26+
}
27+
28+
func modelAt(_ index: Int) -> Animal {
29+
return favoriteAnimals[index]
30+
}
31+
32+
//MARK: Core Data Methods
2133
func getFavoriteAnimals(completion: @escaping () -> Void) {
2234
guard newCoreDataChanges else { return }
2335

@@ -29,14 +41,6 @@ class FavoritesViewModel {
2941
completion()
3042
}
3143

32-
func numberOfRows() -> Int {
33-
return favoriteAnimals.count
34-
}
35-
36-
func modelAt(_ index: Int) -> Animal {
37-
return favoriteAnimals[index]
38-
}
39-
4044
func removeFavorite(at index: Int, completion: @escaping () -> Void) {
4145
guard let id = favoriteAnimals[index].id else { return }
4246
favoriteAnimals.remove(at: index)
@@ -45,6 +49,7 @@ class FavoritesViewModel {
4549
}
4650
}
4751

52+
//MARK: Update Delegate Protocol
4853
extension FavoritesViewModel: UpdateDelegateProtocol {
4954
func updateFavoriteAnimals() {
5055
newCoreDataChanges = true

AnimalsApp/AnimalsApp/View Models/HomeViewModel.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,19 @@
88
import Foundation
99

1010
class HomeViewModel {
11+
//MARK: Properties
1112
private var webServices: WebServicesContract
1213
private var coreData: CoreDataContract
1314
var animals = [Animal]()
1415

16+
//MARK: Initialization
1517
init(webServices: WebServicesContract = WebServices(), coreData: CoreDataContract = CoreData.shared) {
1618
self.webServices = webServices
1719
self.coreData = coreData
1820
coreData.delegate.append(self)
1921
}
2022

23+
//MARK: Methods
2124
func numberOfRows() -> Int {
2225
return animals.count
2326
}
@@ -80,6 +83,7 @@ class HomeViewModel {
8083
}
8184
}
8285

86+
//MARK: Update Delegate Protocol
8387
extension HomeViewModel: UpdateDelegateProtocol {
8488
func updateFavoriteAnimals() {
8589
setFavorite()

AnimalsApp/AnimalsApp/View Models/RegisterViewModel.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@
88
import Foundation
99

1010
struct RegisterViewModel {
11+
//MARK: Properties
1112
private var webServices: WebServicesContract
1213

14+
//MARK: Initialization
1315
init(webServices: WebServicesContract = WebServices()) {
1416
self.webServices = webServices
1517
}
1618

19+
//MARK: Methods
1720
func registerAnimal(name: String, description: String, age: Int, species: String, image: String, completion: @escaping ((Result<Void, Error>) -> Void)) {
1821

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

AnimalsApp/AnimalsApp/Views/Components/AnimalTableViewCell.swift

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,19 @@ protocol ActionDelegateProtocol: AnyObject {
1414
}
1515

1616
class AnimalTableViewCell: UITableViewCell {
17+
18+
// MARK: Properties
1719
weak var delegate: ActionDelegateProtocol?
1820
var animal: Animal?
1921
var index: Int?
2022

23+
// MARK: Outlets
2124
@IBOutlet weak var animalImage: UIImageView!
2225
@IBOutlet weak var nameLabel: UILabel!
2326
@IBOutlet weak var descriptionLabel: UILabel!
2427
@IBOutlet weak var favoriteButton: UIButton!
2528

26-
override func awakeFromNib() {
27-
super.awakeFromNib()
28-
// Initialization code
29-
}
30-
31-
override func setSelected(_ selected: Bool, animated: Bool) {
32-
super.setSelected(selected, animated: animated)
33-
34-
// Configure the view for the selected state
35-
}
36-
29+
// MARK: Methods
3730
func configure() {
3831
guard let animal = animal else { return }
3932

@@ -57,6 +50,7 @@ class AnimalTableViewCell: UITableViewCell {
5750
animal.isFavorite ?? false ? favoriteButton.setImage(.favorite, for: .normal) : favoriteButton.setImage(.notFavorite, for: .normal)
5851
}
5952

53+
// MARK: Actions
6054
@IBAction func favoritePressed(_ sender: UIButton) {
6155
guard let animal = animal, let index = index else { return }
6256

AnimalsApp/AnimalsApp/Views/DetailViewController/DetailViewController.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class DetailViewController: UIViewController {
2525
setupDetails()
2626
}
2727

28+
// MARK: Methods
2829
private func setupDetails() {
2930
guard let animal = animal else { return }
3031

AnimalsApp/AnimalsApp/Views/HomeViewController/HomeViewController.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ class HomeViewController: UIViewController {
116116
}
117117
}
118118

119-
// MARK: TableView Data Source
119+
// MARK: - TableView Data Source
120120
extension HomeViewController: UITableViewDataSource {
121121
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
122122
return homeVM.numberOfRows()
@@ -138,7 +138,7 @@ extension HomeViewController: UITableViewDataSource {
138138

139139
}
140140

141-
// MARK: TableView Delegate
141+
// MARK: - TableView Delegate
142142
extension HomeViewController: UITableViewDelegate {
143143

144144
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
@@ -154,7 +154,7 @@ extension HomeViewController: UITableViewDelegate {
154154

155155
}
156156

157-
// MARK: Action Delegate Protocol
157+
// MARK: - Action Delegate Protocol
158158
extension HomeViewController: ActionDelegateProtocol {
159159
func addFavoriteTapped(at index: Int, with image: Data) {
160160
homeVM.addFavorite(at: index, with: image)

AnimalsApp/AnimalsApp/Views/TabBarController/MainTabBarController.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ import UIKit
99

1010
class MainTabBarController: UITabBarController {
1111

12+
// MARK: Overrides
1213
override func viewDidLoad() {
1314
super.viewDidLoad()
1415

1516
setupViewControllers()
1617
setupTabBar()
1718
}
1819

20+
// MARK: Methods
1921
func setupViewControllers() {
2022

2123
let homeVC = UINavigationController(rootViewController: HomeViewController())

0 commit comments

Comments
 (0)