diff --git a/OutRun/Models/Workout/MapManagement/Images/WorkoutMapImageManager.swift b/OutRun/Models/Workout/MapManagement/Images/WorkoutMapImageManager.swift index 8076c95..381bfc0 100644 --- a/OutRun/Models/Workout/MapManagement/Images/WorkoutMapImageManager.swift +++ b/OutRun/Models/Workout/MapManagement/Images/WorkoutMapImageManager.swift @@ -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 } @@ -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() } @@ -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 { diff --git a/OutRun/Models/Workout/MapManagement/Images/WorkoutMapImageRequest.swift b/OutRun/Models/Workout/MapManagement/Images/WorkoutMapImageRequest.swift index b55304b..625ecc6 100644 --- a/OutRun/Models/Workout/MapManagement/Images/WorkoutMapImageRequest.swift +++ b/OutRun/Models/Workout/MapManagement/Images/WorkoutMapImageRequest.swift @@ -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 }