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

Preferred lane direction is now a ManeuverDirection #535

Merged
merged 5 commits into from
Apr 14, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 10 additions & 5 deletions Sources/MapboxDirections/Intersection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public struct Intersection {
approachLanes: [LaneIndication]?,
usableApproachLanes: IndexSet?,
preferredApproachLanes: IndexSet?,
usableLaneIndication: LaneIndication?,
usableLaneIndication: ManeuverDirection?,
outletRoadClasses: RoadClasses? = nil,
tollCollection: TollCollection? = nil,
tunnelName: String? = nil,
Expand Down Expand Up @@ -165,7 +165,7 @@ public struct Intersection {

If no lane information is available for the intersection, this property’s value is `nil`
*/
public let usableLaneIndication: LaneIndication?
public let usableLaneIndication: ManeuverDirection?
}

extension Intersection: Codable {
Expand Down Expand Up @@ -267,7 +267,11 @@ extension Intersection: Codable {
lanes = approachLanes.map { Lane(indications: $0) }
for i in usableApproachLanes {
lanes![i].isValid = true
if lanes![i].indications.contains(usableLaneIndication){
// if lanes![i].indications.contains(usableLaneIndication){
// lanes![i].validIndication = usableLaneIndication
// }
// let usable = LaneIndication(descriptions: [usableLaneIndication.rawValue])
1ec5 marked this conversation as resolved.
Show resolved Hide resolved
if lanes![i].indications.contains(LaneIndication(descriptions: [usableLaneIndication.rawValue])!) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is implicitly reinterpreting a maneuver direction description string as a lane indication description string. That is what we want, but we should make this step more explicit. Let’s create a convenience initializer on LaneIndication that takes a ManeuverDirection. It can switch over all the anticipated description strings to create the corresponding LaneIndication, or it can use the same rawValue-based approach here, but at least it would be better documented that way.

Copy link
Contributor Author

@jill-cardamon jill-cardamon Apr 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another way is to compare the string descriptions of the LaneIndication to the ManeuverDirection's raw value:

lanes![i].indications.descriptions.contains(usableLaneIndication.rawValue).

If this comparison is used, is the convenience initializer even necessary?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be preferable to make the conversion explicit in some fashion. Otherwise it would be more difficult to find this raw value–based conversion if the assumption ever breaks in the future.

lanes![i].validIndication = usableLaneIndication
}
}
Expand Down Expand Up @@ -319,8 +323,9 @@ extension Intersection: Codable {
approachLanes = lanes.map { $0.indications }
usableApproachLanes = lanes.indices { $0.isValid }
preferredApproachLanes = lanes.indices { ($0.isActive ?? false) }
let usableIndications = lanes.compactMap { $0.validIndication }
usableLaneIndication = usableIndications.reduce(LaneIndication(rawValue: 0)) { $0.union($1) }
usableLaneIndication = lanes.compactMap { $0.validIndication }[0]
1ec5 marked this conversation as resolved.
Show resolved Hide resolved
// let usableIndications = lanes.compactMap { $0.validIndication }
// usableLaneIndication = usableIndications.reduce(ManeuverDirection(rawValue: "none")) { $0.union($1) }
1ec5 marked this conversation as resolved.
Show resolved Hide resolved
} else {
approachLanes = nil
usableApproachLanes = nil
Expand Down
13 changes: 7 additions & 6 deletions Sources/MapboxDirections/Lane.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ struct Lane: Equatable {
/**
Which of the `indications` is applicable to the current route, when there is more than one
*/
var validIndication: LaneIndication?
var validIndication: ManeuverDirection?

init(indications: LaneIndication, valid: Bool = false, active: Bool? = false, preferred: LaneIndication? = nil) {
init(indications: LaneIndication, valid: Bool = false, active: Bool? = false, preferred: ManeuverDirection? = nil) {
self.indications = indications
self.isValid = valid
self.isActive = active
Expand All @@ -46,16 +46,17 @@ extension Lane: Codable {
try container.encode(indications, forKey: .indications)
try container.encode(isValid, forKey: .valid)
try container.encodeIfPresent(isActive, forKey: .active)
try container.encodeIfPresent(validIndication?.descriptions.first, forKey: .preferred)
try container.encodeIfPresent(validIndication, forKey: .preferred)
}

init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
indications = try container.decode(LaneIndication.self, forKey: .indications)
isValid = try container.decode(Bool.self, forKey: .valid)
isActive = try container.decodeIfPresent(Bool.self, forKey: .active)
if let validIndicationDescription = try container.decodeIfPresent(String.self, forKey: .preferred) {
validIndication = LaneIndication(descriptions: [validIndicationDescription])
}
// if let validIndicationDescription = try container.decodeIfPresent(String.self, forKey: .preferred) {
// validIndication = LaneIndication(descriptions: [validIndicationDescription])
// }
validIndication = try container.decodeIfPresent(ManeuverDirection.self, forKey: .preferred)
}
}
13 changes: 7 additions & 6 deletions Sources/MapboxDirections/VisualInstructionComponent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public extension VisualInstruction {
- parameter isUsable: Whether the user can use this lane to continue along the current route.
- parameter preferredDirection: Which of the `indications` is applicable to the current route when there is more than one
*/
case lane(indications: LaneIndication, isUsable: Bool, preferredDirection: LaneIndication?)
case lane(indications: LaneIndication, isUsable: Bool, preferredDirection: ManeuverDirection?)
}
}

Expand Down Expand Up @@ -217,10 +217,11 @@ extension VisualInstruction.Component: Codable {
if kind == .lane {
let indications = try container.decode(LaneIndication.self, forKey: .directions)
let isUsable = try container.decode(Bool.self, forKey: .isActive)
var preferredDirection: LaneIndication? = nil
if let preferredDirectionDescription = try container.decodeIfPresent(String.self, forKey: .activeDirection) {
preferredDirection = LaneIndication(descriptions: [preferredDirectionDescription])
}
// var preferredDirection: ManeuverDirection? = nil
// if let preferredDirectionDescription = try container.decodeIfPresent(ManeuverDirection.self, forKey: .activeDirection) {
// preferredDirection = ManeuverDirection(rawValue: preferredDirectionDescription)
// }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this commented-out code.

let preferredDirection = try container.decodeIfPresent(ManeuverDirection.self, forKey: .activeDirection)
self = .lane(indications: indications, isUsable: isUsable, preferredDirection: preferredDirection)
return
}
Expand Down Expand Up @@ -284,7 +285,7 @@ extension VisualInstruction.Component: Codable {
textRepresentation = .init(text: "", abbreviation: nil, abbreviationPriority: nil)
try container.encode(indications, forKey: .directions)
try container.encode(isUsable, forKey: .isActive)
try container.encodeIfPresent(preferredDirection?.descriptions.first, forKey: .activeDirection)
try container.encodeIfPresent(preferredDirection, forKey: .activeDirection)
case .guidanceView(let image, let alternativeText):
try container.encode(Kind.guidanceView, forKey: .kind)
textRepresentation = alternativeText
Expand Down
2 changes: 1 addition & 1 deletion Tests/MapboxDirectionsTests/IntersectionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class IntersectionTests: XCTestCase {
approachLanes: [.straightAhead, [.straightAhead, .right]],
usableApproachLanes: IndexSet([0, 1]),
preferredApproachLanes: IndexSet([1]),
usableLaneIndication: [.straightAhead],
usableLaneIndication: .straightAhead,
outletRoadClasses: nil,
tollCollection: nil,
tunnelName: nil,
Expand Down