SPB Git

Repository Public MIT

MathCameraView.swift 7.6 KB 148 lines swift

1// ORIX Calc2// File: App/Sources/MathCameraView.swift3// Description: ORIX Math Camera (Beta) — photograph or pick an image of a printed expression, review and edit the reading, send it to the calculator.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import SwiftUI8import PhotosUI9import ORIXUI10import ORIXExpression11import ORIXMathCamera12#if canImport(UIKit)13import UIKit14#endif1516struct MathCameraView: View {17    @Environment(AppModel.self) private var app18    @Environment(\.dismiss) private var dismiss19    @State private var pickerItem: PhotosPickerItem?20    @State private var image: UIImage?21    @State private var lines: [[MathCandidate]] = []22    @State private var text = ""23    @State private var isWorking = false24    @State private var errorMessage: String?25    @State private var showCamera = false2627    private var parsed: Expr? { try? Parser.parse(text, options: app.engine.parserOptions) }28    private var cameraAvailable: Bool { UIImagePickerController.isSourceTypeAvailable(.camera) }2930    var body: some View {31        NavigationStack {32            ORIXPage {33                MessageBanner(.info, title: "Beta — always review the reading",34                              message: "Math Camera reads printed, single-line expressions such as 12.5 × (3 + 4). Stacked fractions, raised exponents, radicals and handwriting are not read reliably. Nothing is calculated until you confirm the text. Images stay on this device.")35                ORIXCard("Image", symbol: "camera.viewfinder") {36                    if let image {37                        Image(uiImage: image).resizable().scaledToFit().frame(maxHeight: 220).clipShape(RoundedRectangle(cornerRadius: 12))38                            .accessibilityLabel("Selected image")39                    }40                    HStack(spacing: ORIXSpacing.m) {41                        if cameraAvailable {42                            Button { showCamera = true } label: { Label("Camera", systemImage: "camera") }.buttonStyle(.borderedProminent)43                        }44                        PhotosPicker(selection: $pickerItem, matching: .images) { Label("Photo Library", systemImage: "photo") }.buttonStyle(.bordered)45                        Button { pasteImage() } label: { Label("Paste", systemImage: "doc.on.clipboard") }.buttonStyle(.bordered)46                    }47                    if isWorking { ProgressView("Reading…") }48                    if let errorMessage { MessageBanner(.warning, message: errorMessage) }49                }50                if !lines.isEmpty {51                    ORIXCard("Readings", symbol: "text.viewfinder") {52                        ForEach(Array(lines.enumerated()), id: \.offset) { _, candidates in53                            ForEach(candidates) { c in54                                Button { text = c.normalized } label: {55                                    HStack {56                                        Image(systemName: c.parses ? "checkmark.circle" : "questionmark.circle").foregroundStyle(c.parses ? ORIXColor.success : ORIXColor.warning)57                                        Text(c.normalized).font(ORIXFont.mono).foregroundStyle(ORIXColor.textPrimary).lineLimit(2)58                                        Spacer()59                                        Text("\(Int(c.confidence * 100))%").font(.caption).foregroundStyle(ORIXColor.textTertiary)60                                    }61                                }62                                .buttonStyle(.plain)63                                .accessibilityLabel("\(c.normalized), confidence \(Int(c.confidence * 100)) percent, \(c.parses ? "valid syntax" : "not valid yet")")64                            }65                        }66                        Text("A check mark means the syntax is valid — not that the reading matches the image.").font(.caption).foregroundStyle(ORIXColor.textSecondary)67                    }68                }69                ORIXCard("Expression", symbol: "pencil") {70                    TextField("Edit the expression", text: $text, axis: .vertical)71                        .font(ORIXFont.mono)72                        .textInputAutocapitalization(.never).autocorrectionDisabled()73                        .padding(10).background(ORIXColor.surfaceRaised, in: RoundedRectangle(cornerRadius: ORIXRadius.field))74                        .accessibilityIdentifier("camera.expression")75                    if let parsed {76                        LabeledContent("Read as") { Text(Printer.string(parsed)).font(ORIXFont.value) }77                    } else if !text.isEmpty {78                        Text("Not a valid expression yet.").font(.callout).foregroundStyle(ORIXColor.warning)79                    }80                    PrimaryButton("Use in Calculator", symbol: "arrow.right.circle", tint: CalcMode.scientific.tint) {81                        app.sendToCalculator(text)82                        dismiss()83                    }84                    .disabled(parsed == nil)85                    .opacity(parsed == nil ? 0.5 : 1)86                }87            }88            .navigationTitle("Math Camera")89            .navigationBarTitleDisplayMode(.inline)90            .toolbar { ToolbarItem(placement: .cancellationAction) { Button("Close") { dismiss() } } }91            .onChange(of: pickerItem) { _, item in92                guard let item else { return }93                Task {94                    if let data = try? await item.loadTransferable(type: Data.self), let picked = UIImage(data: data) { await read(picked) }95                    else { errorMessage = "The image could not be loaded." }96                }97            }98            .fullScreenCover(isPresented: $showCamera) {99                CameraPicker { captured in100                    showCamera = false101                    if let captured { Task { await read(captured) } }102                }103                .ignoresSafeArea()104            }105        }106    }107108    private func pasteImage() {109        guard let pasted = UIPasteboard.general.image else { errorMessage = "There is no image on the clipboard."; return }110        Task { await read(pasted) }111    }112113    private func read(_ picked: UIImage) async {114        image = picked115        errorMessage = nil116        lines = []117        guard let cg = picked.cgImage else { errorMessage = "The image could not be read."; return }118        isWorking = true119        defer { isWorking = false }120        do {121            lines = try await MathRecognizer.recognize(cg)122            if let best = lines.first?.first { text = best.normalized }123        } catch let e as MathRecognizer.RecognitionError { errorMessage = e.description }124        catch { errorMessage = "Recognition failed." }125    }126}127128private struct CameraPicker: UIViewControllerRepresentable {129    let onFinish: (UIImage?) -> Void130    func makeCoordinator() -> Coordinator { Coordinator(onFinish: onFinish) }131    func makeUIViewController(context: Context) -> UIImagePickerController {132        let picker = UIImagePickerController()133        picker.sourceType = .camera134        picker.delegate = context.coordinator135        return picker136    }137    func updateUIViewController(_ controller: UIImagePickerController, context: Context) {}138139    final class Coordinator: NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate {140        let onFinish: (UIImage?) -> Void141        init(onFinish: @escaping (UIImage?) -> Void) { self.onFinish = onFinish }142        func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {143            onFinish(info[.originalImage] as? UIImage)144        }145        func imagePickerControllerDidCancel(_ picker: UIImagePickerController) { onFinish(nil) }146    }147}148
Files main
Menu

Theme