Skip to content

Commit 566c14d

Browse files
committed
change favorite button pressed protocol
1 parent 8de976e commit 566c14d

4 files changed

Lines changed: 26 additions & 19 deletions

File tree

AnimalsApp/AnimalsApp/View Models/HomeViewModel.swift

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,16 @@ class HomeViewModel {
5959
}
6060
}
6161

62-
func addOrRemoveFavorite(at index: Int, with image: Data) {
62+
func removeFavorite(at index: Int) {
63+
guard let id = animals[index].id else { return }
64+
coreData.removeFavorite(id: id)
65+
animals[index].isFavorite = false
66+
}
67+
68+
func addFavorite(at index: Int, with image: Data) {
6369
animals[index].imageData = image
64-
65-
guard let isFavorite = animals[index].isFavorite,
66-
let id = animals[index].id else { return }
67-
isFavorite ? coreData.removeFavorite(id: id) : coreData.addFavorite(animals[index])
68-
69-
animals[index].isFavorite = !isFavorite
70+
coreData.addFavorite(animals[index])
71+
animals[index].isFavorite = true
7072
}
7173

7274
func loadFavorites(completion: @escaping () -> ()) {

AnimalsApp/AnimalsApp/Views/Components/AnimalTableViewCell.swift

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

1111
protocol ActionDelegateProtocol: AnyObject {
12-
func favoriteButtonTapped(at index: Int, with image: Data)
12+
func addFavoriteTapped(at index: Int, with image: Data)
13+
func removeFavoriteTapped(at index: Int)
1314
}
1415

1516
class AnimalTableViewCell: UITableViewCell {
@@ -57,21 +58,18 @@ class AnimalTableViewCell: UITableViewCell {
5758
}
5859

5960
@IBAction func favoritePressed(_ sender: UIButton) {
60-
guard let animal = animal else { return }
61+
guard let animal = animal, let index = index else { return }
6162

6263
if animal.isFavorite ?? false {
6364
sender.setImage(.notFavorite, for: .normal)
6465
self.animal?.isFavorite = false
66+
delegate?.removeFavoriteTapped(at: index)
6567
} else {
6668
sender.setImage(.favorite, for: .normal)
6769
self.animal?.isFavorite = true
68-
}
69-
70-
let image = SDImageCache.shared.imageFromDiskCache(forKey: animal.imageURL.absoluteString)
71-
72-
if let index = index {
70+
let image = SDImageCache.shared.imageFromDiskCache(forKey: animal.imageURL.absoluteString)
7371
let imageData = image?.jpegData(compressionQuality: 0.9)
74-
delegate?.favoriteButtonTapped(at: index, with: imageData ?? Data())
72+
delegate?.addFavoriteTapped(at: index, with: imageData ?? Data())
7573
}
7674
}
7775
}

AnimalsApp/AnimalsApp/Views/FavoritesViewController/FavoritesViewController.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,10 @@ extension FavoritesViewController: UITableViewDelegate {
8686

8787
// MARK: Action Delegate Protocol
8888
extension FavoritesViewController: ActionDelegateProtocol {
89-
func favoriteButtonTapped(at index: Int, with image: Data) {
89+
func addFavoriteTapped(at index: Int, with image: Data) {
90+
}
91+
92+
func removeFavoriteTapped(at index: Int) {
9093
favoritesVM?.removeFavorite(at: index) { [weak self] in
9194
self?.tableView.reloadData()
9295
}

AnimalsApp/AnimalsApp/Views/HomeViewController/HomeViewController.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,11 @@ extension HomeViewController: UITableViewDelegate {
124124

125125
// MARK: Action Delegate Protocol
126126
extension HomeViewController: ActionDelegateProtocol {
127-
func favoriteButtonTapped(at index: Int, with image: Data) {
128-
homeVM.addOrRemoveFavorite(at: index, with: image)
129-
}
127+
func addFavoriteTapped(at index: Int, with image: Data) {
128+
homeVM.addFavorite(at: index, with: image)
129+
}
130+
131+
func removeFavoriteTapped(at index: Int) {
132+
homeVM.removeFavorite(at: index)
133+
}
130134
}

0 commit comments

Comments
 (0)