Repository Public MIT
1// ORIX Calc2// File: App/Sources/SettingsView.swift3// Description: Preferences — calculation conventions, number display, navigation, data backup, privacy and acknowledgements.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import SwiftUI8import UniformTypeIdentifiers9import ORIXUI10import ORIXExpression1112struct BackupDocument: FileDocument {13 static let readableContentTypes: [UTType] = [.json]14 var data: Data15 init(data: Data) { self.data = data }16 init(configuration: ReadConfiguration) throws { data = configuration.file.regularFileContents ?? Data() }17 func fileWrapper(configuration: WriteConfiguration) throws -> FileWrapper { FileWrapper(regularFileWithContents: data) }18}1920struct SettingsView: View {21 @Environment(AppModel.self) private var app22 @Environment(\.dismiss) private var dismiss23 @State private var exporting = false24 @State private var importing = false25 @State private var backup = BackupDocument(data: Data())26 @State private var message: String?2728 var body: some View {29 @Bindable var settings = app.settings30 NavigationStack {31 Form {32 Section {33 Picker("Angle unit", selection: $settings.data.angleMode) {34 ForEach(AngleMode.allCases, id: \.self) { Text($0.rawValue.capitalized).tag($0) }35 }36 Toggle("Complex numbers", isOn: $settings.data.complexEnabled)37 Picker("Percent key", selection: $settings.data.percentMode) {38 Text("A + B% adds B% of A").tag(PercentMode.contextual)39 Text("B% is always B ÷ 100").tag(PercentMode.strict)40 }41 Picker("Entry", selection: $settings.data.entryMode) {42 ForEach(EntryMode.allCases, id: \.self) { Text($0.title).tag($0) }43 }44 } header: { Text("Calculation") } footer: {45 Text(settings.data.percentMode == .contextual46 ? "Default ORIX convention: 200 + 10% = 220 and 200 − 10% = 180. After × or ÷, and on its own, B% is B ÷ 100 (200 × 10% = 20)."47 : "Strict convention: 200 + 10% = 200.1, because 10% is always 0.1.")48 }4950 Section {51 Picker("Notation", selection: $settings.data.notation) {52 Text("Automatic").tag(NumberFormat.Notation.automatic)53 Text("Fixed").tag(NumberFormat.Notation.fixed)54 Text("Scientific").tag(NumberFormat.Notation.scientific)55 Text("Engineering").tag(NumberFormat.Notation.engineering)56 }57 Stepper("Significant digits: \(settings.data.significantDigits)", value: $settings.data.significantDigits, in: 4...17)58 Picker("Decimal places", selection: $settings.data.decimalPlaces) {59 Text("Floating").tag(Int?.none)60 ForEach(0...10, id: \.self) { Text("\($0)").tag(Int?.some($0)) }61 }62 Toggle("Group digits", isOn: $settings.data.useGrouping)63 Toggle("Show exact fractions", isOn: $settings.data.showFractions)64 LabeledContent("Preview") {65 Text(NumberFormatting.format(Value(BigRational(decimalLiteral: "1234567.125")!), settings.numberFormat) + " " + NumberFormatting.format(Value(BigRational(2, 3)), settings.numberFormat))66 .font(ORIXFont.value)67 }68 } header: { Text("Number display") } footer: {69 Text("Display settings never change stored values: calculations always continue with full precision. Separators follow the device region (\(Locale.current.identifier)).")70 }7172 Section("Navigation") {73 Picker("Open at launch", selection: $settings.data.defaultMode) {74 Text("Last used").tag(CalcMode?.none)75 ForEach(CalcMode.allCases) { Text($0.title).tag(CalcMode?.some($0)) }76 }77 NavigationLink("Favorites") {78 List(CalcMode.allCases) { mode in79 Toggle(isOn: Binding(get: { settings.data.favorites.contains(mode) }, set: { _ in settings.toggleFavorite(mode) })) {80 Label(mode.title, systemImage: mode.symbol)81 }82 }83 .navigationTitle("Favorites")84 }85 }8687 Section {88 Picker("Appearance", selection: $settings.data.appearance) {89 Text("System").tag(AppearancePreference.system)90 Text("Light").tag(AppearancePreference.light)91 Text("Dark").tag(AppearancePreference.dark)92 }93 Toggle("Haptic feedback", isOn: $settings.data.hapticsEnabled)94 Toggle("Memory keys (MC, MR, M+)", isOn: $settings.data.showMemoryRow)95 Picker("Mortgage convention", selection: $settings.data.jurisdiction) {96 ForEach(MortgageJurisdiction.allCases, id: \.self) { Text($0.title).tag($0) }97 }98 } header: { Text("General") } footer: {99 Text("Canada: fixed-rate mortgages quote a nominal rate compounded semi-annually. United States: compounded monthly. You can change the convention for each loan.")100 }101102 Section {103 Button { if let data = app.exportBackup() { backup = BackupDocument(data: data); exporting = true } } label: { Label("Export Backup…", systemImage: "square.and.arrow.up") }104 Button { importing = true } label: { Label("Import Backup…", systemImage: "square.and.arrow.down") }105 } header: { Text("Your data") } footer: {106 Text("History, variables, functions, workspaces and saved models are stored only on this device. A backup is a JSON file that you keep wherever you like.")107 }108109 Section("Privacy") {110 Label("No account, no sign-in", systemImage: "person.crop.circle.badge.xmark")111 Label("No analytics, no advertising, no tracking", systemImage: "eye.slash")112 Label("All calculations run on this device", systemImage: "cpu")113 Label("The only network request is the optional currency-rate update, when you tap it", systemImage: "network")114 }115 .font(.callout)116117 Section("About") {118 LabeledContent("Version", value: "\(Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "–") (\(Bundle.main.infoDictionary?["CFBundleVersion"] as? String ?? "–"))")119 NavigationLink("Precision") { PrecisionNotes() }120 NavigationLink("Acknowledgements") { Acknowledgements() }121 }122 }123 .navigationTitle("Settings")124 #if os(iOS)125 .navigationBarTitleDisplayMode(.inline)126 #endif127 .toolbar { ToolbarItem(placement: .confirmationAction) { Button("Done") { dismiss() } } }128 .onChange(of: settings.data) { _, _ in app.applySettings() }129 .fileExporter(isPresented: $exporting, document: backup, contentType: .json, defaultFilename: "ORIX Calc Backup") { result in130 if case .failure(let e) = result { message = e.localizedDescription }131 }132 .fileImporter(isPresented: $importing, allowedContentTypes: [.json]) { result in133 guard case .success(let url) = result else { return }134 let scoped = url.startAccessingSecurityScopedResource()135 defer { if scoped { url.stopAccessingSecurityScopedResource() } }136 message = (try? Data(contentsOf: url)).flatMap { app.importBackup($0) } ?? "This file is not an ORIX Calc backup."137 }138 .alert("Backup", isPresented: Binding(get: { message != nil }, set: { if !$0 { message = nil } })) { Button("OK", role: .cancel) {} } message: { Text(message ?? "") }139 }140 }141}142143private struct PrecisionNotes: View {144 var body: some View {145 List {146 Section("Exact arithmetic") {147 Text("Whole numbers are arbitrary-precision integers and decimals are exact fractions: 0.1 + 0.2 is exactly 0.3, 2^200 and 100! are shown digit for digit, and 1 ÷ 3 × 3 is exactly 1.")148 }149 Section("Floating point") {150 Text("Roots, logarithms, trigonometry and other transcendental functions are computed in IEEE-754 double precision, about 15 to 16 significant digits. Results are labelled “≈ floating point”.")151 Text("Very large exact fractions (more than 8192 bits) continue in floating point, with a note in Explain.")152 }153 Section("Verification") {154 Text("The Verified badge appears only when checks were actually executed on that result: domain, overflow, loss of significance, residuals of solved equations, convergence of numerical methods. Tap it to see each check.")155 }156 Section("Money") {157 Text("Loan schedules round each payment and interest amount to the cent in exact decimal arithmetic. Lender statements can differ by a cent depending on their rounding policy.")158 }159 }160 .font(.callout)161 .navigationTitle("Precision")162 }163}164165private struct Acknowledgements: View {166 var body: some View {167 List {168 Section("Open-source components") {169 LabeledContent("BigInt 5.7.0", value: "attaswift · MIT License")170 LabeledContent("Swift Numerics 1.1.1", value: "Apple · Apache License 2.0")171 }172 Section("Apple frameworks") {173 Text("SwiftUI, SwiftData, Swift Charts, Accelerate (LAPACK), Vision, App Intents, CryptoKit.")174 }175 Section("Reference data") {176 Text("Physical constants: CODATA 2022 recommended values (NIST). Unit definitions: BIPM SI Brochure, NIST SP 811. Currency reference rates, when you download them: European Central Bank.")177 }178 }179 .font(.callout)180 .navigationTitle("Acknowledgements")181 }182}183