-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLibraryTableViewController.swift
116 lines (89 loc) · 3.67 KB
/
LibraryTableViewController.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
//
// LibraryTableViewController.swift
// Anibuddy
//
// Created by Kyle Grande.
//
import UIKit
import CoreData
class LibraryTableViewController: UITableViewController {
// Array to store the list of favorite Anime objects.
var favoriteAnimes: [Anime] = []
// FetchedResultsController to fetch Anime objects from Core Data.
var fetchedResultsController: NSFetchedResultsController<Anime>!
override func viewDidLoad() {
super.viewDidLoad()
// Register SearchResultCell
let cellNib = UINib(nibName: "SearchResultCell", bundle: nil)
tableView.register(cellNib, forCellReuseIdentifier: "SearchResultCell")
// Configure the fetchedResultsController
configureFetchedResultsController()
// Load favorite animes
loadFavoriteAnimes()
}
// MARK: - Helper Functions
// Function to configure FetchedResultsController.
// Get the AppDelegate instance to access the PersistentContainer.
func configureFetchedResultsController() {
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
return
}
let context = appDelegate.persistentContainer.viewContext
let fetchRequest: NSFetchRequest<Anime> = Anime.fetchRequest()
// The favorite animes are sorted by their title.
let sortDescriptor = NSSortDescriptor(key: "title", ascending: true)
fetchRequest.sortDescriptors = [sortDescriptor]
// Only fetch the animes that are marked as favorite. (Not Needed by nice to have)
fetchRequest.predicate = NSPredicate(format: "isFavorite == %@", NSNumber(value: true))
fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil)
fetchedResultsController.delegate = self
}
// Function to load favorite animes.
func loadFavoriteAnimes() {
do {
// Perform the fetch request.
try fetchedResultsController.performFetch()
// Store the fetched animes in the favoriteAnimes array.
favoriteAnimes = fetchedResultsController.fetchedObjects ?? []
// Reload the table view to display the new data.
tableView.reloadData()
} catch {
print("Failed to fetch favorite animes: \(error)")
}
}
// Prepare for the segue transition to the DetailViewController.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "ShowDetailFromLibrary" {
let detailViewController = segue.destination as! DetailViewController
let indexPath = sender as! IndexPath
let anime = favoriteAnimes[indexPath.row]
detailViewController.anime = anime
}
}
}
// MARK: - Table view data source
extension LibraryTableViewController {
// Define the number of rows in the table view.
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return favoriteAnimes.count
}
// Configure each cell in the table view.
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "SearchResultCell", for: indexPath) as! SearchResultCell
// Configure the cell...
let anime = favoriteAnimes[indexPath.row]
cell.configure(with: anime)
return cell
}
// Define what happens when a row is selected in the table view.
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
performSegue(withIdentifier: "ShowDetailFromLibrary", sender: indexPath)
}
}
// MARK: - NSFetchedResultsControllerDelegate
extension LibraryTableViewController: NSFetchedResultsControllerDelegate {
func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
loadFavoriteAnimes()
}
}