summaryrefslogtreecommitdiff
path: root/Jel/Views/Library/LibraryIconView.swift
blob: 71c6e1452748fb41fa4bc7069e48d59f391f5062 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//
//  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?
  var body: some View {
    VStack {
      LazyImage(url: imageUrl) {state in
        if let image = state.image {
          image
            .resizable()
        } else if state.error != nil {
          Color.red
        } else {
          Image(uiImage: blurHashImage)
            .resizable()
        }
      }
      .aspectRatio(contentMode: .fit)
      .frame(width: width, height: height)
      .clipShape(RoundedRectangle(cornerRadius: 5))
      
      Text(library.name ?? "Unknown")
        .font(.subheadline)
        .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 ?? "")
        }
    }
  }
}

//#Preview {
//  LibraryIconView(library: BaseItemDto())
//}