mirror of
https://github.com/tonytins/amtkstat.git
synced 2026-10-24 05:32:54 -04:00
Compare commits
No commits in common. "ef98531ca28fe6a2f66d981b0d94aeecf380aef7" and "79176585b441580b2af85b148c0d8bd7e9b468d9" have entirely different histories.
ef98531ca2
...
79176585b4
6 changed files with 48 additions and 113 deletions
|
|
@ -1,4 +1,3 @@
|
|||
# https://swiftbundler.dev/documentation/swift-bundler/configuration
|
||||
format_version = 2
|
||||
|
||||
[apps.AmtrakStatus]
|
||||
|
|
@ -6,7 +5,6 @@ identifier = 'com.tonybark.AmtrakStatus'
|
|||
product = 'AmtrakStatus'
|
||||
version = '0.2.103'
|
||||
icon = "./assets/icon.png"
|
||||
category = "public.app-category.travel"
|
||||
|
||||
|
||||
[[apps.AmtrakStatus.overlays]]
|
||||
|
|
|
|||
|
|
@ -28,10 +28,6 @@ struct AmtrakClient {
|
|||
try await get(trainsBaseURL)
|
||||
}
|
||||
|
||||
func fetchStaleStatus() async throws -> StaleData {
|
||||
try await get(staleBaseURL)
|
||||
}
|
||||
|
||||
func fetchAllTrainStatus(num trainNum: String) async throws -> [Train] {
|
||||
let reponse = try await fetchTrain(number: trainNum)
|
||||
return reponse.values.flatMap(\.self)
|
||||
|
|
|
|||
|
|
@ -26,21 +26,21 @@ struct AmtrakDateFormatting {
|
|||
return isoFormatter.date(from: isoString)
|
||||
}
|
||||
|
||||
func time(from isoString: String?, in zone: TimeZone?, comparedTo originZone: TimeZone? = nil) -> String? {
|
||||
func time(from isoString: String?, timeZone: TimeZone?, relativeTo otherZone: TimeZone? = nil) -> String? {
|
||||
guard let isoString, !isoString.isEmpty, let date = isoFormatter.date(
|
||||
from: isoString) else {
|
||||
return nil
|
||||
}
|
||||
|
||||
timeFormatter.timeZone = zone
|
||||
timeFormatter.timeZone = timeZone
|
||||
|
||||
let formatted = timeFormatter.string(from: date)
|
||||
|
||||
guard
|
||||
let zone,
|
||||
let originZone,
|
||||
zone.identifier != originZone.identifier,
|
||||
let abbreviation = zone.abbreviation(for: date)
|
||||
let timeZone,
|
||||
let otherZone,
|
||||
timeZone.identifier != otherZone.identifier,
|
||||
let abbreviation = timeZone.abbreviation(for: date)
|
||||
else {
|
||||
return formatted
|
||||
}
|
||||
|
|
@ -48,29 +48,6 @@ struct AmtrakDateFormatting {
|
|||
return "\(formatted) \(abbreviation)"
|
||||
}
|
||||
|
||||
|
||||
func isOnTime(actual: String?, scheduled: String?) -> LateTrain {
|
||||
|
||||
guard
|
||||
let actualDate = parsedDate(from: actual),
|
||||
let scheduledDate = parsedDate(from: scheduled)
|
||||
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 localTime(timeZone: TimeZone?) -> String {
|
||||
let now = Date()
|
||||
timeFormatter.timeZone = timeZone
|
||||
|
|
|
|||
|
|
@ -14,7 +14,6 @@ struct ContentView: View {
|
|||
|
||||
let amtrakDate = AmtrakDateFormatting()
|
||||
let unknown = "Unknown"
|
||||
let staleDataThreshold: TimeInterval = 60 * 60 * 24 // 24 hours
|
||||
|
||||
var body: some View {
|
||||
VStack {
|
||||
|
|
@ -138,7 +137,6 @@ struct ContentView: View {
|
|||
}
|
||||
|
||||
var zoneCache: [String: TimeZone?] = [code: lookup.timeZone]
|
||||
let staleness = await staleTrainLookup()
|
||||
|
||||
var newRows: [TrainStatusRow] = []
|
||||
for number in lookup.trainNumbers {
|
||||
|
|
@ -148,16 +146,15 @@ struct ContentView: View {
|
|||
where: { $0.code == code
|
||||
},
|
||||
)?.platform
|
||||
let row = await trainStatus(
|
||||
await newRows
|
||||
.append(
|
||||
trainStatus(
|
||||
for: train,
|
||||
code: code,
|
||||
zone: lookup.timeZone,
|
||||
cache: &zoneCache,
|
||||
staleness: staleness
|
||||
atStationCode: code,
|
||||
timeZone: lookup.timeZone,
|
||||
zoneCache: &zoneCache
|
||||
)
|
||||
if let row {
|
||||
newRows.append(row)
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -172,22 +169,34 @@ struct ContentView: View {
|
|||
}
|
||||
}
|
||||
|
||||
func trainStatus(
|
||||
for train: Train,
|
||||
code stationCode: String,
|
||||
zone timeZone: TimeZone?,
|
||||
cache zoneCache: inout [String: TimeZone?],
|
||||
staleness: [String: TimeInterval]
|
||||
) async -> TrainStatusRow? {
|
||||
let leg = train.stations?.first { $0.code == stationCode }
|
||||
func isOnTime(arr: String?, schArr: String?) -> TrainLateness {
|
||||
|
||||
guard
|
||||
!isStale(leg, timeZone: timeZone) &&
|
||||
!isDataStale(train.trainID, staleness: staleness)
|
||||
let actualDate = amtrakDate.parsedDate(from: arr),
|
||||
let scheduledDate = amtrakDate.parsedDate(from: schArr)
|
||||
else {
|
||||
return nil
|
||||
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,
|
||||
timeZone: TimeZone?,
|
||||
zoneCache: inout [String: TimeZone?]
|
||||
) async -> TrainStatusRow {
|
||||
let leg = train.stations?.first { $0.code == code }
|
||||
let stationName = leg?.name ?? unknown
|
||||
let track = leg?.platform ?? ""
|
||||
|
||||
|
|
@ -201,12 +210,12 @@ struct ContentView: View {
|
|||
|
||||
let time = amtrakDate.time(
|
||||
from: leg?.arr,
|
||||
in: timeZone,
|
||||
comparedTo: originZone
|
||||
timeZone: timeZone,
|
||||
relativeTo: originZone
|
||||
) ?? amtrakDate.time(
|
||||
from: leg?.schArr,
|
||||
in: timeZone,
|
||||
comparedTo: originZone
|
||||
timeZone: timeZone,
|
||||
relativeTo: originZone
|
||||
)
|
||||
|
||||
currentStation = stationName
|
||||
|
|
@ -220,53 +229,14 @@ struct ContentView: View {
|
|||
train: train.routeName,
|
||||
to: train.destName,
|
||||
from: train.origName,
|
||||
status: amtrakDate.isOnTime(
|
||||
actual: leg?.arr,
|
||||
scheduled: leg?.schArr
|
||||
status: isOnTime(
|
||||
arr: leg?.arr,
|
||||
schArr: leg?.schArr
|
||||
),
|
||||
track: track.isEmpty ? "" : track
|
||||
)
|
||||
}
|
||||
|
||||
func isStale(_ leg: Station?, timeZone: TimeZone?) -> Bool {
|
||||
guard leg?.status == .departed else { return false }
|
||||
|
||||
guard
|
||||
let deparatureDate = amtrakDate.parsedDate(
|
||||
from: leg?.dep
|
||||
) ?? amtrakDate.parsedDate(from: leg?.schDep)
|
||||
else {
|
||||
return false
|
||||
}
|
||||
|
||||
var calendar = Calendar.current
|
||||
calendar.timeZone = timeZone ?? .current
|
||||
return calendar
|
||||
.compare(
|
||||
deparatureDate,
|
||||
to: Date(),
|
||||
toGranularity: .day
|
||||
) == .orderedAscending
|
||||
}
|
||||
|
||||
func isDataStale(
|
||||
_ trainId: String,
|
||||
staleness: [String: TimeInterval]
|
||||
) -> Bool {
|
||||
guard let timeSince = staleness[trainId] else { return false }
|
||||
return timeSince > staleDataThreshold
|
||||
}
|
||||
|
||||
func staleTrainLookup() async -> [String: TimeInterval] {
|
||||
guard let staleData = try? await client.fetchStaleStatus() else {
|
||||
return [:]
|
||||
}
|
||||
return Dictionary(
|
||||
staleData.lastUpdatedArr.map { ( $0.trainID, $0.timeSince) },
|
||||
uniquingKeysWith: { first, _ in first }
|
||||
)
|
||||
}
|
||||
|
||||
func fetchTimeZone(forStationCode code: String, cache: inout [String: TimeZone?]) async -> TimeZone? {
|
||||
if let cached = cache[code] {
|
||||
return cached
|
||||
|
|
|
|||
|
|
@ -81,15 +81,9 @@ struct StationMeta: Codable {
|
|||
}
|
||||
|
||||
struct StaleData: Codable {
|
||||
var activeTrains: Int
|
||||
var avgLastUpdate: Double
|
||||
var lastUpdatedArr: [TrainFreshness]
|
||||
var medianLastUpdate: Double
|
||||
}
|
||||
|
||||
struct TrainFreshness: Codable, Sendable {
|
||||
var trainID: String
|
||||
var timeSince: Double
|
||||
var activeTrains: Int
|
||||
var stale: Bool
|
||||
}
|
||||
|
||||
typealias TrainResponse = [String: [Train]]
|
||||
|
|
@ -105,6 +99,6 @@ struct TrainStatusRow: Identifiable {
|
|||
var train: String
|
||||
var to: String
|
||||
var from: String
|
||||
var status: LateTrain
|
||||
var status: TrainLateness
|
||||
var track: String
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
import Foundation
|
||||
import SwiftCrossUI
|
||||
|
||||
enum LateTrain {
|
||||
enum TrainLateness {
|
||||
case onTime
|
||||
case early(minutes: Int)
|
||||
case late(minutes: Int)
|
||||
Loading…
Add table
Add a link
Reference in a new issue