Files
Swift/Speedo/ContentView.swift
T

183 lines
6.1 KiB
Swift
Raw Normal View History

2025-05-05 09:33:00 -07:00
//
// ContentView.swift
// Speedometer
//
// Created by Justin Oros on 5/2/2025.
//
import SwiftUI
import CoreLocation
struct ContentView: View {
2025-05-05 18:44:20 -07:00
@StateObject private var locationManager = LocationManager() // Manages live GPS data
@State private var isMPH = true // Toggle between MPH and KPH
2025-05-05 09:33:00 -07:00
@State private var previousLocation: CLLocation?
2025-05-05 18:44:20 -07:00
@State private var tripDistanceMeters: Double = 0.0 // Trip distance in meters
2025-05-05 18:54:46 -07:00
@State private var isDarkMode = true // Toggle dark/light mode
@State private var isMinimalView = false // Show only speed + unit if true
2025-05-05 09:33:00 -07:00
2025-05-05 18:54:46 -07:00
// Load saved trip distance from UserDefaults on launch
2025-05-05 18:44:20 -07:00
init() {
if let savedDistance = UserDefaults.standard.object(forKey: "TripDistance") as? Double {
_tripDistanceMeters = State(initialValue: savedDistance)
}
}
2025-05-05 18:54:46 -07:00
// Current speed value, converted to MPH or KPH
2025-05-05 09:33:00 -07:00
var currentSpeed: Int {
2025-05-05 10:02:55 -07:00
let multiplier = isMPH ? 1.0 : 1.60934
2025-05-05 09:33:00 -07:00
return Int(Double(locationManager.speed) * multiplier)
}
2025-05-05 18:54:46 -07:00
// Speed unit label
2025-05-05 09:33:00 -07:00
var unitLabel: String {
isMPH ? "mph" : "kph"
}
2025-05-05 18:54:46 -07:00
// Formatted trip distance string
2025-05-05 09:33:00 -07:00
var distanceLabel: String {
let distance = isMPH ? tripDistanceMeters * 0.000621371 : tripDistanceMeters / 1000
let roundedDistance = Int(distance.rounded())
return "\(roundedDistance) \(isMPH ? "miles" : "km")"
}
2025-05-05 18:54:46 -07:00
// Dynamic color based on GPS accuracy
2025-05-05 09:33:00 -07:00
var accuracyColor: Color {
switch locationManager.accuracy {
case ..<10:
return .green
case 10..<30:
return .orange
default:
return .red
}
}
2025-05-05 18:44:20 -07:00
// Theme-based color helpers
2025-05-05 10:02:55 -07:00
var foregroundColor: Color {
isDarkMode ? .white : .black
}
var secondaryTextColor: Color {
isDarkMode ? .gray : .gray
}
var backgroundColor: Color {
isDarkMode ? .black : .white
}
2025-05-05 09:33:00 -07:00
var body: some View {
ZStack {
2025-05-05 10:02:55 -07:00
backgroundColor.ignoresSafeArea()
2025-05-05 09:33:00 -07:00
VStack(spacing: 30) {
Spacer()
2025-05-05 18:54:46 -07:00
// Speed + unit
2025-05-05 09:33:00 -07:00
HStack(alignment: .bottom, spacing: 4) {
Text("\(currentSpeed)")
.font(.system(size: 140, weight: .bold, design: .rounded))
2025-05-05 10:02:55 -07:00
.foregroundColor(foregroundColor)
2025-05-05 09:33:00 -07:00
Text(unitLabel)
.font(.title2)
2025-05-05 10:02:55 -07:00
.foregroundColor(secondaryTextColor)
2025-05-05 09:33:00 -07:00
.onTapGesture {
2025-05-05 18:54:46 -07:00
isMPH.toggle()
2025-05-05 09:33:00 -07:00
}
}
2025-05-05 18:54:46 -07:00
// Optional elements hidden in minimal mode
2025-05-05 17:44:04 -07:00
if !isMinimalView {
2025-05-05 18:54:46 -07:00
// Trip distance + reset icon
HStack(spacing: 8) {
Text("Trip: \(distanceLabel)")
.font(.title2)
.foregroundColor(secondaryTextColor)
2025-05-05 09:33:00 -07:00
2025-05-05 18:54:46 -07:00
Button(action: {
tripDistanceMeters = 0
previousLocation = nil
UserDefaults.standard.set(0.0, forKey: "TripDistance") // Clear saved value
}) {
Image(systemName: "arrow.counterclockwise.circle")
.font(.title2)
.foregroundColor(secondaryTextColor)
}
.buttonStyle(PlainButtonStyle()) // No tap glow
}
// GPS accuracy display
2025-05-05 17:44:04 -07:00
Text(String(format: "GPS Accuracy: ±%.0f meters", locationManager.accuracy))
2025-05-05 09:33:00 -07:00
.font(.headline)
2025-05-05 17:44:04 -07:00
.foregroundColor(accuracyColor)
2025-05-05 09:33:00 -07:00
}
Spacer()
}
.padding()
2025-05-05 18:54:46 -07:00
// Bottom toolbar
2025-05-05 09:33:00 -07:00
VStack {
Spacer()
HStack {
2025-05-05 18:54:46 -07:00
// Minimal mode toggle (eye icon)
2025-05-05 17:44:04 -07:00
Button(action: {
isMinimalView.toggle()
}) {
Image(systemName: isMinimalView ? "eye.slash.fill" : "eye.fill")
.font(.title2)
.foregroundColor(foregroundColor)
.padding()
}
2025-05-05 09:33:00 -07:00
Spacer()
2025-05-05 17:44:04 -07:00
2025-05-05 18:54:46 -07:00
// Open app Settings
2025-05-05 09:33:00 -07:00
Button(action: {
if let url = URL(string: UIApplication.openSettingsURLString) {
UIApplication.shared.open(url)
}
}) {
2025-05-05 10:02:55 -07:00
Image(systemName: "gearshape.fill")
2025-05-05 09:33:00 -07:00
.font(.title2)
2025-05-05 10:02:55 -07:00
.foregroundColor(foregroundColor)
2025-05-05 09:33:00 -07:00
.padding()
}
}
2025-05-05 17:44:04 -07:00
.padding([.leading, .trailing, .bottom], 10)
2025-05-05 09:33:00 -07:00
}
}
2025-05-05 18:44:20 -07:00
2025-05-05 18:54:46 -07:00
// Tap background to toggle dark/light mode
2025-05-05 10:02:55 -07:00
.onTapGesture {
2025-05-05 18:44:20 -07:00
isDarkMode.toggle()
2025-05-05 10:02:55 -07:00
}
2025-05-05 18:44:20 -07:00
2025-05-05 18:54:46 -07:00
// Show alert when location fails
2025-05-05 09:33:00 -07:00
.alert(isPresented: $locationManager.showAlert) {
Alert(
title: Text("Location Services"),
message: Text(locationManager.alertMessage),
dismissButton: .default(Text("Settings")) {
if let url = URL(string: UIApplication.openSettingsURLString) {
UIApplication.shared.open(url)
}
}
)
}
2025-05-05 18:44:20 -07:00
2025-05-05 18:54:46 -07:00
// Track trip distance by accumulating changes between locations
2025-05-05 09:33:00 -07:00
.onReceive(locationManager.$location) { location in
if let newLocation = location {
if let oldLocation = previousLocation {
let distance = newLocation.distance(from: oldLocation)
tripDistanceMeters += distance
2025-05-05 18:54:46 -07:00
UserDefaults.standard.set(tripDistanceMeters, forKey: "TripDistance") // Persist distance
2025-05-05 09:33:00 -07:00
}
previousLocation = newLocation
}
}
}
}