Files
Swift/Speedo/ContentView.swift
T

190 lines
6.7 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-06 20:33:04 -07:00
@StateObject private var locationManager = LocationManager() // 📍 Manages real-time GPS data
2025-05-05 09:33:00 -07:00
2025-05-06 20:33:04 -07:00
@State private var isMPH = true // 🚗 Speed unit toggle: true = MPH, false = KPH
@State private var previousLocation: CLLocation? // 📌 Track last known location to compute trip distance
@State private var tripDistanceMeters: Double = 0.0 // 📏 Track trip distance in meters
// 🌗 Track current theme (loaded from UserDefaults)
@State private var isDarkMode: Bool = UserDefaults.standard.bool(forKey: "DarkModeEnabled")
// 👁️ Track minimal view preference (loaded from UserDefaults)
@State private var isMinimalView: Bool = UserDefaults.standard.bool(forKey: "MinimalViewEnabled")
// 🧠 Load trip distance if saved
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-06 20:33:04 -07:00
// 🧮 Calculate speed based on unit selection
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-06 20:33:04 -07:00
// 🏷️ Speed unit label
2025-05-05 09:33:00 -07:00
var unitLabel: String {
isMPH ? "mph" : "kph"
}
2025-05-06 20:33:04 -07:00
// 📐 Distance label (miles or km)
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-06 20:33:04 -07:00
// 🎯 Color indicator for 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-06 20:33:04 -07:00
// 🎨 Theme-dependent 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-06 20:33:04 -07:00
// ⏩ Speed display and unit toggle
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-06 20:33:04 -07:00
isMPH.toggle() // 🔁 Tap to switch MPH/KPH
2025-05-05 09:33:00 -07:00
}
}
2025-05-06 20:33:04 -07:00
// 📊 Only show details if not in minimal mode
2025-05-05 17:44:04 -07:00
if !isMinimalView {
2025-05-06 20:33:04 -07:00
// Trip meter + reset icon
2025-05-05 18:54:46 -07:00
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
2025-05-06 20:33:04 -07:00
UserDefaults.standard.set(0.0, forKey: "TripDistance") // 🧹 Clear saved trip
2025-05-05 18:54:46 -07:00
}) {
2025-05-06 20:33:04 -07:00
Image(systemName: "arrow.counterclockwise.circle") // 🔄 Reset icon
2025-05-05 18:54:46 -07:00
.font(.title2)
.foregroundColor(secondaryTextColor)
}
2025-05-06 20:33:04 -07:00
.buttonStyle(PlainButtonStyle()) // Remove button glow
2025-05-05 18:54:46 -07:00
}
2025-05-06 20:33:04 -07:00
// GPS accuracy indicator
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-06 20:33:04 -07:00
// ⚙️ Bottom control bar (eye + gear)
2025-05-05 09:33:00 -07:00
VStack {
Spacer()
HStack {
2025-05-06 20:33:04 -07:00
// 👁️ Minimal view toggle
2025-05-05 17:44:04 -07:00
Button(action: {
isMinimalView.toggle()
2025-05-06 20:33:04 -07:00
UserDefaults.standard.set(isMinimalView, forKey: "MinimalViewEnabled") // 💾 Save preference
2025-05-05 17:44:04 -07:00
}) {
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-06 20:33:04 -07:00
// ⚙️ Open system 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-06 20:33:04 -07:00
// 🌗 Tap anywhere to toggle dark/light mode and save it
2025-05-05 10:02:55 -07:00
.onTapGesture {
2025-05-05 18:44:20 -07:00
isDarkMode.toggle()
2025-05-06 20:33:04 -07:00
UserDefaults.standard.set(isDarkMode, forKey: "DarkModeEnabled") // 💾 Save theme mode
2025-05-05 10:02:55 -07:00
}
2025-05-05 18:44:20 -07:00
2025-05-06 20:33:04 -07:00
// 🚨 Show alert for location errors or denied permissions
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-06 20:33:04 -07:00
// 🧮 Update trip distance as new GPS data comes in
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-06 20:33:04 -07:00
UserDefaults.standard.set(tripDistanceMeters, forKey: "TripDistance") // 💾 Save new trip value
2025-05-05 09:33:00 -07:00
}
previousLocation = newLocation
}
}
}
}