summaryrefslogtreecommitdiff
path: root/Jel/Views/Item/ItemIconView.swift
blob: 906ed0109a3aefedc56c0a727d5f2300d46946cd (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//
//  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
  
  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 placeHolder: AnyView?
  
  var shouldShowCaption: Bool = false
  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 {
            if let blurHash = blurHashImage {
              Image(uiImage: blurHash)
                .resizable()
                .aspectRatio(contentMode: .fill)
            } else {
              Color(uiColor: UIColor.secondarySystemBackground)
            }
          }
        }
      }
      .frame(width: width, height: height)
      .clipShape(RoundedRectangle(cornerRadius: imageCornerRadius))
      .onAppear {
        let blurhash = getBlurHash(imageType: imageType)
        blurHashImage = UIImage(blurHash: blurhash, size: CGSize(width: 32, height: 32))
        
        let imageId = item.id ?? ""
        let request = Paths.getItemImage(itemID: imageId, imageType: imageType)
        imageUrl = jellyfinClient.getUrl()?.appending(path: request.url?.absoluteString ?? "")
      }
      
      if shouldShowCaption {
        Text(item.name ?? "Unknown")
          .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 {
    var copy = self
    copy.shouldShowCaption = showCaption
    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
  }
}

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