Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions Sources/AsyncMultiplexImage-Nuke/AsyncMultiplexImageNuke.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import AsyncMultiplexImage
import SwiftUI

public struct AsyncMultiplexImageNuke: View {

public let image: MultiplexImage

public init(image: MultiplexImage) {
self.image = image
}

public var body: some View {
AsyncMultiplexImage(
multiplexImage: image,
downloader: AsyncMultiplexImageNukeDownloader(pipeline: .shared, debugDelay: 0)
) { phase in
Group {
switch phase {
case .empty:
Rectangle()
.foregroundColor(Color.init(.displayP3, white: 0.9, opacity: 1))
case .progress(let image):
image
.resizable()
.scaledToFill()
.transition(.opacity.animation(.bouncy))
case .success(let image):
image
.resizable()
.scaledToFill()
.transition(.opacity.animation(.bouncy))
case .failure:
Rectangle()
.foregroundColor(Color.init(.displayP3, white: 0.9, opacity: 1))
}
}
}
}

}

#Preview {
AsyncMultiplexImageNuke(image: .init(constant: URL(string: "https://images.unsplash.com/photo-1492446845049-9c50cc313f00?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8")!))
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ public struct AsyncMultiplexImageNukeDownloader: AsyncMultiplexImageDownloader {
self.debugDelay = debugDelay
}

public func download(candidate: AsyncMultiplexImageCandidate, displaySize: CGSize) async throws
-> UIImage
{
public func download(
candidate: AsyncMultiplexImageCandidate,
displaySize: CGSize
) async throws -> UIImage {

#if DEBUG

Expand Down
27 changes: 0 additions & 27 deletions Sources/AsyncMultiplexImage/AsyncMultiplexImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,33 +93,6 @@ public struct AsyncMultiplexImageCandidate: Hashable {

}

public struct MultiplexImage: Hashable {

public static func == (lhs: MultiplexImage, rhs: MultiplexImage) -> Bool {
lhs.identifier == rhs.identifier
}

public func hash(into hasher: inout Hasher) {
identifier.hash(into: &hasher)
}

public let identifier: String

private(set) var _urlsProvider: @MainActor (CGSize) -> [URL]

public init(
identifier: String,
urlsProvider: @escaping @MainActor (CGSize) -> [URL]
) {
self.identifier = identifier
self._urlsProvider = urlsProvider
}

public init(identifier: String, urls: [URL]) {
self.init(identifier: identifier, urlsProvider: { _ in urls })
}

}

public struct AsyncMultiplexImage<Content: View, Downloader: AsyncMultiplexImageDownloader>: View {

Expand Down
38 changes: 38 additions & 0 deletions Sources/AsyncMultiplexImage/MultiplexImage.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import Foundation

public struct MultiplexImage: Hashable {

public static func == (lhs: MultiplexImage, rhs: MultiplexImage) -> Bool {
lhs.identifier == rhs.identifier
}

public func hash(into hasher: inout Hasher) {
identifier.hash(into: &hasher)
}

public let identifier: String

private(set) var _urlsProvider: @MainActor (CGSize) -> [URL]

public init(
identifier: String,
urlsProvider: @escaping @MainActor (CGSize) -> [URL]
) {
self.identifier = identifier
self._urlsProvider = urlsProvider
}

public init(identifier: String, urls: [URL]) {
self.init(identifier: identifier, urlsProvider: { _ in urls })
}

}

// MARK: convenience init
extension MultiplexImage {

public init(constant: URL) {
self.identifier = constant.absoluteString
self._urlsProvider = { _ in [constant] }
}
}