summaryrefslogtreecommitdiff
path: root/Sora/Other/AsyncImageWithPreview.swift
blob: 48df44e9078de1397f0e3dc236083efa501a7333 (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
import SwiftUI

struct AsyncImageWithPreview<Placeholder: View>: View {
  var url: URL?
  @Binding var loadingState: PostLoadingState
  var finalLoadingState: PostLoadingState
  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

  init(
    url: URL?,
    loadingStage: Binding<PostLoadingState>,
    finalLoadingState: PostLoadingState = .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
  }

  var body: some View {
    GeometryReader { geometry in
      AsyncImage(url: url) { image in
        image
          .resizable()
          .scaledToFit()
          .onAppear {
            loadingState = finalLoadingState
          }
          .scaleEffect(finalScale * currentScale)
          .offset(
            x: finalOffset.width + currentOffset.width,
            y: finalOffset.height + currentOffset.height
          )
          .frame(width: geometry.size.width, height: geometry.size.height)
          .position(x: geometry.size.width / 2, y: geometry.size.height / 2)
          .gesture(
            DragGesture()
              .onChanged { value in
                let translation = value.translation
                let newOffset = CGSize(
                  width: finalOffset.width + translation.width,
                  height: finalOffset.height + translation.height
                )
                let scale = finalScale * currentScale
                let imageWidth = geometry.size.width * scale
                let imageHeight = geometry.size.height * scale
                let maxX = max((imageWidth - geometry.size.width) / 2, 0)
                let maxY = max((imageHeight - geometry.size.height) / 2, 0)
                let clampedX = min(max(newOffset.width, -maxX), maxX)
                let clampedY = min(max(newOffset.height, -maxY), maxY)

                currentOffset = CGSize(
                  width: clampedX - finalOffset.width,
                  height: clampedY - finalOffset.height
                )
              }
              .onEnded { value in
                let translation = value.translation
                var newOffset = CGSize(
                  width: finalOffset.width + translation.width,
                  height: finalOffset.height + translation.height
                )
                let scale = finalScale * currentScale
                let imageWidth = geometry.size.width * scale
                let imageHeight = geometry.size.height * scale
                let maxX = max((imageWidth - geometry.size.width) / 2, 0)
                let maxY = max((imageHeight - geometry.size.height) / 2, 0)

                newOffset.width = min(max(newOffset.width, -maxX), maxX)
                newOffset.height = min(max(newOffset.height, -maxY), maxY)
                finalOffset = newOffset
                currentOffset = .zero
              }
          )
          .simultaneousGesture(
            MagnificationGesture()
              .onChanged { value in
                currentScale = value
              }
              .onEnded { _ in
                finalScale *= currentScale
                currentScale = 1.0

                let scale = finalScale
                let imageWidth = geometry.size.width * scale
                let imageHeight = geometry.size.height * scale
                let maxX = max((imageWidth - geometry.size.width) / 2, 0)
                let maxY = max((imageHeight - geometry.size.height) / 2, 0)

                finalOffset.width = min(max(finalOffset.width, -maxX), maxX)
                finalOffset.height = min(max(finalOffset.height, -maxY), maxY)
              }
          )
          .highPriorityGesture(
            TapGesture(count: 2)
              .onEnded {
                withAnimation {
                  finalScale = 1.0
                  currentScale = 1.0
                  finalOffset = .zero
                  currentOffset = .zero
                }
              }
          )
          .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)
              Button {
                let activityViewController = UIActivityViewController(
                  activityItems: [url ?? URL(string: "")!], applicationActivities: nil)

                UIApplication.shared.windows.first?.rootViewController?.present(
                  activityViewController, 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
          }
      }
    }
  }
}