-
Notifications
You must be signed in to change notification settings - Fork 0
/
FavoritesViewController.swift
134 lines (82 loc) · 3.88 KB
/
FavoritesViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import UIKit
import CoreData
class FavoritesViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
//var
var favorites = [String]()
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return favorites.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let favCell = tableView.dequeueReusableCell(withIdentifier: "favCell")
let contentView = favCell?.contentView
let label = contentView?.viewWithTag(1) as! UILabel
let imageView = contentView?.viewWithTag(2) as! UIImageView
label.text = favorites[indexPath.row]
imageView.image = UIImage(named: favorites[indexPath.row])
return favCell!
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let movie = favorites[indexPath.row]
performSegue(withIdentifier: "mSegue2", sender: movie)
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
deleteElement(tableView: tableView, index: indexPath.row)
print("CELL DELETING ...")
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let param = sender as! String
if segue.identifier == "mSegue2" {
let destination = segue.destination as! DetailsViewController
destination.movieName = param
}
}
override func viewDidLoad() {
super.viewDidLoad()
fetchData()
// Do any additional setup after loading the view.
}
func fetchData() {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let persistentContainer = appDelegate.persistentContainer
let managedContext = persistentContainer.viewContext
let request = NSFetchRequest<NSManagedObject>(entityName: "Movie")
do {
let data = try managedContext.fetch(request)
for item in data {
favorites.append(item.value(forKey: "movieName") as! String)
}
} catch {
print("Fetching error !")
}
}
func getByCreateria(movieName: String) -> NSManagedObject{
var movieExist:NSManagedObject?
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let persistentContainer = appDelegate.persistentContainer
let managedContext = persistentContainer.viewContext
let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Movie")
let predicate = NSPredicate(format: "movieName = %@", movieName)
request.predicate = predicate
do {
let result = try managedContext.fetch(request)
if result.count > 0 {
movieExist = (result[0] as! NSManagedObject)
print("Movie exists !")
}
} catch {
print("Fetching by criteria error !")
}
return movieExist!
}
func deleteElement(tableView: UITableView, index: Int) {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let persistentContainer = appDelegate.persistentContainer
let managedContext = persistentContainer.viewContext
let object = getByCreateria(movieName: favorites[index])
managedContext.delete(object)
favorites.remove(at: index)
tableView.reloadData()
}
}