Centralize lateness logic

- I finally put the app in the Travel category. xD
- I also moved the train lateness and arrival calculations into AmtrakDateFormatting from ContentView. I also renamed the timezone formatting parameters for clarity.
This commit is contained in:
Tony Bark 2026-08-24 21:05:11 -04:00
parent 79176585b4
commit e0077ec13b
3 changed files with 38 additions and 34 deletions

View file

@ -169,27 +169,6 @@ struct ContentView: View {
}
}
func isOnTime(arr: String?, schArr: String?) -> TrainLateness {
guard
let actualDate = amtrakDate.parsedDate(from: arr),
let scheduledDate = amtrakDate.parsedDate(from: schArr)
else {
return .unknown
}
let differenceInMinutes = Int(actualDate.timeIntervalSince(scheduledDate) / 60)
switch differenceInMinutes {
case -1...1:
return .onTime
case ..<0:
return .early(minutes: -differenceInMinutes)
default:
return .late(minutes: differenceInMinutes)
}
}
func trainStatus(
for train: Train,
atStationCode code: String,
@ -210,12 +189,12 @@ struct ContentView: View {
let time = amtrakDate.time(
from: leg?.arr,
timeZone: timeZone,
relativeTo: originZone
in: timeZone,
comparedTo: originZone
) ?? amtrakDate.time(
from: leg?.schArr,
timeZone: timeZone,
relativeTo: originZone
in: timeZone,
comparedTo: originZone
)
currentStation = stationName
@ -229,9 +208,9 @@ struct ContentView: View {
train: train.routeName,
to: train.destName,
from: train.origName,
status: isOnTime(
arr: leg?.arr,
schArr: leg?.schArr
status: amtrakDate.isOnTime(
actual: leg?.arr,
scheduled: leg?.schArr
),
track: track.isEmpty ? "" : track
)