SPB Git

Repository Public MIT

Intents.swift 3.4 KB 78 lines swift

1// ORIX Calc2// File: App/Sources/Intents.swift3// Description: App Intents for Siri, Shortcuts and Spotlight — evaluate an expression or open an environment.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import AppIntents8import Foundation9import ORIXExpression10import ORIXUI1112struct CalculateIntent: AppIntent {13    static let title: LocalizedStringResource = "Calculate"14    static let description = IntentDescription("Evaluates a mathematical expression on this device with the ORIX Calc engine.")15    static let openAppWhenRun = false1617    @Parameter(title: "Expression", description: "For example 25 × 4, sqrt(2) or 200 + 10%")18    var expression: String1920    static var parameterSummary: some ParameterSummary { Summary("Calculate \(\.$expression)") }2122    @MainActor23    func perform() async throws -> some IntentResult & ReturnsValue<String> & ProvidesDialog {24        var engine = CalculatorEngine()25        let settings = AppSettings()26        engine.context.angleMode = settings.data.angleMode27        engine.context.complexEnabled = settings.data.complexEnabled28        engine.context.percentMode = settings.data.percentMode29        engine.format = settings.numberFormat30        do {31            let value = try engine.evaluate(expression)32            let text = NumberFormatting.format(value, engine.format)33            return .result(value: text, dialog: "\(text)")34        } catch let error as CalcError {35            throw CalculationIntentError(message: "\(error.category): \(error.description)")36        }37    }38}3940struct CalculationIntentError: Error, CustomLocalizedStringResourceConvertible {41    let message: String42    var localizedStringResource: LocalizedStringResource { "\(message)" }43}4445enum EnvironmentEntity: String, AppEnum {46    case standard, scientific, financial, graph, math, statistics, convert, workspace47    static let typeDisplayRepresentation = TypeDisplayRepresentation(name: "Environment")48    static let caseDisplayRepresentations: [EnvironmentEntity: DisplayRepresentation] = [49        .standard: "Standard", .scientific: "Scientific", .financial: "Financial", .graph: "Graph",50        .math: "Math", .statistics: "Statistics", .convert: "Convert", .workspace: "Workspace",51    ]52}5354struct OpenEnvironmentIntent: AppIntent {55    static let title: LocalizedStringResource = "Open Environment"56    static let description = IntentDescription("Opens ORIX Calc in the chosen environment.")57    static let openAppWhenRun = true5859    @Parameter(title: "Environment") var environment: EnvironmentEntity6061    @MainActor62    func perform() async throws -> some IntentResult {63        // The app reads this value when it becomes active.64        UserDefaults.standard.set(environment.rawValue, forKey: "orix.calc.requestedMode")65        NotificationCenter.default.post(name: Notification.Name("orix.requestedMode"), object: environment.rawValue)66        return .result()67    }68}6970struct ORIXShortcuts: AppShortcutsProvider {71    static var appShortcuts: [AppShortcut] {72        AppShortcut(intent: CalculateIntent(), phrases: ["Calculate with \(.applicationName)", "Compute with \(.applicationName)"],73                    shortTitle: "Calculate", systemImageName: "equal.circle")74        AppShortcut(intent: OpenEnvironmentIntent(), phrases: ["Open \(\.$environment) in \(.applicationName)"],75                    shortTitle: "Open Environment", systemImageName: "square.grid.2x2")76    }77}78
Files main
Menu

Theme