Skip to content

Commit

Permalink
Give annotations different priorities to reduce the number of overlap…
Browse files Browse the repository at this point in the history
…ping annotations.
  • Loading branch information
mhdhejazi committed Apr 20, 2020
1 parent 43643cb commit 3c146d2
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 27 deletions.
40 changes: 14 additions & 26 deletions Corona/Controller/Map/MapController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ class MapController: UIViewController {

private var cityZoomLevel: CGFloat { (view.bounds.width > 1_000) ? 6 : 5 }
private var allAnnotations: [RegionAnnotation] = []
private var countryAnnotations: [RegionAnnotation] = []
private var currentAnnotations: [RegionAnnotation] = []

private var panelController: FloatingPanelController!
private var regionPanelController: RegionPanelController!
Expand Down Expand Up @@ -139,7 +137,11 @@ class MapController: UIViewController {
}

func selectAnnotation(for region: Region, onlyIfVisible: Bool = false) {
guard let annotation = self.currentAnnotations.first(where: { $0.region == region }) else { return }
guard let annotation = mapView.annotations.first(where: { annotation in
(annotation as? RegionAnnotation)?.region == region
}) else {
return
}

if onlyIfVisible, !mapView.visibleMapRect.contains(MKMapPoint(annotation.coordinate)) {
return
Expand All @@ -153,15 +155,13 @@ class MapController: UIViewController {
.filter({ $0.report?.stat.number(for: mode) ?? 0 > 0 })
.map({ RegionAnnotation(region: $0, mode: mode) })

countryAnnotations = DataManager.shared.regions(of: .country)
.filter({ $0.report?.stat.number(for: mode) ?? 0 > 0 })
.map({ RegionAnnotation(region: $0, mode: mode) })

currentAnnotations = mapView.zoomLevel > cityZoomLevel ? allAnnotations : countryAnnotations
let currentAnnotations = allAnnotations.filter { annotation in
annotation.region.isCountry || mapView.zoomLevel > cityZoomLevel
}

mapView.superview?.transition {
self.mapView.removeAnnotations(self.mapView.annotations)
self.mapView.addAnnotations(self.currentAnnotations)
self.mapView.addAnnotations(currentAnnotations)
}

regionPanelController.regionDataController.region = nil
Expand Down Expand Up @@ -276,39 +276,27 @@ extension MapController: MKMapViewDelegate {
}

func mapViewDidChangeVisibleRegion(_ mapView: MKMapView) {
for annotation in currentAnnotations {
for annotation in allAnnotations {
if let view = mapView.view(for: annotation) as? RegionAnnotationView {
view.mapZoomLevel = mapView.zoomLevel
}
}
}

func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
var annotationToSelect: MKAnnotation?

if mapView.zoomLevel > cityZoomLevel {
if currentAnnotations.count != allAnnotations.count {
if mapView.annotations.count != allAnnotations.count {
mapView.superview?.transition {
annotationToSelect = mapView.selectedAnnotations.first
mapView.removeAnnotations(mapView.annotations)
self.currentAnnotations = self.allAnnotations
mapView.addAnnotations(self.currentAnnotations)
mapView.addAnnotations(self.allAnnotations.filter { !$0.region.isCountry })
}
}
} else {
if currentAnnotations.count != countryAnnotations.count {
if mapView.annotations.count == allAnnotations.count {
mapView.superview?.transition {
annotationToSelect = mapView.selectedAnnotations.first
mapView.removeAnnotations(mapView.annotations)
self.currentAnnotations = self.countryAnnotations
mapView.addAnnotations(self.currentAnnotations)
mapView.removeAnnotations(self.allAnnotations.filter { !$0.region.isCountry })
}
}
}

if let region = (annotationToSelect as? RegionAnnotation)?.region {
selectAnnotation(for: region, onlyIfVisible: true)
}
}

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
Expand Down
7 changes: 6 additions & 1 deletion Corona/Controller/Map/RegionAnnotationView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,12 @@ class RegionAnnotationView: MKAnnotationView {
detailAccessoryView?.detailsLabel?.attributedText = detailsString

if #available(iOS 11.0, *) {
displayPriority = isProvinceRegion ? .defaultLow : .required
/// Give provinces a low display priority
/// Give the top 30 countries a required priority
/// Give the others a high priority
displayPriority = isProvinceRegion
? .defaultLow
: ((region?.order ?? Int.max) < 30 ? .required : .defaultHigh)
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions Corona/Data/DataManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ extension DataManager {

self.world = worldRegion

let sortedCountries: [Region] = countries.lazy.sorted().reversed()
for index in sortedCountries.indices {
sortedCountries[index].order = index
}

try? Disk.save(self.world, to: .caches, as: Self.dataFileName)

completion(true)
Expand Down
1 change: 1 addition & 0 deletions Corona/Model/Region.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class Region: Codable {
public let parentName: String? /// Country name
public let location: Coordinate

public var order: Int = Int.max
public var report: Report?
public var timeSeries: TimeSeries?
public lazy var dailyChange: Change? = { generateDailyChange() }()
Expand Down

0 comments on commit 3c146d2

Please sign in to comment.