summaryrefslogtreecommitdiff
path: root/Sora/Views/FavoritePostThumbnailView.swift
blob: 661f34a0f7aba94cf43e9c6df9dc8511f5374d20 (plain) (blame)
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
import NetworkImage
import SwiftUI

struct FavoritePostThumbnailView: View {
  @EnvironmentObject var settings: SettingsManager
  let favorite: SettingsFavoritePost
  let onRemove: () -> Void

  private var thumbnailURL: URL? {
    switch settings.thumbnailQuality {
    case .preview:
      favorite.previewUrl.flatMap(URL.init)

    case .sample:
      favorite.previewUrl.flatMap(URL.init)

    case .original:
      favorite.fileUrl.flatMap(URL.init)
    }
  }

  @ViewBuilder
  private func primaryImageContent(image: Image) -> some View {
    let isFiltered = settings.blurRatings.contains(favorite.rating)

    image
      .resizable()
      .aspectRatio(contentMode: .fit)
      .blur(radius: isFiltered ? 8 : 0)
      .clipped()
      .animation(.default, value: isFiltered)
      .clipShape(RoundedRectangle(cornerRadius: 8))
  }

  @ViewBuilder
  private func imageContent(image: Image) -> some View {
    if settings.uniformThumbnailGrid {
      GeometryReader { proxy in
        primaryImageContent(image: image)
          .frame(width: proxy.size.width, height: proxy.size.width)
      }
      .clipped()
      .aspectRatio(1, contentMode: .fit)
    } else {
      primaryImageContent(image: image)
    }
  }

  var body: some View {
    NetworkImage(
      url: thumbnailURL,
      transaction: Transaction(animation: .default)
    ) { image in
      imageContent(image: image)
    } placeholder: {
      PostGridThumbnailPlaceholderView()
    }
  }
}

#Preview {
  let sampleFavorite = SettingsFavoritePost(
    post: BooruPost(
      id: "123",
      height: 100,
      score: "10",
      fileURL: URL(string: "https://example.com/file.jpg")!,
      parentID: "0",
      sampleURL: URL(string: "https://example.com/sample.jpg")!,
      sampleWidth: 100,
      sampleHeight: 100,
      previewURL: URL(string: "https://example.com/preview.jpg")!,
      rating: .safe,
      tags: ["sample", "test"],
      width: 100,
      change: nil,
      md5: "abc123",
      creatorID: "1",
      authorID: nil,
      createdAt: Date(),
      status: "active",
      source: "",
      previewWidth: 100,
      previewHeight: 100
    ),
    provider: .yandere
  )

  FavoritePostThumbnailView(favorite: sampleFavorite) { () }
    .environmentObject(SettingsManager())
}