Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/luaswatch ios #31

Open
wants to merge 16 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .github/workflows/ios.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@ jobs:
name: Build and Test LuasKit scheme using watchOS simulator
runs-on: macos-14
env:
scheme: 'LuasKit'
scheme: 'LuasAPI'
project: 'LuasWatch.xcodeproj'
platform: 'watchOS Simulator'
device: 'Apple Watch SE (40mm) (2nd generation)'

steps:
- name: Checkout
uses: actions/checkout@v3
- name: Setup Swift
uses: SwiftyLab/setup-swift@latest

- name: Build
run: |
# xcrun xctrace returns via stderr, not the expected stdout (see https://developer.apple.com/forums/thread/663959)
Expand Down
44 changes: 22 additions & 22 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ GEM
artifactory (3.0.17)
atomos (0.1.3)
aws-eventstream (1.3.0)
aws-partitions (1.1003.0)
aws-sdk-core (3.212.0)
aws-partitions (1.1044.0)
aws-sdk-core (3.217.1)
aws-eventstream (~> 1, >= 1.3.0)
aws-partitions (~> 1, >= 1.992.0)
aws-sigv4 (~> 1.9)
jmespath (~> 1, >= 1.6.1)
aws-sdk-kms (1.95.0)
aws-sdk-core (~> 3, >= 3.210.0)
aws-sdk-kms (1.97.0)
aws-sdk-core (~> 3, >= 3.216.0)
aws-sigv4 (~> 1.5)
aws-sdk-s3 (1.170.0)
aws-sdk-core (~> 3, >= 3.210.0)
aws-sdk-s3 (1.179.0)
aws-sdk-core (~> 3, >= 3.216.0)
aws-sdk-kms (~> 1)
aws-sigv4 (~> 1.5)
aws-sigv4 (1.10.1)
aws-sigv4 (1.11.0)
aws-eventstream (~> 1, >= 1.0.2)
babosa (1.0.4)
base64 (0.2.0)
Expand All @@ -33,7 +33,7 @@ GEM
commander (4.6.0)
highline (~> 2.0.0)
declarative (0.0.20)
digest-crc (0.6.5)
digest-crc (0.7.0)
rake (>= 12.0.0, < 14.0.0)
domain_name (0.6.20240107)
dotenv (2.8.1)
Expand All @@ -58,17 +58,17 @@ GEM
faraday-em_synchrony (1.0.0)
faraday-excon (1.1.0)
faraday-httpclient (1.0.1)
faraday-multipart (1.0.4)
multipart-post (~> 2)
faraday-multipart (1.1.0)
multipart-post (~> 2.0)
faraday-net_http (1.0.2)
faraday-net_http_persistent (1.2.0)
faraday-patron (1.0.0)
faraday-rack (1.0.0)
faraday-retry (1.0.3)
faraday_middleware (1.2.1)
faraday (~> 1.0)
fastimage (2.3.1)
fastlane (2.225.0)
fastimage (2.4.0)
fastlane (2.226.0)
CFPropertyList (>= 2.3, < 4.0.0)
addressable (>= 2.8, < 3.0.0)
artifactory (~> 3.0)
Expand Down Expand Up @@ -108,7 +108,7 @@ GEM
tty-spinner (>= 0.8.0, < 1.0.0)
word_wrap (~> 1.0.0)
xcodeproj (>= 1.13.0, < 2.0.0)
xcpretty (~> 0.3.0)
xcpretty (~> 0.4.0)
xcpretty-travis-formatter (>= 0.0.3, < 2.0.0)
fastlane-sirp (1.0.0)
sysrandom (~> 1.0)
Expand Down Expand Up @@ -150,12 +150,12 @@ GEM
os (>= 0.9, < 2.0)
signet (>= 0.16, < 2.a)
highline (2.0.3)
http-cookie (1.0.7)
http-cookie (1.0.8)
domain_name (~> 0.5)
httpclient (2.8.3)
jmespath (1.6.2)
json (2.8.1)
jwt (2.9.3)
json (2.9.1)
jwt (2.10.1)
base64
mini_magick (4.13.2)
mini_mime (1.1.5)
Expand All @@ -166,18 +166,18 @@ GEM
nkf (0.2.0)
optparse (0.6.0)
os (1.1.4)
plist (3.7.1)
plist (3.7.2)
public_suffix (6.0.1)
rake (13.2.1)
representable (3.2.0)
declarative (< 0.1.0)
trailblazer-option (>= 0.1.1, < 0.2.0)
uber (< 0.2.0)
retriable (3.1.2)
rexml (3.3.9)
rouge (2.0.7)
rexml (3.4.0)
rouge (3.28.0)
ruby2_keywords (0.0.5)
rubyzip (2.3.2)
rubyzip (2.4.1)
security (0.1.5)
signet (0.19.0)
addressable (~> 2.8)
Expand Down Expand Up @@ -209,8 +209,8 @@ GEM
colored2 (~> 3.1)
nanaimo (~> 0.4.0)
rexml (>= 3.3.6, < 4.0)
xcpretty (0.3.0)
rouge (~> 2.0.7)
xcpretty (0.4.0)
rouge (~> 3.28.0)
xcpretty-travis-formatter (1.0.1)
xcpretty (~> 0.2, >= 0.0.7)

Expand Down
9 changes: 7 additions & 2 deletions LuasAPI/Sources/LuasAPI/APIError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
// Created by Roland Gropmair on 26/12/2024.
//

public enum APIError: Error {
case noTrains(String)
public enum APIError: Error, Equatable {

/// returns a string if there was a `message` field in the API response XML
case noTrainsButMessageFromAPI(String)

case noTrains

case invalidXML(String)
}
25 changes: 24 additions & 1 deletion LuasAPI/Sources/LuasAPI/LuasAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,37 @@ public struct LuasAPI {
)
}

public func getTrains(stationShortCode: String) async throws -> Data {
internal func getTrains(stationShortCode: String) async throws -> Data {

let request = buildRequest(stationShortCode: stationShortCode)

let (data, _) = try await session.data(for: request)

return data
}

public func dueTimes(for trainStation: TrainStation) async throws -> TrainsByDirection {

let data = try await getTrains(stationShortCode: trainStation.shortCode)

let trainsByDirection = try APIParser.parse(xml: data, for: trainStation)

if trainsByDirection.inbound.isEmpty && trainsByDirection.outbound.isEmpty {
/// success - but no trains?!?

if let messageFromAPI = trainsByDirection.message,
messageFromAPI.count > 0 {
/// if we get `message` from response XML, we return that as an error
throw APIError.noTrainsButMessageFromAPI(messageFromAPI)

} else {
throw APIError.noTrains
}

} else {
return trainsByDirection
}
}
}

public protocol URLSessionLoading {
Expand Down
27 changes: 25 additions & 2 deletions LuasAPI/Sources/LuasAPI/Models/TrainStation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public struct TrainStation: CustomStringConvertible, Hashable, Identifiable, Sen
route: Route,
name: String,
location: CLLocation,
stationType: TrainStation.StationType = .twoway
stationType: StationType = .twoway
) {
self.stationIdShort = stationIdShort
self.shortCode = shortCode
Expand All @@ -30,11 +30,22 @@ public struct TrainStation: CustomStringConvertible, Hashable, Identifiable, Sen
self.stationType = stationType
}

// need custom implementation because location does not compare lat/long when checking for equatable
static public func == (lhs: TrainStation, rhs: TrainStation) -> Bool {
lhs.stationIdShort == rhs.stationIdShort &&
lhs.shortCode == rhs.shortCode &&
lhs.route == rhs.route &&
lhs.name == rhs.name &&
lhs.location.coordinate.latitude == rhs.location.coordinate.latitude &&
lhs.location.coordinate.longitude == rhs.location.coordinate.longitude &&
lhs.stationType == rhs.stationType
}

public var id: String {
stationIdShort
}

public enum StationType: String, Sendable {
public enum StationType: String, Sendable, Equatable {
case twoway, oneway, terminal
}

Expand Down Expand Up @@ -68,4 +79,16 @@ public struct TrainStation: CustomStringConvertible, Hashable, Identifiable, Sen

return formatter.string(from: distance)
}

public static var unknown: TrainStation {
TrainStation(
stationIdShort: "unknown",
shortCode: "unknown",
route: .green,
name: "Unknown",
location: CLLocation(
latitude: CLLocationDegrees(53.3163934083453),
longitude: CLLocationDegrees(-6.25344151996991))
)
}
}
70 changes: 32 additions & 38 deletions LuasAPI/Sources/LuasAPI/Models/TrainStations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public struct TrainStations: Sendable {

// MARK: - Properties

public let stations: [TrainStation]
public let allStations: [TrainStation]

// MARK: - Initializers

Expand All @@ -31,11 +31,11 @@ public struct TrainStations: Sendable {
fatalError("could not parse JSON file")
}

self.stations = Self.trainStations(from: stationsArray)
self.allStations = Self.trainStations(from: stationsArray)
}

internal init(stations: [TrainStation]) {
self.stations = stations
self.allStations = stations
}

// MARK: - Private Methods
Expand Down Expand Up @@ -80,26 +80,47 @@ public struct TrainStations: Sendable {
// MARK: - Helpers

public var redLineStations: [TrainStation] {
stations
allStations
.filter { $0.route == .red }
}

public var greenLineStations: [TrainStation] {
stations
allStations
.filter { $0.route == .green }
}

public func closestStation(from location: CLLocation) -> TrainStation? {
closestStation(from: location, stations: stations)
allStations.closestStation(from: location)
}

public func closestStation(
from location: CLLocation,
stations: [TrainStation]
) -> TrainStation? {
public func closestStation(from location: CLLocation, route: Route) -> TrainStation? {
switch route {
case .red:
return redLineStations.closestStation(from: location)
case .green:
return greenLineStations.closestStation(from: location)
}
}

public func closestStationsSorted(from location: CLLocation) -> [TrainStation] {
allStations.sorted { (station1, station2) -> Bool in
station1.location.distance(from: location) < station2.location.distance(from: location)
}
}

public func station(shortCode: String) -> TrainStation? {
allStations
.filter { $0.shortCode == shortCode }
.first
}
}

private extension Array where Element == TrainStation {

func closestStation(from location: CLLocation) -> TrainStation? {
var closestStationSoFar: TrainStation?

stations.forEach { (station) in
self.forEach { (station) in
// don't consider stations if they're too far away, currently 20km
if station.location.distance(from: location) > 20000 {
return
Expand All @@ -116,31 +137,4 @@ public struct TrainStations: Sendable {

return closestStationSoFar
}

public func closestStation(from location: CLLocation, route: Route) -> TrainStation? {
switch route {
case .red:
return closestStation(from: location, stations: redLineStations)
case .green:
return closestStation(from: location, stations: greenLineStations)
}
}

public func station(shortCode: String) -> TrainStation? {
stations
.filter { $0.shortCode == shortCode }
.first
}

public static var unknown: TrainStation {
TrainStation(
stationIdShort: "unknown",
shortCode: "unknown",
route: .green,
name: "Unknown",
location: CLLocation(
latitude: CLLocationDegrees(53.3163934083453),
longitude: CLLocationDegrees(-6.25344151996991))
)
}
}
Loading