Skip to content

Commit

Permalink
Merge pull request #1306 from mapbox/zjm-annotation-drag-not-working-…
Browse files Browse the repository at this point in the history
…out-of-the-box2

Fix AnnotationManager isDraggable behavior
  • Loading branch information
ZiZasaurus authored Jan 4, 2023
2 parents 83ab0c8 + 76f5c80 commit a4528f8
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 38 deletions.
28 changes: 18 additions & 10 deletions codegen/annotation-generator/templates/Annotation.swift.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -69,54 +69,62 @@ public struct <%- camelize(type) %>Annotation: Annotation {
<%_ if (type === symbol) { _%>
/// Create a point annotation with a `Point` and an optional identifier.
public init(id: String = UUID().uuidString, point: Point) {
public init(id: String = UUID().uuidString, point: Point, isSelected: Bool = false, isDraggable: Bool = false) {
self.id = id
self.point = point
self.isSelected = isSelected
self.isDraggable = isDraggable
}
/// Create a point annotation with a coordinate and an optional identifier
/// - Parameters:
/// - id: Optional identifier for this annotation
/// - coordinate: Coordinate where this annotation should be rendered
public init(id: String = UUID().uuidString, coordinate: CLLocationCoordinate2D) {
public init(id: String = UUID().uuidString, coordinate: CLLocationCoordinate2D, isSelected: Bool = false, isDraggable: Bool = false) {
let point = Point(coordinate)
self.init(id: id, point: point)
self.init(id: id, point: point, isSelected: isSelected, isDraggable: isDraggable)
}
<%_ } else if(type === circle) { _%>
/// Create a circle annotation with a `Point` and an optional identifier.
public init(id: String = UUID().uuidString, point: Point) {
public init(id: String = UUID().uuidString, point: Point, isSelected: Bool = false, isDraggable: Bool = false) {
self.id = id
self.point = point
self.isSelected = isSelected
self.isDraggable = isDraggable
}
/// Create a circle annotation with a center coordinate and an optional identifier
/// - Parameters:
/// - id: Optional identifier for this annotation
/// - coordinate: Coordinate where this circle annotation should be centered
public init(id: String = UUID().uuidString, centerCoordinate: CLLocationCoordinate2D) {
public init(id: String = UUID().uuidString, centerCoordinate: CLLocationCoordinate2D, isSelected: Bool = false, isDraggable: Bool = false) {
let point = Point(centerCoordinate)
self.init(id: id, point: point)
self.init(id: id, point: point, isSelected: isSelected, isDraggable: isDraggable)
}
<%_ } else if(type === fill) { _%>
/// Create a polygon annotation with a `Polygon` and an optional identifier.
public init(id: String = UUID().uuidString, polygon: Polygon) {
public init(id: String = UUID().uuidString, polygon: Polygon, isSelected: Bool = false, isDraggable: Bool = false) {
self.id = id
self.polygon = polygon
self.isSelected = isSelected
self.isDraggable = isDraggable
}
<%_ } else if(type === line) { _%>
/// Create a polyline annotation with a `LineString` and an optional identifier.
public init(id: String = UUID().uuidString, lineString: LineString) {
public init(id: String = UUID().uuidString, lineString: LineString, isSelected: Bool = false, isDraggable: Bool = false) {
self.id = id
self.lineString = lineString
self.isSelected = isSelected
self.isDraggable = isDraggable
}
/// Create a polyline annotation with an array of coordinates and an optional identifier.
public init(id: String = UUID().uuidString, lineCoordinates: [CLLocationCoordinate2D]) {
public init(id: String = UUID().uuidString, lineCoordinates: [CLLocationCoordinate2D], isSelected: Bool = false, isDraggable: Bool = false) {
let lineString = LineString(lineCoordinates)
self.init(id: id, lineString: lineString)
self.init(id: id, lineString: lineString, isSelected: isSelected, isDraggable: isDraggable)
}
<%_ } _%>

Expand Down
10 changes: 5 additions & 5 deletions codegen/annotation-generator/templates/AnnotationLocal.swift.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
switch (locals.annotationType) {
case 'point':
_%>
<%- locals.declarationKeyword %> annotation = PointAnnotation(point: .init(.init(latitude: 0, longitude: 0)))
<%- locals.declarationKeyword %> annotation = PointAnnotation(point: .init(.init(latitude: 0, longitude: 0)), isSelected: false, isDraggable: false)
<%_
break;
case 'circle':
_%>
<%- locals.declarationKeyword %> annotation = CircleAnnotation(point: .init(.init(latitude: 0, longitude: 0)))
<%- locals.declarationKeyword %> annotation = CircleAnnotation(point: .init(.init(latitude: 0, longitude: 0)), isSelected: false, isDraggable: false)
<%_
break;
case 'polygon':
Expand All @@ -19,13 +19,13 @@ let polygonCoords = [
CLLocationCoordinate2DMake(26.244156283890756, -89.857177734375),
CLLocationCoordinate2DMake(24.51713945052515, -89.857177734375)
]
<%- locals.declarationKeyword %> annotation = PolygonAnnotation(polygon: .init(outerRing: .init(coordinates: polygonCoords)))
<%- locals.declarationKeyword %> annotation = PolygonAnnotation(polygon: .init(outerRing: .init(coordinates: polygonCoords)), isSelected: false, isDraggable: false)
<%_
break;
case 'polyline':
_%>
let lineCoordinates = [ CLLocationCoordinate2DMake(0, 0), CLLocationCoordinate2DMake(10, 10) ]
<%- locals.declarationKeyword %> annotation = PolylineAnnotation(lineString: .init(lineCoordinates))
<%- locals.declarationKeyword %> annotation = PolylineAnnotation(lineString: .init(lineCoordinates), isSelected: false, isDraggable: false)
<%_
}
_%>
_%>
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ public class <%- camelize(type) %>AnnotationManager: AnnotationManagerInternal {
}
internal func handleDragBegin(with featureIdentifiers: [String]) {
guard let annotation = annotations.first(where: { featureIdentifiers.contains($0.id) }) else { return }
guard let annotation = annotations.first(where: { featureIdentifiers.contains($0.id) && $0.isDraggable }) else { return }
createDragSourceAndLayer()
annotationBeingDragged = annotation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,6 @@ final class <%- camelize(type) %>AnnotationManagerTests: XCTestCase, AnnotationI
XCTAssertTrue(annotations.compactMap(\.image?.name).allSatisfy(manager.isUsingStyleImage(_:)))
}
func testUnusedImagesRemovedFromStyle() {
// given
let allAnnotations = Array.random(withLength: 10) {
Expand Down Expand Up @@ -503,7 +502,7 @@ final class <%- camelize(type) %>AnnotationManagerTests: XCTestCase, AnnotationI
let clusterOptions = ClusterOptions()
var annotations = [PointAnnotation]()
for _ in 0...500 {
let annotation = PointAnnotation(coordinate: .random())
let annotation = PointAnnotation(coordinate: .random(), isSelected: false, isDraggable: false)
annotations.append(annotation)
}
Expand Down Expand Up @@ -553,7 +552,7 @@ final class <%- camelize(type) %>AnnotationManagerTests: XCTestCase, AnnotationI
clusterProperties: testClusterProperties)
var annotations = [PointAnnotation]()
for _ in 0...500 {
let annotation = PointAnnotation(coordinate: .random())
let annotation = PointAnnotation(coordinate: .random(), isSelected: false, isDraggable: false)
annotations.append(annotation)
}
Expand Down Expand Up @@ -595,7 +594,7 @@ final class <%- camelize(type) %>AnnotationManagerTests: XCTestCase, AnnotationI
circleColor: testCircleColor)
var annotations = [PointAnnotation]()
for _ in 0...500 {
let annotation = PointAnnotation(coordinate: .random())
let annotation = PointAnnotation(coordinate: .random(), isSelected: false, isDraggable: false)
annotations.append(annotation)
}
Expand Down Expand Up @@ -644,7 +643,7 @@ final class <%- camelize(type) %>AnnotationManagerTests: XCTestCase, AnnotationI
textField: testTextField)
var annotations = [PointAnnotation]()
for _ in 0...500 {
let annotation = PointAnnotation(coordinate: .random())
let annotation = PointAnnotation(coordinate: .random(), isSelected: false, isDraggable: false)
annotations.append(annotation)
}
Expand Down Expand Up @@ -685,7 +684,7 @@ final class <%- camelize(type) %>AnnotationManagerTests: XCTestCase, AnnotationI
let clusterOptions = ClusterOptions()
var annotations = [PointAnnotation]()
for _ in 0...500 {
let annotation = PointAnnotation(coordinate: .random())
let annotation = PointAnnotation(coordinate: .random(), isSelected: false, isDraggable: false)
annotations.append(annotation)
}
Expand Down Expand Up @@ -728,12 +727,12 @@ final class <%- camelize(type) %>AnnotationManagerTests: XCTestCase, AnnotationI
let clusterOptions = ClusterOptions()
var annotations = [PointAnnotation]()
for _ in 0...500 {
let annotation = PointAnnotation(coordinate: .random())
let annotation = PointAnnotation(coordinate: .random(), isSelected: false, isDraggable: false)
annotations.append(annotation)
}
var newAnnotations = [PointAnnotation]()
for _ in 0...100 {
let annotation = PointAnnotation(coordinate: .random())
let annotation = PointAnnotation(coordinate: .random(), isSelected: false, isDraggable: false)
newAnnotations.append(annotation)
}
Expand Down Expand Up @@ -808,6 +807,50 @@ final class <%- camelize(type) %>AnnotationManagerTests: XCTestCase, AnnotationI
XCTAssertEqual(removeLayerInvocations[2].parameters, id)
}
<%_ } _%>
func testHandleDragBeginIsDraggableFalse() throws {
<%_ if ( type === symbol) { _%>
manager.annotations = [
PointAnnotation(id: "point1", coordinate: .random(), isSelected: false, isDraggable: false)
]
<%_ } else if ( type === circle ) { _%>
manager.annotations = [
CircleAnnotation(id: "circle1", centerCoordinate: .random(), isSelected: false, isDraggable: false)
]
<%_ } else if ( type === line ) { _%>
manager.annotations = [
PolylineAnnotation(id: "line1", lineCoordinates: [ CLLocationCoordinate2D(latitude: 0, longitude: 0), CLLocationCoordinate2D(latitude: 10, longitude: 10)], isSelected: false, isDraggable: false)
]
<%_ } else if ( type === fill ) { _%>
manager.annotations = [
PolygonAnnotation(id: "polygon1", polygon: .init([[
CLLocationCoordinate2DMake(24.51713945052515, -89.857177734375),
CLLocationCoordinate2DMake(24.51713945052515, -87.967529296875),
CLLocationCoordinate2DMake(26.244156283890756, -87.967529296875),
CLLocationCoordinate2DMake(26.244156283890756, -89.857177734375),
CLLocationCoordinate2DMake(24.51713945052515, -89.857177734375)
]]), isSelected: false, isDraggable: false)
]
<%_ } _%>
style.addSourceStub.reset()
style.addPersistentLayerWithPropertiesStub.reset()
<%_ if ( type === circle ) { _%>
manager.handleDragBegin(with: ["circle1"])
<%_ } else if ( type === symbol ) { _%>
manager.handleDragBegin(with: ["point1"])
<%_ } else if ( type === line ) { _%>
manager.handleDragBegin(with: ["line1"])
<%_ } else if ( type === fill ) { _%>
manager.handleDragBegin(with: ["polygon1"])
<%_ } _%>
XCTAssertEqual(style.addSourceStub.invocations.count, 0)
XCTAssertEqual(style.addPersistentLayerWithPropertiesStub.invocations.count, 0)
XCTAssertEqual(style.updateGeoJSONSourceStub.invocations.count, 0)
}
func testHandleDragBeginNoFeatureId() {
style.addSourceStub.reset()
style.addPersistentLayerWithPropertiesStub.reset()
Expand All @@ -833,15 +876,15 @@ final class <%- camelize(type) %>AnnotationManagerTests: XCTestCase, AnnotationI
func testHandleDragBegin() throws {
<%_ if ( type === symbol) { _%>
manager.annotations = [
PointAnnotation(id: "point1", coordinate: .random())
PointAnnotation(id: "point1", coordinate: .random(), isSelected: false, isDraggable: true)
]
<%_ } else if ( type === circle ) { _%>
manager.annotations = [
CircleAnnotation(id: "circle1", centerCoordinate: .random())
CircleAnnotation(id: "circle1", centerCoordinate: .random(), isSelected: false, isDraggable: true)
]
<%_ } else if ( type === line ) { _%>
manager.annotations = [
PolylineAnnotation(id: "line1", lineCoordinates: [ CLLocationCoordinate2D(latitude: 0, longitude: 0), CLLocationCoordinate2D(latitude: 10, longitude: 10)])
PolylineAnnotation(id: "line1", lineCoordinates: [ CLLocationCoordinate2D(latitude: 0, longitude: 0), CLLocationCoordinate2D(latitude: 10, longitude: 10)], isSelected: false, isDraggable: true)
]
<%_ } else if ( type === fill ) { _%>
manager.annotations = [
Expand All @@ -851,7 +894,7 @@ final class <%- camelize(type) %>AnnotationManagerTests: XCTestCase, AnnotationI
CLLocationCoordinate2DMake(26.244156283890756, -87.967529296875),
CLLocationCoordinate2DMake(26.244156283890756, -89.857177734375),
CLLocationCoordinate2DMake(24.51713945052515, -89.857177734375)
]]))
]]), isSelected: false, isDraggable: true)
]
<%_ } _%>
Expand Down Expand Up @@ -894,15 +937,15 @@ final class <%- camelize(type) %>AnnotationManagerTests: XCTestCase, AnnotationI
<%_ if ( type === symbol) { _%>
manager.annotations = [
PointAnnotation(id: "point1", coordinate: .init(latitude: 0, longitude: 0))
PointAnnotation(id: "point1", coordinate: .init(latitude: 0, longitude: 0), isSelected: false, isDraggable: true)
]
<%_ } else if ( type === circle ) { _%>
manager.annotations = [
CircleAnnotation(id: "circle1", centerCoordinate: .init(latitude: 0, longitude: 0))
CircleAnnotation(id: "circle1", centerCoordinate: .init(latitude: 0, longitude: 0), isSelected: false, isDraggable: true)
]
<%_ } else if ( type === line ) { _%>
manager.annotations = [
PolylineAnnotation(id: "line1", lineCoordinates: [ CLLocationCoordinate2D(latitude: 0, longitude: 0), CLLocationCoordinate2D(latitude: 10, longitude: 10)])
PolylineAnnotation(id: "line1", lineCoordinates: [ CLLocationCoordinate2D(latitude: 0, longitude: 0), CLLocationCoordinate2D(latitude: 10, longitude: 10)], isSelected: false, isDraggable: true)
]
<%_ } else if ( type === fill ) { _%>
manager.annotations = [
Expand All @@ -912,7 +955,7 @@ final class <%- camelize(type) %>AnnotationManagerTests: XCTestCase, AnnotationI
CLLocationCoordinate2DMake(26.244156283890756, -87.967529296875),
CLLocationCoordinate2DMake(26.244156283890756, -89.857177734375),
CLLocationCoordinate2DMake(24.51713945052515, -89.857177734375)
]]))
]]), isSelected: false, isDraggable: true)
]
<%_ } _%>
Expand Down Expand Up @@ -942,15 +985,15 @@ final class <%- camelize(type) %>AnnotationManagerTests: XCTestCase, AnnotationI
func testHandleDragEnded() throws {
<%_ if ( type === symbol) { _%>
manager.annotations = [
PointAnnotation(id: "point1", coordinate: .init(latitude: 0, longitude: 0))
PointAnnotation(id: "point1", coordinate: .init(latitude: 0, longitude: 0), isSelected: false, isDraggable: true)
]
<%_ } else if ( type === circle ) { _%>
manager.annotations = [
CircleAnnotation(id: "circle1", centerCoordinate: .init(latitude: 0, longitude: 0))
CircleAnnotation(id: "circle1", centerCoordinate: .init(latitude: 0, longitude: 0), isSelected: false, isDraggable: true)
]
<%_ } else if ( type === line ) { _%>
manager.annotations = [
PolylineAnnotation(id: "line1", lineCoordinates: [ CLLocationCoordinate2D(latitude: 0, longitude: 0), CLLocationCoordinate2D(latitude: 10, longitude: 10)])
PolylineAnnotation(id: "line1", lineCoordinates: [ CLLocationCoordinate2D(latitude: 0, longitude: 0), CLLocationCoordinate2D(latitude: 10, longitude: 10)], isSelected: false, isDraggable: true)
]
<%_ } else if ( type === fill ) { _%>
manager.annotations = [
Expand All @@ -960,7 +1003,7 @@ final class <%- camelize(type) %>AnnotationManagerTests: XCTestCase, AnnotationI
CLLocationCoordinate2DMake(26.244156283890756, -87.967529296875),
CLLocationCoordinate2DMake(26.244156283890756, -89.857177734375),
CLLocationCoordinate2DMake(24.51713945052515, -89.857177734375)
]]))
]]), isSelected: false, isDraggable: true)
]
<%_ } _%>
Expand Down
2 changes: 1 addition & 1 deletion mapbox-maps-ios
Submodule mapbox-maps-ios updated 23 files
+1 −0 Sources/MapboxMaps/Annotations/AnnotationManagerFactory.swift
+5 −3 Sources/MapboxMaps/Annotations/Generated/CircleAnnotation.swift
+1 −1 Sources/MapboxMaps/Annotations/Generated/CircleAnnotationManager.swift
+5 −3 Sources/MapboxMaps/Annotations/Generated/PointAnnotation.swift
+1 −1 Sources/MapboxMaps/Annotations/Generated/PointAnnotationManager.swift
+3 −1 Sources/MapboxMaps/Annotations/Generated/PolygonAnnotation.swift
+1 −1 Sources/MapboxMaps/Annotations/Generated/PolygonAnnotationManager.swift
+5 −3 Sources/MapboxMaps/Annotations/Generated/PolylineAnnotation.swift
+1 −1 Sources/MapboxMaps/Annotations/Generated/PolylineAnnotationManager.swift
+10 −1 Tests/MapboxMapsTests/Annotations/AnnotationOrchestratorImplTests.swift
+9 −9 Tests/MapboxMapsTests/Annotations/Generated/CircleAnnotationIntegrationTests.swift
+28 −12 Tests/MapboxMapsTests/Annotations/Generated/CircleAnnotationManagerTests.swift
+8 −8 Tests/MapboxMapsTests/Annotations/Generated/CircleAnnotationTests.swift
+28 −28 Tests/MapboxMapsTests/Annotations/Generated/PointAnnotationIntegrationTests.swift
+59 −44 Tests/MapboxMapsTests/Annotations/Generated/PointAnnotationManagerTests.swift
+27 −27 Tests/MapboxMapsTests/Annotations/Generated/PointAnnotationTests.swift
+6 −6 Tests/MapboxMapsTests/Annotations/Generated/PolygonAnnotationIntegrationTests.swift
+33 −11 Tests/MapboxMapsTests/Annotations/Generated/PolygonAnnotationManagerTests.swift
+5 −5 Tests/MapboxMapsTests/Annotations/Generated/PolygonAnnotationTests.swift
+10 −10 Tests/MapboxMapsTests/Annotations/Generated/PolylineAnnotationIntegrationTests.swift
+31 −15 Tests/MapboxMapsTests/Annotations/Generated/PolylineAnnotationManagerTests.swift
+9 −9 Tests/MapboxMapsTests/Annotations/Generated/PolylineAnnotationTests.swift
+8 −0 scripts/api-compatibility-check/breakage_allowlist.txt
2 changes: 1 addition & 1 deletion mobile-metrics

0 comments on commit a4528f8

Please sign in to comment.