summaryrefslogtreecommitdiff
path: root/Sora/Views/Post/Details/PostDetailsImageView.swift
diff options
context:
space:
mode:
authorFuwn <[email protected]>2025-02-22 07:07:57 -0800
committerFuwn <[email protected]>2025-02-22 07:07:57 -0800
commitcafece91bae45194d64f4932bb04be018b82d21b (patch)
tree02d727a13fe530550e3b29b28b7ee9980262eef2 /Sora/Views/Post/Details/PostDetailsImageView.swift
parentfeat: Development commit (diff)
downloadsora-testing-cafece91bae45194d64f4932bb04be018b82d21b.tar.xz
sora-testing-cafece91bae45194d64f4932bb04be018b82d21b.zip
feat: Development commit
Diffstat (limited to 'Sora/Views/Post/Details/PostDetailsImageView.swift')
-rw-r--r--Sora/Views/Post/Details/PostDetailsImageView.swift106
1 files changed, 106 insertions, 0 deletions
diff --git a/Sora/Views/Post/Details/PostDetailsImageView.swift b/Sora/Views/Post/Details/PostDetailsImageView.swift
new file mode 100644
index 0000000..d6307d5
--- /dev/null
+++ b/Sora/Views/Post/Details/PostDetailsImageView.swift
@@ -0,0 +1,106 @@
+import SwiftUI
+
+struct PostDetailsImageView<Placeholder: View>: View {
+ @EnvironmentObject var settings: Settings
+ var url: URL?
+ @Binding var loadingState: BooruPostLoadingState
+ var finalLoadingState: BooruPostLoadingState
+ var postURL: URL?
+ let placeholder: () -> Placeholder
+ @State private var currentScale: CGFloat = 1.0
+ @State private var finalScale: CGFloat = 1.0
+ @State private var currentOffset: CGSize = .zero
+ @State private var finalOffset: CGSize = .zero
+
+ #if os(iOS)
+ var keyWindow: UIWindow? {
+ guard
+ let window = UIApplication.shared.connectedScenes
+ .compactMap({ $0 as? UIWindowScene })
+ .flatMap(\.windows)
+ .first(where: \.isKeyWindow)
+ else {
+ return nil
+ }
+
+ return window
+ }
+ #endif
+
+ var body: some View {
+ AsyncImage(url: url) { image in
+ InteractiveImageView(image: image)
+ .onAppear {
+ loadingState = finalLoadingState
+ }
+ .contextMenu {
+ #if os(iOS)
+ Button {
+ guard let url else { return }
+
+ URLSession.shared.dataTask(with: url) { data, _, _ in
+ guard let data, let uiImage = UIImage(data: data) else { return }
+
+ UIImageWriteToSavedPhotosAlbum(uiImage, nil, nil, nil)
+ }
+ .resume()
+ } label: {
+ Label("Save Image", systemImage: "square.and.arrow.down")
+ }
+ #endif
+
+ #if os(iOS)
+ if settings.enableShareShortcut {
+ Button {
+ keyWindow?.rootViewController?.present(
+ UIActivityViewController(
+ activityItems: [url ?? URL(string: "")!], applicationActivities: nil
+ ), animated: true
+ )
+ } label: {
+ Label("Share Image", systemImage: "square.and.arrow.up")
+ }
+ }
+ #endif
+
+ if let url = postURL {
+ Button {
+ #if os(iOS)
+ UIApplication.shared.open(url)
+ #else
+ NSWorkspace.shared.open(url)
+ #endif
+ } label: {
+ Label("Open in Safari", systemImage: "safari")
+ }
+ }
+ }
+ } placeholder: {
+ placeholder()
+ .onAppear {
+ loadingState = .loadingPreview
+ }
+ }
+ }
+
+ init(
+ url: URL?,
+ loadingStage: Binding<BooruPostLoadingState>,
+ finalLoadingState: BooruPostLoadingState = .loadingFile,
+ postURL: URL? = nil,
+ @ViewBuilder placeholder: @escaping () -> Placeholder = {
+ GeometryReader { geometry in
+ ProgressView()
+ .frame(width: geometry.size.width, height: geometry.size.height)
+ .position(x: geometry.size.width / 2, y: geometry.size.height / 2)
+ .padding()
+ }
+ }
+ ) {
+ self.url = url
+ _loadingState = loadingStage
+ self.finalLoadingState = finalLoadingState
+ self.postURL = postURL
+ self.placeholder = placeholder
+ }
+}