Skip to content

Commit

Permalink
Refactor ClusterManager private extension for improved readability
Browse files Browse the repository at this point in the history
  • Loading branch information
vospennikov committed Oct 1, 2023
1 parent e97a35f commit 872a200
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
43 changes: 31 additions & 12 deletions Sources/ClusterMap/Public/Core/ClusterManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public final class ClusterManager<Annotation: CoordinateIdentifiable>
return completion(Difference())
}
autoreleasepool {
let changes = self.clusteredAnnotations(
let changes = self.performAnnotationClustering(
zoomScale: zoomScale,
visibleMapRect: visibleMapRect,
operation: operation
Expand Down Expand Up @@ -181,34 +181,51 @@ public final class ClusterManager<Annotation: CoordinateIdentifiable>
}

private extension ClusterManager {
func clusteredAnnotations(zoomScale: Double, visibleMapRect: MKMapRect, operation: Operation? = nil) -> Difference {
func performAnnotationClustering(
zoomScale: Double,
visibleMapRect: MKMapRect,
operation: Operation? = nil
) -> Difference {
var isCancelled: Bool { operation?.isCancelled ?? false }

guard !isCancelled else { return Difference() }

let mapRects = mapRects(zoomScale: zoomScale, visibleMapRect: visibleMapRect)
let mapRects = divideMapIntoGridCells(for: zoomScale, visibleMapRect: visibleMapRect)

guard !isCancelled else { return Difference() }

if configuration.shouldDistributeAnnotationsOnSameCoordinate {
distributeAnnotations(mapRect: visibleMapRect)
adjustOverlappingAnnotations(within: visibleMapRect)
}

let allAnnotations = dispatchQueue.sync {
clusteredAnnotations(mapRects: mapRects, zoomLevel: zoomLevel)
clusterAnnotations(within: mapRects, zoomLevel: zoomLevel)
}

guard !isCancelled else { return Difference() }

let (toAdd, toRemove) = determineAnnotationChanges(
allAnnotations: allAnnotations,
visibleMapRect: visibleMapRect
)
applyVisibleAnnotationChanges(toAdd: toAdd, toRemove: toRemove)

return Difference(insertions: toAdd, removals: toRemove)
}

func determineAnnotationChanges(
allAnnotations: [AnnotationType],
visibleMapRect: MKMapRect
) -> (toAdd: [AnnotationType], toRemove: [AnnotationType]) {
let before = visibleAnnotations
let after = allAnnotations

var toRemove = before.subtracted(after)
let toAdd = after.subtracted(before)

if !configuration.shouldRemoveInvisibleAnnotations {
let toKeep = toRemove.filter { kindOf in
switch kindOf {
let toKeep = toRemove.filter { annotationType in
switch annotationType {
case .annotation(let annotation):
return !visibleMapRect.contains(annotation.coordinate)
case .cluster(let clusterAnnotation):
Expand All @@ -218,15 +235,17 @@ private extension ClusterManager {
toRemove.subtract(toKeep)
}

return (toAdd, toRemove)
}

func applyVisibleAnnotationChanges(toAdd: [AnnotationType], toRemove: [AnnotationType]) {
dispatchQueue.async(flags: .barrier) { [weak self] in
self?.visibleAnnotations.subtract(toRemove)
self?.visibleAnnotations.add(toAdd)
}

return Difference(insertions: toAdd, removals: toRemove)
}

func clusteredAnnotations(mapRects: [MKMapRect], zoomLevel: Double) -> [AnnotationType] {
func clusterAnnotations(within mapRects: [MKMapRect], zoomLevel: Double) -> [AnnotationType] {
var allAnnotations: [AnnotationType] = []
for mapRect in mapRects {
var annotations: [Annotation] = []
Expand Down Expand Up @@ -256,7 +275,7 @@ private extension ClusterManager {
return allAnnotations
}

func distributeAnnotations(mapRect: MKMapRect) {
func adjustOverlappingAnnotations(within mapRect: MKMapRect) {
let annotations = dispatchQueue.sync {
tree.findAnnotations(in: mapRect)
}
Expand All @@ -278,7 +297,7 @@ private extension ClusterManager {
}
}

func mapRects(zoomScale: Double, visibleMapRect: MKMapRect) -> [MKMapRect] {
func divideMapIntoGridCells(for zoomScale: Double, visibleMapRect: MKMapRect) -> [MKMapRect] {
guard !zoomScale.isInfinite, !zoomScale.isNaN else { return [] }

zoomLevel = zoomScale.zoomLevel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public extension ClusterManager {
/// This closure takes an `Int` value representing the zoom level and returns a `CGSize` that sets the
/// dimensions of the cell size at that zoom level.
///
/// Use this to control how cells should be sized at different zoom levels to optimize clustering and
/// Use this to control how cells should be sized at different zoom levels to optimize clustering and
/// performance.
///
/// - Parameter zoom: The zoom level as an `Int`.
Expand Down

0 comments on commit 872a200

Please sign in to comment.