jel/Jel/Views/Library/LibraryIconView.swift

78 lines
2.0 KiB
Swift

//
// LibraryIconView.swift
// Jel
//
// Created by zerocool on 12/15/23.
//
import SwiftUI
import JellyfinKit
import NukeUI
struct LibraryIconView: View {
@EnvironmentObject var jellyfinClient: JellyfinClientController
var library: BaseItemDto
var imageType: String = "Primary"
var width: CGFloat?
var height: CGFloat?
@State var blurHashImage: UIImage = UIImage()
@State var imageUrl: URL?
@State var contentMode: ContentMode = .fit
@State var placeHolder: AnyView?
var shouldShowCaption: Bool = true
var imageCornerRadius: CGFloat = 5
var body: some View {
VStack {
LazyImage(url: imageUrl) {state in
if let image = state.image {
image
.resizable()
} else {
if let content = placeHolder {
content
} else {
Image(uiImage: blurHashImage)
.resizable()
}
}
}
.aspectRatio(contentMode: contentMode)
.frame(width: width, height: height)
.clipShape(RoundedRectangle(cornerRadius: imageCornerRadius))
.onAppear {
let blurhash = library.imageBlurHashes?.primary?[library.imageTags?[imageType] ?? ""] ?? ""
blurHashImage = UIImage(blurHash: blurhash, size: CGSize(width: 16, height: 16)) ?? UIImage()
let imageId = library.id ?? ""
let request = Paths.getItemImage(itemID: imageId, imageType: imageType)
imageUrl = jellyfinClient.getUrl()?.appending(path: request.url?.absoluteString ?? "")
}
if shouldShowCaption {
Text(library.name ?? "Unknown")
.font(.subheadline)
}
}
}
func hideCaption(_ isHidden: Bool = true) -> Self {
var copy = self
copy.shouldShowCaption = !isHidden
return copy
}
func setCornerRadius(_ cornerRadius: CGFloat = 5) -> Self {
var copy = self
copy.imageCornerRadius = cornerRadius
return copy
}
}
//#Preview {
// LibraryIconView(library: BaseItemDto())
//}