Missed fillups

This commit is contained in:
Kameron Kenny 2025-03-19 22:45:16 -04:00
parent e4818a2c2f
commit 00d6d89fcb
7 changed files with 32 additions and 7 deletions

View File

@ -23,9 +23,9 @@ struct AddFuelLogView: View {
@State private var locationName = "" @State private var locationName = ""
@State private var selectedOctane: Int = 87 // Default or from previous record @State private var selectedOctane: Int = 87 // Default or from previous record
@State private var pricePerGalon = "" @State private var pricePerGalon = ""
// New Full Tank toggle; default is on (true)
@State private var fullTank: Bool = true @State private var fullTank: Bool = true
@State private var missedPrevious: Bool = false
// Allowed octane options // Allowed octane options
let octaneOptions = [87, 89, 91, 92, 93, 95] let octaneOptions = [87, 89, 91, 92, 93, 95]
@ -115,6 +115,9 @@ struct AddFuelLogView: View {
Toggle("Full Tank", isOn: $fullTank) Toggle("Full Tank", isOn: $fullTank)
.toggleStyle(SwitchToggleStyle(tint: .blue)) .toggleStyle(SwitchToggleStyle(tint: .blue))
Toggle("Missed Previous Fill-up", isOn: $missedPrevious)
.toggleStyle(SwitchToggleStyle(tint: .blue))
Button("Get Current Location") { Button("Get Current Location") {
locationManager.requestLocation() locationManager.requestLocation()
} }
@ -255,8 +258,8 @@ struct AddFuelLogView: View {
newLog.locationName = locationName newLog.locationName = locationName
newLog.octane = Int16(selectedOctane) newLog.octane = Int16(selectedOctane)
newLog.pricePerGalon = Double(pricePerGalon) ?? 0 newLog.pricePerGalon = Double(pricePerGalon) ?? 0
// Set the fullTank property from the toggle:
newLog.fullTank = fullTank newLog.fullTank = fullTank
newLog.missedPrevious = missedPrevious
if let vehicleID = selectedVehicleID, if let vehicleID = selectedVehicleID,
let selectedVehicle = vehicles.first(where: { $0.id == vehicleID }) { let selectedVehicle = vehicles.first(where: { $0.id == vehicleID }) {

View File

@ -24,8 +24,8 @@ struct EditFuelLogView: View {
@State private var locationName = "" @State private var locationName = ""
@State private var selectedOctane: Int = 87 @State private var selectedOctane: Int = 87
@State private var pricePerGalon = "" @State private var pricePerGalon = ""
// Full Tank toggle
@State private var fullTank: Bool = true @State private var fullTank: Bool = true
@State private var missedPrevious: Bool = false
// Allowed octane options // Allowed octane options
let octaneOptions = [87, 89, 91, 92, 93, 95] let octaneOptions = [87, 89, 91, 92, 93, 95]
@ -89,6 +89,9 @@ struct EditFuelLogView: View {
Toggle("Full Tank", isOn: $fullTank) Toggle("Full Tank", isOn: $fullTank)
.toggleStyle(SwitchToggleStyle(tint: .blue)) .toggleStyle(SwitchToggleStyle(tint: .blue))
Toggle("Missed Previous Fill-up", isOn: $missedPrevious)
.toggleStyle(SwitchToggleStyle(tint: .blue))
Button("Get Current Location") { Button("Get Current Location") {
// Optionally trigger location update // Optionally trigger location update
} }
@ -130,6 +133,7 @@ struct EditFuelLogView: View {
cost = String(format: "%.2f", fuelLog.cost) cost = String(format: "%.2f", fuelLog.cost)
pricePerGalon = String(format: "%.3f", fuelLog.pricePerGalon) pricePerGalon = String(format: "%.3f", fuelLog.pricePerGalon)
fullTank = fuelLog.fullTank fullTank = fuelLog.fullTank
missedPrevious = fuelLog.missedPrevious
locationCoordinates = fuelLog.locationCoordinates ?? "" locationCoordinates = fuelLog.locationCoordinates ?? ""
locationName = fuelLog.locationName ?? "" locationName = fuelLog.locationName ?? ""
selectedOctane = Int(fuelLog.octane) selectedOctane = Int(fuelLog.octane)
@ -187,6 +191,7 @@ struct EditFuelLogView: View {
fuelLog.octane = Int16(selectedOctane) fuelLog.octane = Int16(selectedOctane)
fuelLog.pricePerGalon = Double(pricePerGalon) ?? 0 fuelLog.pricePerGalon = Double(pricePerGalon) ?? 0
fuelLog.fullTank = fullTank fuelLog.fullTank = fullTank
fuelLog.missedPrevious = missedPrevious
do { do {
try viewContext.save() try viewContext.save()

View File

@ -73,6 +73,21 @@ struct FuelLogDetailView: View {
} }
} }
} }
Section {
HStack {
Text("Previous Fill-Up")
Spacer()
if fuelLog.missedPrevious {
Image(systemName: "xmark.circle.fill")
.foregroundColor(.red)
Text("Missed")
} else {
Image(systemName: "checkmark.circle.fill")
.foregroundColor(.green)
Text("Exists")
}
}
}
Section(header: Text("Location")) { Section(header: Text("Location")) {
HStack { HStack {
Text("Coordinates:") Text("Coordinates:")

View File

@ -57,7 +57,7 @@ struct FuelLogListView: View {
let current = logs[i] let current = logs[i]
let next = logs[i + 1] let next = logs[i + 1]
// Only include if fullTank is true, fuelVolume is positive, and odometer difference is positive. // Only include if fullTank is true, fuelVolume is positive, and odometer difference is positive.
if current.fullTank, current.fuelVolume > 0, current.odometer > next.odometer { if current.fullTank, !current.missedPrevious, current.fuelVolume > 0, current.odometer > next.odometer {
let mpg = (current.odometer - next.odometer) / current.fuelVolume let mpg = (current.odometer - next.odometer) / current.fuelVolume
mpgValues.append(mpg) mpgValues.append(mpg)
} }
@ -152,6 +152,7 @@ struct FuelLogListView: View {
let mpg: Double? = { let mpg: Double? = {
// Only calculate MPG if fullTank is true. // Only calculate MPG if fullTank is true.
guard log.fullTank else { return nil } guard log.fullTank else { return nil }
guard !log.missedPrevious else { return nil }
guard index < filteredFuelLogs.count - 1 else { return nil } guard index < filteredFuelLogs.count - 1 else { return nil }
let previousLog = filteredFuelLogs[index + 1] let previousLog = filteredFuelLogs[index + 1]
let milesDriven = log.odometer - previousLog.odometer let milesDriven = log.odometer - previousLog.odometer

View File

@ -8,6 +8,7 @@
<attribute name="id" optional="YES" attributeType="UUID" usesScalarValueType="NO"/> <attribute name="id" optional="YES" attributeType="UUID" usesScalarValueType="NO"/>
<attribute name="locationCoordinates" optional="YES" attributeType="String"/> <attribute name="locationCoordinates" optional="YES" attributeType="String"/>
<attribute name="locationName" optional="YES" attributeType="String"/> <attribute name="locationName" optional="YES" attributeType="String"/>
<attribute name="missedPrevious" optional="YES" attributeType="Boolean" usesScalarValueType="YES"/>
<attribute name="octane" attributeType="Integer 16" defaultValueString="0" usesScalarValueType="YES"/> <attribute name="octane" attributeType="Integer 16" defaultValueString="0" usesScalarValueType="YES"/>
<attribute name="odometer" attributeType="Double" defaultValueString="0.0" usesScalarValueType="YES"/> <attribute name="odometer" attributeType="Double" defaultValueString="0.0" usesScalarValueType="YES"/>
<attribute name="pricePerGalon" attributeType="Double" defaultValueString="0.0" usesScalarValueType="YES"/> <attribute name="pricePerGalon" attributeType="Double" defaultValueString="0.0" usesScalarValueType="YES"/>

View File

@ -14,7 +14,7 @@ struct MPGTrendChartView: View {
for i in 1..<sortedLogs.count { for i in 1..<sortedLogs.count {
let previous = sortedLogs[i - 1] let previous = sortedLogs[i - 1]
let current = sortedLogs[i] let current = sortedLogs[i]
if current.fullTank, let date = current.date, if current.fullTank, !current.missedPrevious, let date = current.date,
current.odometer > previous.odometer, current.odometer > previous.odometer,
current.fuelVolume > 0 { current.fuelVolume > 0 {
let mpg = (current.odometer - previous.odometer) / current.fuelVolume let mpg = (current.odometer - previous.odometer) / current.fuelVolume

View File

@ -49,7 +49,7 @@ struct StatsView: View {
for i in 0..<logs.count - 1 { for i in 0..<logs.count - 1 {
let current = logs[i] let current = logs[i]
let next = logs[i + 1] let next = logs[i + 1]
if current.fullTank, current.fuelVolume > 0, current.odometer > next.odometer { if current.fullTank, !current.missedPrevious, current.fuelVolume > 0, current.odometer > next.odometer {
let mpg = (current.odometer - next.odometer) / current.fuelVolume let mpg = (current.odometer - next.odometer) / current.fuelVolume
mpgValues.append(mpg) mpgValues.append(mpg)
} }