Skip to content

Commit 2a6f31d

Browse files
committed
add core data to save favorite animals
1 parent 49b2290 commit 2a6f31d

7 files changed

Lines changed: 101 additions & 6 deletions

File tree

AnimalsApp/AnimalsApp.xcodeproj/project.pbxproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
A463D071285A373800929A3C /* Animals.swift in Sources */ = {isa = PBXBuildFile; fileRef = A463D070285A373700929A3C /* Animals.swift */; };
3535
A463D074285A398900929A3C /* HomeViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = A463D073285A398900929A3C /* HomeViewModel.swift */; };
3636
A463D078285A5A8000929A3C /* OpenSans-Regular.ttf in Resources */ = {isa = PBXBuildFile; fileRef = A463D077285A5A8000929A3C /* OpenSans-Regular.ttf */; };
37+
A463D07C285B9B6300929A3C /* CoreData.swift in Sources */ = {isa = PBXBuildFile; fileRef = A463D07B285B9B6300929A3C /* CoreData.swift */; };
3738
/* End PBXBuildFile section */
3839

3940
/* Begin PBXContainerItemProxy section */
@@ -91,6 +92,7 @@
9192
A463D070285A373700929A3C /* Animals.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Animals.swift; sourceTree = "<group>"; };
9293
A463D073285A398900929A3C /* HomeViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HomeViewModel.swift; sourceTree = "<group>"; };
9394
A463D077285A5A8000929A3C /* OpenSans-Regular.ttf */ = {isa = PBXFileReference; lastKnownFileType = file; path = "OpenSans-Regular.ttf"; sourceTree = "<group>"; };
95+
A463D07B285B9B6300929A3C /* CoreData.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CoreData.swift; sourceTree = "<group>"; };
9496
/* End PBXFileReference section */
9597

9698
/* Begin PBXFrameworksBuildPhase section */
@@ -169,7 +171,6 @@
169171
A463D0222858EBBD00929A3C /* Assets.xcassets */,
170172
A463D0242858EBBD00929A3C /* LaunchScreen.storyboard */,
171173
A463D0272858EBBD00929A3C /* Info.plist */,
172-
A463D01F2858EBBB00929A3C /* AnimalsApp.xcdatamodeld */,
173174
);
174175
path = AnimalsApp;
175176
sourceTree = "<group>";
@@ -252,6 +253,7 @@
252253
isa = PBXGroup;
253254
children = (
254255
A463D068285A1A5A00929A3C /* WebServices.swift */,
256+
A463D07B285B9B6300929A3C /* CoreData.swift */,
255257
);
256258
path = Services;
257259
sourceTree = "<group>";
@@ -268,6 +270,7 @@
268270
A463D06F285A371F00929A3C /* Models */ = {
269271
isa = PBXGroup;
270272
children = (
273+
A463D01F2858EBBB00929A3C /* AnimalsApp.xcdatamodeld */,
271274
A463D070285A373700929A3C /* Animals.swift */,
272275
);
273276
path = Models;
@@ -554,6 +557,7 @@
554557
buildActionMask = 2147483647;
555558
files = (
556559
A463D06228590D0300929A3C /* Images.swift in Sources */,
560+
A463D07C285B9B6300929A3C /* CoreData.swift in Sources */,
557561
A463D065285953F900929A3C /* AnimalTableViewCell.swift in Sources */,
558562
A463D0212858EBBB00929A3C /* AnimalsApp.xcdatamodeld in Sources */,
559563
A463D0572858F62600929A3C /* RegisterViewController.swift in Sources */,

AnimalsApp/AnimalsApp/AnimalsApp.xcdatamodeld/AnimalsApp.xcdatamodel/contents

Lines changed: 0 additions & 4 deletions
This file was deleted.

AnimalsApp/AnimalsApp/Models/Animals.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ struct Animal: Codable {
2121
let created_at: String
2222
var updated_at: String
2323
var isFavorite: Bool? = false
24+
var imageData: Data?
2425
}
2526

2627
extension Animal {

AnimalsApp/AnimalsApp/AnimalsApp.xcdatamodeld/.xccurrentversion renamed to AnimalsApp/AnimalsApp/Models/AnimalsApp.xcdatamodeld/.xccurrentversion

File renamed without changes.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="20086" systemVersion="21F79" minimumToolsVersion="Automatic" sourceLanguage="Swift" userDefinedModelVersionIdentifier="">
3+
<entity name="FavoriteAnimal" representedClassName=".FavoriteAnimal" syncable="YES" codeGenerationType="class">
4+
<attribute name="age" optional="YES" attributeType="Integer 32" defaultValueString="0" usesScalarValueType="YES"/>
5+
<attribute name="descript" optional="YES" attributeType="String"/>
6+
<attribute name="id" optional="YES" attributeType="String"/>
7+
<attribute name="image" optional="YES" attributeType="Binary"/>
8+
<attribute name="name" optional="YES" attributeType="String"/>
9+
<attribute name="species" optional="YES" attributeType="String"/>
10+
</entity>
11+
<elements>
12+
<element name="FavoriteAnimal" positionX="-63" positionY="-18" width="128" height="119"/>
13+
</elements>
14+
</model>
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
//
2+
// CoreData.swift
3+
// AnimalsApp
4+
//
5+
// Created by Laura Pinheiro Marson on 16/06/22.
6+
//
7+
8+
import UIKit
9+
import CoreData
10+
11+
class CoreData {
12+
var managedContext: NSManagedObjectContext?
13+
var favoriteAnimals = [FavoriteAnimal]()
14+
15+
init() {
16+
managedContext = (UIApplication.shared.delegate as? AppDelegate)?
17+
.persistentContainer
18+
.viewContext
19+
}
20+
21+
func loadFavoriteAnimals(completion: @escaping () -> ()) {
22+
guard let managedContext = managedContext else { return }
23+
24+
let fetchRequest: NSFetchRequest<FavoriteAnimal> = FavoriteAnimal.fetchRequest()
25+
26+
do {
27+
favoriteAnimals = try managedContext.fetch(fetchRequest)
28+
completion()
29+
} catch let error as NSError {
30+
print("Could not fetch. \(error), \(error.userInfo)")
31+
}
32+
}
33+
34+
func isFavorite(id: String) -> Bool {
35+
36+
for animal in favoriteAnimals {
37+
if animal.id == id {
38+
return true
39+
}
40+
}
41+
return false
42+
}
43+
44+
func addFavorite(_ animal: Animal) {
45+
46+
guard let managedContext = managedContext else { return }
47+
48+
let newFavoriteAnimal = FavoriteAnimal(context: managedContext)
49+
newFavoriteAnimal.id = animal.id
50+
newFavoriteAnimal.name = animal.name
51+
newFavoriteAnimal.image = animal.imageData
52+
newFavoriteAnimal.descript = animal.description
53+
newFavoriteAnimal.age = Int32(animal.age ?? 0)
54+
newFavoriteAnimal.species = animal.species
55+
self.favoriteAnimals.append(newFavoriteAnimal)
56+
}
57+
58+
func removeFavorite(id: String) {
59+
60+
guard let managedContext = managedContext else { return }
61+
62+
var removeAnimal = FavoriteAnimal()
63+
64+
for (index, animal) in favoriteAnimals.enumerated() {
65+
if animal.id == id {
66+
removeAnimal = animal
67+
favoriteAnimals.remove(at: index)
68+
}
69+
}
70+
71+
managedContext.delete(removeAnimal)
72+
}
73+
74+
func saveChanges() {
75+
do {
76+
try self.managedContext?.save()
77+
} catch let error as NSError {
78+
print("Could not save. \(error), \(error.userInfo)")
79+
}
80+
}
81+
}

AnimalsApp/AnimalsApp/Views/HomeViewController/HomeViewController.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ class HomeViewController: UIViewController {
2727

2828
tableView.register(UINib(nibName: "AnimalTableViewCell", bundle: nil), forCellReuseIdentifier: "Animal")
2929

30-
3130
setNavigationItems()
3231
populateTableView()
3332

0 commit comments

Comments
 (0)