Skip to content

Commit 7fba581

Browse files
committed
add favorite view controller
1 parent 2e69d26 commit 7fba581

3 files changed

Lines changed: 96 additions & 8 deletions

File tree

AnimalsApp/AnimalsApp.xcodeproj/project.pbxproj

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
A458A08C2860BD2D0057BC34 /* Alert+UIViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A458A08B2860BD2D0057BC34 /* Alert+UIViewController.swift */; };
1616
A458A0922860F7C40057BC34 /* success.json in Resources */ = {isa = PBXBuildFile; fileRef = A458A0912860F7C40057BC34 /* success.json */; };
1717
A458A094286128FF0057BC34 /* empty.json in Resources */ = {isa = PBXBuildFile; fileRef = A458A093286128FF0057BC34 /* empty.json */; };
18+
A458A09E28628B600057BC34 /* FavoritesViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A458A09C28628B600057BC34 /* FavoritesViewController.swift */; };
19+
A458A09F28628B600057BC34 /* FavoritesViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = A458A09D28628B600057BC34 /* FavoritesViewController.xib */; };
1820
A463D0172858EBBB00929A3C /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = A463D0162858EBBB00929A3C /* AppDelegate.swift */; };
1921
A463D0212858EBBB00929A3C /* AnimalsApp.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = A463D01F2858EBBB00929A3C /* AnimalsApp.xcdatamodeld */; };
2022
A463D0232858EBBD00929A3C /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = A463D0222858EBBD00929A3C /* Assets.xcassets */; };
@@ -28,8 +30,6 @@
2830
A463D0542858F60C00929A3C /* HomeViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = A463D0522858F60C00929A3C /* HomeViewController.xib */; };
2931
A463D0572858F62600929A3C /* RegisterViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A463D0552858F62600929A3C /* RegisterViewController.swift */; };
3032
A463D0582858F62600929A3C /* RegisterViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = A463D0562858F62600929A3C /* RegisterViewController.xib */; };
31-
A463D05D2858F64B00929A3C /* FavoritesViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A463D05B2858F64B00929A3C /* FavoritesViewController.swift */; };
32-
A463D05E2858F64B00929A3C /* FavoritesViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = A463D05C2858F64B00929A3C /* FavoritesViewController.xib */; };
3333
A463D06228590D0300929A3C /* Images.swift in Sources */ = {isa = PBXBuildFile; fileRef = A463D06128590D0300929A3C /* Images.swift */; };
3434
A463D065285953F900929A3C /* AnimalTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = A463D063285953F900929A3C /* AnimalTableViewCell.swift */; };
3535
A463D066285953F900929A3C /* AnimalTableViewCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = A463D064285953F900929A3C /* AnimalTableViewCell.xib */; };
@@ -76,6 +76,8 @@
7676
A458A08B2860BD2D0057BC34 /* Alert+UIViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Alert+UIViewController.swift"; sourceTree = "<group>"; };
7777
A458A0912860F7C40057BC34 /* success.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = success.json; sourceTree = "<group>"; };
7878
A458A093286128FF0057BC34 /* empty.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = empty.json; sourceTree = "<group>"; };
79+
A458A09C28628B600057BC34 /* FavoritesViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FavoritesViewController.swift; sourceTree = "<group>"; };
80+
A458A09D28628B600057BC34 /* FavoritesViewController.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = FavoritesViewController.xib; sourceTree = "<group>"; };
7981
A463D0132858EBBB00929A3C /* AnimalsApp.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = AnimalsApp.app; sourceTree = BUILT_PRODUCTS_DIR; };
8082
A463D0162858EBBB00929A3C /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
8183
A463D0202858EBBB00929A3C /* AnimalsApp.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = AnimalsApp.xcdatamodel; sourceTree = "<group>"; };
@@ -93,8 +95,6 @@
9395
A463D0522858F60C00929A3C /* HomeViewController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = HomeViewController.xib; sourceTree = "<group>"; };
9496
A463D0552858F62600929A3C /* RegisterViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RegisterViewController.swift; sourceTree = "<group>"; };
9597
A463D0562858F62600929A3C /* RegisterViewController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = RegisterViewController.xib; sourceTree = "<group>"; };
96-
A463D05B2858F64B00929A3C /* FavoritesViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FavoritesViewController.swift; sourceTree = "<group>"; };
97-
A463D05C2858F64B00929A3C /* FavoritesViewController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = FavoritesViewController.xib; sourceTree = "<group>"; };
9898
A463D06128590D0300929A3C /* Images.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Images.swift; sourceTree = "<group>"; };
9999
A463D063285953F900929A3C /* AnimalTableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AnimalTableViewCell.swift; sourceTree = "<group>"; };
100100
A463D064285953F900929A3C /* AnimalTableViewCell.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = AnimalTableViewCell.xib; sourceTree = "<group>"; };
@@ -262,8 +262,8 @@
262262
A463D0502858F4DE00929A3C /* FavoritesViewController */ = {
263263
isa = PBXGroup;
264264
children = (
265-
A463D05B2858F64B00929A3C /* FavoritesViewController.swift */,
266-
A463D05C2858F64B00929A3C /* FavoritesViewController.xib */,
265+
A458A09C28628B600057BC34 /* FavoritesViewController.swift */,
266+
A458A09D28628B600057BC34 /* FavoritesViewController.xib */,
267267
);
268268
path = FavoritesViewController;
269269
sourceTree = "<group>";
@@ -470,10 +470,10 @@
470470
A458A094286128FF0057BC34 /* empty.json in Resources */,
471471
A463D066285953F900929A3C /* AnimalTableViewCell.xib in Resources */,
472472
A463D06E285A22D300929A3C /* DetailViewController.xib in Resources */,
473+
A458A09F28628B600057BC34 /* FavoritesViewController.xib in Resources */,
473474
A463D0262858EBBD00929A3C /* LaunchScreen.storyboard in Resources */,
474475
A458A0922860F7C40057BC34 /* success.json in Resources */,
475476
A463D0232858EBBD00929A3C /* Assets.xcassets in Resources */,
476-
A463D05E2858F64B00929A3C /* FavoritesViewController.xib in Resources */,
477477
A463D078285A5A8000929A3C /* OpenSans-Regular.ttf in Resources */,
478478
A463D0542858F60C00929A3C /* HomeViewController.xib in Resources */,
479479
);
@@ -613,10 +613,10 @@
613613
A463D071285A373800929A3C /* Animals.swift in Sources */,
614614
A458A086285E58CC0057BC34 /* FavoritesViewModel.swift in Sources */,
615615
A463D04B2858F39000929A3C /* MainTabBarController.swift in Sources */,
616+
A458A09E28628B600057BC34 /* FavoritesViewController.swift in Sources */,
616617
A463D074285A398900929A3C /* HomeViewModel.swift in Sources */,
617618
A463D07A285B8D8E00929A3C /* Colors.swift in Sources */,
618619
5CA996F8285A98FB00FF5D79 /* RegisterViewModel.swift in Sources */,
619-
A463D05D2858F64B00929A3C /* FavoritesViewController.swift in Sources */,
620620
A463D06D285A22D200929A3C /* DetailViewController.swift in Sources */,
621621
A463D07E285CF28200929A3C /* String.swift in Sources */,
622622
A463D069285A1A5A00929A3C /* WebServices.swift in Sources */,

AnimalsApp/AnimalsApp/Views/FavoritesViewController/FavoritesViewController.swift

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

1010
class FavoritesViewController: UIViewController {
1111

12+
var favoritesVM: FavoritesViewModel?
13+
14+
@IBOutlet weak var tableView: UITableView!
15+
1216
override func viewDidLoad() {
1317
super.viewDidLoad()
18+
title = "Favoritos"
19+
favoritesVM = FavoritesViewModel()
20+
21+
setTableView()
22+
23+
favoritesVM?.getFavoriteAnimals { [weak self] in self?.tableView.reloadData()
24+
}
25+
}
26+
27+
override func viewWillAppear(_ animated: Bool) {
28+
super.viewWillAppear(animated)
29+
favoritesVM?.getFavoriteAnimals { [weak self] in self?.tableView.reloadData()
30+
}
31+
}
32+
33+
private func setTableView() {
34+
tableView.dataSource = self
35+
tableView.delegate = self
36+
37+
tableView.register(UINib(nibName: "AnimalTableViewCell", bundle: nil), forCellReuseIdentifier: "Animal")
38+
}
39+
}
40+
41+
// MARK: TableView Data Source
42+
extension FavoritesViewController: UITableViewDataSource {
43+
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
44+
return favoritesVM?.numberOfRows() ?? 0
45+
}
46+
47+
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
1448

49+
guard let cell = tableView.dequeueReusableCell(withIdentifier: "Animal", for: indexPath) as? AnimalTableViewCell else {
50+
return UITableViewCell()
1551
}
52+
53+
cell.animal = favoritesVM?.modelAt(indexPath.row)
54+
cell.index = indexPath.row
55+
cell.delegate = self
56+
cell.configure()
57+
58+
return cell
59+
}
1660

1761
}
1862

63+
// MARK: TableView Delegate
64+
extension FavoritesViewController: UITableViewDelegate {
65+
66+
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
67+
let detailVC = DetailViewController(nibName: "DetailViewController", bundle: nil)
68+
69+
let animal = favoritesVM?.modelAt(indexPath.row)
70+
detailVC.animal = animal
1971

72+
navigationController?.pushViewController(detailVC, animated: true)
73+
74+
tableView.deselectRow(at: indexPath, animated: true)
75+
}
76+
77+
}
2078

79+
// MARK: Action Delegate Protocol
80+
extension FavoritesViewController: ActionDelegateProtocol {
81+
func addFavoriteTapped(at index: Int, with image: Data) {
82+
}
83+
84+
func removeFavoriteTapped(at index: Int) {
85+
favoritesVM?.removeFavorite(at: index) { [weak self] in
86+
self?.tableView.reloadData()
87+
}
88+
}
89+
}
2190

AnimalsApp/AnimalsApp/Views/FavoritesViewController/FavoritesViewController.xib

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,45 @@
44
<dependencies>
55
<deployment identifier="iOS"/>
66
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="20020"/>
7+
<capability name="Named colors" minToolsVersion="9.0"/>
78
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
89
<capability name="System colors in document resources" minToolsVersion="11.0"/>
910
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
1011
</dependencies>
1112
<objects>
1213
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner" customClass="FavoritesViewController" customModule="AnimalsApp" customModuleProvider="target">
1314
<connections>
15+
<outlet property="tableView" destination="Z7H-5c-G5G" id="KBe-qr-ZCy"/>
1416
<outlet property="view" destination="i5M-Pr-FkT" id="sfx-zR-JGt"/>
1517
</connections>
1618
</placeholder>
1719
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
1820
<view clearsContextBeforeDrawing="NO" contentMode="scaleToFill" id="i5M-Pr-FkT">
1921
<rect key="frame" x="0.0" y="0.0" width="414" height="896"/>
2022
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
23+
<subviews>
24+
<tableView clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" style="plain" rowHeight="-1" estimatedRowHeight="-1" sectionHeaderHeight="-1" estimatedSectionHeaderHeight="-1" sectionFooterHeight="-1" estimatedSectionFooterHeight="-1" translatesAutoresizingMaskIntoConstraints="NO" id="Z7H-5c-G5G">
25+
<rect key="frame" x="0.0" y="44" width="414" height="818"/>
26+
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
27+
<color key="separatorColor" name="grayCellFrame"/>
28+
<inset key="separatorInset" minX="0.0" minY="0.0" maxX="0.0" maxY="0.0"/>
29+
</tableView>
30+
</subviews>
2131
<viewLayoutGuide key="safeArea" id="fnl-2z-Ty3"/>
2232
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
33+
<constraints>
34+
<constraint firstItem="Z7H-5c-G5G" firstAttribute="top" secondItem="fnl-2z-Ty3" secondAttribute="top" id="Gij-fh-f3w"/>
35+
<constraint firstItem="fnl-2z-Ty3" firstAttribute="trailing" secondItem="Z7H-5c-G5G" secondAttribute="trailing" id="Mbr-5U-mqf"/>
36+
<constraint firstItem="fnl-2z-Ty3" firstAttribute="bottom" secondItem="Z7H-5c-G5G" secondAttribute="bottom" id="OEE-7F-ts3"/>
37+
<constraint firstItem="Z7H-5c-G5G" firstAttribute="leading" secondItem="fnl-2z-Ty3" secondAttribute="leading" id="e7r-Gn-PgY"/>
38+
</constraints>
2339
<point key="canvasLocation" x="132" y="33"/>
2440
</view>
2541
</objects>
2642
<resources>
43+
<namedColor name="grayCellFrame">
44+
<color red="0.63921568627450975" green="0.63921568627450975" blue="0.63921568627450975" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
45+
</namedColor>
2746
<systemColor name="systemBackgroundColor">
2847
<color white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
2948
</systemColor>

0 commit comments

Comments
 (0)