Skip to content

Commit dfac5ac

Browse files
authored
Feature/routing card initial modes (#406)
* Allow passing initial mode selection to TKUIRoutingResultsCard * Also add to `TKUIRoutingResultsViewController`
1 parent e700bf6 commit dfac5ac

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

Sources/TripKitUI/cards/TKUIRoutingResultsCard.swift

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public class TKUIRoutingResultsCard: TKUITableCard {
4747
private let origin: MKAnnotation?
4848
private var request: TripRequest? // Updated for debugging purposes
4949
private let editable: Bool
50+
private let initialModes: Set<String>?
5051

5152
private var viewModel: TKUIRoutingResultsViewModel!
5253
let disposeBag = DisposeBag()
@@ -85,6 +86,7 @@ public class TKUIRoutingResultsCard: TKUITableCard {
8586
self.origin = origin
8687
self.request = nil
8788
self.editable = true
89+
self.initialModes = nil
8890

8991
let mapManager = Self.config.mapManagerFactory(destination, zoomToDestination)
9092

@@ -109,11 +111,18 @@ public class TKUIRoutingResultsCard: TKUITableCard {
109111
didInit()
110112
}
111113

112-
public init(request: TripRequest, editable: Bool = true) {
114+
/// Initializes and returns a newly allocated card showing results for a specific routing request.
115+
///
116+
/// - Parameters:
117+
/// - request: The routing request to display results for.
118+
/// - editable: Whether the user can modify the routing parameters. (Defaults to `true` if not provided.)
119+
/// - modes: Optional set of transport mode identifiers to set as the initial mode selection. If provided and non-empty, these modes will be enabled initially and all others will be disabled. Users can then interact with the mode picker to change the selection. Walking modes (`wa_wal`, `wa_whe`) are handled specially: if no walking modes are specified in this set, their current enabled/disabled state is preserved. If `nil` or empty, the user's current mode preferences are used unchanged.
120+
public init(request: TripRequest, editable: Bool = true, modes: Set<String>? = nil) {
113121
self.destination = nil
114122
self.origin = nil
115123
self.request = request
116124
self.editable = editable
125+
self.initialModes = modes
117126

118127
let mapManager = Self.config.mapManagerFactory(request.toLocation, true)
119128

@@ -206,6 +215,24 @@ public class TKUIRoutingResultsCard: TKUITableCard {
206215
tappedPin: (mapManager as? TKUIRoutingResultsMapManager)?.tappedPin ?? .empty()
207216
)
208217

218+
// Set initial mode selection from URL if provided (must be done before view model creation)
219+
if let initialModes = self.initialModes, !initialModes.isEmpty, let request = self.request {
220+
// Use same logic as mode picker: start, end, and spanning regions
221+
let regions = [request.startRegion, request.endRegion, request.spanningRegion].compactMap { $0 }
222+
let availableModes = Set(TKRegionManager.sortedModes(in: regions).map(\.identifier))
223+
let walkingModesInURL = initialModes.filter(TKTransportMode.modeIdentifierIsWalking)
224+
let modesToHide = availableModes.subtracting(initialModes)
225+
226+
// Update hidden modes to show only the URL-specified modes
227+
for modeIdentifier in availableModes {
228+
// Skip walking modes if none were specified in the URL to preserve current state
229+
if TKTransportMode.modeIdentifierIsWalking(modeIdentifier) && walkingModesInURL.isEmpty {
230+
continue
231+
}
232+
TKSettings.setModeIdentifier(modeIdentifier, toHidden: modesToHide.contains(modeIdentifier))
233+
}
234+
}
235+
209236
let viewModel: TKUIRoutingResultsViewModel
210237
if let destination = self.destination {
211238
viewModel = TKUIRoutingResultsViewModel(destination: destination, origin: origin, limitTo: Self.config.limitToModes, inputs: inputs, mapInput: mapInput)

Sources/TripKitUI/controller/TKUIRoutingResultsViewController.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,14 @@ public class TKUIRoutingResultsViewController: TGCardViewController {
5252
/// other than the user's current location, or when you want to set the
5353
/// departure or arrival time.
5454
///
55-
/// - Parameter request: The trip request object, which should be instatiated
56-
/// using `TripRequest.insert(from:to:for:timeType:info:)`
57-
public init(request: TripRequest) {
55+
/// - Parameters:
56+
/// - request: The trip request object, which should be instatiated
57+
/// using `TripRequest.insert(from:to:for:timeType:info:)`
58+
/// - modes: Optional set of transport mode identifiers to set as the initial mode selection. If provided and non-empty, these modes will be enabled initially and all others will be disabled. Users can then interact with the mode picker to change the selection. Walking modes (`wa_wal`, `wa_whe`) are handled specially: if no walking modes are specified in this set, their current enabled/disabled state is preserved. If `nil` or empty, the user's current mode preferences are used unchanged.
59+
public init(request: TripRequest, modes: Set<String>? = nil) {
5860
super.init(nibName: "TGCardViewController", bundle: TGCardViewController.bundle)
5961

60-
let resultsCard = TKUIRoutingResultsCard(request: request)
62+
let resultsCard = TKUIRoutingResultsCard(request: request, modes: modes)
6163
resultsCard.style = TKUICustomization.shared.cardStyle
6264
rootCard = resultsCard
6365
}

0 commit comments

Comments
 (0)