Bubble/Threaded/Components/OnlineImage.swift

81 lines
2.4 KiB
Swift
Raw Normal View History

2023-12-29 11:17:37 +01:00
//Made by Lumaa
import SwiftUI
import Nuke
import NukeUI
2023-12-29 11:17:37 +01:00
struct OnlineImage: View {
var url: URL?
var size: CGFloat = 500
var priority: ImageRequest.Priority = .normal
var useNuke: Bool = true
2023-12-29 11:17:37 +01:00
var body: some View {
if useNuke {
LazyImage(url: url) { state in
if let image = state.image {
image
.resizable()
.scaledToFit()
.aspectRatio(1.0, contentMode: .fit)
} 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
}
}
.priority(priority)
.processors([.resize(width: size)])
} else {
AsyncImage(url: url) { element in
element
.resizable()
.scaledToFit()
.aspectRatio(1.0, contentMode: .fit)
2024-01-02 19:10:53 +01:00
.frame(width: size)
} placeholder: {
Rectangle()
.fill(Color.gray)
.overlay {
ProgressView()
.progressViewStyle(.circular)
}
}
2023-12-29 11:17:37 +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
}
/// 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
}
/// 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
}