jel/Jel/Views/Item/ItemIconView.swift

107 lines
2.7 KiB
Swift
Raw Normal View History

//
// ItemIconView.swift
// Jel
//
// Created by zerocool on 12/15/23.
//
import SwiftUI
import JellyfinKit
import NukeUI
struct ItemIconView: View {
@EnvironmentObject var jellyfinClient: JellyfinClientController
var item: BaseItemDto
2023-12-23 05:53:44 +00:00
var imageType: String = "Primary"
2023-12-23 05:53:44 +00:00
var width: CGFloat?
var height: CGFloat?
2023-12-23 16:14:53 +00:00
@State var blurHashImage: UIImage = UIImage()
@State var imageUrl: URL?
2023-12-24 04:47:21 +00:00
@State var contentMode: ContentMode = .fit
2024-02-12 16:51:06 +00:00
var placeHolder: AnyView? = AnyView(Color(uiColor: UIColor.secondarySystemBackground))
var shouldShowCaption: Bool = false
2023-12-24 04:47:21 +00:00
var imageCornerRadius: CGFloat = 5
var body: some View {
VStack(alignment: .leading) {
LazyImage(url: imageUrl) {state in
if let image = state.image {
image
.resizable()
.aspectRatio(contentMode: contentMode)
} else {
if let content = placeHolder {
content
} else {
Image(uiImage: blurHashImage)
.resizable()
.aspectRatio(contentMode: .fill)
}
}
2023-12-23 16:14:53 +00:00
}
.frame(width: width, height: height)
2023-12-24 04:47:21 +00:00
.clipShape(RoundedRectangle(cornerRadius: imageCornerRadius))
.onAppear {
let blurhash = getBlurHash(imageType: imageType)
blurHashImage = UIImage(blurHash: blurhash, size: CGSize(width: 32, height: 32)) ?? UIImage()
2023-12-24 04:47:21 +00:00
let imageId = item.id ?? ""
2023-12-24 04:47:21 +00:00
let request = Paths.getItemImage(itemID: imageId, imageType: imageType)
imageUrl = jellyfinClient.getUrl()?.appending(path: request.url?.absoluteString ?? "")
}
2023-12-24 04:47:21 +00:00
if shouldShowCaption {
Text(item.name ?? "Unknown")
2023-12-24 04:47:21 +00:00
.font(.subheadline)
}
}
}
private func getBlurHash(imageType: String) -> String {
switch imageType {
case "Primary":
return item.imageBlurHashes?.primary?[item.imageTags?[imageType] ?? ""] ?? ""
case "Backdrop":
return item.imageBlurHashes?.backdrop?[item.backdropImageTags?[0] ?? ""] ?? ""
default:
return ""
}
}
func showCaption(_ showCaption: Bool = true) -> Self {
2023-12-24 04:47:21 +00:00
var copy = self
copy.shouldShowCaption = showCaption
2023-12-24 04:47:21 +00:00
return copy
}
func setCornerRadius(_ cornerRadius: CGFloat = 5) -> Self {
var copy = self
copy.imageCornerRadius = cornerRadius
return copy
}
func setAspectRatio(_ aspectRatio: Double?) -> Self {
var copy = self
if aspectRatio == nil {
return copy
}
if let newWidth = copy.width {
copy.height = newWidth / aspectRatio!
}
if let newHeight = copy.height {
copy.width = newHeight * aspectRatio!
}
return copy
}
}
2023-12-23 00:29:07 +00:00
//#Preview {
// LibraryIconView(library: BaseItemDto())
//}