-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move AsyncImageView to AsyncImageKit (#23938)
- Loading branch information
Showing
27 changed files
with
205 additions
and
157 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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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 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
53 changes: 53 additions & 0 deletions
53
Modules/Sources/AsyncImageKit/Views/ImageLoadingController.swift
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,53 @@ | ||
import UIKit | ||
|
||
/// A convenience class for managing image downloads for individual views. | ||
@MainActor | ||
public final class ImageLoadingController { | ||
public var downloader: ImageDownloader = .shared | ||
public var onStateChanged: (State) -> Void = { _ in } | ||
|
||
public private(set) var task: Task<Void, Never>? | ||
|
||
public enum State { | ||
case loading | ||
case success(UIImage) | ||
case failure(Error) | ||
} | ||
|
||
deinit { | ||
task?.cancel() | ||
} | ||
|
||
public init() {} | ||
|
||
public func prepareForReuse() { | ||
task?.cancel() | ||
task = nil | ||
} | ||
|
||
/// - parameter completion: Gets called on completion _after_ `onStateChanged`. | ||
public func setImage(with request: ImageRequest, completion: (@MainActor (Result<UIImage, Error>) -> Void)? = nil) { | ||
task?.cancel() | ||
|
||
if let image = downloader.cachedImage(for: request) { | ||
onStateChanged(.success(image)) | ||
completion?(.success(image)) | ||
} else { | ||
onStateChanged(.loading) | ||
task = Task { @MainActor [downloader, weak self] in | ||
do { | ||
let image = try await downloader.image(for: request) | ||
// This line guarantees that if you cancel on the main thread, | ||
// none of the `onStateChanged` callbacks get called. | ||
guard !Task.isCancelled else { return } | ||
self?.onStateChanged(.success(image)) | ||
completion?(.success(image)) | ||
} catch { | ||
guard !Task.isCancelled else { return } | ||
self?.onStateChanged(.failure(error)) | ||
completion?(.failure(error)) | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.