Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
object: nil,
queue: .main
) { [weak self] _ in
self?.viewModel.refreshCurrentFeeding()

Check warning on line 56 in animeal/src/Flows/Main/Modules/Home/Main/View/HomeViewController.swift

View workflow job for this annotation

GitHub Actions / Build and Test default scheme using iPhone simulator

call to main actor-isolated instance method 'refreshCurrentFeeding()' in a synchronous nonisolated context; this is an error in the Swift 6 language mode
}
}

Expand Down Expand Up @@ -365,9 +365,9 @@
view.addSubview(mapView.view)
mapView.mapboxMap.loadStyleURI(styleURI)

mapView.didTapAnnotations = { [weak self] in
let points = Set($0.map(\.id))
self?.viewModel.handleActionEvent(.tapFeedingPoints(Array(points)))
mapView.didTapAnnotations = { [weak self] annotations in
guard let tappedAnnotation = annotations.first else { return }
self?.viewModel.handleActionEvent(.tapFeedingPoints([tappedAnnotation.id]))
}

mapView.cameraAnimationQueue.append {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ class NavigationMapController: NavigationViewControllerDelegate {
var didChangeLocation: ((CLLocation, Bool) -> Void)?
var didTapAnnotations: (([Annotation]) -> Void)?

private var lastTapLocation: CGPoint?
private weak var tapGestureRecognizer: UITapGestureRecognizer?

Comment thread
steryokhin marked this conversation as resolved.
var view: UIView {
return navigationMapView
}
Expand Down Expand Up @@ -84,6 +87,27 @@ class NavigationMapController: NavigationViewControllerDelegate {
}

annotationManager.point.delegate = self
setupTapGesture()
}

deinit {
if let tapGestureRecognizer {
view.removeGestureRecognizer(tapGestureRecognizer)

}
}

private func setupTapGesture() {
let tapGesture = UITapGestureRecognizer()
tapGesture.addTarget(self, action: #selector(handleMapTap(_:)))
tapGesture.cancelsTouchesInView = false
navigationMapView.mapView.addGestureRecognizer(tapGesture)
tapGestureRecognizer = tapGesture
}

@objc private func handleMapTap(_ gesture: UITapGestureRecognizer) {
guard gesture.state == .ended else { return }
lastTapLocation = gesture.location(in: navigationMapView.mapView)
}

// MARK: - Public API
Expand Down Expand Up @@ -315,6 +339,49 @@ extension NavigationMapController: NavigationMapViewDelegate {
// MARK: - AnnotationInteractionDelegate conformance
extension NavigationMapController: AnnotationInteractionDelegate {
func annotationManager(_ manager: AnnotationManager, didDetectTappedAnnotations annotations: [Annotation]) {
didTapAnnotations?(annotations)
guard !annotations.isEmpty else {
lastTapLocation = nil
return
}

let sortedAnnotations: [Annotation]
if let tapLocation = lastTapLocation, annotations.count > 1 {
let tapCoordinate = navigationMapView.mapView.mapboxMap.coordinate(for: tapLocation)
sortedAnnotations = annotations.sorted { annotation1, annotation2 in
distance(from: tapCoordinate, to: annotation1) < distance(from: tapCoordinate, to: annotation2)
}
} else {
sortedAnnotations = annotations
}

lastTapLocation = nil
didTapAnnotations?(sortedAnnotations)
}

private func distance(from coordinate: CLLocationCoordinate2D, to annotation: Annotation) -> CLLocationDistance {
guard let annotationCoordinate = getCoordinate(from: annotation) else {
return .infinity
}
return coordinate.distance(to: annotationCoordinate)
}

private func getCoordinate(from annotation: Annotation) -> CLLocationCoordinate2D? {
switch annotation.geometry {
case .point(let point):
return point.coordinates
Comment thread
steryokhin marked this conversation as resolved.
case .polygon(let polygon):
return polygon.center
default:
return nil
}
}
}

// MARK: - CLLocationCoordinate2D Extension
private extension CLLocationCoordinate2D {
func distance(to coordinate: CLLocationCoordinate2D) -> CLLocationDistance {
let location1 = CLLocation(latitude: latitude, longitude: longitude)
let location2 = CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude)
return location1.distance(from: location2)
}
}
Loading