Finished wiring up loadTrainStatus()

Did a little code formatting after that cause Xcode is weird
This commit is contained in:
Tony Bark 2026-08-22 17:49:41 -04:00
parent 3eb2018a8e
commit f20d49f075
5 changed files with 62 additions and 45 deletions

View file

@ -8,7 +8,7 @@ let package = Package(
dependencies: [
.package(
url: "https://github.com/moreSwift/swift-cross-ui",
.upToNextMinor(from: "0.7.0")
.upToNextMinor(from: "0.7.0"),
),
],
targets: [
@ -17,7 +17,7 @@ let package = Package(
dependencies: [
.product(name: "SwiftCrossUI", package: "swift-cross-ui"),
.product(name: "DefaultBackend", package: "swift-cross-ui"),
]
],
),
]
],
)

View file

@ -22,6 +22,11 @@ struct AmtrakClient {
try await get(trainsBaseURL)
}
func fetchAllTrainStatus(num trainNum: String) async throws -> [Train] {
let reponse = try await fetchTrain(number: trainNum)
return reponse.values.flatMap(\.self)
}
func fetchStation(code stationid: String) async throws -> StationResponse {
try await get("\(stationBaseURL)/\(stationid)")
}
@ -45,7 +50,6 @@ struct AmtrakClient {
}
func get<T: Decodable>(_ urlString: String) async throws -> T {
guard let url = URL(string: urlString) else {
throw AmtrakError.invalidURL(urlString)
}

View file

@ -1,5 +1,5 @@
import SwiftCrossUI
import DefaultBackend
import SwiftCrossUI
@main
struct AmtrakStatusApp: App {

View file

@ -16,8 +16,7 @@ struct ContentView: View {
TextField("e.g. ASD", text: $stationCode)
.frame(width: 100)
Button("Refresh")
{
Button("Refresh") {
Task {
await loadTrainStatus(forStationCode: normalizedStationCode)
}
@ -28,15 +27,13 @@ struct ContentView: View {
Text(errorMessage).padding(10)
}
Table(rows)
{
Table(rows) {
TableColumn("Number", value: \TrainStatusRow.trainNum)
TableColumn("Number", value: \TrainStatusRow.routeName)
TableColumn("On Time", value: \TrainStatusRow.onTime)
TableColumn("Platform", value: \TrainStatusRow.platform)
}.overlay(alignment: .bottomTrailing)
{
}.overlay(alignment: .bottomTrailing) {
if isLoading {
ProgressView()
.padding(10)
@ -66,7 +63,7 @@ struct ContentView: View {
do {
let trainNumbers = try await client.fetchStationTrainNumbers(
stationCode: code
stationCode: code,
)
guard !trainNumbers.isEmpty else {
@ -77,11 +74,25 @@ struct ContentView: View {
var newRows: [TrainStatusRow] = []
for number in trainNumbers {
// let trains = await client.fet
let trains = try await client.fetchAllTrainStatus(num: number)
for train in trains {
let platform = train.stations?.first(
where: { $0.code == code
},
)?.platform
newRows.append(TrainStatusRow(
trainID: train.trainID,
trainNum: train.trainNum,
routeName: train.routeName,
onTime: train.trainTimely.isEmpty ? "" : train.trainTimely,
platform: (
platform?.isEmpty == false,
) ? platform! : "",
))
}
} catch AmtrakError.stationNotFound(let code)
{
}
rows = newRows
} catch let AmtrakError.stationNotFound(code) {
errorMessage = "Station \(code) not foudn"
rows = []
} catch {

View file

@ -66,7 +66,9 @@ typealias TrainResponse = [String: [Train]]
typealias StationResponse = [String: StationMeta]
struct TrainStatusRow: Identifiable {
var id: String { trainID }
var id: String {
trainID
}
var trainID: String
var trainNum: String