Skip to content

Commit

Permalink
resolved #9
Browse files Browse the repository at this point in the history
when saving generated images to the cache it could happen that the cache identifier contained reference to the wrong appearance e.g. meaning a dark image was set as light, this is now checked before saving

Additionally when the appearance changed while rendering the image the request is added back to the queue for an updated version
  • Loading branch information
timfraedrich committed Aug 16, 2020
1 parent 2c385c4 commit 8199701
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ enum WorkoutMapImageManager {
/// - Parameter request: An instance of WorkoutMapImageRequest indicating the type of image being requested
public static func execute(_ request: WorkoutMapImageRequest) {

if let id = request.cacheIdentifier, let image = CustomImageCache.mapImageCache.getMapImage(for: id) {
if let id = request.cacheIdentifier(), let image = CustomImageCache.mapImageCache.getMapImage(for: id) {
request.completion(true, image)
return
}
Expand Down Expand Up @@ -80,8 +80,8 @@ enum WorkoutMapImageManager {

let imageUsesDarkMode = Config.isDarkModeEnabled
let completion: (Bool, UIImage?) -> Void = { (success, image) in
requestQueue.remove(request)
DispatchQueue.main.async {
requestQueue.remove(request)
request.completion(success, image)
executeNextInQueue()
}
Expand Down Expand Up @@ -137,12 +137,18 @@ enum WorkoutMapImageManager {
let resultImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

if let image = resultImage, let id = request.cacheIdentifier {
if let image = resultImage, let id = request.cacheIdentifier(forDarkAppearance: imageUsesDarkMode) {
CustomImageCache.mapImageCache.set(mapImage: image, for: id)
}

DispatchQueue.main.async {

completion(true, resultImage)

if Config.isDarkModeEnabled != imageUsesDarkMode {
self.requestQueue.add(request)
}

}

} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ class WorkoutMapImageRequest: Equatable {
let highPriority: Bool
var completion: (Bool, UIImage?) -> Void

var cacheIdentifier: String? {
func cacheIdentifier(forDarkAppearance usesDarkAppearance: Bool = Config.isDarkModeEnabled) -> String? {
guard let uuid = workoutUUID else {
return nil
}
let id = String(describing: uuid)
let size = self.size.identifier
let appearance = Config.isDarkModeEnabled ? "dark" : "light"
let appearance = usesDarkAppearance ? "dark" : "light"
return id + "_" + size + "_" + appearance
}

Expand Down

0 comments on commit 8199701

Please sign in to comment.