2023-12-29 11:17:37 +01:00
|
|
|
//Made by Lumaa
|
|
|
|
|
|
|
|
import SwiftUI
|
2024-01-02 14:23:36 +01:00
|
|
|
import Nuke
|
|
|
|
import NukeUI
|
2023-12-29 11:17:37 +01:00
|
|
|
|
|
|
|
struct OnlineImage: View {
|
2024-01-02 14:23:36 +01:00
|
|
|
var url: URL?
|
2024-01-04 22:19:35 +01:00
|
|
|
var size: CGFloat? = 500
|
|
|
|
var maxSize: CGFloat? = 500
|
2024-01-02 14:23:36 +01:00
|
|
|
var priority: ImageRequest.Priority = .normal
|
|
|
|
var useNuke: Bool = true
|
2023-12-29 11:17:37 +01:00
|
|
|
|
|
|
|
var body: some View {
|
2024-01-02 14:23:36 +01:00
|
|
|
if useNuke {
|
|
|
|
LazyImage(url: url) { state in
|
|
|
|
if let image = state.image {
|
|
|
|
image
|
|
|
|
.resizable()
|
|
|
|
.scaledToFit()
|
|
|
|
.aspectRatio(1.0, contentMode: .fit)
|
2024-01-04 22:19:35 +01:00
|
|
|
.frame(idealWidth: size, maxWidth: maxSize)
|
2024-01-02 14:23:36 +01:00
|
|
|
} else if state.error != nil {
|
|
|
|
ContentUnavailableView("error.loading-image", systemImage: "rectangle.slash")
|
|
|
|
} else {
|
|
|
|
Rectangle()
|
|
|
|
.fill(Color.gray)
|
|
|
|
.overlay {
|
|
|
|
ProgressView()
|
|
|
|
.progressViewStyle(.circular)
|
|
|
|
}
|
2023-12-29 11:17:37 +01:00
|
|
|
}
|
2024-01-02 14:23:36 +01:00
|
|
|
}
|
|
|
|
.priority(priority)
|
2024-01-04 22:19:35 +01:00
|
|
|
.processors([.resize(width: size ?? 500)])
|
2024-01-02 14:23:36 +01:00
|
|
|
} else {
|
|
|
|
AsyncImage(url: url) { element in
|
|
|
|
element
|
|
|
|
.resizable()
|
|
|
|
.scaledToFit()
|
|
|
|
.aspectRatio(1.0, contentMode: .fit)
|
2024-01-04 22:19:35 +01:00
|
|
|
.frame(minWidth: size, maxWidth: maxSize, alignment: .topLeading)
|
2024-01-02 14:23:36 +01:00
|
|
|
} placeholder: {
|
|
|
|
Rectangle()
|
|
|
|
.fill(Color.gray)
|
|
|
|
.overlay {
|
|
|
|
ProgressView()
|
|
|
|
.progressViewStyle(.circular)
|
|
|
|
}
|
|
|
|
}
|
2023-12-29 11:17:37 +01:00
|
|
|
}
|
|
|
|
}
|
2024-01-02 14:23:36 +01:00
|
|
|
|
2024-01-02 19:10:53 +01:00
|
|
|
init(url: URL? = nil, useNuke: Bool) {
|
|
|
|
self.url = url
|
|
|
|
self.size = 500
|
|
|
|
self.priority = .normal
|
|
|
|
self.useNuke = useNuke
|
|
|
|
}
|
|
|
|
|
2024-01-02 14:23:36 +01:00
|
|
|
/// Creates a new OnlineImage using Nuke or not, default priority is .normal
|
|
|
|
init(url: URL? = nil, size: CGFloat, useNuke: Bool) {
|
|
|
|
self.url = url
|
|
|
|
self.size = size
|
|
|
|
self.priority = .normal
|
|
|
|
self.useNuke = useNuke
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a new OnlineImage using Nuke, using the selected priority
|
|
|
|
init(url: URL? = nil, size: CGFloat, priority: ImageRequest.Priority) {
|
|
|
|
self.url = url
|
|
|
|
self.size = size
|
|
|
|
self.priority = priority
|
|
|
|
self.useNuke = true
|
|
|
|
}
|
|
|
|
|
2024-01-04 22:19:35 +01:00
|
|
|
init(url: URL? = nil, maxSize: CGFloat, useNuke: Bool) {
|
|
|
|
self.url = url
|
|
|
|
self.maxSize = maxSize
|
|
|
|
self.size = nil
|
|
|
|
self.priority = .normal
|
|
|
|
self.useNuke = useNuke
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a new OnlineImage using Nuke, using the selected priority
|
|
|
|
init(url: URL? = nil, maxSize: CGFloat, priority: ImageRequest.Priority) {
|
|
|
|
self.url = url
|
|
|
|
self.maxSize = maxSize
|
|
|
|
self.size = nil
|
|
|
|
self.priority = priority
|
|
|
|
self.useNuke = true
|
|
|
|
}
|
|
|
|
|
|
|
|
init(url: URL? = nil, size: CGFloat, maxSize: CGFloat, useNuke: Bool) {
|
|
|
|
self.url = url
|
|
|
|
self.maxSize = maxSize
|
|
|
|
self.size = size
|
|
|
|
self.priority = .normal
|
|
|
|
self.useNuke = useNuke
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a new OnlineImage using Nuke, using the selected priority
|
|
|
|
init(url: URL? = nil, size: CGFloat, maxSize: CGFloat, priority: ImageRequest.Priority) {
|
|
|
|
self.url = url
|
|
|
|
self.maxSize = maxSize
|
|
|
|
self.size = size
|
|
|
|
self.priority = priority
|
|
|
|
self.useNuke = true
|
|
|
|
}
|
|
|
|
|
2024-01-02 14:23:36 +01:00
|
|
|
/// Change the priority of the Nuke OnlineImage
|
|
|
|
mutating func setPriority(_ priority: ImageRequest.Priority) {
|
|
|
|
guard self.useNuke == true else { return }
|
|
|
|
self.priority = priority
|
|
|
|
}
|
2023-12-29 11:17:37 +01:00
|
|
|
}
|