mirror of
https://github.com/tonytins/amtkstat.git
synced 2026-10-24 05:32:54 -04:00
Train status is now coloured coded for your convenience
- Introduced TrainLateness enum to represent on-time, early, late (with minutes), and unknown states, including description text, color, and formatting logic. - Replaced TrainStatusRow.status String with TrainLateness and update ContentView to render a colored status pill and to return TrainLateness from isOnTime.
This commit is contained in:
parent
3533e5d4b8
commit
915d0ec2dc
3 changed files with 60 additions and 10 deletions
|
|
@ -54,7 +54,15 @@ struct ContentView: View {
|
||||||
TableColumn("Train", value: \TrainStatusRow.routeName)
|
TableColumn("Train", value: \TrainStatusRow.routeName)
|
||||||
TableColumn("To", value: \TrainStatusRow.origin)
|
TableColumn("To", value: \TrainStatusRow.origin)
|
||||||
TableColumn("From", value: \TrainStatusRow.destination)
|
TableColumn("From", value: \TrainStatusRow.destination)
|
||||||
TableColumn("Status", value: \TrainStatusRow.status)
|
TableColumn("Status") {
|
||||||
|
(row: TrainStatusRow) in
|
||||||
|
ZStack {
|
||||||
|
RoundedRectangle(cornerRadius: 0)
|
||||||
|
.fill(row.status.color)
|
||||||
|
Text(row.status.descrption)
|
||||||
|
.foregroundColor(Color.white)
|
||||||
|
}
|
||||||
|
}
|
||||||
TableColumn("Track", value: \TrainStatusRow.platform)
|
TableColumn("Track", value: \TrainStatusRow.platform)
|
||||||
}.frame(minWidth: 600)
|
}.frame(minWidth: 600)
|
||||||
.overlay(alignment: .bottomTrailing) {
|
.overlay(alignment: .bottomTrailing) {
|
||||||
|
|
@ -129,28 +137,25 @@ struct ContentView: View {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func isOnTime(arr: String?, schArr: String?) -> String {
|
func isOnTime(arr: String?, schArr: String?) -> TrainLateness {
|
||||||
let amtrakDate = AmtrakDateFormatting()
|
let amtrakDate = AmtrakDateFormatting()
|
||||||
|
|
||||||
guard
|
guard
|
||||||
let actualDate = amtrakDate.parsedDate(from: arr),
|
let actualDate = amtrakDate.parsedDate(from: arr),
|
||||||
let scheduledDate = amtrakDate.parsedDate(from: schArr)
|
let scheduledDate = amtrakDate.parsedDate(from: schArr)
|
||||||
else {
|
else {
|
||||||
return "On Time"
|
return .unknown
|
||||||
}
|
}
|
||||||
|
|
||||||
let differenceInMinutes = Int(actualDate.timeIntervalSince(scheduledDate) / 60)
|
let differenceInMinutes = Int(actualDate.timeIntervalSince(scheduledDate) / 60)
|
||||||
|
|
||||||
switch differenceInMinutes {
|
switch differenceInMinutes {
|
||||||
case -1...1:
|
case -1...1:
|
||||||
return "On Time"
|
return .onTime
|
||||||
case ..<0:
|
case ..<0:
|
||||||
return "\(-differenceInMinutes) min early"
|
return .early(minutes: -differenceInMinutes)
|
||||||
case 0..<60:
|
|
||||||
return "\(differenceInMinutes) min late"
|
|
||||||
default:
|
default:
|
||||||
let hours = differenceInMinutes / 60
|
return .late(minutes: differenceInMinutes)
|
||||||
return "\(hours) hr late"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -97,7 +97,7 @@ struct TrainStatusRow: Identifiable {
|
||||||
var trainID: String
|
var trainID: String
|
||||||
var trainNum: String
|
var trainNum: String
|
||||||
var routeName: String
|
var routeName: String
|
||||||
var status: String
|
var status: TrainLateness
|
||||||
var platform: String
|
var platform: String
|
||||||
var arrival: String
|
var arrival: String
|
||||||
var departure: String
|
var departure: String
|
||||||
|
|
|
||||||
45
Sources/AmtrakStatus/TrainLateness.swift
Normal file
45
Sources/AmtrakStatus/TrainLateness.swift
Normal file
|
|
@ -0,0 +1,45 @@
|
||||||
|
import Foundation
|
||||||
|
import SwiftCrossUI
|
||||||
|
|
||||||
|
enum TrainLateness {
|
||||||
|
case onTime
|
||||||
|
case early(minutes: Int)
|
||||||
|
case late(minutes: Int)
|
||||||
|
case unknown
|
||||||
|
|
||||||
|
var descrption: String {
|
||||||
|
switch self {
|
||||||
|
case .onTime:
|
||||||
|
return "On Time"
|
||||||
|
case .early(minutes: let minutes):
|
||||||
|
return "\(Self.formatted(minutes)) early"
|
||||||
|
case .late(minutes: let minutes):
|
||||||
|
return "\(Self.formatted(minutes)) late"
|
||||||
|
case .unknown:
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var color: Color {
|
||||||
|
switch self {
|
||||||
|
case .onTime:
|
||||||
|
return .green
|
||||||
|
case .early:
|
||||||
|
return .blue
|
||||||
|
case .late:
|
||||||
|
return .red
|
||||||
|
case .unknown:
|
||||||
|
return .gray
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static func formatted(_ minutes: Int) -> String {
|
||||||
|
let hours = minutes / 60
|
||||||
|
let remainder = minutes % 60
|
||||||
|
guard hours > 0 else {
|
||||||
|
return "\(minutes) mins"
|
||||||
|
}
|
||||||
|
return "\(hours) hr late"
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue