Skip to content
Open
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
35 changes: 28 additions & 7 deletions GoInfoGame/GoInfoGame/UI/Map/CustomMap.swift
Original file line number Diff line number Diff line change
Expand Up @@ -149,17 +149,36 @@ struct CustomMap: UIViewRepresentable {
var previosMultiSelectionMode: Bool = false
private var annotations: [DisplayUnitAnnotation] = []
private(set) var clusterManager: ClusterManager = ClusterManager<DisplayUnitAnnotation>(configuration: .init(maxZoomLevel: 17, minCountForClustering: 2, clusterPosition: .nearCenter))
lazy private(set) var mapReloader: MapReloader = MapReloader(coordinator: self)

init(_ parent: CustomMap, shadowOverlay: ShadowOverlay) {
self.parent = parent
self.contextualInfo = parent.contextualInfo
self.shadowOverlay = shadowOverlay
}

func reloadMap() async {
guard let mapView = mapView else { return }
async let changes = clusterManager.reload(mapViewSize: mapView.bounds.size, coordinateRegion: mapView.region)
await applyChanges(changes)
actor MapReloader {
private var currentTask: Task<Void, Never>?
let coordinator: Coordinator

init(coordinator: Coordinator) {
self.coordinator = coordinator
}

func reload(using mapView: MKMapView?) {
// Cancel the old reload if it's still running
currentTask?.cancel()

currentTask = Task {
guard let mapView = mapView else { return }
async let changes = coordinator.clusterManager.reload(
mapViewSize: mapView.bounds.size,
coordinateRegion: mapView.region
)
debugPrint("map issue insertions: \(await changes.insertions.count) deletions: \(await changes.removals.count)")
await coordinator.applyChanges(changes)
}
}
}

@MainActor
Expand Down Expand Up @@ -405,7 +424,9 @@ struct CustomMap: UIViewRepresentable {
}

func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
Task { await reloadMap() }
Task {
await mapReloader.reload(using: mapView)
}
}

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

await context.coordinator.clusterManager.removeAll()
await context.coordinator.reloadMap()
await context.coordinator.mapReloader.reload(using: mapView)
// let existingCoordinates = mapView.annotations.compactMap {
// ($0 as? DisplayUnitAnnotation)?.coordinate
// }
Expand All @@ -544,7 +565,7 @@ struct CustomMap: UIViewRepresentable {
.filter { !$0.isHidden }
.map { $0.annotation }
await context.coordinator.clusterManager.add(visibleAnnotations)
await context.coordinator.reloadMap()
await context.coordinator.mapReloader.reload(using: mapView)
}

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