SPB Git

Repository Public BSD-3-Clause

CompassScreen.swift 8.1 KB 163 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project          : ORIX Maps9// File             : Apps/ORIXMaps/Sources/Tools/CompassScreen.swift10// Module           : Tools11// Repository       : https://git.spboucher.ai/orix-maps12// Created          : 2026-11-2513// Last Modified    : 2026-11-2514//15// =============================================================================16// AUTHOR & PROFESSIONAL AFFILIATION17// =============================================================================18//19// Author           : Simon-Pierre Boucher20// Position         : Professor of Finance21// Institution      : Université du Québec en Outaouais (UQO)22// Department       : Département des sciences administratives23// Organization     : SP Boucher24//25// Website          : https://spboucher.ai26// Git              : https://git.spboucher.ai27//28// =============================================================================29// OFFICIAL CONTACT DIRECTORY30// =============================================================================31//32// General          : info@spboucher.ai33// Professional     : contact@spboucher.ai34// Direct           : simon@spboucher.ai35// Research         : research@spboucher.ai36// Development      : dev@spboucher.ai37// Support          : support@spboucher.ai38// Security/Admin   : admin@spboucher.ai39// Notifications    : notifications@spboucher.ai40//41// Preferred Contact: support@spboucher.ai42//43// =============================================================================44// COPYRIGHT & INTELLECTUAL PROPERTY45// =============================================================================46//47// Copyright (c) 202648// Simon-Pierre Boucher49//50// All rights reserved where applicable.51//52// This file is governed by the applicable project license,53// copyright notices, institutional agreements and relevant law.54//55// Unless expressly authorized by the applicable license or56// rights holder, copying, redistribution, sublicensing,57// publication and commercial exploitation are not permitted.58//59// Existing third-party rights and notices remain unaffected.60//61// =============================================================================62// CONFIDENTIALITY & SECURITY63// =============================================================================64//65// Classification   : Public66// Distribution     : Public repository; use is governed by the project license67// Security Contact : admin@spboucher.ai68//69// Access to this source does not independently grant permission70// to redistribute or disclose confidential materials.71//72// Do not embed credentials, private keys, access tokens,73// certificates or confidential personal information.74//75// =============================================================================76// LICENSE & MAINTENANCE77// =============================================================================78//79// License          : Proprietary, all rights reserved (see license file)80// License File     : LICENSE81// Maintainer       : Simon-Pierre Boucher82// Technical Contact: dev@spboucher.ai83//84// =============================================================================85// END OF SP BOUCHER SOURCE HEADER86// =============================================================================8788import ORIXDesignSystem89import ORIXGeoCore90import ORIXLocation91import SwiftUI9293/// ORIX Compass: heading, coordinates, accuracy and altitude, readable with no map and no network.94struct CompassScreen: View {95    @Environment(AppModel.self) private var app9697    var body: some View {98        ScrollView {99            VStack(spacing: ORIX.Spacing.xl) {100                if let h = app.location.heading {101                    ZStack {102                        Circle().strokeBorder(.quaternary, lineWidth: 2)103                        ForEach(0..<72, id: \.self) { i in104                            Rectangle().fill(i % 18 == 0 ? Color.primary : Color.secondary.opacity(0.5))105                                .frame(width: i % 18 == 0 ? 3 : 1, height: i % 6 == 0 ? 14 : 7)106                                .offset(y: -118)107                                .rotationEffect(.degrees(Double(i) * 5))108                        }109                        ForEach(Array(["N", "E", "S", "W"].enumerated()), id: \.offset) { i, letter in110                            Text(verbatim: app.settings.interfaceLanguageCode == "fr" && letter == "W" ? "O" : letter)111                                .font(.title3.weight(.bold)).foregroundStyle(i == 0 ? ORIX.Palette.danger : .primary)112                                .rotationEffect(.degrees(-Double(i) * 90 + h.best))113                                .offset(y: -92).rotationEffect(.degrees(Double(i) * 90))114                        }115                    }116                    .frame(width: 260, height: 260)117                    .rotationEffect(.degrees(-h.best))118                    .overlay(alignment: .top) { Triangle().fill(ORIX.Palette.accent).frame(width: 16, height: 18).rotationEffect(.degrees(180)).offset(y: -22) }119                    .padding(.top, 28)120                    .accessibilityElement()121                    .accessibilityLabel("Compass")122                    .accessibilityValue(Text("\(Int(h.best.rounded())) degrees \(cardinal(h.best))"))123                    Text(verbatim: "\(Int(h.best.rounded()))° \(cardinal(h.best))").font(.system(size: 44, weight: .bold, design: .rounded).monospacedDigit())124                    Text(h.trueHeading == nil ? "Magnetic north (true north needs a position)" : "True north · accuracy ±\(Int(h.accuracy.rounded()))°")125                        .font(.footnote).foregroundStyle(.secondary)126                } else {127                    EmptyStateView("Compass not available", message: "This device reports no heading, or the position permission was refused. Move away from magnets and metal if the compass stays unavailable.", systemImage: "safari")128                }129                if let fix = app.location.lastFix {130                    VStack(spacing: ORIX.Spacing.m) {131                        Text(verbatim: CoordinateText.format(fix.coordinate, style: app.settings.coordinateStyle))132                            .font(.title3.monospacedDigit().weight(.semibold)).textSelection(.enabled)133                        HStack {134                            StatView("Accuracy", value: "±" + app.settings.formatter.distance(fix.horizontalAccuracy))135                            StatView("Altitude", value: fix.altitude.map { app.settings.formatter.altitude($0) } ?? "—")136                            StatView("Speed", value: fix.speed.map { app.settings.formatter.speed(metersPerSecond: $0) } ?? "—")137                        }138                        if !app.location.isFixFresh { NoticeView("This position is old. Waiting for a new one.", kind: .warning) }139                    }140                    .padding(ORIX.Spacing.l)141                    .background(.quaternary.opacity(0.6), in: .rect(cornerRadius: ORIX.Radius.card))142                } else if app.location.authorization == .denied || app.location.authorization == .restricted {143                    NoticeView("Position access is off. Turn it on in Settings to see coordinates, altitude and true north.", kind: .warning)144                } else {145                    ProgressView("Waiting for a position")146                }147            }148            .padding(ORIX.Spacing.l)149            .frame(maxWidth: .infinity)150        }151        .navigationTitle("Compass")152        .navigationBarTitleDisplayMode(.inline)153        .onAppear { app.location.start(.browsing); app.location.retainHeading() }154        .onDisappear { app.location.releaseHeading() }155    }156157    private func cardinal(_ degrees: Double) -> String {158        let fr = app.settings.interfaceLanguageCode == "fr"159        let names = fr ? ["N", "NE", "E", "SE", "S", "SO", "O", "NO"] : ["N", "NE", "E", "SE", "S", "SW", "W", "NW"]160        return names[Int((degrees + 22.5).truncatingRemainder(dividingBy: 360) / 45) % 8]161    }162}163
Files main
Menu

Theme