summaryrefslogtreecommitdiff
path: root/Jel/Views/Library/LibraryIconView.swift
blob: 6a7d5ae73167810ee78a7037fed9eadd672a4f5d (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
57
58
59
//
//  LibraryIconView.swift
//  Jel
//
//  Created by zerocool on 12/15/23.
//

import SwiftUI
import JellyfinKit

struct LibraryIconView: View {
  @EnvironmentObject var jellyfinClient: JellyfinClientController
  
  @State var library: BaseItemDto
  
  @State var loadingImage: Bool = true
  @State var imageType: String = "Primary"
  var width: CGFloat?
  var height: CGFloat?
  
  @State var blurHashImage: UIImage = UIImage()
  @State var imageUrl: URL?
  var body: some View {
    AsyncImage(url: imageUrl) {image in
      VStack {
        image
          .resizable()
          .aspectRatio(contentMode: .fit)
          .frame(width: width, height: height)
          .clipShape(RoundedRectangle(cornerRadius: 3))
        Text(library.name ?? "Unknown")
          .font(.subheadline)
      }
    } placeholder: {
      VStack {
        Image(uiImage: blurHashImage)
          .resizable()
          .aspectRatio(contentMode: .fit)
          .frame(width: width, height: height)
          .clipShape(RoundedRectangle(cornerRadius: 3))
        Text(library.name ?? "Unknown")
          .font(.subheadline)
      }
    }
    
    .onAppear {
      let blurhash = library.imageBlurHashes?.primary?[library.imageTags?[imageType] ?? ""] ?? ""
      blurHashImage = UIImage(blurHash: blurhash, size: CGSize(width: 32, height: 32)) ?? 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())
//}