Skip to content

Commit f2046a3

Browse files
committed
Fixed duplicate annotations issue after answering the multi quests
1 parent 54f7de3 commit f2046a3

1 file changed

Lines changed: 28 additions & 7 deletions

File tree

GoInfoGame/GoInfoGame/UI/Map/CustomMap.swift

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -149,17 +149,36 @@ struct CustomMap: UIViewRepresentable {
149149
var previosMultiSelectionMode: Bool = false
150150
private var annotations: [DisplayUnitAnnotation] = []
151151
private(set) var clusterManager: ClusterManager = ClusterManager<DisplayUnitAnnotation>(configuration: .init(maxZoomLevel: 17, minCountForClustering: 2, clusterPosition: .nearCenter))
152+
lazy private(set) var mapReloader: MapReloader = MapReloader(coordinator: self)
152153

153154
init(_ parent: CustomMap, shadowOverlay: ShadowOverlay) {
154155
self.parent = parent
155156
self.contextualInfo = parent.contextualInfo
156157
self.shadowOverlay = shadowOverlay
157158
}
158159

159-
func reloadMap() async {
160-
guard let mapView = mapView else { return }
161-
async let changes = clusterManager.reload(mapViewSize: mapView.bounds.size, coordinateRegion: mapView.region)
162-
await applyChanges(changes)
160+
actor MapReloader {
161+
private var currentTask: Task<Void, Never>?
162+
let coordinator: Coordinator
163+
164+
init(coordinator: Coordinator) {
165+
self.coordinator = coordinator
166+
}
167+
168+
func reload(using mapView: MKMapView?, clusterManager: ClusterManager<DisplayUnitAnnotation>) {
169+
// Cancel the old reload if it's still running
170+
currentTask?.cancel()
171+
172+
currentTask = Task {
173+
guard let mapView = mapView else { return }
174+
async let changes = clusterManager.reload(
175+
mapViewSize: mapView.bounds.size,
176+
coordinateRegion: mapView.region
177+
)
178+
debugPrint("map issue insertions: \(await changes.insertions.count) deletions: \(await changes.removals.count)")
179+
await coordinator.applyChanges(changes)
180+
}
181+
}
163182
}
164183

165184
@MainActor
@@ -405,7 +424,9 @@ struct CustomMap: UIViewRepresentable {
405424
}
406425

407426
func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
408-
Task { await reloadMap() }
427+
Task {
428+
await mapReloader.reload(using: mapView, clusterManager: clusterManager)
429+
}
409430
}
410431

411432
private func selectedAnAnnotation(selectedQuest: DisplayUnitAnnotation) {
@@ -522,7 +543,7 @@ struct CustomMap: UIViewRepresentable {
522543
private func manageAnnotations(_ mapView: MKMapView, context: Context) async {
523544

524545
await context.coordinator.clusterManager.removeAll()
525-
await context.coordinator.reloadMap()
546+
await context.coordinator.mapReloader.reload(using: mapView, clusterManager: context.coordinator.clusterManager)
526547
// let existingCoordinates = mapView.annotations.compactMap {
527548
// ($0 as? DisplayUnitAnnotation)?.coordinate
528549
// }
@@ -544,7 +565,7 @@ struct CustomMap: UIViewRepresentable {
544565
.filter { !$0.isHidden }
545566
.map { $0.annotation }
546567
await context.coordinator.clusterManager.add(visibleAnnotations)
547-
await context.coordinator.reloadMap()
568+
context.coordinator.mapReloader.reload(using: mapView, clusterManager: context.coordinator.clusterManager)
548569
}
549570

550571
func adjustCoordinateForOverlap(_ coordinate: CLLocationCoordinate2D, with index: Int) -> CLLocationCoordinate2D {

0 commit comments

Comments
 (0)