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
206
207
208
209
210
211
212
213
214
215
216
217
|
//
// DetailedSnippetView.swift
// iTexSnip
//
// Created by Navan Chauhan on 10/21/24.
//
import Foundation
import LaTeXSwiftUI
import SwiftUI
struct DetailedSnippetView: View {
@Environment(\.modelContext) var modelContext
@Environment(\.dismiss) private var dismiss
@AppStorage("apiEndpoint") var apiEndpoint: String =
"https://api.itexsnip.navan.dev/rate_snippet"
@State var showOriginal = false
var snippet: ImageSnippet
var body: some View {
VStack {
HStack {
Toggle("Show Original", isOn: $showOriginal)
.toggleStyle(.switch)
.padding()
Spacer()
HStack {
Button {
if snippet.rating == true {
updateRating(nil)
} else {
updateRating(true)
}
} label: {
if snippet.rating == true {
Image(systemName: "hand.thumbsup")
.foregroundStyle(.green)
.imageScale(.large)
} else {
Image(systemName: "hand.thumbsup")
.imageScale(.large)
}
}.buttonStyle(PlainButtonStyle())
Button {
if snippet.rating == false {
updateRating(nil)
} else {
updateRating(false)
}
} label: {
if snippet.rating == false {
Image(systemName: "hand.thumbsdown")
.foregroundStyle(.red)
.imageScale(.large)
} else {
Image(systemName: "hand.thumbsdown")
.imageScale(.large)
}
}.buttonStyle(PlainButtonStyle())
Button(role: .destructive) {
withAnimation {
modelContext.delete(snippet)
do {
try modelContext.save()
dismiss()
} catch {
print("Failed to delete snippet: \(error)")
}
}
} label: {
Image(systemName: "trash")
.imageScale(.large)
}
.padding()
.buttonStyle(PlainButtonStyle())
}.padding()
}
ScrollView {
if showOriginal {
HStack {
Spacer()
Image(nsImage: NSImage(data: snippet.image)!)
.resizable()
.clipped()
.cornerRadius(10)
.scaledToFit()
.frame(height: 100)
Spacer()
}.padding()
}
HStack {
Spacer()
if snippet.transcribedText != nil {
LaTeXEquationView(equation: snippet.transcribedText!)
.clipped()
.scaledToFit()
.frame(height: 100)
}
Spacer()
}
VStack {
if snippet.transcribedText != nil {
LaTeXCopyView(latex: snippet.transcribedText!, textStart: "", textEnd: "")
LaTeXCopyView(latex: snippet.transcribedText!, textStart: "$", textEnd: "$")
LaTeXCopyView(latex: snippet.transcribedText!, textStart: "$$", textEnd: "$$")
LaTeXCopyView(
latex: snippet.transcribedText!, textStart: "\\begin{equation}",
textEnd: "\\end{equation}")
}
}
}.frame(height: 450)
Spacer()
}
}
func updateRating(_ rating: Bool?) {
withAnimation {
self.snippet.rate(rating)
do {
if let rating = rating {
if var url = URL(string: "\(self.apiEndpoint)") {
let queryItem = URLQueryItem(name: "good", value: "\(rating)")
url.append(queryItems: [queryItem])
let boundary = "Boundary-\(UUID().uuidString)"
let headers = [
"accept": "application/json",
"Content-Type": "multipart/form-data; boundary=\(boundary)",
]
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.allHTTPHeaderFields = headers
var body = Data()
body.append("--\(boundary)\r\n".data(using: .utf8)!)
body.append("Content-Disposition: form-data; name=\"good\"\r\n\r\n".data(using: .utf8)!)
body.append("\(rating)\r\n".data(using: .utf8)!)
let data = self.snippet.image
let mimeType = "image/jpeg" // We detect the filetype on the server anyway
let filename = "image"
body.append("--\(boundary)\r\n".data(using: .utf8)!)
body.append(
"Content-Disposition: form-data; name=\"image\"; filename=\"\(filename)\"\r\n".data(
using: .utf8)!)
body.append("Content-Type: \(mimeType)\r\n\r\n".data(using: .utf8)!)
body.append(data)
body.append("\r\n".data(using: .utf8)!)
body.append("--\(boundary)--\r\n".data(using: .utf8)!)
request.httpBody = body
let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
if let error = error {
print(error)
} else if let data = data {
let str = String(data: data, encoding: .utf8)
print(str ?? "")
}
}
task.resume()
}
} else {
print("Could not create URLRequest")
}
try modelContext.save()
} catch {
print("Error saving rating: \(error)")
}
}
}
}
struct LaTeXCopyView: View {
var latex: String
var textStart: String
var textEnd: String
var body: some View {
HStack {
ScrollView(.horizontal) {
Text("\(textStart) \(latex) \(textEnd)")
.frame(height: 20)
.textSelection(.enabled)
.padding()
}
.frame(width: 400)
.border(Color.accentColor)
Button {
print("Should Copy")
let pasteboard = NSPasteboard.general
pasteboard.clearContents()
pasteboard.setString("\(textStart) \(latex) \(textEnd)", forType: .string)
} label: {
Image(systemName: "document.on.clipboard")
}
.buttonStyle(PlainButtonStyle())
.imageScale(.large)
}
}
}
struct LaTeXEquationView: View {
var equation: String
var body: some View {
LaTeX(equation)
.parsingMode(.all)
.font(.system(size: 28, weight: .bold))
}
}
|