Skip to content

Commit 8de976e

Browse files
committed
conform homeVM and favoritesVM to coredata delegate protocol
1 parent e0edbb1 commit 8de976e

5 files changed

Lines changed: 45 additions & 11 deletions

File tree

AnimalsApp/AnimalsApp/Services/CoreData.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import UIKit
99
import CoreData
1010

1111
protocol CoreDataContract: AnyObject {
12+
var delegate: [UpdateDelegateProtocol] { get set }
1213
var favoriteAnimals: [FavoriteAnimal] { get set }
1314
func loadFavoriteAnimals(completion: @escaping () -> ())
1415
func isFavorite(id: String) -> Bool
@@ -26,10 +27,10 @@ class CoreData: CoreDataContract {
2627

2728
private var managedContext: NSManagedObjectContext?
2829

29-
var delegate: [UpdateDelegateProtocol]?
30+
var delegate = [UpdateDelegateProtocol]()
3031
var favoriteAnimals = [FavoriteAnimal]() {
3132
didSet {
32-
delegate?.forEach { delegate in
33+
delegate.forEach { delegate in
3334
delegate.updateFavoriteAnimals()
3435
}
3536
}

AnimalsApp/AnimalsApp/View Models/FavoritesViewModel.swift

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,24 @@
88
import Foundation
99

1010
class FavoritesViewModel {
11-
var favoriteAnimals = [Animal]()
1211
let coreData: CoreDataContract
1312

13+
var favoriteAnimals = [Animal]()
14+
private var newCoreDataChanges = true
15+
1416
init(coreData: CoreDataContract = CoreData.shared) {
1517
self.coreData = coreData
18+
coreData.delegate.append(self)
1619
}
1720

1821
func getFavoriteAnimals(completion: @escaping () -> Void) {
19-
22+
guard newCoreDataChanges else { return }
23+
2024
favoriteAnimals = coreData.favoriteAnimals.map { favoriteAnimal in
2125
Animal(id: favoriteAnimal.id, name: favoriteAnimal.name, description: favoriteAnimal.descript, age: Int(favoriteAnimal.age), species: favoriteAnimal.species, isFavorite: true, imageData: favoriteAnimal.image)
2226
}
2327

28+
newCoreDataChanges = false
2429
completion()
2530
}
2631

@@ -31,4 +36,18 @@ class FavoritesViewModel {
3136
func modelAt(_ index: Int) -> Animal {
3237
return favoriteAnimals[index]
3338
}
39+
40+
func removeFavorite(at index: Int, completion: @escaping () -> Void) {
41+
guard let id = favoriteAnimals[index].id else { return }
42+
favoriteAnimals.remove(at: index)
43+
coreData.removeFavorite(id: id)
44+
completion()
45+
}
46+
}
47+
48+
extension FavoritesViewModel: UpdateDelegateProtocol {
49+
func updateFavoriteAnimals() {
50+
newCoreDataChanges = true
51+
}
52+
3453
}

AnimalsApp/AnimalsApp/View Models/HomeViewModel.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class HomeViewModel {
1515
init(webServices: WebServicesContract = WebServices(), coreData: CoreDataContract = CoreData.shared) {
1616
self.webServices = webServices
1717
self.coreData = coreData
18+
coreData.delegate.append(self)
1819
}
1920

2021
func numberOfRows() -> Int {
@@ -78,3 +79,9 @@ class HomeViewModel {
7879
coreData.saveChanges()
7980
}
8081
}
82+
83+
extension HomeViewModel: UpdateDelegateProtocol {
84+
func updateFavoriteAnimals() {
85+
// setFavorite()
86+
}
87+
}

AnimalsApp/AnimalsApp/Views/Components/AnimalTableViewCell.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,10 @@ class AnimalTableViewCell: UITableViewCell {
6767
self.animal?.isFavorite = true
6868
}
6969

70-
if let index = index, let image = SDImageCache.shared.imageFromDiskCache(forKey: animal.imageURL.absoluteString) {
71-
let imageData = image.jpegData(compressionQuality: 0.9)
70+
let image = SDImageCache.shared.imageFromDiskCache(forKey: animal.imageURL.absoluteString)
71+
72+
if let index = index {
73+
let imageData = image?.jpegData(compressionQuality: 0.9)
7274
delegate?.favoriteButtonTapped(at: index, with: imageData ?? Data())
7375
}
7476
}

AnimalsApp/AnimalsApp/Views/FavoritesViewController/FavoritesViewController.swift

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,27 @@ import UIKit
99

1010
class FavoritesViewController: UIViewController {
1111

12-
var favoritesVM = FavoritesViewModel()
12+
var favoritesVM: FavoritesViewModel?
1313

1414
@IBOutlet weak var tableView: UITableView!
1515

1616
override func viewDidLoad() {
1717
super.viewDidLoad()
18+
favoritesVM = FavoritesViewModel()
1819

1920
tableView.dataSource = self
2021
tableView.delegate = self
2122

2223
tableView.register(UINib(nibName: "AnimalTableViewCell", bundle: nil), forCellReuseIdentifier: "Animal")
2324

2425
setNavigationItems()
26+
favoritesVM?.getFavoriteAnimals { [weak self] in self?.tableView.reloadData()
27+
}
2528
}
2629

2730
override func viewWillAppear(_ animated: Bool) {
2831
super.viewWillAppear(animated)
29-
favoritesVM.getFavoriteAnimals() { [weak self] in self?.tableView.reloadData()
32+
favoritesVM?.getFavoriteAnimals { [weak self] in self?.tableView.reloadData()
3033
}
3134
}
3235

@@ -47,7 +50,7 @@ class FavoritesViewController: UIViewController {
4750
// MARK: TableView Data Source
4851
extension FavoritesViewController: UITableViewDataSource {
4952
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
50-
return favoritesVM.numberOfRows()
53+
return favoritesVM?.numberOfRows() ?? 0
5154
}
5255

5356
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
@@ -56,7 +59,7 @@ extension FavoritesViewController: UITableViewDataSource {
5659
return UITableViewCell()
5760
}
5861

59-
cell.animal = favoritesVM.modelAt(indexPath.row)
62+
cell.animal = favoritesVM?.modelAt(indexPath.row)
6063
cell.index = indexPath.row
6164
cell.delegate = self
6265
cell.configure()
@@ -84,7 +87,9 @@ extension FavoritesViewController: UITableViewDelegate {
8487
// MARK: Action Delegate Protocol
8588
extension FavoritesViewController: ActionDelegateProtocol {
8689
func favoriteButtonTapped(at index: Int, with image: Data) {
87-
// homeVM.addOrRemoveFavorite(at: index, with: image)
90+
favoritesVM?.removeFavorite(at: index) { [weak self] in
91+
self?.tableView.reloadData()
92+
}
8893
}
8994
}
9095

0 commit comments

Comments
 (0)