-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add our own image repository for caching images * Add now playing artwork in async way * Use our own Image Repository to store images * Cleanup * Remove `CachedAsyncImage` from packages
- Loading branch information
Showing
13 changed files
with
271 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 0 additions & 14 deletions
14
accentor.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
// | ||
// ImageRepository.swift | ||
// accentor | ||
// | ||
// Created by Robbe Van Petegem on 07/05/2023. | ||
// | ||
|
||
import Foundation | ||
|
||
// NOTE: it isn't exactly great that we need to duplicate all this code, just make the difference between NSImage and UIImage | ||
// Make figure out if there is a better way of doing this? | ||
|
||
#if os(macOS) | ||
import AppKit | ||
#else | ||
import UIKit | ||
#endif | ||
|
||
#if os(macOS) | ||
protocol ImageRepositoryProtocol { | ||
func getImage(imageURL: URL) async -> NSImage? | ||
func downloadImage(imageURL: URL) async -> NSImage? | ||
func loadImageFromCache(imageURL: URL) async -> NSImage? | ||
} | ||
#else | ||
protocol ImageRepositoryProtocol { | ||
func getImage(imageURL: URL) async -> UIImage? | ||
func downloadImage(imageURL: URL) async -> UIImage? | ||
func loadImageFromCache(imageURL: URL) async -> UIImage? | ||
} | ||
#endif | ||
|
||
#if os(macOS) | ||
public class ImageRepository: ImageRepositoryProtocol { | ||
|
||
let cache = URLCache.shared | ||
|
||
func getImage(imageURL: URL) async -> NSImage? { | ||
let request = URLRequest(url: imageURL) | ||
|
||
if (self.cache.cachedResponse(for: request) != nil) { | ||
let image = self.loadImageFromCache(imageURL: imageURL) | ||
if (image != nil) { return image! } | ||
} | ||
|
||
return await self.downloadImage(imageURL: imageURL) | ||
} | ||
|
||
func downloadImage(imageURL: URL) async -> NSImage? { | ||
do { | ||
let request = URLRequest(url: imageURL) | ||
let (data, response) = try await URLSession.shared.data(for: request) | ||
|
||
guard let httpResponse = response as? HTTPURLResponse, httpResponse.statusCode == 200 else { | ||
return nil | ||
} | ||
|
||
guard let image = NSImage(data: data) else { | ||
return nil | ||
} | ||
|
||
// Only cache the data if we got a correct image | ||
let cachedData = CachedURLResponse(response: response, data: data) | ||
self.cache.storeCachedResponse(cachedData, for: request) | ||
|
||
return image | ||
|
||
} catch { | ||
return nil | ||
} | ||
} | ||
|
||
func loadImageFromCache(imageURL: URL) -> NSImage? { | ||
let request = URLRequest(url: imageURL) | ||
|
||
guard let data = self.cache.cachedResponse(for: request)?.data else { | ||
return nil | ||
} | ||
|
||
return NSImage(data: data) | ||
} | ||
} | ||
#else | ||
public class ImageRepository: ImageRepositoryProtocol { | ||
|
||
let cache = URLCache.shared | ||
|
||
func getImage(imageURL: URL) async -> UIImage? { | ||
let request = URLRequest(url: imageURL) | ||
|
||
if (self.cache.cachedResponse(for: request) != nil) { | ||
let image = self.loadImageFromCache(imageURL: imageURL) | ||
if (image != nil) { return image! } | ||
} | ||
|
||
return await self.downloadImage(imageURL: imageURL) | ||
} | ||
|
||
func downloadImage(imageURL: URL) async -> UIImage? { | ||
do { | ||
let request = URLRequest(url: imageURL) | ||
let (data, response) = try await URLSession.shared.data(for: request) | ||
|
||
guard let httpResponse = response as? HTTPURLResponse, httpResponse.statusCode == 200 else { | ||
return nil | ||
} | ||
|
||
guard let image = UIImage(data: data) else { | ||
return nil | ||
} | ||
|
||
// Only cache the data if we got a correct image | ||
let cachedData = CachedURLResponse(response: response, data: data) | ||
self.cache.storeCachedResponse(cachedData, for: request) | ||
|
||
return image | ||
|
||
} catch { | ||
return nil | ||
} | ||
} | ||
|
||
func loadImageFromCache(imageURL: URL) -> UIImage? { | ||
let request = URLRequest(url: imageURL) | ||
|
||
guard let data = self.cache.cachedResponse(for: request)?.data else { | ||
return nil | ||
} | ||
|
||
return UIImage(data: data) | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.