Skip to content

Commit

Permalink
Update templates for PointAnnotationManager. (#1303)
Browse files Browse the repository at this point in the history
* Update templates for Point annotation manager.
- Delegate all add/remove images tasks from PointAnnotationManager to AnnotationImagesManager.
- Update templates for PointAnnotationManagerTests and PointAnnotationIntegrationTests.
  • Loading branch information
maios authored Dec 22, 2022
1 parent ae05ba1 commit 83ab0c8
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,65 @@ final class <%- camelize(type) %>AnnotationIntegrationTests: MapViewIntegrationT
}
<%_ } _%>
<%_ } _%>
<%_ if (type == "point") { %>
func testImagesAddedToStyleIfNotExist() throws {
let existingImage = PointAnnotation.Image(image: try XCTUnwrap(UIImage.emptyImage()), name: UUID().uuidString)
try style.addImage(existingImage.image, id: existingImage.name)
var annotation1 = PointAnnotation(coordinate: .random())
annotation1.image = existingImage
var annotation2 = PointAnnotation(coordinate: .random())
annotation2.image = .init(image: try XCTUnwrap(UIImage.emptyImage()), name: "test-image-2")
manager.annotations = [annotation1, annotation2]
manager.syncSourceAndLayerIfNeeded()
XCTAssertTrue(style.imageExists(withId: existingImage.name))
XCTAssertTrue(style.imageExists(withId: "test-image-2"))
manager.annotations = []
manager.syncSourceAndLayerIfNeeded()
// Images added externally will not be removed from Style when PointAnnotationManager is updated.
XCTAssertTrue(style.imageExists(withId: existingImage.name))
XCTAssertFalse(style.imageExists(withId: "test-image-2"))
}
func testStyleImagesSharedBetweenMultipleManagers() throws {
let otherManager = mapView.annotations.makePointAnnotationManager()
let sharedImageID = UUID().uuidString
let sharedImage = PointAnnotation.Image(image: try XCTUnwrap(UIImage.emptyImage()), name: sharedImageID)
var pointAnnotation1 = PointAnnotation(coordinate: .random())
pointAnnotation1.image = sharedImage
manager.annotations = [pointAnnotation1]
var pointAnnotation2 = PointAnnotation(coordinate: .random())
pointAnnotation2.image = sharedImage
otherManager.annotations = [pointAnnotation2]
manager.syncSourceAndLayerIfNeeded()
otherManager.syncSourceAndLayerIfNeeded()
XCTAssertTrue(style.imageExists(withId: sharedImageID))
XCTAssertTrue(manager.isUsingStyleImage(sharedImageID))
XCTAssertTrue(otherManager.isUsingStyleImage(sharedImageID))
manager.annotations = []
manager.syncSourceAndLayerIfNeeded()
XCTAssertTrue(style.imageExists(withId: sharedImageID))
XCTAssertFalse(manager.isUsingStyleImage(sharedImageID))
XCTAssertTrue(otherManager.isUsingStyleImage(sharedImageID))
otherManager.annotations = []
otherManager.syncSourceAndLayerIfNeeded()
XCTAssertFalse(style.imageExists(withId: sharedImageID))
XCTAssertFalse(manager.isUsingStyleImage(sharedImageID))
XCTAssertFalse(otherManager.isUsingStyleImage(sharedImageID))
}
<%_ } -%>
}

// End of generated file
52 changes: 41 additions & 11 deletions codegen/annotation-generator/templates/AnnotationManager.swift.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ public class <%- camelize(type) %>AnnotationManager: AnnotationManagerInternal {

private var needsSyncSourceAndLayer = false
<%_ if (isPointAnnotationManager) { _%>
private var addedImages = Set<String>()
/// List of images used by this ``PointAnnotationManager``.
private(set) internal var allImages = Set<String>()
private let imagesManager: AnnotationImagesManagerProtocol
private var clusterOptions: ClusterOptions?
<%_ } _%>
Expand Down Expand Up @@ -92,7 +94,8 @@ public class <%- camelize(type) %>AnnotationManager: AnnotationManagerInternal {
style: StyleProtocol,
layerPosition: LayerPosition?,
displayLinkCoordinator: DisplayLinkCoordinator<%_ if (isPointAnnotationManager) { _%>,
clusterOptions: ClusterOptions? = nil<%_ } _%>,
clusterOptions: ClusterOptions? = nil,
imagesManager: AnnotationImagesManagerProtocol<%_ } _%>,
<%_ if (type === circle || type === symbol) { _%>
offsetPointCalculator: OffsetPointCalculator) {
<%_ } else if ( type === line ) { _%>
Expand All @@ -103,10 +106,13 @@ public class <%- camelize(type) %>AnnotationManager: AnnotationManagerInternal {
self.id = id
self.sourceId = id
self.layerId = id
self.style = style<% if (isPointAnnotationManager) { %>
self.clusterOptions = clusterOptions<% } %>
self.style = style
<%_ if (isPointAnnotationManager) { _%>
self.clusterOptions = clusterOptions
self.imagesManager = imagesManager
<%_ } _%>
self.displayLinkCoordinator = displayLinkCoordinator
<%_ if (type === circle || type === symbol) { _%>
<%_ if (type === circle || type === symbol) { _%>
self.offsetPointCalculator = offsetPointCalculator
<%_ } else if ( type === line ) { _%>
self.offsetLineStringCalculator = offsetLineStringCalculator
Expand All @@ -115,7 +121,9 @@ public class <%- camelize(type) %>AnnotationManager: AnnotationManagerInternal {
<%_ } _%>
self.dragLayerId = id + "_drag-layer"
self.dragSourceId = id + "_drag-source"
<%_ if (isPointAnnotationManager) { %>
imagesManager.register(imagesConsumer: self)
<%_ } %>
do {
// Add the source with empty `data` property
var source = GeoJSONSource()
Expand Down Expand Up @@ -242,7 +250,7 @@ public class <%- camelize(type) %>AnnotationManager: AnnotationManagerInternal {
category: "Annotations")
}
<%_ if (isPointAnnotationManager) { _%>
removeImages(from: style, images: addedImages)
removeAllImages()
<%_ } _%>
displayLinkCoordinator?.remove(displayLinkParticipant)
}
Expand All @@ -262,12 +270,12 @@ public class <%- camelize(type) %>AnnotationManager: AnnotationManagerInternal {
<%_ if (isPointAnnotationManager) { _%>
let newImages = Set(annotations.compactMap(\.image) + [annotationBeingDragged].compactMap(\.?.image))
let newImageNames = Set(newImages.map(\.name))
let unusedImages = addedImages.subtracting(newImageNames)
let unusedImages = allImages.subtracting(newImageNames)
addImagesToStyleIfNeeded(style: style, images: newImages)
removeImages(from: style, images: unusedImages)
addImages(newImages)
allImages = newImageNames
addedImages = newImageNames
removeImages(unusedImages)
<%_ } _%>
// Construct the properties dictionary from the annotations
Expand Down Expand Up @@ -470,4 +478,26 @@ extension <%- camelize(type) %>AnnotationManager: DelegatingDisplayLinkParticipa
}
}
<%_ if (isPointAnnotationManager) { _%>
private extension <%- camelize(type) %>AnnotationManager {
func addImages(_ images: Set<PointAnnotation.Image>) {
for image in images {
imagesManager.addImage(image.image, id: image.name, sdf: false, contentInsets: .zero)
}
}
func removeImages(_ names: Set<String>) {
for imageName in names {
imagesManager.removeImage(imageName)
}
}
func removeAllImages() {
let imagesToRemove = allImages
allImages.removeAll()
removeImages(imagesToRemove)
}
}
<%_ } _%>
// End of generated file.
Loading

0 comments on commit 83ab0c8

Please sign in to comment.