Resolved location-related errors.

Blek! Bugs are gross...
This commit is contained in:
Justin Oros
2025-05-07 19:58:01 -07:00
committed by GitHub
parent dfd246dd15
commit ab100f6cef
7 changed files with 146 additions and 314 deletions
Binary file not shown.

After

Width:  |  Height:  |  Size: 99 KiB

@@ -1,6 +1,7 @@
{ {
"images" : [ "images" : [
{ {
"filename" : "1024.png",
"idiom" : "universal", "idiom" : "universal",
"platform" : "ios", "platform" : "ios",
"size" : "1024x1024" "size" : "1024x1024"
+31 -222
View File
@@ -6,33 +6,31 @@
objectVersion = 77; objectVersion = 77;
objects = { objects = {
/* Begin PBXContainerItemProxy section */ /* Begin PBXBuildFile section */
97A3CA3D2DC90F75001ED670 /* PBXContainerItemProxy */ = { 97DBB1252DCC4CDC0027503D /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 97DBB0D32DCB175B0027503D /* Assets.xcassets */; };
isa = PBXContainerItemProxy; /* End PBXBuildFile section */
containerPortal = 97A3CA272DC90F73001ED670 /* Project object */;
proxyType = 1;
remoteGlobalIDString = 97A3CA2E2DC90F73001ED670;
remoteInfo = Speedo;
};
97A3CA472DC90F75001ED670 /* PBXContainerItemProxy */ = {
isa = PBXContainerItemProxy;
containerPortal = 97A3CA272DC90F73001ED670 /* Project object */;
proxyType = 1;
remoteGlobalIDString = 97A3CA2E2DC90F73001ED670;
remoteInfo = Speedo;
};
/* End PBXContainerItemProxy section */
/* Begin PBXFileReference section */ /* Begin PBXFileReference section */
97A3CA2F2DC90F73001ED670 /* Speedo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Speedo.app; sourceTree = BUILT_PRODUCTS_DIR; }; 97A3CA2F2DC90F73001ED670 /* Speedo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Speedo.app; sourceTree = BUILT_PRODUCTS_DIR; };
97A3CA3C2DC90F75001ED670 /* SpeedoTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = SpeedoTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 97DBB0D32DCB175B0027503D /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
97A3CA462DC90F75001ED670 /* SpeedoUITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = SpeedoUITests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
97E3710E2DC91223009146A0 /* Base */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; name = Base; path = Base.lproj/Info.plist; sourceTree = "<group>"; };
/* End PBXFileReference section */ /* End PBXFileReference section */
/* Begin PBXFileSystemSynchronizedBuildFileExceptionSet section */
97DBB1282DCC4D870027503D /* Exceptions for "Speedo" folder in "Speedo" target */ = {
isa = PBXFileSystemSynchronizedBuildFileExceptionSet;
membershipExceptions = (
Info.plist,
);
target = 97A3CA2E2DC90F73001ED670 /* Speedo */;
};
/* End PBXFileSystemSynchronizedBuildFileExceptionSet section */
/* Begin PBXFileSystemSynchronizedRootGroup section */ /* Begin PBXFileSystemSynchronizedRootGroup section */
97A3CA312DC90F73001ED670 /* Speedo */ = { 97A3CA312DC90F73001ED670 /* Speedo */ = {
isa = PBXFileSystemSynchronizedRootGroup; isa = PBXFileSystemSynchronizedRootGroup;
exceptions = (
97DBB1282DCC4D870027503D /* Exceptions for "Speedo" folder in "Speedo" target */,
);
path = Speedo; path = Speedo;
sourceTree = "<group>"; sourceTree = "<group>";
}; };
@@ -46,20 +44,6 @@
); );
runOnlyForDeploymentPostprocessing = 0; runOnlyForDeploymentPostprocessing = 0;
}; };
97A3CA392DC90F75001ED670 /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
);
runOnlyForDeploymentPostprocessing = 0;
};
97A3CA432DC90F75001ED670 /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXFrameworksBuildPhase section */ /* End PBXFrameworksBuildPhase section */
/* Begin PBXGroup section */ /* Begin PBXGroup section */
@@ -68,7 +52,7 @@
children = ( children = (
97A3CA312DC90F73001ED670 /* Speedo */, 97A3CA312DC90F73001ED670 /* Speedo */,
97A3CA302DC90F73001ED670 /* Products */, 97A3CA302DC90F73001ED670 /* Products */,
97E3710F2DC91223009146A0 /* Info.plist */, 97DBB0D32DCB175B0027503D /* Assets.xcassets */,
); );
sourceTree = "<group>"; sourceTree = "<group>";
}; };
@@ -76,8 +60,6 @@
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
97A3CA2F2DC90F73001ED670 /* Speedo.app */, 97A3CA2F2DC90F73001ED670 /* Speedo.app */,
97A3CA3C2DC90F75001ED670 /* SpeedoTests.xctest */,
97A3CA462DC90F75001ED670 /* SpeedoUITests.xctest */,
); );
name = Products; name = Products;
sourceTree = "<group>"; sourceTree = "<group>";
@@ -91,6 +73,7 @@
buildPhases = ( buildPhases = (
97A3CA2B2DC90F73001ED670 /* Sources */, 97A3CA2B2DC90F73001ED670 /* Sources */,
97A3CA2C2DC90F73001ED670 /* Frameworks */, 97A3CA2C2DC90F73001ED670 /* Frameworks */,
97DBB1232DCC4CD20027503D /* Resources */,
); );
buildRules = ( buildRules = (
); );
@@ -106,46 +89,6 @@
productReference = 97A3CA2F2DC90F73001ED670 /* Speedo.app */; productReference = 97A3CA2F2DC90F73001ED670 /* Speedo.app */;
productType = "com.apple.product-type.application"; productType = "com.apple.product-type.application";
}; };
97A3CA3B2DC90F75001ED670 /* SpeedoTests */ = {
isa = PBXNativeTarget;
buildConfigurationList = 97A3CA532DC90F75001ED670 /* Build configuration list for PBXNativeTarget "SpeedoTests" */;
buildPhases = (
97A3CA382DC90F75001ED670 /* Sources */,
97A3CA392DC90F75001ED670 /* Frameworks */,
97A3CA3A2DC90F75001ED670 /* Resources */,
);
buildRules = (
);
dependencies = (
97A3CA3E2DC90F75001ED670 /* PBXTargetDependency */,
);
name = SpeedoTests;
packageProductDependencies = (
);
productName = SpeedoTests;
productReference = 97A3CA3C2DC90F75001ED670 /* SpeedoTests.xctest */;
productType = "com.apple.product-type.bundle.unit-test";
};
97A3CA452DC90F75001ED670 /* SpeedoUITests */ = {
isa = PBXNativeTarget;
buildConfigurationList = 97A3CA562DC90F75001ED670 /* Build configuration list for PBXNativeTarget "SpeedoUITests" */;
buildPhases = (
97A3CA422DC90F75001ED670 /* Sources */,
97A3CA432DC90F75001ED670 /* Frameworks */,
97A3CA442DC90F75001ED670 /* Resources */,
);
buildRules = (
);
dependencies = (
97A3CA482DC90F75001ED670 /* PBXTargetDependency */,
);
name = SpeedoUITests;
packageProductDependencies = (
);
productName = SpeedoUITests;
productReference = 97A3CA462DC90F75001ED670 /* SpeedoUITests.xctest */;
productType = "com.apple.product-type.bundle.ui-testing";
};
/* End PBXNativeTarget section */ /* End PBXNativeTarget section */
/* Begin PBXProject section */ /* Begin PBXProject section */
@@ -159,14 +102,6 @@
97A3CA2E2DC90F73001ED670 = { 97A3CA2E2DC90F73001ED670 = {
CreatedOnToolsVersion = 16.3; CreatedOnToolsVersion = 16.3;
}; };
97A3CA3B2DC90F75001ED670 = {
CreatedOnToolsVersion = 16.3;
TestTargetID = 97A3CA2E2DC90F73001ED670;
};
97A3CA452DC90F75001ED670 = {
CreatedOnToolsVersion = 16.3;
TestTargetID = 97A3CA2E2DC90F73001ED670;
};
}; };
}; };
buildConfigurationList = 97A3CA2A2DC90F73001ED670 /* Build configuration list for PBXProject "Speedo" */; buildConfigurationList = 97A3CA2A2DC90F73001ED670 /* Build configuration list for PBXProject "Speedo" */;
@@ -184,24 +119,16 @@
projectRoot = ""; projectRoot = "";
targets = ( targets = (
97A3CA2E2DC90F73001ED670 /* Speedo */, 97A3CA2E2DC90F73001ED670 /* Speedo */,
97A3CA3B2DC90F75001ED670 /* SpeedoTests */,
97A3CA452DC90F75001ED670 /* SpeedoUITests */,
); );
}; };
/* End PBXProject section */ /* End PBXProject section */
/* Begin PBXResourcesBuildPhase section */ /* Begin PBXResourcesBuildPhase section */
97A3CA3A2DC90F75001ED670 /* Resources */ = { 97DBB1232DCC4CD20027503D /* Resources */ = {
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
);
runOnlyForDeploymentPostprocessing = 0;
};
97A3CA442DC90F75001ED670 /* Resources */ = {
isa = PBXResourcesBuildPhase; isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647; buildActionMask = 2147483647;
files = ( files = (
97DBB1252DCC4CDC0027503D /* Assets.xcassets in Resources */,
); );
runOnlyForDeploymentPostprocessing = 0; runOnlyForDeploymentPostprocessing = 0;
}; };
@@ -215,46 +142,8 @@
); );
runOnlyForDeploymentPostprocessing = 0; runOnlyForDeploymentPostprocessing = 0;
}; };
97A3CA382DC90F75001ED670 /* Sources */ = {
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
);
runOnlyForDeploymentPostprocessing = 0;
};
97A3CA422DC90F75001ED670 /* Sources */ = {
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXSourcesBuildPhase section */ /* End PBXSourcesBuildPhase section */
/* Begin PBXTargetDependency section */
97A3CA3E2DC90F75001ED670 /* PBXTargetDependency */ = {
isa = PBXTargetDependency;
target = 97A3CA2E2DC90F73001ED670 /* Speedo */;
targetProxy = 97A3CA3D2DC90F75001ED670 /* PBXContainerItemProxy */;
};
97A3CA482DC90F75001ED670 /* PBXTargetDependency */ = {
isa = PBXTargetDependency;
target = 97A3CA2E2DC90F73001ED670 /* Speedo */;
targetProxy = 97A3CA472DC90F75001ED670 /* PBXContainerItemProxy */;
};
/* End PBXTargetDependency section */
/* Begin PBXVariantGroup section */
97E3710F2DC91223009146A0 /* Info.plist */ = {
isa = PBXVariantGroup;
children = (
97E3710E2DC91223009146A0 /* Base */,
);
name = Info.plist;
sourceTree = "<group>";
};
/* End PBXVariantGroup section */
/* Begin XCBuildConfiguration section */ /* Begin XCBuildConfiguration section */
97A3CA4E2DC90F75001ED670 /* Debug */ = { 97A3CA4E2DC90F75001ED670 /* Debug */ = {
isa = XCBuildConfiguration; isa = XCBuildConfiguration;
@@ -382,6 +271,7 @@
buildSettings = { buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
ASSETCATALOG_COMPILER_INCLUDE_ALL_APPICON_ASSETS = YES;
CODE_SIGN_STYLE = Automatic; CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1; CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_TEAM = 5RZLK3W7ZH; DEVELOPMENT_TEAM = 5RZLK3W7ZH;
@@ -400,6 +290,10 @@
MARKETING_VERSION = 1.0; MARKETING_VERSION = 1.0;
PRODUCT_BUNDLE_IDENTIFIER = com.justinoros.Speedo; PRODUCT_BUNDLE_IDENTIFIER = com.justinoros.Speedo;
PRODUCT_NAME = "$(TARGET_NAME)"; PRODUCT_NAME = "$(TARGET_NAME)";
SUPPORTED_PLATFORMS = "iphoneos iphonesimulator";
SUPPORTS_MACCATALYST = NO;
SUPPORTS_MAC_DESIGNED_FOR_IPHONE_IPAD = NO;
SUPPORTS_XR_DESIGNED_FOR_IPHONE_IPAD = NO;
SWIFT_EMIT_LOC_STRINGS = YES; SWIFT_EMIT_LOC_STRINGS = YES;
SWIFT_VERSION = 5.0; SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2"; TARGETED_DEVICE_FAMILY = "1,2";
@@ -411,6 +305,7 @@
buildSettings = { buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
ASSETCATALOG_COMPILER_INCLUDE_ALL_APPICON_ASSETS = YES;
CODE_SIGN_STYLE = Automatic; CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1; CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_TEAM = 5RZLK3W7ZH; DEVELOPMENT_TEAM = 5RZLK3W7ZH;
@@ -429,84 +324,16 @@
MARKETING_VERSION = 1.0; MARKETING_VERSION = 1.0;
PRODUCT_BUNDLE_IDENTIFIER = com.justinoros.Speedo; PRODUCT_BUNDLE_IDENTIFIER = com.justinoros.Speedo;
PRODUCT_NAME = "$(TARGET_NAME)"; PRODUCT_NAME = "$(TARGET_NAME)";
SUPPORTED_PLATFORMS = "iphoneos iphonesimulator";
SUPPORTS_MACCATALYST = NO;
SUPPORTS_MAC_DESIGNED_FOR_IPHONE_IPAD = NO;
SUPPORTS_XR_DESIGNED_FOR_IPHONE_IPAD = NO;
SWIFT_EMIT_LOC_STRINGS = YES; SWIFT_EMIT_LOC_STRINGS = YES;
SWIFT_VERSION = 5.0; SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2"; TARGETED_DEVICE_FAMILY = "1,2";
}; };
name = Release; name = Release;
}; };
97A3CA542DC90F75001ED670 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
BUNDLE_LOADER = "$(TEST_HOST)";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_TEAM = 5RZLK3W7ZH;
GENERATE_INFOPLIST_FILE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 18.4;
MARKETING_VERSION = 1.0;
PRODUCT_BUNDLE_IDENTIFIER = com.justinoros.SpeedoTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_EMIT_LOC_STRINGS = NO;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Speedo.app/$(BUNDLE_EXECUTABLE_FOLDER_PATH)/Speedo";
};
name = Debug;
};
97A3CA552DC90F75001ED670 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
BUNDLE_LOADER = "$(TEST_HOST)";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_TEAM = 5RZLK3W7ZH;
GENERATE_INFOPLIST_FILE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 18.4;
MARKETING_VERSION = 1.0;
PRODUCT_BUNDLE_IDENTIFIER = com.justinoros.SpeedoTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_EMIT_LOC_STRINGS = NO;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Speedo.app/$(BUNDLE_EXECUTABLE_FOLDER_PATH)/Speedo";
};
name = Release;
};
97A3CA572DC90F75001ED670 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_TEAM = 5RZLK3W7ZH;
GENERATE_INFOPLIST_FILE = YES;
MARKETING_VERSION = 1.0;
PRODUCT_BUNDLE_IDENTIFIER = com.justinoros.SpeedoUITests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_EMIT_LOC_STRINGS = NO;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
TEST_TARGET_NAME = Speedo;
};
name = Debug;
};
97A3CA582DC90F75001ED670 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_TEAM = 5RZLK3W7ZH;
GENERATE_INFOPLIST_FILE = YES;
MARKETING_VERSION = 1.0;
PRODUCT_BUNDLE_IDENTIFIER = com.justinoros.SpeedoUITests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_EMIT_LOC_STRINGS = NO;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
TEST_TARGET_NAME = Speedo;
};
name = Release;
};
/* End XCBuildConfiguration section */ /* End XCBuildConfiguration section */
/* Begin XCConfigurationList section */ /* Begin XCConfigurationList section */
@@ -528,24 +355,6 @@
defaultConfigurationIsVisible = 0; defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release; defaultConfigurationName = Release;
}; };
97A3CA532DC90F75001ED670 /* Build configuration list for PBXNativeTarget "SpeedoTests" */ = {
isa = XCConfigurationList;
buildConfigurations = (
97A3CA542DC90F75001ED670 /* Debug */,
97A3CA552DC90F75001ED670 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
97A3CA562DC90F75001ED670 /* Build configuration list for PBXNativeTarget "SpeedoUITests" */ = {
isa = XCConfigurationList;
buildConfigurations = (
97A3CA572DC90F75001ED670 /* Debug */,
97A3CA582DC90F75001ED670 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */ /* End XCConfigurationList section */
}; };
rootObject = 97A3CA272DC90F73001ED670 /* Project object */; rootObject = 97A3CA272DC90F73001ED670 /* Project object */;
+85 -48
View File
@@ -1,114 +1,147 @@
// // Speedometer App for iOS
// ContentView.swift // Author: Justin Oros
// Speedometer // ContentView.swift
//
// Created by Justin Oros on 5/2/2025.
//
import SwiftUI import SwiftUI
import CoreLocation import CoreLocation
struct ContentView: View { struct ContentView: View {
@StateObject private var locationManager = LocationManager() @StateObject private var locationManager = LocationManager()
@State private var isMPH = true
@State private var previousLocation: CLLocation?
@State private var tripDistanceMeters: Double = 0.0
// App storage settings
@AppStorage("SpeedUnitIsMPH") private var isMPH: Bool = true
@AppStorage("DarkModeEnabled") private var isDarkMode: Bool = true // Default to dark mode
@AppStorage("MinimalViewEnabled") private var isMinimalView: Bool = false
@AppStorage("TripDistance") private var tripDistanceMeters: Double = 0.0
@State private var previousLocation: CLLocation?
// Converts speed in m/s to selected unit
var currentSpeed: Int { var currentSpeed: Int {
let multiplier = isMPH ? 1.0 : 1.60934 // Already in MPH from LocationManager let speedMS = locationManager.speedInMS
return Int(Double(locationManager.speed) * multiplier) let converted = isMPH ? speedMS * 2.23694 : speedMS * 3.6
return Int(converted.rounded())
} }
// Returns unit label string
var unitLabel: String { var unitLabel: String {
isMPH ? "mph" : "kph" isMPH ? "mph" : "kph"
} }
// Converts and formats trip distance string
var distanceLabel: String { var distanceLabel: String {
let distance = isMPH ? tripDistanceMeters * 0.000621371 : tripDistanceMeters / 1000 let distance = isMPH ? tripDistanceMeters * 0.000621371 : tripDistanceMeters / 1000
let roundedDistance = Int(distance.rounded()) return "\(Int(distance.rounded())) \(isMPH ? "miles" : "km")"
return "\(roundedDistance) \(isMPH ? "miles" : "km")"
} }
// Determines color based on GPS accuracy
var accuracyColor: Color { var accuracyColor: Color {
switch locationManager.accuracy { switch locationManager.accuracy {
case ..<10: case ..<10: return .green
return .green case 10..<30: return .orange
case 10..<30: default: return .red
return .orange
default:
return .red
} }
} }
// UI colors
var foregroundColor: Color {
isDarkMode ? .white : .black
}
var secondaryTextColor: Color {
.gray
}
var backgroundColor: Color {
isDarkMode ? .black : .white
}
var body: some View { var body: some View {
ZStack { ZStack {
Color.black.ignoresSafeArea() backgroundColor.ignoresSafeArea()
VStack(spacing: 30) { VStack(spacing: 30) {
Spacer() Spacer()
// Center Speed Display with Unit // Main speed display
HStack(alignment: .bottom, spacing: 4) { HStack(alignment: .bottom, spacing: 4) {
Text("\(currentSpeed)") Text("\(currentSpeed)")
.font(.system(size: 140, weight: .bold, design: .rounded)) .font(.system(size: 140, weight: .bold, design: .rounded))
.foregroundColor(.white) .foregroundColor(foregroundColor)
Text(unitLabel) Text(unitLabel)
.font(.title2) .font(.title2)
.foregroundColor(.gray) .foregroundColor(secondaryTextColor)
.onTapGesture { .onTapGesture {
isMPH.toggle() isMPH.toggle()
} }
} }
// Trip Distance if !isMinimalView {
Text("Trip: \(distanceLabel)") // Trip distance and reset button
.font(.title2) HStack(spacing: 8) {
.foregroundColor(.gray) Text("Trip: \(distanceLabel)")
.font(.title2)
.foregroundColor(secondaryTextColor)
// GPS Accuracy Display Button(action: {
Text(String(format: "GPS Accuracy: ±%.0f meters", locationManager.accuracy)) tripDistanceMeters = 0
.font(.headline) previousLocation = nil
.foregroundColor(accuracyColor) }) {
Image(systemName: "arrow.counterclockwise.circle")
.font(.title2)
.foregroundColor(secondaryTextColor)
}
.buttonStyle(PlainButtonStyle())
}
// Reset Button (smaller width) // GPS accuracy display
Button(action: { Text(String(format: "GPS Accuracy: ±%.0f meters", locationManager.accuracy))
tripDistanceMeters = 0
previousLocation = nil
}) {
Text("Reset Trip")
.font(.headline) .font(.headline)
.padding(.horizontal, 20) .foregroundColor(accuracyColor)
.padding(.vertical, 10) } else if locationManager.accuracy == 0 {
.background(Color.gray) Text("Waiting for GPS...")
.foregroundColor(.black) .foregroundColor(.gray)
.cornerRadius(12)
} }
Spacer() Spacer()
} }
.padding() .padding()
// Settings Button (Sprocket icon) at the bottom right with padding // Toggle buttons
VStack { VStack {
Spacer() Spacer()
HStack { HStack {
// Toggle minimal/full view
Button(action: {
isMinimalView.toggle()
}) {
Image(systemName: isMinimalView ? "eye.slash.fill" : "eye.fill")
.font(.title2)
.foregroundColor(foregroundColor)
.padding()
}
Spacer() Spacer()
// Open app settings
Button(action: { Button(action: {
if let url = URL(string: UIApplication.openSettingsURLString) { if let url = URL(string: UIApplication.openSettingsURLString) {
UIApplication.shared.open(url) UIApplication.shared.open(url)
} }
}) { }) {
Image(systemName: "gearshape.fill") // Sprocket icon Image(systemName: "gearshape.fill")
.font(.title2) .font(.title2)
.foregroundColor(.white) .foregroundColor(foregroundColor)
.padding() .padding()
} }
.padding(.bottom, 10) // Add some bottom padding
} }
.padding(.trailing) // Keep the trailing padding for right alignment .padding([.leading, .trailing, .bottom], 10)
} }
} }
.onTapGesture {
isDarkMode.toggle()
}
.alert(isPresented: $locationManager.showAlert) { .alert(isPresented: $locationManager.showAlert) {
Alert( Alert(
title: Text("Location Services"), title: Text("Location Services"),
@@ -120,8 +153,11 @@ struct ContentView: View {
} }
) )
} }
// Update trip distance with each valid location update
.onReceive(locationManager.$location) { location in .onReceive(locationManager.$location) { location in
if let newLocation = location { if let newLocation = location,
newLocation.speed > 0.5,
newLocation.horizontalAccuracy < 20 {
if let oldLocation = previousLocation { if let oldLocation = previousLocation {
let distance = newLocation.distance(from: oldLocation) let distance = newLocation.distance(from: oldLocation)
tripDistanceMeters += distance tripDistanceMeters += distance
@@ -131,3 +167,4 @@ struct ContentView: View {
} }
} }
} }
+27 -39
View File
@@ -1,9 +1,6 @@
// // Speedometer App for iOS
// LocationManager.swift // Author: Justin Oros
// Speedometer // LocationManager.swift
//
// Created by Justin Oros on 5/2/2025.
//
import Foundation import Foundation
import CoreLocation import CoreLocation
@@ -12,7 +9,7 @@ import SwiftUI
class LocationManager: NSObject, ObservableObject, CLLocationManagerDelegate { class LocationManager: NSObject, ObservableObject, CLLocationManagerDelegate {
private let manager = CLLocationManager() private let manager = CLLocationManager()
@Published var speed: Int = 0 @Published var speedInMS: Double = 0.0
@Published var location: CLLocation? @Published var location: CLLocation?
@Published var accuracy: Double = 0.0 @Published var accuracy: Double = 0.0
@Published var authorizationStatus: CLAuthorizationStatus = .notDetermined @Published var authorizationStatus: CLAuthorizationStatus = .notDetermined
@@ -25,89 +22,80 @@ class LocationManager: NSObject, ObservableObject, CLLocationManagerDelegate {
manager.activityType = .automotiveNavigation manager.activityType = .automotiveNavigation
manager.desiredAccuracy = kCLLocationAccuracyBestForNavigation manager.desiredAccuracy = kCLLocationAccuracyBestForNavigation
manager.distanceFilter = 1 manager.distanceFilter = 1
print("LocationManager initialized")
requestAuthorization() requestAuthorization()
startUpdatingLocationIfNeeded() startUpdatingLocationIfNeeded()
} }
// Request location access permission
private func requestAuthorization() { private func requestAuthorization() {
print("Requesting location authorization")
manager.requestWhenInUseAuthorization() manager.requestWhenInUseAuthorization()
} }
// Begin location updates if permission is granted
private func startUpdatingLocationIfNeeded() { private func startUpdatingLocationIfNeeded() {
if authorizationStatus == .authorizedAlways || authorizationStatus == .authorizedWhenInUse { if authorizationStatus == .authorizedAlways || authorizationStatus == .authorizedWhenInUse {
print("Attempting to start location updates")
manager.startUpdatingLocation() manager.startUpdatingLocation()
} else if authorizationStatus == .denied || authorizationStatus == .restricted { } else if authorizationStatus == .denied || authorizationStatus == .restricted {
print("Location updates not started due to denied/restricted authorization")
DispatchQueue.main.async { DispatchQueue.main.async {
self.showAlert = true self.showAlert = true
self.alertMessage = "Location services are disabled. Please enable them in Settings." self.alertMessage = "Location services are disabled. Please enable them in Settings."
} }
} else {
print("Authorization not yet determined: \(authorizationStatus.rawValue)")
} }
} }
// Handle changes in authorization status
func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) { func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
let status = manager.authorizationStatus let status = manager.authorizationStatus
DispatchQueue.main.async { DispatchQueue.main.async {
self.authorizationStatus = status self.authorizationStatus = status
print("Authorization status changed: \(status.rawValue)")
self.startUpdatingLocationIfNeeded() self.startUpdatingLocationIfNeeded()
} }
switch status { switch status {
case .authorizedAlways:
print("Location authorization: Authorized Always")
case .authorizedWhenInUse:
print("Location authorization: Authorized When In Use")
case .denied: case .denied:
print("Location authorization: Denied")
DispatchQueue.main.async { DispatchQueue.main.async {
self.showAlert = true self.showAlert = true
self.alertMessage = "Location access was denied. Please enable it in Settings for the app to function correctly." self.alertMessage = "Location access was denied. Please enable it in Settings."
} }
case .restricted: case .restricted:
print("Location authorization: Restricted")
DispatchQueue.main.async { DispatchQueue.main.async {
self.showAlert = true self.showAlert = true
self.alertMessage = "Location access is restricted. This might be due to parental controls or system settings." self.alertMessage = "Location access is restricted."
} }
case .notDetermined: default: break
print("Location authorization: Not Determined")
@unknown default:
print("Location authorization: Unknown status")
} }
} }
// Handle new location data
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let latest = locations.last else { guard let latest = locations.last,
print("No latest location found") latest.horizontalAccuracy >= 0,
return latest.horizontalAccuracy < 20 else { return }
}
print("New location: \(latest.coordinate.latitude), \(latest.coordinate.longitude)")
print("Speed (m/s): \(latest.speed)")
print("Speed (MPH): \(latest.speed * 2.23694)")
print("Accuracy: \(latest.horizontalAccuracy)")
DispatchQueue.main.async { DispatchQueue.main.async {
let rawSpeed = max(latest.speed, 0) let rawSpeed = max(latest.speed, 0)
let mph = rawSpeed * 2.23694 self.speedInMS = rawSpeed < 0.5 ? 0 : rawSpeed
self.speed = Int(mph.rounded())
self.location = latest self.location = latest
self.accuracy = latest.horizontalAccuracy self.accuracy = latest.horizontalAccuracy
} }
} }
// Handle location update errors
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) { func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("Location update failed: \(error.localizedDescription)") let nsError = error as NSError
// Ignore transient or non-critical errors
if nsError.code == CLError.locationUnknown.rawValue ||
nsError.code == CLError.network.rawValue ||
nsError.code == CLError.denied.rawValue ||
nsError.code == CLError.deferredFailed.rawValue {
return
}
DispatchQueue.main.async { DispatchQueue.main.async {
self.showAlert = true self.showAlert = true
self.alertMessage = "Location update failed: \(error.localizedDescription)" self.alertMessage = "Location update failed: \(error.localizedDescription)"
} }
} }
} }
+2 -5
View File
@@ -1,9 +1,6 @@
// // Speedometer App for iOS
// Author: Justin Oros
// SpeedometerApp.swift // SpeedometerApp.swift
// Speedometer
//
// Created by Justin Oros on 5/2/2025.
//
import SwiftUI import SwiftUI