Skip to content
Merged
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
39 changes: 33 additions & 6 deletions Sources/AsyncMultiplexImage/MultiplexImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ import Foundation

public struct MultiplexImage: Hashable, Sendable {

public enum URLContainer: Sendable {
case single(URL)
case multiple([URL])
case dynamic(@Sendable (borrowing Context) -> [URL])
}

public struct Context: ~Copyable {
public let targetSize: CGSize
public let displayScale: CGFloat
Expand All @@ -25,27 +31,47 @@ public struct MultiplexImage: Hashable, Sendable {

public let identifier: String

private let _urlsProvider: @Sendable (borrowing Context) -> [URL]
private let urlContainer: URLContainer

/**
User defined flag for any purpose.
*/
public var flag: UInt64

/**
- Parameters:
- identifier: The unique identifier of the image.
- urlsProvider: The provider of the image URLs as the first item is the top priority.
*/
public init(
flag: UInt64 = 0,
identifier: String,
urlsProvider: @escaping @Sendable (borrowing Context) -> [URL]
) {
self.flag = flag
self.identifier = identifier
self._urlsProvider = urlsProvider
self.urlContainer = .dynamic(urlsProvider)
}

public init(identifier: String, urls: [URL]) {
self.init(identifier: identifier, urlsProvider: { _ in urls })
public init(
flag: UInt64 = 0,
identifier: String,
urls: [URL]
) {
self.flag = flag
self.identifier = identifier
self.urlContainer = .multiple(urls)
}

func makeURLs(context: borrowing Context) -> [URL] {
_urlsProvider(context)
switch urlContainer {
case .single(let url):
return [url]
case .multiple(let urls):
return urls
case .dynamic(let provider):
return provider(context)
}
}

}
Expand All @@ -54,7 +80,8 @@ public struct MultiplexImage: Hashable, Sendable {
extension MultiplexImage {

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