summaryrefslogtreecommitdiff
path: root/Sora/Views/Post/Details/PostDetailsView.swift
blob: bb58c4967bd17f3e478d171698cb5f3d5a8cf003 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import SwiftUI

struct PostDetailsView: View {
  @EnvironmentObject var manager: BooruManager
  @EnvironmentObject var settings: SettingsManager
  let post: BooruPost
  @Binding var navigationPath: NavigationPath
  @State private var loadingStage: BooruPostLoadingState = .loadingPreview
  @State private var isTagsSheetPresented = false
  @State private var activePost: BooruPost
  // swiftlint:disable:next discouraged_optional_collection
  let posts: [BooruPost]?
  let baseSearchText: String?

  private var imageURL: URL? {
    switch settings.detailViewQuality {
    case .preview:
      currentPost.previewURL

    case .sample:
      currentPost.sampleURL

    case .original:
      currentPost.fileURL
    }
  }

  init(
    post: BooruPost,
    navigationPath: Binding<NavigationPath>,
    // swiftlint:disable:next discouraged_optional_collection
    posts: [BooruPost]? = nil,
    baseSearchText: String? = nil
  ) {
    self.post = post
    self._navigationPath = navigationPath
    self._activePost = State(initialValue: post)
    self.posts = posts
    self.baseSearchText = baseSearchText
  }

  var filteredPosts: [BooruPost] {
    let sourcePosts = posts ?? manager.posts

    return sourcePosts.filter { settings.displayRatings.contains($0.rating) }
  }

  private var currentPost: BooruPost {
    activePost
  }

  var body: some View {
    VStack(spacing: 0) {
      #if os(macOS)
        PostDetailsImageView(
          url: imageURL,
          loadingStage: $loadingStage,
          finalLoadingState: .loaded,
          post: currentPost
        ) {
          PostDetailsImageView(
            url: currentPost.previewURL,
            loadingStage: $loadingStage
          )
          .id(currentPost.previewURL)
        }
        .id(imageURL)
      #else
        PostDetailsCarouselView(
          posts: filteredPosts,
          loadingStage: $loadingStage,
          focusedPost: currentPost,
          onFocusedPostChange: updateActivePost
        )
        .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .center)
      #endif

      if settings.displayDetailsInformationBar {
        VStack(spacing: 5) {
          HStack {
            Text(currentPost.createdAt.formatted())
              .frame(maxWidth: .infinity, alignment: .leading)

            Group {
              switch loadingStage {
              case .loadingPreview:
                Text("Loading Preview…")

              case .loadingFile:
                Text("Loading \(settings.detailViewQuality.rawValue)…")

              case .loaded:
                EmptyView()
              }
            }
            .padding(.trailing, 5)
          }
          .frame(maxWidth: .infinity, alignment: .leading)
          .foregroundStyle(.secondary)
        }
        .padding(.horizontal, 10)
        .padding(.vertical, 10 / 1.33)
        .textSelection(.enabled)
        .font(.footnote)
        #if os(iOS)
          .background(.ultraThinMaterial)
        #else
          .background(.opacity(0.1))
        #endif
        .zIndex(1)
      }
    }
    .ifiOS26Unavailable { view in
      view
        .navigationTitle("Details")
        #if os(iOS)
          .navigationBarTitleDisplayMode(.inline)
        #endif
    }
    .toolbar {
      ToolbarItem {
        PostGridFavoriteButtonView(post: currentPost)
      }

      ToolbarItem {
        Button(action: {
          isTagsSheetPresented.toggle()
        }) {
          Label("Tags", systemImage: "tag")
        }
      }

      #if os(macOS)
        if settings.enableShareShortcut {
          if let imageURL {
            ToolbarItem {
              ShareLink(item: imageURL) {
                Label("Share", systemImage: "square.and.arrow.up")
              }
            }
          }
        }
      #endif
    }
    .sheet(isPresented: $isTagsSheetPresented) {
      if #available(macOS 15.0, *) {
        tagsSheetContent()
          #if os(macOS)
            .presentationSizing(.page)
          #endif
      } else {
        tagsSheetContent()
      }
    }
    .onAppear {
      syncActivePost()
    }
    .onChange(of: post.id) {
      activePost = post
      syncActivePost()
    }
    .onChange(of: manager.selectedPost?.id) {
      syncActivePost()
    }
  }

  @ViewBuilder
  private func tagsSheetContent() -> some View {
    PostDetailsTagsView(
      isPresented: $isTagsSheetPresented,
      navigationPath: $navigationPath,
      tags: currentPost.tags,
      isNestedContext: posts != nil,
      baseSearchText: baseSearchText
    )
    #if os(macOS)
      .frame(
        minHeight: (NSScreen.main?.frame.height ?? 1_080) / 2,
        maxHeight: .infinity
      )
    #endif
  }

  private func updateActivePost(_ post: BooruPost) {
    guard activePost.id != post.id else { return }

    activePost = post
  }

  private func syncActivePost() {
    #if os(macOS)
      if let selectedPost = manager.selectedPost {
        updateActivePost(selectedPost)
      } else {
        updateActivePost(post)
      }
    #else
      if let selectedPost = manager.selectedPost, posts == nil {
        updateActivePost(selectedPost)
      } else {
        updateActivePost(post)
      }
    #endif
  }
}