aboutsummaryrefslogtreecommitdiff
path: root/iTexSnip/Views/DetailedSnippetView.swift
blob: f63284dbf44e0a53c0b2b8059b0479b971b831b0 (plain)
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
//
//  DetailedSnippetView.swift
//  iTexSnip
//
//  Created by Navan Chauhan on 10/21/24.
//

import LaTeXSwiftUI
import SwiftUI

struct DetailedSnippetView: View {
  @Environment(\.modelContext) var modelContext
  @Environment(\.dismiss) private var dismiss

  @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()
      }
      if showOriginal {
        HStack {
          Spacer()
          Image(nsImage: NSImage(data: snippet.image)!)
            .resizable()
            .clipped()
            .cornerRadius(10)
            .scaledToFit()
            .frame(height: 100)
          Spacer()
        }.padding()
      }
      HStack {
        Spacer()
        LaTeXEquationView(equation: snippet.transcribedText!)
          .clipped()
          .scaledToFit()
          .frame(height: 100)
        Spacer()
      }
      GeometryReader { geometry in
        HStack {
          Button {
            print("Should Copy")
          } label: {
            Image(systemName: "document.on.clipboard")
          }
          ScrollView(.horizontal) {
            Text(snippet.transcribedText!)
              .frame(height: 50)
              .textSelection(.enabled)
          }
          .frame(width: geometry.size.width * 0.8)
          .border(Color.red)

          Spacer()
          Button {
            print("Should Copy")
          } label: {
            Image(systemName: "document.on.clipboard")
          }
        }
      }
      Spacer()
    }
  }

  func updateRating(_ rating: Bool?) {
    withAnimation {
      self.snippet.rate(rating)
      do {
        try modelContext.save()
      } catch {
        print("Error saving rating: \(error)")
      }
    }
  }
}

struct LaTeXEquationView: View {
  var equation: String
  var body: some View {
    LaTeX(equation)
      .parsingMode(.all)
      .font(.system(size: 28, weight: .bold))
  }
}