Skip to content

Commit

Permalink
refactor(app): use departures v2 endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
krystxf committed Nov 15, 2024
1 parent df0c598 commit e711048
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 58 deletions.
91 changes: 35 additions & 56 deletions apps/mobile/metro-now/common/managers/network-manager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,68 +3,36 @@

import Foundation

enum VehicleType: String {
case METRO = "metro"
case ALL = "all"
}

final class NetworkManager {
static let shared = NetworkManager(
)
static let shared = NetworkManager()

private init() {}

func getMetroStops(
func getStops(
metroOnly: Bool,
completed: @escaping (Result<[ApiStop], FetchErrorNew>) -> Void
) {
guard let url = URL(string: "\(ENDPOINT)/v1/stop/all?metroOnly=true") else {
guard let url = URL(string: "\(ENDPOINT)/v1/stop/all?metroOnly=\(metroOnly)") else {
completed(.failure(.invalidUrl))
return
}

let task = URLSession.shared.dataTask(
with: URLRequest(url: url)
) {
data, response, error in

if let _ = error {
completed(.failure(.general))
return
}

guard let response = response as? HTTPURLResponse, response.statusCode == 200 else {
completed(.failure(.invalidResponse))
return
}

guard let data else {
completed(.failure(.invalidData))
return
}

do {
let decoder = JSONDecoder()
let decodedResponse = try decoder.decode([ApiStop].self, from: data)
completed(.success(decodedResponse))
return
} catch {
completed(.failure(.invalidData))
return
}
}

task.resume()
}

func getAllStops(
completed: @escaping (Result<[ApiStop], FetchErrorNew>) -> Void
) {
guard let url = URL(string: "\(ENDPOINT)/v1/stop/all") else {
completed(.failure(.invalidUrl))
return
}
print("GET")
print(url)

let task = URLSession.shared.dataTask(
with: URLRequest(url: url)
) {
data, response, error in
data,
response,
error in

if let _ = error {
if error != nil {
completed(.failure(.general))
return
}
Expand Down Expand Up @@ -94,9 +62,13 @@ final class NetworkManager {
}

func getDepartures(
stopIds: [String], platformIds: [String], completed: @escaping (Result<[ApiDeparture], FetchErrorNew>) -> Void
includeVehicle: VehicleType,
excludeMetro: Bool,
stopIds: [String],
platformIds: [String],
completed: @escaping (Result<[ApiDeparture], FetchErrorNew>) -> Void
) {
guard let baseUrl = URL(string: "\(ENDPOINT)/v1/departure") else {
guard let baseUrl = URL(string: "\(ENDPOINT)/v2/departure") else {
completed(.failure(.invalidUrl))
return
}
Expand All @@ -107,18 +79,24 @@ final class NetworkManager {
let stopsQueryParams: [URLQueryItem] = stopIds.map {
URLQueryItem(name: "stop[]", value: $0)
}
let vehicleQueryParams: [URLQueryItem] =
[URLQueryItem(name: "vehicleType", value: includeVehicle.rawValue)]
+ (excludeMetro ? [URLQueryItem(name: "excludeVehicleType", value: "metro")] : [])

let url = baseUrl.appending(
queryItems: stopsQueryParams + platformsQueryParams + vehicleQueryParams + [
URLQueryItem(name: "limit", value: String(4)),
]
)

let url = baseUrl
.appending(queryItems: stopsQueryParams + platformsQueryParams + [
URLQueryItem(name: "metroOnly", value: "true"),
])
print("GET")
print(url)

let task = URLSession.shared.dataTask(
with: URLRequest(url: url)
) {
data, response, error in
) { data, response, error in

if let _ = error {
if error != nil {
completed(.failure(.general))
return
}
Expand All @@ -143,6 +121,7 @@ final class NetworkManager {
)

completed(.success(decodedResponse))

return
} catch {
completed(.failure(.invalidData))
Expand Down
3 changes: 3 additions & 0 deletions apps/mobile/metro-now/common/types/api-types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ struct ApiPlatform: Codable {
let id: String
let latitude, longitude: Double
let name: String
let code: String?
let isMetro: Bool
let routes: [ApiRoute]
}
Expand All @@ -27,7 +28,9 @@ struct ApiDepartureDate: Codable {
}

struct ApiDeparture: Codable {
let id: String?
let platformId: String
let platformCode: String?
let headsign: String

let departure: ApiDepartureDate
Expand Down
8 changes: 6 additions & 2 deletions apps/mobile/metro-now/metro-now Watch App/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ struct ContentView: View {
}

func getAllMetroStops() {
NetworkManager.shared.getMetroStops { result in
NetworkManager.shared.getStops(metroOnly: true) { result in
DispatchQueue.main.async {
switch result {
case let .success(stops):
Expand All @@ -93,7 +93,11 @@ struct ContentView: View {
}

NetworkManager.shared
.getDepartures(stopIds: [closestStop.id], platformIds: []) { result in
.getDepartures(
includeVehicle: .METRO,
excludeMetro: false,
stopIds: [closestStop.id], platformIds: []
) { result in
DispatchQueue.main.async {
switch result {
case let .success(departures):
Expand Down

0 comments on commit e711048

Please sign in to comment.