Save GIFs Locally

This commit is contained in:
Joshua Higgins
2025-06-03 22:18:23 -04:00
parent a09e08763f
commit 8ce5adc766
8 changed files with 283 additions and 116 deletions

View File

@@ -24,7 +24,7 @@ class GIFCollectionViewCell: UICollectionViewCell {
return label
}()
private var currentTask: URLSessionDataTask?
private var gifData: Data?
override init(frame: CGRect) {
super.init(frame: frame)
@@ -38,8 +38,7 @@ class GIFCollectionViewCell: UICollectionViewCell {
override func prepareForReuse() {
super.prepareForReuse()
currentTask?.cancel()
currentTask = nil
gifData = nil
gifPlayerView.stopAnimating()
placeholderLabel.isHidden = false
loadingIndicator.stopAnimating()
@@ -72,36 +71,32 @@ class GIFCollectionViewCell: UICollectionViewCell {
contentView.layer.borderColor = UIColor.systemGray4.cgColor
}
func configure(with urlString: String) {
// Cancel any existing task
currentTask?.cancel()
func configure(with gif: GIF) {
// Reset UI
gifPlayerView.stopAnimating()
placeholderLabel.isHidden = false
loadingIndicator.startAnimating()
guard let url = URL(string: urlString) else {
loadingIndicator.stopAnimating()
return
}
// Load the GIF
currentTask = URLSession.shared.dataTask(with: url) { [weak self] data, _, error in
DispatchQueue.main.async {
guard let self = self else { return }
self.loadingIndicator.stopAnimating()
// Load the GIF data from local storage
DispatchQueue.global(qos: .userInitiated).async { [weak self] in
if let gifData = GIFStorageService.shared.getGIFData(for: gif) {
if let data = data, error == nil {
self.gifPlayerView.loadGIF(from: data)
DispatchQueue.main.async {
guard let self = self else { return }
self.loadingIndicator.stopAnimating()
self.gifData = gifData
self.gifPlayerView.loadGIF(from: gifData)
self.gifPlayerView.startAnimating()
self.placeholderLabel.isHidden = true
} else {
}
} else {
DispatchQueue.main.async {
guard let self = self else { return }
self.loadingIndicator.stopAnimating()
self.placeholderLabel.isHidden = false
}
}
}
currentTask?.resume()
}
}