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

Add junction views marshaling support. #425

Merged
merged 4 commits into from
Jun 10, 2020
Merged

Conversation

fabian-guerra
Copy link
Contributor

@fabian-guerra fabian-guerra commented Jun 3, 2020

Adds marshaling support for guidance views. This is a port from #405 adds marshaling test and fixes a parsing issue.

@fabian-guerra fabian-guerra self-assigned this Jun 3, 2020
@fabian-guerra fabian-guerra force-pushed the fabian-junction-views branch from 07c6a54 to 9125c6c Compare June 5, 2020 22:18
@fabian-guerra fabian-guerra added the improvement Improvement for an existing feature. label Jun 5, 2020
@fabian-guerra fabian-guerra requested a review from 1ec5 June 5, 2020 22:21
@fabian-guerra fabian-guerra marked this pull request as ready for review June 5, 2020 22:21
@fabian-guerra fabian-guerra changed the title Fabian junction views Add junction views marshaling support. Jun 5, 2020
@fabian-guerra fabian-guerra requested a review from avi-c June 8, 2020 16:14
Copy link
Contributor

@1ec5 1ec5 left a comment

Choose a reason for hiding this comment

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

Apart from the feedback below, also add @avi-c as a coauthor for the commit, since it’s heavily based on the earlier commits in #405.

@@ -34,6 +34,8 @@ public extension VisualInstruction {
*/
case image(image: ImageRepresentation, alternativeText: TextRepresentation)

case guidanceView(image: GuidanceViewImageRepresentation, alternativeText: TextRepresentation)
Copy link
Contributor

Choose a reason for hiding this comment

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

Add documentation about this case; otherwise it won’t show up in documentation.

@@ -149,13 +151,29 @@ public extension VisualInstruction.Component {
}
}

public struct GuidanceViewImageRepresentation: Equatable {
Copy link
Contributor

Choose a reason for hiding this comment

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

ImageRepresentation isn’t meant to be limited to the image case; it just means that it contains the details for rendering an image. So does this struct, but the difference is in how the URL is interpreted. I wonder if we should try to use ImageRepresentation for both cases but genericize it to make sense in both. If we must have a separate struct, we should probably rename the two structs to better reflect the distinction. Perhaps one would be ScalableImageRepresentation or something to that effect. Needs more thought.

/ref #405 (comment)

@1ec5 1ec5 added this to the v1.0.0 milestone Jun 9, 2020
@fabian-guerra fabian-guerra force-pushed the fabian-junction-views branch from 9125c6c to 5215e84 Compare June 9, 2020 15:55
@mapbox mapbox deleted a comment from 1ec5 Jun 9, 2020
@mapbox mapbox deleted a comment from 1ec5 Jun 9, 2020
Copy link
Contributor

@1ec5 1ec5 left a comment

Choose a reason for hiding this comment

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

In addition to the feedback below, please describe the changes in CHANGELOG.md, for example:

  • Added the VisualInstructionBanner.quaternaryInstruction property and VisualInstruction.Component.guidanceView(image:alternativeText:) enumeration case to represent a detailed image of an upcoming junction. (#425)
  • Renamed the VisualInstruction.Component.ImageRepresentation(imageBaseURL:) initializer to VisualInstruction.Component.ImageRepresentation(imageBaseURL:availableScales:availableFormats:) and added the VisualInstruction.Component.ImageRepresentation.availableScales and VisualInstruction.Component.ImageRepresentation.availableFormats properties. (#425)

@@ -35,7 +35,12 @@ public extension VisualInstruction {
case image(image: ImageRepresentation, alternativeText: TextRepresentation)

/**
The compoment contains the localized word for “Exit”.
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for fixing this typo. 😄

/// - imageBaseURL: The image's base URL.
/// - availableScales: The image’s available scale factors. If this argument is unspecified, the current screen’s native scale factor is used.
/// - availableFormats: The available file formats of the image.
public init(imageBaseURL: URL?, availableScales: Set<CGFloat>? = nil, availableFormats: Set<Format>) {
Copy link
Contributor

Choose a reason for hiding this comment

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

availableScales doesn’t have to have a default value, since it’s easy enough to pass in nil in the couple places that would need to have it be nil. (It would be necessary to define a default value if we were concerned about backwards-incompatibility, but in that case, availableFormats would also have a default value.)

@@ -195,14 +235,22 @@ extension VisualInstruction.Component: Codable {
if let imageBaseURLString = try container.decodeIfPresent(String.self, forKey: .imageBaseURL) {
imageBaseURL = URL(string: imageBaseURLString)
}
let imageRepresentation = ImageRepresentation(imageBaseURL: imageBaseURL)
let imageRepresentation = ImageRepresentation(imageBaseURL: imageBaseURL, availableFormats: [.png, .svg])
Copy link
Contributor

Choose a reason for hiding this comment

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

This representation is available at 1×, 2×, and 3× scales.

@@ -124,11 +159,14 @@ public extension VisualInstruction.Component {
- returns: A remote URL to the image.
*/
public func imageURL(scale: CGFloat? = nil, format: Format = .png) -> URL? {
Copy link
Contributor

Choose a reason for hiding this comment

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

Update the scale documentation to replace:

Only the values 1, 2, and 3 are currently supported.

with:

Only the values in the availableScales property are currently supported.

guard let imageBaseURL = imageBaseURL,
self.availableScales.contains(imageScale),
self.availableFormats.contains(format),
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: it’s OK to omit self. here, but not a big deal.

Comment on lines 135 to 136
let defaultScales: Set<CGFloat> = [ImageRepresentation.currentScale]
self.availableScales = availableScales ?? defaultScales
Copy link
Contributor

Choose a reason for hiding this comment

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

Think of availableScales as the set of scales that can be explicitly part of the URL, rather than the scale that the image happens to be in. So the default here should be an empty set.

/// Initializes an image representation bearing the image at the given URL.
/// - Parameters:
/// - imageURL: The image URL.
/// - availableScales: The image’s available scale factors. If this argument is unspecified, the current screen’s native scale factor is used.
Copy link
Contributor

Choose a reason for hiding this comment

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

If this argument is unspecified, the current screen’s native scale factor is used.

This is true of imageURL(scale:format:), but I think it would be better for a nil availableScales to mean “don’t append a scale to the URL”.

var imageURLComponents = URLComponents(url: imageBaseURL, resolvingAgainstBaseURL: false) else {
return nil
}
imageURLComponents.path += "@\(Int(scale ?? ImageRepresentation.currentScale))x.\(format)"
imageURLComponents.path += "@\(Int(imageScale))x.\(format)"
Copy link
Contributor

Choose a reason for hiding this comment

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

Does the Guidance Views API put @1x etc. and a file extension in the URL? If not, let’s use an empty availableScales or availableFormats set to detect that these things should not be added to the path.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It doesn't its a direct URL that's the reason that I consider if you initialized an image with an imageURL you can use the property instead

Copy link
Contributor

@1ec5 1ec5 Jun 9, 2020

Choose a reason for hiding this comment

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

This line is unconditionally adding e.g. @1x.png to the URL. We should either avoid adding it to the URL or remove it from the URL before initializing the component. Let’s add an assertion to VisualInstructionsTests to verify that the caller of this method will get the same URL that the Directions API returned for a guidance view image.

Comment on lines 148 to 152
/// The image's available scales.
public let availableScales: Set<CGFloat>

/// The image's available formats.
public let availableFormats: Set<Format>
Copy link
Contributor

@1ec5 1ec5 Jun 9, 2020

Choose a reason for hiding this comment

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

(Note to self: These properties aren’t being encoded, decoded, and compared explicitly because they’re derived from the component type, which is already being encoded, decoded, and compared.)

@1ec5
Copy link
Contributor

1ec5 commented Jun 9, 2020

https://app.circleci.com/pipelines/github/mapbox/mapbox-directions-swift/585/workflows/b57babdc-132a-4c5e-ac62-69117412d8b9/jobs/1725/steps

Test Case '-[MapboxDirectionsTests.VisualInstructionComponentTests testImageComponent]' started.

/Users/distiller/project/Tests/MapboxDirectionsTests/VisualInstructionComponentTests.swift:39: error: -[MapboxDirectionsTests.VisualInstructionComponentTests testImageComponent] : XCTAssertEqual failed: ("nil") is not equal to ("Optional("https://s3.amazonaws.com/mapbox/shields/v3/us-42@1x.svg")")

Test Case '-[MapboxDirectionsTests.VisualInstructionComponentTests testImageComponent]' failed (0.008 seconds).

@fabian-guerra fabian-guerra force-pushed the fabian-junction-views branch from bb3cd33 to d2d66de Compare June 10, 2020 18:26
Copy link
Contributor

@1ec5 1ec5 left a comment

Choose a reason for hiding this comment

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

Just a documentation comment to add, but otherwise good to go. Thanks!

@@ -149,13 +154,29 @@ public extension VisualInstruction.Component {
}
}

public struct GuidanceViewImageRepresentation: Equatable {
Copy link
Contributor

Choose a reason for hiding this comment

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

GuidanceViewImageRepresentation still needs to be documented. Otherwise it won’t show up in the docset.

@fabian-guerra fabian-guerra merged commit c228ecf into master Jun 10, 2020
@fabian-guerra fabian-guerra deleted the fabian-junction-views branch June 10, 2020 22:23
@1ec5 1ec5 modified the milestones: v1.0.0, v0.32.0 Jun 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
improvement Improvement for an existing feature.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants