-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
562 additions
and
249 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
apps/mobile/metro-now/widgets/departures/DeparturesWidget.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// metro-now | ||
// https://github.com/krystxf/metro-now | ||
|
||
import CoreLocation | ||
import SwiftUI | ||
import WidgetKit | ||
|
||
struct DeparturesWidget: Widget { | ||
let kind: String = "Widgets" | ||
|
||
var body: some WidgetConfiguration { | ||
StaticConfiguration(kind: kind, provider: DeparturesWidgetTimelineProvider()) { entry in | ||
if #available(iOS 17.0, *) { | ||
DeparturesWidgetView(entry: entry) | ||
.containerBackground(.fill.tertiary, for: .widget) | ||
} else { | ||
DeparturesWidgetView(entry: entry) | ||
.padding() | ||
.background() | ||
} | ||
} | ||
.configurationDisplayName("Metro Departures") | ||
.description("Show the closest metro stop dynamically.") | ||
.supportedFamilies([.systemLarge]) | ||
} | ||
} | ||
|
||
#Preview("large", as: .systemLarge) { | ||
DeparturesWidget() | ||
} timeline: { | ||
DeparturesWidgetTimelineEntry( | ||
date: .now, | ||
closestStop: "Muzeum", | ||
departures: [ | ||
"Route A to Station 1 at 12:45 PM", | ||
"Route B to Station 2 at 12:50 PM", | ||
], | ||
location: CLLocation(latitude: 50.08, longitude: 14.43) | ||
) | ||
} |
91 changes: 91 additions & 0 deletions
91
apps/mobile/metro-now/widgets/departures/DeparturesWidgetManager.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// metro-now | ||
// https://github.com/krystxf/metro-now | ||
|
||
import Alamofire | ||
import CoreLocation | ||
|
||
class DeparturesWidgetManager: NSObject, ObservableObject, CLLocationManagerDelegate { | ||
private let locationManager = CLLocationManager() | ||
@Published var location: CLLocation? | ||
@Published var metroStops: [ApiStop]? | ||
@Published var closestMetroStop: ApiStop? | ||
@Published var nearestDepartures: [ApiDeparture] = [] | ||
|
||
override init() { | ||
super.init() | ||
locationManager.delegate = self | ||
locationManager.requestWhenInUseAuthorization() | ||
locationManager.startUpdatingLocation() | ||
fetchMetroStops() | ||
} | ||
|
||
func locationManager(_: CLLocationManager, didUpdateLocations locations: [CLLocation]) { | ||
guard let location = locations.last else { return } | ||
self.location = location | ||
updateClosestMetroStop() | ||
fetchDeparturesForClosestStop() | ||
} | ||
|
||
private func fetchMetroStops() { | ||
let request = AF.request( | ||
"\(API_URL)/v1/stop/all", | ||
method: .get, | ||
parameters: ["metroOnly": "true"] | ||
) | ||
|
||
request.validate().responseDecodable(of: [ApiStop].self) { response in | ||
switch response.result { | ||
case let .success(stops): | ||
DispatchQueue.main.async { | ||
self.metroStops = stops | ||
self.updateClosestMetroStop() | ||
} | ||
case let .failure(error): | ||
print("Failed to fetch metro stops: \(error)") | ||
} | ||
} | ||
} | ||
|
||
private func updateClosestMetroStop() { | ||
guard let location, let metroStops else { return } | ||
closestMetroStop = metroStops.min(by: { | ||
let distance1 = location.distance(from: CLLocation(latitude: $0.avgLatitude, longitude: $0.avgLongitude)) | ||
let distance2 = location.distance(from: CLLocation(latitude: $1.avgLatitude, longitude: $1.avgLongitude)) | ||
return distance1 < distance2 | ||
}) | ||
} | ||
|
||
private func fetchDeparturesForClosestStop() { | ||
guard let closestStop = closestMetroStop else { return } | ||
|
||
// Collecting stop and platform ids for the request | ||
let stopIds = closestStop.id | ||
let platformIds = closestStop.platforms.map(\.id) | ||
|
||
// Construct the API request URL with array-like syntax for `stop[]` and `platform[]` | ||
let stopQuery = stopIds.map { "stop[]=\($0)" }.joined(separator: "&") | ||
let platformQuery = platformIds.map { "platform[]=\($0)" }.joined(separator: "&") | ||
|
||
let url = "\(API_URL)/v2/departure?\(stopQuery)&\(platformQuery)&limit=4&minutesBefore=1&minutesAfter=\(3 * 60)" | ||
|
||
// Print the full API request URL for debugging | ||
print("API Request URL for Departures: \(url)") | ||
|
||
// API request to fetch departures | ||
let request = AF.request(url, method: .get) | ||
|
||
let decoder = JSONDecoder() | ||
decoder.dateDecodingStrategy = .iso8601 | ||
|
||
request.validate().responseDecodable(of: [ApiDeparture].self, decoder: decoder) { response in | ||
switch response.result { | ||
case let .success(departures): | ||
DispatchQueue.main.async { | ||
self.nearestDepartures = departures | ||
} | ||
case let .failure(error): | ||
print("Failed to fetch departures: \(error)") | ||
} | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
apps/mobile/metro-now/widgets/departures/DeparturesWidgetTimelineEntry.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// metro-now | ||
// https://github.com/krystxf/metro-now | ||
|
||
import CoreLocation | ||
import WidgetKit | ||
|
||
struct DeparturesWidgetTimelineEntry: TimelineEntry { | ||
let date: Date | ||
let closestStop: String | ||
let departures: [String] | ||
let location: CLLocation? | ||
} |
53 changes: 53 additions & 0 deletions
53
apps/mobile/metro-now/widgets/departures/DeparturesWidgetTimelineProvider.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// metro-now | ||
// https://github.com/krystxf/metro-now | ||
|
||
import WidgetKit | ||
|
||
struct DeparturesWidgetTimelineProvider: TimelineProvider { | ||
private let stopManager = DeparturesWidgetManager() | ||
|
||
func placeholder(in _: Context) -> DeparturesWidgetTimelineEntry { | ||
DeparturesWidgetTimelineEntry(date: Date(), closestStop: "Loading...", departures: [], location: nil) | ||
} | ||
|
||
func getSnapshot(in _: Context, completion: @escaping (DeparturesWidgetTimelineEntry) -> Void) { | ||
let entry = DeparturesWidgetTimelineEntry(date: Date(), closestStop: "Loading...", departures: [], location: nil) | ||
completion(entry) | ||
} | ||
|
||
func getTimeline(in _: Context, completion: @escaping (Timeline<DeparturesWidgetTimelineEntry>) -> Void) { | ||
// Wait for data to be fetched | ||
DispatchQueue.main.asyncAfter(deadline: .now() + 1) { [weak stopManager] in | ||
guard let stopManager, | ||
let closestStop = stopManager.closestMetroStop, | ||
let location = stopManager.location | ||
else { | ||
let entry = DeparturesWidgetTimelineEntry(date: Date(), closestStop: "Unknown", departures: [], location: nil) | ||
let timeline = Timeline(entries: [entry], policy: .atEnd) | ||
completion(timeline) | ||
return | ||
} | ||
|
||
let departures = stopManager.nearestDepartures.map { departure in | ||
"\(departure.route) to \(departure.headsign) at \(formattedDepartureTime(departure.departure.predicted))" | ||
} | ||
|
||
// Create a timeline entry | ||
let entry = DeparturesWidgetTimelineEntry( | ||
date: Date(), | ||
closestStop: closestStop.name, | ||
departures: departures, | ||
location: location | ||
) | ||
|
||
let timeline = Timeline(entries: [entry], policy: .atEnd) | ||
completion(timeline) | ||
} | ||
} | ||
|
||
private func formattedDepartureTime(_ departureDate: Date) -> String { | ||
let formatter = DateFormatter() | ||
formatter.timeStyle = .short | ||
return formatter.string(from: departureDate) | ||
} | ||
} |
Oops, something went wrong.