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

@ -2,12 +2,12 @@ import SwiftCrossUI
struct ContentView: View {
@State var client = AmtrakClient()
@State var stationCode = "ASD" // Ashland, VA
@State var rows: [TrainStatusRow] = []
@State var isLoading = false
@State var errorMessage: String?
var body: some View {
VStack {
// TODO: Add pre-selected options in the form of buttons like the original
@ -15,28 +15,25 @@ struct ContentView: View {
Text("Station Code:")
TextField("e.g. ASD", text: $stationCode)
.frame(width: 100)
Button("Refresh")
{
Button("Refresh") {
Task {
await loadTrainStatus(forStationCode: normalizedStationCode)
}
}.disabled(normalizedStationCode.isEmpty || isLoading)
}.padding(10)
if let errorMessage {
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)
@ -49,39 +46,53 @@ struct ContentView: View {
}
}
}
var normalizedStationCode: String {
stationCode.trimmingCharacters(in: .whitespacesAndNewlines).uppercased()
}
func loadTrainStatus(forStationCode code: String) async {
guard !code.isEmpty else {
return
}
isLoading = true
errorMessage = nil
defer { isLoading = false }
do {
let trainNumbers = try await client.fetchStationTrainNumbers(
stationCode: code
stationCode: code,
)
guard !trainNumbers.isEmpty else {
errorMessage = "Station \(code) has no trains listed right now"
rows = []
return
}
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 {