Skip to content

Commit

Permalink
Fix bug on elements disappearing!
Browse files Browse the repository at this point in the history
  • Loading branch information
ramselTenna committed Feb 22, 2023
1 parent aacab22 commit 9c469c0
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions BTCMap/MapViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import CoreLocation
import SwiftUI
import Combine
import Cluster
import os

class ElementAnnotation: NSObject, MKAnnotation, Identifiable {
let element: API.Element
Expand All @@ -29,7 +30,8 @@ class MapViewController: UIViewController, MKMapViewDelegate, UISheetPresentatio
private var locationManager = CLLocationManager()
private var elementsQueue = DispatchQueue(label: "org.btcmap.app.map.elements")
private var elementAnnotations: [String: ElementAnnotation] = [:]

private let logger = Logger(subsystem: "org.btcmap.app", category: "Map")

private var cancellables = Set<AnyCancellable>()

private func setupMapStateObservers() {
Expand Down Expand Up @@ -86,9 +88,9 @@ class MapViewController: UIViewController, MKMapViewDelegate, UISheetPresentatio
}

DispatchQueue.main.async {
self.logger.log("elementsChanged - adding: \(annotationsToAdd.count) - removing: \(annotationsToRemove.count)")
self.elementAnnotations = annotations
self.addAnnotations(annotationsToAdd)
self.removeAnnotations(annotationsToRemove)
self.removeThenAddAnnotations(remove: annotationsToRemove, add: annotationsToAdd)
}
}
}
Expand All @@ -104,6 +106,13 @@ class MapViewController: UIViewController, MKMapViewDelegate, UISheetPresentatio
manager.reload(mapView: mapView)
}

private func removeThenAddAnnotations(remove: [MKAnnotation]?, add: [MKAnnotation]?) {
if let remove = remove { manager.remove(remove) }
if let add = add { manager.add(add) }
self.logger.log("removeThenAddAnnotations - remove: \(remove != nil ? remove!.count : 0) - then add: \(add != nil ? add!.count : 0) ")
manager.reload(mapView: mapView)
}

// MARK: - MKMapViewDelegate

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
Expand Down

0 comments on commit 9c469c0

Please sign in to comment.