2023-12-15 16:06:36 +00:00
|
|
|
//
|
|
|
|
// LibraryIconView.swift
|
|
|
|
// Jel
|
|
|
|
//
|
|
|
|
// Created by zerocool on 12/15/23.
|
|
|
|
//
|
|
|
|
|
|
|
|
import SwiftUI
|
2023-12-22 22:14:21 +00:00
|
|
|
import JellyfinKit
|
2023-12-23 19:15:01 +00:00
|
|
|
import NukeUI
|
2023-12-15 16:06:36 +00:00
|
|
|
|
|
|
|
struct LibraryIconView: View {
|
2023-12-22 22:14:21 +00:00
|
|
|
@EnvironmentObject var jellyfinClient: JellyfinClientController
|
|
|
|
|
2023-12-23 19:15:01 +00:00
|
|
|
var library: BaseItemDto
|
2023-12-23 05:53:44 +00:00
|
|
|
|
2023-12-23 19:15:01 +00:00
|
|
|
var imageType: String = "Primary"
|
2023-12-23 05:53:44 +00:00
|
|
|
var width: CGFloat?
|
|
|
|
var height: CGFloat?
|
2023-12-22 22:14:21 +00:00
|
|
|
|
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
|
|
|
|
|
2023-12-24 07:36:14 +00:00
|
|
|
@State var placeHolder: AnyView?
|
|
|
|
|
2023-12-27 18:07:20 +00:00
|
|
|
var shouldShowCaption: Bool = false
|
2023-12-24 04:47:21 +00:00
|
|
|
var imageCornerRadius: CGFloat = 5
|
2023-12-22 22:14:21 +00:00
|
|
|
var body: some View {
|
2023-12-27 18:07:20 +00:00
|
|
|
VStack(alignment: .leading) {
|
2023-12-23 19:15:01 +00:00
|
|
|
LazyImage(url: imageUrl) {state in
|
|
|
|
if let image = state.image {
|
|
|
|
image
|
|
|
|
.resizable()
|
2023-12-27 13:25:35 +00:00
|
|
|
.aspectRatio(contentMode: contentMode)
|
2023-12-23 19:15:01 +00:00
|
|
|
} else {
|
2023-12-24 07:36:14 +00:00
|
|
|
if let content = placeHolder {
|
|
|
|
content
|
|
|
|
} else {
|
|
|
|
Image(uiImage: blurHashImage)
|
|
|
|
.resizable()
|
2023-12-27 13:25:35 +00:00
|
|
|
.aspectRatio(contentMode: .fill)
|
2023-12-24 07:36:14 +00:00
|
|
|
}
|
2023-12-23 19:15:01 +00:00
|
|
|
}
|
2023-12-23 16:14:53 +00:00
|
|
|
}
|
2023-12-23 19:15:01 +00:00
|
|
|
.frame(width: width, height: height)
|
2023-12-24 04:47:21 +00:00
|
|
|
.clipShape(RoundedRectangle(cornerRadius: imageCornerRadius))
|
|
|
|
.onAppear {
|
|
|
|
let blurhash = library.imageBlurHashes?.primary?[library.imageTags?[imageType] ?? ""] ?? ""
|
2023-12-27 13:25:35 +00:00
|
|
|
blurHashImage = UIImage(blurHash: blurhash, size: CGSize(width: 32, height: 32)) ?? UIImage()
|
2023-12-24 04:47:21 +00:00
|
|
|
|
|
|
|
let imageId = library.id ?? ""
|
|
|
|
let request = Paths.getItemImage(itemID: imageId, imageType: imageType)
|
|
|
|
imageUrl = jellyfinClient.getUrl()?.appending(path: request.url?.absoluteString ?? "")
|
|
|
|
}
|
2023-12-22 22:14:21 +00:00
|
|
|
|
2023-12-24 04:47:21 +00:00
|
|
|
if shouldShowCaption {
|
|
|
|
Text(library.name ?? "Unknown")
|
|
|
|
.font(.subheadline)
|
|
|
|
}
|
2023-12-15 16:06:36 +00:00
|
|
|
}
|
2023-12-22 22:14:21 +00:00
|
|
|
}
|
2023-12-27 18:07:20 +00:00
|
|
|
func showCaption(_ showCaption: Bool = true) -> Self {
|
2023-12-24 04:47:21 +00:00
|
|
|
var copy = self
|
2023-12-27 18:07:20 +00:00
|
|
|
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
|
|
|
|
}
|
2023-12-27 13:25:35 +00:00
|
|
|
|
|
|
|
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-15 16:06:36 +00:00
|
|
|
}
|
|
|
|
|
2023-12-23 00:29:07 +00:00
|
|
|
//#Preview {
|
|
|
|
// LibraryIconView(library: BaseItemDto())
|
|
|
|
//}
|