Skip to content

Commit

Permalink
Add DetailCalloutAccessory
Browse files Browse the repository at this point in the history
This was authored by @pauljohanneskraft in the comment at pauljohanneskraft#46 (comment)
  • Loading branch information
darronschall committed Oct 4, 2023
1 parent 2d8951c commit 6162e36
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions Sources/Annotations/DetailCalloutAccessory.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//
// DetailCalloutAccessory.swift
// Map
//
// Created by Darron Schall on 10/4/23.
//

#if !os(watchOS)

import Foundation
import MapKit
import SwiftUI

private struct DetailCalloutAccessory<Annotation: MapAnnotation, Content: View>: MapAnnotation {

// MARK: Static Functions

static func registerView(on mapView: MKMapView) {
Annotation.registerView(on: mapView)
}

// MARK: Stored Properties

private let wrappedAnnotation: Annotation
private let content: () -> Content

// MARK: Computed Properties

var annotation: MKAnnotation {
wrappedAnnotation.annotation
}

// MARK: Initialization

init(_ annotation: Annotation, @ViewBuilder content: @escaping () -> Content) {
self.wrappedAnnotation = annotation
self.content = content
}

// MARK: Methods

func view(for mapView: MKMapView) -> MKAnnotationView? {
guard let view = wrappedAnnotation.view(for: mapView) else {
return nil
}

if Content.self != EmptyView.self {
view.canShowCallout = true
view.detailCalloutAccessoryView = NativeHostingController(rootView: content()).view
} else {
view.canShowCallout = false
view.detailCalloutAccessoryView = nil
}

return view
}

}

extension MapAnnotation {

public func detailCalloutAccessory<Content: View>(@ViewBuilder content: @escaping () -> Content) -> some MapAnnotation {
DetailCalloutAccessory(self, content: content)
}

}

#endif

0 comments on commit 6162e36

Please sign in to comment.