Skip to content

Commit

Permalink
Tweaks & Fixes (#224)
Browse files Browse the repository at this point in the history
* Add a parser test for info/routes JSON output

* Allow creating a `TKDeparturesProvider.Filter`, too

* Make a few more alert properties public

* Update example to include how to use `TKUIHomeCard.config.selectionMode`

* Allow setting a default location on `TKUINearbyMapManager`

Closes #18568

* Fix some existing views not getting re-selected

Closes #18570
  • Loading branch information
nighthawk authored Dec 12, 2022
1 parent e365471 commit 078b011
Show file tree
Hide file tree
Showing 8 changed files with 11,106 additions and 13 deletions.
39 changes: 38 additions & 1 deletion Examples/TripKitUIExample/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -270,24 +270,61 @@ extension MainViewController: TKUIRoutingResultsViewControllerDelegate {
extension MainViewController {

func showHome(nearby: Bool) {

let mapManager: TKUICompatibleHomeMapManager?
if nearby {
TKUIHomeCard.config.componentViewModelClasses = [
TKUINearbyViewModel.self,
InMemoryHistoryManager.self,
]

mapManager = TKUINearbyMapManager(defaultMapRect:
.forCoordinateRegion(
.init(center: .init(latitude: -33.8, longitude: 151.1),
span: .init(latitudeDelta: 1, longitudeDelta: 1))
))
} else {
TKUIHomeCard.config.componentViewModelClasses = [
InMemoryHistoryManager.self,
]

mapManager = nil
}

let homeController = TKUIHomeViewController(mapManager: nearby ? TKUINearbyMapManager() : nil)
let homeController = TKUIHomeViewController(mapManager: mapManager)

homeController.autocompletionDataProviders = [
TKAppleGeocoder(),
TKTripGoGeocoder(),
TKRouteAutocompleter(),
InMemoryFavoriteManager.shared,
InMemoryHistoryManager.shared,
]

TKUIHomeCard.config.selectionMode = .callback({ selection, _ in
switch selection {
case .result(let result):
if #available(iOS 15.0, *), let route = result.object as? TKAPI.Route {
// do something with the route
let controller = UIHostingController(rootView: RouteView(route: route))
homeController.present(controller, animated: false)

} else {
// handle other objects; shouldn't get there unless you implement your own auto-completer that passes `nil` from `annotation(for:completion:)`
assertionFailure()
}
return false

case let .annotation(stop as TKUIStopAnnotation):
homeController.push(TKUITimetableCard(stops: [stop]))
return false
case let .annotation(annotation):
homeController.push(TKUIRoutingResultsCard(destination: annotation))
return false
}
})


homeController.searchResultsDelegate = self
navigationController?.setNavigationBarHidden(false, animated: true)
navigationController?.pushViewController(homeController, animated: true)
Expand Down
12 changes: 6 additions & 6 deletions Sources/TripKit/model/API/AlertAPIModels.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ extension TKAPI {
}

public struct Action: Hashable {
enum ActionType: Hashable {
public enum ActionType: Hashable {
case reroute([String])
}

let text: String
let type: ActionType
public let text: String
public let type: ActionType
}

public let title: String
Expand All @@ -36,9 +36,9 @@ extension TKAPI {
public let severity: Severity
public let hashCode: Int

let action: Action?
let location: TKAPI.Location?
let serviceTripID: String?
public let action: Action?
public let location: TKAPI.Location?
public let serviceTripID: String?

// MARK: - Codable
public init(from decoder: Decoder) throws {
Expand Down
6 changes: 6 additions & 0 deletions Sources/TripKit/server/TKDeparturesProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ public class TKDeparturesProvider: NSObject {

/// Filter to apply to the results, treated as an "AND" condition.
public struct Filter: Codable {
public init(operatorID: String, routeID: String? = nil, directionID: String? = nil) {
self.operatorID = operatorID
self.routeID = routeID
self.directionID = directionID
}

/// Operator identifier
public let operatorID: String
/// Route identifier for the provided operator
Expand Down
5 changes: 2 additions & 3 deletions Sources/TripKitUI/cards/TKUIMapManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -401,9 +401,8 @@ extension TKUIMapManager {

switch updateMode {
case .updateSelection:
// updates visible views; new views updated from `mapView(_:didAdd:)`
let views = mapView.annotations(in: mapView.visibleMapRect)
.compactMap { $0 as? MKAnnotation }
// updates existing views; new views updated from `mapView(_:didAdd:)`
let views = mapView.annotations
.compactMap { mapView.view(for: $0) }
updateAnnotationsViewsForSelection(views)

Expand Down
14 changes: 11 additions & 3 deletions Sources/TripKitUI/cards/TKUINearbyMapManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,18 @@ public class TKUINearbyMapManager: TKUIMapManager {

public weak var viewModel: TKUINearbyViewModel?

public override init() {
/// - Parameter defaultMapRect: The map rect to show when the user hasn't granted access
/// to their current location.
public init(defaultMapRect: MKMapRect = .null) {
self.defaultMapRect = defaultMapRect

super.init()

self.preferredZoomLevel = .road
self.showOverlayPolygon = true
}

let defaultMapRect: MKMapRect
private var mapTrackingPublisher = PublishSubject<MKUserTrackingMode>()
private var mapRectPublisher = PublishSubject<MKMapRect>()

Expand Down Expand Up @@ -55,7 +60,7 @@ public class TKUINearbyMapManager: TKUIMapManager {
if viewModel == nil {
viewModel = TKUINearbyViewModel.homeInstance
}
guard let viewModel = viewModel else { assertionFailure(); return }
guard let viewModel else { assertionFailure(); return }

// Default content on taking charge

Expand All @@ -64,7 +69,7 @@ public class TKUINearbyMapManager: TKUIMapManager {
let showCurrentLocation = TKLocationManager.shared.authorizationStatus() == .authorized
mapView.showsUserLocation = showCurrentLocation

if let searchResult = self.searchResult {
if let searchResult {
mapView.setUserTrackingMode(.none, animated: animated)
mapView.addAnnotation(searchResult)
zoom(to: [searchResult], animated: animated)
Expand All @@ -75,6 +80,9 @@ public class TKUINearbyMapManager: TKUIMapManager {

} else if showCurrentLocation {
mapView.setUserTrackingMode(.follow, animated: animated)

} else if !MKMapRectEqualToRect(defaultMapRect, .null) {
mapView.setVisibleMapRect(defaultMapRect, animated: animated)
}


Expand Down
Loading

0 comments on commit 078b011

Please sign in to comment.