Skip to content

Commit

Permalink
Remove AnimatedImageCache and related types (#23926)
Browse files Browse the repository at this point in the history
  • Loading branch information
kean authored Dec 24, 2024
2 parents e07e9b0 + 0136de2 commit f09a0cd
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 204 deletions.
25 changes: 9 additions & 16 deletions WordPress/Classes/Utility/Media/MediaExternalExporter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,17 @@ class MediaExternalExporter: MediaExporter {
return Progress.discreteCompletedProgress()
}

/// Downloads an external GIF file, or uses one from the AnimatedImageCache.
///
private func downloadGif(from url: URL, onCompletion: @escaping OnMediaExport, onError: @escaping OnExportError) -> Progress {
let request = URLRequest(url: url)
let task = AnimatedImageCache.shared.animatedImage(request, placeholderImage: nil,
success: { (data, _) in
self.gifDataDownloaded(data: data,
fromURL: url,
error: nil,
onCompletion: onCompletion,
onError: onError)
}, failure: { error in
if let error {
onError(self.exporterErrorWith(error: error))
Task {
do {
let options = ImageRequestOptions(isMemoryCacheEnabled: false)
let data = try await ImageDownloader.shared.data(for: ImageRequest(url: url, options: options))
self.gifDataDownloaded(data: data, fromURL: url, error: nil, onCompletion: onCompletion, onError: onError)
} catch {
onError(ExportError.downloadError(error as NSError))
}
})

return task?.progress ?? Progress.discreteCompletedProgress()
}
return Progress.discreteCompletedProgress()
}

/// Saves downloaded GIF data to the filesystem and exports it.
Expand Down
2 changes: 0 additions & 2 deletions WordPress/Classes/Utility/Media/MemoryCache+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ extension MemoryCache {
UIImageView.af.sharedImageDownloader = AlamofireImage.ImageDownloader(
imageCache: AlamofireImageCacheAdapter(cache: .shared)
)

// WordPress.AnimatedImageCache uses WordPress.MemoryCache directly
}
}

Expand Down
89 changes: 21 additions & 68 deletions WordPress/Classes/ViewRelated/Gutenberg/EditorMediaUtility.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,67 +4,17 @@ import Gridicons
import WordPressShared
import WordPressMedia

final class AuthenticatedImageDownload: AsyncOperation, @unchecked Sendable {
enum DownloadError: Error {
case blogNotFound
}

let url: URL
let mediaHost: MediaHost
private let callbackQueue: DispatchQueue
private let onSuccess: (UIImage) -> ()
private let onFailure: (Error) -> ()

init(url: URL, mediaHost: MediaHost, callbackQueue: DispatchQueue, onSuccess: @escaping (UIImage) -> (), onFailure: @escaping (Error) -> ()) {
self.url = url
self.mediaHost = mediaHost
self.callbackQueue = callbackQueue
self.onSuccess = onSuccess
self.onFailure = onFailure
}

override func main() {
let mediaRequestAuthenticator = MediaRequestAuthenticator()
mediaRequestAuthenticator.authenticatedRequest(
for: url,
from: mediaHost,
onComplete: { request in
ImageDownloader.shared.downloadImage(for: request) { (image, error) in
self.state = .isFinished

self.callbackQueue.async {
guard let image else {
DDLogError("Unable to download image for attachment with url = \(String(describing: request.url)). Details: \(String(describing: error?.localizedDescription))")
if let error {
self.onFailure(error)
} else {
self.onFailure(NSError(domain: NSURLErrorDomain, code: NSURLErrorUnknown, userInfo: nil))
}

return
}

self.onSuccess(image)
}
}
},
onFailure: { error in
self.state = .isFinished
self.callbackQueue.async {
self.onFailure(error)
}
}
)
}
}

class EditorMediaUtility {
private static let InternalInconsistencyError = NSError(domain: NSExceptionName.internalInconsistencyException.rawValue, code: 0)

private struct Constants {
static let placeholderDocumentLink = URL(string: "documentUploading://")!
}

enum DownloadError: Error {
case blogNotFound
}

func placeholderImage(for attachment: NSTextAttachment, size: CGSize, tintColor: UIColor?) -> UIImage {
var icon: UIImage
switch attachment {
Expand Down Expand Up @@ -137,17 +87,18 @@ class EditorMediaUtility {
callbackQueue.async {
failure(error)
}
return EmptyImageDownloaderTask()
case let .success((requestURL, mediaHost)):
let imageDownload = AuthenticatedImageDownload(
url: requestURL,
mediaHost: mediaHost,
callbackQueue: callbackQueue,
onSuccess: success,
onFailure: failure
)
imageDownload.start()
return imageDownload
return MeediaUtilityTask { /* do nothing */ }
case let .success((imageURL, host)):
let task = Task { @MainActor in
do {
let image = try await ImageDownloader.shared.image(from: imageURL, host: host)
success(image)
} catch {
failure(error)

}
}
return MeediaUtilityTask { task.cancel() }
}
}

Expand All @@ -160,7 +111,7 @@ class EditorMediaUtility {
) throws -> (URL, MediaHost) {
// This function is added to debug the issue linked below.
let safeExistingObject: (NSManagedObjectID) throws -> NSManagedObject = { objectID in
var object: Result<NSManagedObject, Error> = .failure(AuthenticatedImageDownload.DownloadError.blogNotFound)
var object: Result<NSManagedObject, Error> = .failure(DownloadError.blogNotFound)
do {
// Catch an Objective-C `NSInvalidArgumentException` exception from `existingObject(with:)`.
// See https://github.com/wordpress-mobile/WordPress-iOS/issues/20630
Expand Down Expand Up @@ -251,8 +202,10 @@ class EditorMediaUtility {
}
}

private class EmptyImageDownloaderTask: ImageDownloaderTask {
private struct MeediaUtilityTask: ImageDownloaderTask {
let closure: @Sendable () -> Void

func cancel() {
// Do nothing
closure()
}
}
18 changes: 0 additions & 18 deletions WordPress/Classes/ViewRelated/Gutenberg/GutenbergImageLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@ class GutenbergImageLoader: NSObject, RCTImageURLLoader {
}

func loadImage(for imageURL: URL, size: CGSize, scale: CGFloat, resizeMode: RCTResizeMode, progressHandler: RCTImageLoaderProgressBlock, partialLoadHandler: RCTImageLoaderPartialLoadBlock, completionHandler: @escaping RCTImageLoaderCompletionBlock) -> RCTImageLoaderCancellationBlock? {
let cacheKey = getCacheKey(for: imageURL, size: size)

if let image = AnimatedImageCache.shared.cachedStaticImage(url: cacheKey) {
completionHandler(nil, image)
return {}
}

var finalSize = size
var finalScale = scale
Expand All @@ -36,7 +30,6 @@ class GutenbergImageLoader: NSObject, RCTImageURLLoader {
}

let task = mediaUtility.downloadImage(from: imageURL, size: finalSize, scale: finalScale, post: post, success: { (image) in
AnimatedImageCache.shared.cacheStaticImage(url: cacheKey, image: image)
completionHandler(nil, image)
}, onFailure: { (error) in
completionHandler(error, nil)
Expand All @@ -56,17 +49,6 @@ class GutenbergImageLoader: NSObject, RCTImageURLLoader {
return nil
}

private func getCacheKey(for url: URL, size: CGSize) -> URL? {
guard size != CGSize.zero else {
return url
}
var components = URLComponents(url: url, resolvingAgainstBaseURL: true)
let queryItems = components?.queryItems
let newQueryItems = (queryItems ?? []) + [URLQueryItem(name: "cachekey", value: "\(size)")]
components?.queryItems = newQueryItems
return components?.url
}

static func moduleName() -> String! {
return String(describing: self)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class GutenbergMediaEditorImage: AsyncImage {
init(url: URL, post: AbstractPost) {
originalURL = url
self.post = post
thumb = AnimatedImageCache.shared.cachedStaticImage(url: originalURL)
thumb = ImageDownloader.shared.cachedImage(for: originalURL)
}

/**
Expand Down
99 changes: 0 additions & 99 deletions WordPress/Classes/ViewRelated/Media/AnimatedImageCache.swift

This file was deleted.

0 comments on commit f09a0cd

Please sign in to comment.