@@ -9,93 +9,13 @@ import UIKit
99
1010class FavoritesViewController : UIViewController {
1111
12- var favoritesVM : FavoritesViewModel ?
13-
14- @IBOutlet weak var tableView : UITableView !
15-
1612 override func viewDidLoad( ) {
1713 super. viewDidLoad ( )
18- favoritesVM = FavoritesViewModel ( )
1914
20- tableView. dataSource = self
21- tableView. delegate = self
22-
23- tableView. register ( UINib ( nibName: " AnimalTableViewCell " , bundle: nil ) , forCellReuseIdentifier: " Animal " )
24-
25- setNavigationItems ( )
26- favoritesVM? . getFavoriteAnimals { [ weak self] in self ? . tableView. reloadData ( )
2715 }
28- }
29-
30- override func viewWillAppear( _ animated: Bool ) {
31- super. viewWillAppear ( animated)
32- favoritesVM? . getFavoriteAnimals { [ weak self] in self ? . tableView. reloadData ( )
33- }
34- }
35-
36- private func setNavigationItems( ) {
37- title = " Favoritos "
38-
39- let appearance = UINavigationBarAppearance ( )
40- appearance. configureWithOpaqueBackground ( )
41- appearance. titleTextAttributes = [
42- NSAttributedString . Key. foregroundColor: UIColor . blueTextColor ?? UIColor . blue,
43- NSAttributedString . Key. font: UIFont ( name: " OpenSans " , size: 20 ) ?? UIFont . systemFont ( ofSize: 20 ) ]
44- navigationController? . navigationBar. standardAppearance = appearance
45- navigationController? . navigationBar. scrollEdgeAppearance = navigationController? . navigationBar. standardAppearance
46-
47- navigationItem. backButtonTitle = " "
48- }
4916
5017}
5118
52- // MARK: TableView Data Source
53- extension FavoritesViewController : UITableViewDataSource {
54- func tableView( _ tableView: UITableView , numberOfRowsInSection section: Int ) -> Int {
55- return favoritesVM? . numberOfRows ( ) ?? 0
56- }
57-
58- func tableView( _ tableView: UITableView , cellForRowAt indexPath: IndexPath ) -> UITableViewCell {
59-
60- guard let cell = tableView. dequeueReusableCell ( withIdentifier: " Animal " , for: indexPath) as? AnimalTableViewCell else {
61- return UITableViewCell ( )
62- }
63-
64- cell. animal = favoritesVM? . modelAt ( indexPath. row)
65- cell. index = indexPath. row
66- cell. delegate = self
67- cell. configure ( )
68-
69- return cell
70- }
71-
72- }
73-
74- // MARK: TableView Delegate
75- extension FavoritesViewController : UITableViewDelegate {
76-
77- func tableView( _ tableView: UITableView , didSelectRowAt indexPath: IndexPath ) {
78- let detailVC = DetailViewController ( nibName: " DetailViewController " , bundle: nil )
79-
80- let animal = favoritesVM? . modelAt ( indexPath. row)
81- detailVC. animal = animal
8219
83- navigationController? . pushViewController ( detailVC, animated: true )
84-
85- tableView. deselectRow ( at: indexPath, animated: true )
86- }
87-
88- }
8920
90- // MARK: Action Delegate Protocol
91- extension FavoritesViewController : ActionDelegateProtocol {
92- func addFavoriteTapped( at index: Int , with image: Data ) {
93- }
94-
95- func removeFavoriteTapped( at index: Int ) {
96- favoritesVM? . removeFavorite ( at: index) { [ weak self] in
97- self ? . tableView. reloadData ( )
98- }
99- }
100- }
10121
0 commit comments