-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDetailViewController.swift
130 lines (110 loc) · 3.59 KB
/
DetailViewController.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
//
// DetailViewController.swift
// Anibuddy
//
// Created by Kyle Grande.
//
//SIMILAR TO CHAPTER 37 IN TEXTBOOK
import UIKit
import CoreData
// DetailViewController class, manages detail view of a selected Anime
class DetailViewController: UIViewController {
// Anime object to be shown in detail
var anime: Anime!
var downloadTask: URLSessionDownloadTask?
// Action to close the Detail View
@IBAction func close() {
dismiss(animated: true, completion: nil)
}
// Outlets for UI elements
@IBOutlet weak var popupView: UIView!
@IBOutlet weak var artworkImageView: UIImageView!
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var soreLabel: UILabel!
@IBOutlet weak var typeLabel: UILabel!
@IBOutlet weak var genreLabel: UILabel!
@IBOutlet weak var favButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Setup UI
popupView.layer.cornerRadius = 10
let gestureRecognizer = UITapGestureRecognizer(
target: self,
action: #selector(close))
gestureRecognizer.cancelsTouchesInView = false
gestureRecognizer.delegate = self
view.addGestureRecognizer(gestureRecognizer)
// Fetch Anime from CoreData and update the UI
if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
let context = appDelegate.persistentContainer.viewContext
let fetchRequest: NSFetchRequest<Anime> = Anime.fetchRequest()
fetchRequest.predicate = NSPredicate(format: "mal_id == %d", anime.mal_id)
do {
let results = try context.fetch(fetchRequest)
if let existingAnime = results.first {
anime = existingAnime
}
} catch {
print("Failed to fetch Anime with mal_id \(anime.mal_id): \(error)")
}
}
if anime != nil {
updateUI() }
}
// Action to toggle favorite status of the Anime
@IBAction func toggleFavorite(_ sender: UIButton) {
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
return
}
let context = appDelegate.persistentContainer.viewContext
anime.isFavorite.toggle()
do {
try context.save()
animateFavButton()
updateFavButtonTitle()
print("Anime '\(anime.title ?? "")' favorite status changed to: \(anime.isFavorite)")
} catch {
print("Failed to update favorite status: \(error)")
}
}
// Update UI with Anime information
func updateUI() {
nameLabel.text = anime.title
soreLabel.text = String(anime.score)
typeLabel.text = anime.type
genreLabel.text = String(anime.episodes)
if let imageUrlString = anime.imageUrl, let imageUrl = URL(string: imageUrlString) {
artworkImageView.downloadImage(from: imageUrl)
}
updateFavButtonTitle()
}
// Update favorite button title based on Anime's favorite status
func updateFavButtonTitle() {
if anime.isFavorite {
favButton.setTitle("Unfavorite", for: .normal)
favButton.setImage(UIImage(systemName: "star.fill"), for: .normal)
} else {
favButton.setTitle("Favorite", for: .normal)
favButton.setImage(UIImage(systemName: "star"), for: .normal)
}
favButton.isEnabled = true
}
// Animate favorite button for a click effect
func animateFavButton() {
UIView.animate(withDuration: 0.1, animations: {
self.favButton.transform = CGAffineTransform(scaleX: 1.3, y: 1.3)
}, completion: { _ in
UIView.animate(withDuration: 0.1, animations: {
self.favButton.transform = CGAffineTransform.identity
})
})
}
}
extension DetailViewController: UIGestureRecognizerDelegate {
func gestureRecognizer(
_ gestureRecognizer: UIGestureRecognizer,
shouldReceive touch: UITouch
) -> Bool {
return (touch.view === self.view)
}
}