summaryrefslogtreecommitdiff
path: root/Jel/Views/Library/LibraryIconView.swift
blob: 7651e569ac889cc4e7944a53acee6290c8978082 (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//
//  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
  
  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 state.error != nil {
          Color.red
        } 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())
//}