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

Move options for ornaments into individual structs #318

Merged
merged 9 commits into from
May 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class BasicMapExample: UIViewController, ExampleProtocol {

mapView = MapView(frame: view.bounds)
mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
mapView.ornaments.options.scaleBarVisibility = .visible
mapView.ornaments.options.scaleBar.visibility = .visible

view.addSubview(mapView)
}
Expand Down
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ Mapbox welcomes participation and contributions from everyone.
* Making `bearing` and `pitch` parameters optional
* Adding the `camera(for:camera:rect:)` variant
- `OrnamentOptions` should now be accessed via `MapView.ornaments.options`. `MapConfig.ornaments` has been removed. Updates can be applied directly to `OrnamentsManager.options`. Previously the map's ornament options were updated on `MapConfig.ornaments` with `MapView.update`. ([#310](https://github.com/mapbox/mapbox-maps-ios/pull/310))
- `OrnamentOptions` now uses structs to manage options for individual ornaments. For example, `OrnamentOptions.scaleBarPosition` is now `OrnamentOptions.scaleBar.position`. ([#318](https://github.com/mapbox/mapbox-maps-ios/pull/318))
- The `LogoView` class is now private. ([#310](https://github.com/mapbox/mapbox-maps-ios/pull/310))

### Features ✨ and improvements 🏁

- `OrnamentsManager` is now a public class and can be accessed via the `MapView`'s `ornaments` property.
- `CompassDirectionFormatter` is now public. It provides a string representation of a `CLLocationDirection` and supports the same languages as in pre-v10 versions of the Maps SDK. ([#300](https://github.com/mapbox/mapbox-maps-ios/pull/300))
- `CompassDirectionFormatter` is now public. It provides a string representation of a `CLLocationDirection` and supports the same languages as in pre-v10 versions of the Maps SDK. ([#300](https://github.com/mapbox/mapbox-maps-ios/pull/300))- `OrnamentOptions` should now be accessed via `MapView.ornaments.options`. Updates can be applied directly to the `options` property. Previously the map's ornament options were updated via `MapConfig.ornaments`. ([#310](https://github.com/mapbox/mapbox-maps-ios/pull/310))
- The `LogoView` class is now private. ([#310](https://github.com/mapbox/mapbox-maps-ios/pull/310))

## 10.0.0-beta.18.1 - April 28, 2021

Expand Down
67 changes: 52 additions & 15 deletions Sources/MapboxMaps/Ornaments/OrnamentOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,70 @@ private let defaultOrnamentsMargin = CGPoint(x: 8.0, y: 8.0)
public struct OrnamentOptions: Equatable {

// MARK: - Scale Bar

public var scaleBarPosition: OrnamentPosition = .topLeft
public var scaleBarMargins: CGPoint = defaultOrnamentsMargin
public var scaleBarVisibility: OrnamentVisibility = .adaptive
/// The ornament options for the scale bar.
public var scaleBar = ScaleBarViewOptions()

// MARK: - Compass

public var compassViewPosition: OrnamentPosition = .topRight
public var compassViewMargins: CGPoint = defaultOrnamentsMargin
public var compassVisibility: OrnamentVisibility = .adaptive
/// The ornament options for the compass view.
public var compass = CompassViewOptions()

// MARK: - Logo View

/**
Per our terms of service, a Mapbox map is required to display both
a Mapbox logo as well as an information icon that contains attribution
information. See https://docs.mapbox.com/help/how-mapbox-works/attribution/
for details.
*/

public var _logoViewIsVisible: Bool = true
public var logoViewPosition: OrnamentPosition = .bottomLeft
public var logoViewMargins: CGPoint = defaultOrnamentsMargin
/// The ornament options for the logo view.
public var logo = LogoViewOptions()

// MARK: - Attribution Button
/// The ornament options for the attribution button.
public var attributionButton = AttributionButtonOptions()
}

public protocol BaseOrnamentOptions: Equatable {
var position: OrnamentPosition { get set }
var margins: CGPoint { get set }
}

/// Used to configure position, margin, and visibility for the map's scale bar.
public struct ScaleBarViewOptions: BaseOrnamentOptions {
/// The default value for this property is `.topLeft`.
public var position: OrnamentPosition = .topLeft
/// The default value for this property is `CGPoint(x: 8.0, y: 8.0)`.
public var margins: CGPoint = defaultOrnamentsMargin
/// The default value for this property is `.adaptive`.
public var visibility: OrnamentVisibility = .adaptive
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like visibility is consistent as a property, we should add that as part of the BaseOrnamentOptions protocol

}

/// Used to configure position, margin, and visibility for the map's compass view.
public struct CompassViewOptions: BaseOrnamentOptions {
/// The default value for this property is `.topRight`.
public var position: OrnamentPosition = .topRight
/// The default value for this property is `CGPoint(x: 8.0, y: 8.0)`.
public var margins: CGPoint = defaultOrnamentsMargin
/// The default value for this property is `.adaptive`.
public var visibility: OrnamentVisibility = .adaptive
}

/// Used to configure position, margin, and visibility for the map's attribution button.
public struct AttributionButtonOptions: Equatable {
/// The default value for this property is `.bottomRight`.
public var position: OrnamentPosition = .bottomRight
/// The default value for this property is `CGPoint(x: 8.0, y: 8.0)`.
public var margins: CGPoint = defaultOrnamentsMargin
/// The default value for this property is `true`.
public var _isVisible: Bool = true
Copy link
Contributor

Choose a reason for hiding this comment

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

why don't we use OrnamentVisibility here and make it conform to BaseOrnamentOptions

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think we originally used a BOOL here because these two ornaments wouldn't ever have .adaptive visibility.

Copy link
Contributor

Choose a reason for hiding this comment

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

But we can control that right? Should the user not be allowed to change that configuration?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe we can label that as tailwork, since it may require more discussion.

}

public var _attributionButtonIsVisible: Bool = true
public var attributionButtonPosition: OrnamentPosition = .bottomRight
public var attributionButtonMargins: CGPoint = defaultOrnamentsMargin
/// Used to configure position, margin, and visibility for the map's logo view.
public struct LogoViewOptions: Equatable {
/// The default value for this property is `.bottomLeft`.
public var position: OrnamentPosition = .bottomLeft
/// The default value for this property is `CGPoint(x: 8.0, y: 8.0)`.
public var margins: CGPoint = defaultOrnamentsMargin
/// The default value for this property is `true`.
public var _isVisible: Bool = true
Copy link
Contributor

Choose a reason for hiding this comment

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

Same comment as AttributionButtonOptions

}
26 changes: 17 additions & 9 deletions Sources/MapboxMaps/Ornaments/OrnamentsManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class OrnamentsManager: NSObject {
view.addSubview(scalebarView)

// Compass View
compassView = MapboxCompassOrnamentView(visibility: options.compassVisibility)
compassView = MapboxCompassOrnamentView(visibility: options.compass.visibility)
compassView.translatesAutoresizingMaskIntoConstraints = false
compassView.tapAction = { [weak view] in
view?.compassTapped()
Expand Down Expand Up @@ -80,25 +80,33 @@ public class OrnamentsManager: NSObject {
constraints.removeAll()

// Update the position for the ornaments
let logoViewConstraints = constraints(with: logoView, position: options.logoViewPosition, margins: options.logoViewMargins)
let logoViewConstraints = constraints(with: logoView,
position: options.logo.position,
margins: options.logo.margins)
constraints.append(contentsOf: logoViewConstraints)

let compassViewConstraints = constraints(with: compassView, position: options.compassViewPosition, margins: options.compassViewMargins)
let compassViewConstraints = constraints(with: compassView,
position: options.compass.position,
margins: options.compass.margins)
constraints.append(contentsOf: compassViewConstraints)

let scaleBarViewConstraints = constraints(with: scalebarView, position: options.scaleBarPosition, margins: options.scaleBarMargins)
let scaleBarViewConstraints = constraints(with: scalebarView,
position: options.scaleBar.position,
margins: options.scaleBar.margins)
constraints.append(contentsOf: scaleBarViewConstraints)

let attributionButtonConstraints = constraints(with: attributionButton, position: options.attributionButtonPosition, margins: options.attributionButtonMargins)
let attributionButtonConstraints = constraints(with: attributionButton,
position: options.attributionButton.position,
margins: options.attributionButton.margins)
constraints.append(contentsOf: attributionButtonConstraints)

// Activate new constraints
NSLayoutConstraint.activate(constraints)

logoView.isHidden = !options._logoViewIsVisible
scalebarView.isHidden = options.scaleBarVisibility == .hidden
compassView.isHidden = options.compassVisibility == .hidden
attributionButton.isHidden = !options._attributionButtonIsVisible
logoView.isHidden = !options.logo._isVisible
scalebarView.isHidden = options.scaleBar.visibility == .hidden
compassView.isHidden = options.compass.visibility == .hidden
attributionButton.isHidden = !options.attributionButton._isVisible
}

private func constraints(with view: UIView, position: OrnamentPosition, margins: CGPoint) -> [NSLayoutConstraint] {
Expand Down
8 changes: 4 additions & 4 deletions Tests/MapboxMapsTests/Ornaments/OrnamentManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class OrnamentManagerTests: XCTestCase {

func testInitializer() {
XCTAssertEqual(ornamentSupportableView.subviews.count, 4)
XCTAssertEqual(ornamentsManager.options.attributionButtonMargins, options.attributionButtonMargins)
XCTAssertEqual(ornamentsManager.options.attributionButton.margins, options.attributionButton.margins)

}

Expand All @@ -38,12 +38,12 @@ class OrnamentManagerTests: XCTestCase {
return
}

XCTAssertEqual(options.compassVisibility, .adaptive)
options.compassVisibility = .hidden
XCTAssertEqual(options.compass.visibility, .adaptive)
options.compass.visibility = .hidden

ornamentsManager.options = options

XCTAssertEqual(options.compassVisibility, .hidden)
XCTAssertEqual(options.compass.visibility, .hidden)

let updatedSubviews = ornamentSupportableView.subviews.filter { $0.isKind(of: MapboxCompassOrnamentView.self) }
guard let isUpdatedCompassHidden = updatedSubviews.first?.isHidden else {
Expand Down