mirror of
https://github.com/tonytins/amtkstat.git
synced 2026-10-24 05:32:54 -04:00
Finished wiring up loadTrainStatus()
Did a little code formatting after that cause Xcode is weird
This commit is contained in:
parent
3eb2018a8e
commit
f20d49f075
5 changed files with 62 additions and 45 deletions
|
|
@ -8,7 +8,7 @@ let package = Package(
|
||||||
dependencies: [
|
dependencies: [
|
||||||
.package(
|
.package(
|
||||||
url: "https://github.com/moreSwift/swift-cross-ui",
|
url: "https://github.com/moreSwift/swift-cross-ui",
|
||||||
.upToNextMinor(from: "0.7.0")
|
.upToNextMinor(from: "0.7.0"),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
targets: [
|
targets: [
|
||||||
|
|
@ -17,7 +17,7 @@ let package = Package(
|
||||||
dependencies: [
|
dependencies: [
|
||||||
.product(name: "SwiftCrossUI", package: "swift-cross-ui"),
|
.product(name: "SwiftCrossUI", package: "swift-cross-ui"),
|
||||||
.product(name: "DefaultBackend", package: "swift-cross-ui"),
|
.product(name: "DefaultBackend", package: "swift-cross-ui"),
|
||||||
]
|
],
|
||||||
),
|
),
|
||||||
]
|
],
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,11 @@ struct AmtrakClient {
|
||||||
try await get(trainsBaseURL)
|
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 {
|
func fetchStation(code stationid: String) async throws -> StationResponse {
|
||||||
try await get("\(stationBaseURL)/\(stationid)")
|
try await get("\(stationBaseURL)/\(stationid)")
|
||||||
}
|
}
|
||||||
|
|
@ -45,7 +50,6 @@ struct AmtrakClient {
|
||||||
}
|
}
|
||||||
|
|
||||||
func get<T: Decodable>(_ urlString: String) async throws -> T {
|
func get<T: Decodable>(_ urlString: String) async throws -> T {
|
||||||
|
|
||||||
guard let url = URL(string: urlString) else {
|
guard let url = URL(string: urlString) else {
|
||||||
throw AmtrakError.invalidURL(urlString)
|
throw AmtrakError.invalidURL(urlString)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
import SwiftCrossUI
|
|
||||||
import DefaultBackend
|
import DefaultBackend
|
||||||
|
import SwiftCrossUI
|
||||||
|
|
||||||
@main
|
@main
|
||||||
struct AmtrakStatusApp: App {
|
struct AmtrakStatusApp: App {
|
||||||
|
|
|
||||||
|
|
@ -16,8 +16,7 @@ struct ContentView: View {
|
||||||
TextField("e.g. ASD", text: $stationCode)
|
TextField("e.g. ASD", text: $stationCode)
|
||||||
.frame(width: 100)
|
.frame(width: 100)
|
||||||
|
|
||||||
Button("Refresh")
|
Button("Refresh") {
|
||||||
{
|
|
||||||
Task {
|
Task {
|
||||||
await loadTrainStatus(forStationCode: normalizedStationCode)
|
await loadTrainStatus(forStationCode: normalizedStationCode)
|
||||||
}
|
}
|
||||||
|
|
@ -28,15 +27,13 @@ struct ContentView: View {
|
||||||
Text(errorMessage).padding(10)
|
Text(errorMessage).padding(10)
|
||||||
}
|
}
|
||||||
|
|
||||||
Table(rows)
|
Table(rows) {
|
||||||
{
|
|
||||||
TableColumn("Number", value: \TrainStatusRow.trainNum)
|
TableColumn("Number", value: \TrainStatusRow.trainNum)
|
||||||
TableColumn("Number", value: \TrainStatusRow.routeName)
|
TableColumn("Number", value: \TrainStatusRow.routeName)
|
||||||
TableColumn("On Time", value: \TrainStatusRow.onTime)
|
TableColumn("On Time", value: \TrainStatusRow.onTime)
|
||||||
TableColumn("Platform", value: \TrainStatusRow.platform)
|
TableColumn("Platform", value: \TrainStatusRow.platform)
|
||||||
|
|
||||||
}.overlay(alignment: .bottomTrailing)
|
}.overlay(alignment: .bottomTrailing) {
|
||||||
{
|
|
||||||
if isLoading {
|
if isLoading {
|
||||||
ProgressView()
|
ProgressView()
|
||||||
.padding(10)
|
.padding(10)
|
||||||
|
|
@ -66,7 +63,7 @@ struct ContentView: View {
|
||||||
|
|
||||||
do {
|
do {
|
||||||
let trainNumbers = try await client.fetchStationTrainNumbers(
|
let trainNumbers = try await client.fetchStationTrainNumbers(
|
||||||
stationCode: code
|
stationCode: code,
|
||||||
)
|
)
|
||||||
|
|
||||||
guard !trainNumbers.isEmpty else {
|
guard !trainNumbers.isEmpty else {
|
||||||
|
|
@ -77,11 +74,25 @@ struct ContentView: View {
|
||||||
|
|
||||||
var newRows: [TrainStatusRow] = []
|
var newRows: [TrainStatusRow] = []
|
||||||
for number in trainNumbers {
|
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"
|
errorMessage = "Station \(code) not foudn"
|
||||||
rows = []
|
rows = []
|
||||||
} catch {
|
} catch {
|
||||||
|
|
|
||||||
|
|
@ -66,7 +66,9 @@ typealias TrainResponse = [String: [Train]]
|
||||||
typealias StationResponse = [String: StationMeta]
|
typealias StationResponse = [String: StationMeta]
|
||||||
|
|
||||||
struct TrainStatusRow: Identifiable {
|
struct TrainStatusRow: Identifiable {
|
||||||
var id: String { trainID }
|
var id: String {
|
||||||
|
trainID
|
||||||
|
}
|
||||||
|
|
||||||
var trainID: String
|
var trainID: String
|
||||||
var trainNum: String
|
var trainNum: String
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue