Skip to content

Commit

Permalink
Use default MKUserTrackingMode instead of SwiftUI-like version, add M…
Browse files Browse the repository at this point in the history
…apUserTrackingButton (pauljohanneskraft#16)
  • Loading branch information
pauljohanneskraft authored Jul 10, 2022
1 parent e22722d commit cf20314
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 87 deletions.
63 changes: 0 additions & 63 deletions Sources/Configuration/MapUserTrackingMode.swift

This file was deleted.

2 changes: 1 addition & 1 deletion Sources/Map/Map+Coordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ extension Map {

private func updateUserTracking(on mapView: MKMapView, from previousView: Map?, to newView: Map) {
if #available(macOS 11, *) {
let newTrackingMode = newView.userTrackingMode.actualValue
let newTrackingMode = newView.userTrackingMode
if newView.usesUserTrackingMode, mapView.userTrackingMode != newTrackingMode {
mapView.userTrackingMode = newTrackingMode
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Map/Map+Watch+Coordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ extension Map: WKInterfaceObjectRepresentable {
mapView.setShowsUserLocation(newView.informationVisibility.contains(.userLocation))
}
if newView.usesUserTrackingMode, previousView?.userTrackingMode != newView.userTrackingMode {
mapView.setUserTrackingMode(newView.userTrackingMode.actualValue, animated: animated)
mapView.setUserTrackingMode(newView.userTrackingMode, animated: animated)
}
}

Expand Down
10 changes: 5 additions & 5 deletions Sources/Map/Map+Watch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public struct Map<AnnotationItems: RandomAccessCollection> where AnnotationItems
let usesUserTrackingMode: Bool

@available(watchOS 6.1, *)
@Binding var userTrackingMode: MapUserTrackingMode
@Binding var userTrackingMode: WKInterfaceMap.UserTrackingMode

let annotationItems: AnnotationItems
let annotationContent: (AnnotationItems.Element) -> MapAnnotation
Expand Down Expand Up @@ -71,7 +71,7 @@ extension Map {
public init(
coordinateRegion: Binding<MKCoordinateRegion>,
informationVisibility: MapInformationVisibility = .default,
userTrackingMode: Binding<MapUserTrackingMode>?,
userTrackingMode: Binding<WKInterfaceMap.UserTrackingMode>?,
annotationItems: AnnotationItems,
annotationContent: @escaping (AnnotationItems.Element) -> MapAnnotation
) {
Expand All @@ -94,7 +94,7 @@ extension Map {
public init(
mapRect: Binding<MKMapRect>,
informationVisibility: MapInformationVisibility = .default,
userTrackingMode: Binding<MapUserTrackingMode>?,
userTrackingMode: Binding<WKInterfaceMap.UserTrackingMode>?,
annotationItems: AnnotationItems,
annotationContent: @escaping (AnnotationItems.Element) -> MapAnnotation
) {
Expand Down Expand Up @@ -145,7 +145,7 @@ extension Map where AnnotationItems == EmptyCollection<IdentifiableObject<NSObje
public init(
coordinateRegion: Binding<MKCoordinateRegion>,
informationVisibility: MapInformationVisibility = .default,
userTrackingMode: Binding<MapUserTrackingMode>?
userTrackingMode: Binding<WKInterfaceMap.UserTrackingMode>?
) {
self.init(
coordinateRegion: coordinateRegion,
Expand All @@ -160,7 +160,7 @@ extension Map where AnnotationItems == EmptyCollection<IdentifiableObject<NSObje
public init(
mapRect: Binding<MKMapRect>,
informationVisibility: MapInformationVisibility = .default,
userTrackingMode: Binding<MapUserTrackingMode>?
userTrackingMode: Binding<WKInterfaceMap.UserTrackingMode>?
) {
self.init(
mapRect: mapRect,
Expand Down
34 changes: 17 additions & 17 deletions Sources/Map/Map.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public struct Map<AnnotationItems: RandomAccessCollection, OverlayItems: RandomA
let usesUserTrackingMode: Bool

@available(macOS 11, *)
@Binding var userTrackingMode: MapUserTrackingMode
@Binding var userTrackingMode: MKUserTrackingMode

let annotationItems: AnnotationItems
let annotationContent: (AnnotationItems.Element) -> MapAnnotation
Expand Down Expand Up @@ -105,7 +105,7 @@ extension Map {
informationVisibility: MapInformationVisibility = .default,
interactionModes: MapInteractionModes = .all,
showsUserLocation: Bool = false,
userTrackingMode: Binding<MapUserTrackingMode>?,
userTrackingMode: Binding<MKUserTrackingMode>?,
annotationItems: AnnotationItems,
@MapAnnotationBuilder annotationContent: @escaping (AnnotationItems.Element) -> MapAnnotation,
overlayItems: OverlayItems,
Expand Down Expand Up @@ -138,7 +138,7 @@ extension Map {
pointOfInterestFilter: MKPointOfInterestFilter? = nil,
informationVisibility: MapInformationVisibility = .default,
interactionModes: MapInteractionModes = .all,
userTrackingMode: Binding<MapUserTrackingMode>?,
userTrackingMode: Binding<MKUserTrackingMode>?,
annotationItems: AnnotationItems,
@MapAnnotationBuilder annotationContent: @escaping (AnnotationItems.Element) -> MapAnnotation,
overlayItems: OverlayItems,
Expand Down Expand Up @@ -176,7 +176,7 @@ extension Map {
pointOfInterestFilter: MKPointOfInterestFilter? = nil,
informationVisibility: MapInformationVisibility = .default,
interactionModes: MapInteractionModes = .all,
userTrackingMode: Binding<MapUserTrackingMode>? = nil,
userTrackingMode: Binding<MKUserTrackingMode>? = nil,
annotationItems: AnnotationItems,
@MapAnnotationBuilder annotationContent: @escaping (AnnotationItems.Element) -> MapAnnotation,
overlayItems: OverlayItems,
Expand Down Expand Up @@ -208,7 +208,7 @@ extension Map {
pointOfInterestFilter: MKPointOfInterestFilter? = nil,
informationVisibility: MapInformationVisibility = .default,
interactionModes: MapInteractionModes = .all,
userTrackingMode: Binding<MapUserTrackingMode>? = nil,
userTrackingMode: Binding<MKUserTrackingMode>? = nil,
annotationItems: AnnotationItems,
@MapAnnotationBuilder annotationContent: @escaping (AnnotationItems.Element) -> MapAnnotation,
overlayItems: OverlayItems,
Expand Down Expand Up @@ -309,7 +309,7 @@ extension Map where AnnotationItems == [IdentifiableObject<MKAnnotation>] {
pointOfInterestFilter: MKPointOfInterestFilter? = nil,
informationVisibility: MapInformationVisibility = .default,
interactionModes: MapInteractionModes = .all,
userTrackingMode: Binding<MapUserTrackingMode>?,
userTrackingMode: Binding<MKUserTrackingMode>?,
annotations: [MKAnnotation] = [],
@MapAnnotationBuilder annotationContent: @escaping (MKAnnotation) -> MapAnnotation = { annotation in
assertionFailure("Please provide an `annotationContent` closure for the values in `annotations`.")
Expand Down Expand Up @@ -339,7 +339,7 @@ extension Map where AnnotationItems == [IdentifiableObject<MKAnnotation>] {
pointOfInterestFilter: MKPointOfInterestFilter? = nil,
informationVisibility: MapInformationVisibility = .default,
interactionModes: MapInteractionModes = .all,
userTrackingMode: Binding<MapUserTrackingMode>?,
userTrackingMode: Binding<MKUserTrackingMode>?,
annotations: [MKAnnotation] = [],
@MapAnnotationBuilder annotationContent: @escaping (MKAnnotation) -> MapAnnotation = { annotation in
assertionFailure("Please provide an `annotationContent` closure for the values in `annotations`.")
Expand Down Expand Up @@ -374,7 +374,7 @@ extension Map where AnnotationItems == [IdentifiableObject<MKAnnotation>] {
pointOfInterestFilter: MKPointOfInterestFilter? = nil,
informationVisibility: MapInformationVisibility = .default,
interactionModes: MapInteractionModes = .all,
userTrackingMode: Binding<MapUserTrackingMode>? = nil,
userTrackingMode: Binding<MKUserTrackingMode>? = nil,
annotations: [MKAnnotation] = [],
@MapAnnotationBuilder annotationContent: @escaping (MKAnnotation) -> MapAnnotation = { annotation in
assertionFailure("Please provide an `annotationContent` closure for the values in `annotations`.")
Expand Down Expand Up @@ -403,7 +403,7 @@ extension Map where AnnotationItems == [IdentifiableObject<MKAnnotation>] {
pointOfInterestFilter: MKPointOfInterestFilter? = nil,
informationVisibility: MapInformationVisibility = .default,
interactionModes: MapInteractionModes = .all,
userTrackingMode: Binding<MapUserTrackingMode>? = nil,
userTrackingMode: Binding<MKUserTrackingMode>? = nil,
annotations: [MKAnnotation] = [],
@MapAnnotationBuilder annotationContent: @escaping (MKAnnotation) -> MapAnnotation = { annotation in
assertionFailure("Please provide an `annotationContent` closure for the values in `annotations`.")
Expand Down Expand Up @@ -506,7 +506,7 @@ extension Map where OverlayItems == [IdentifiableObject<MKOverlay>] {
pointOfInterestFilter: MKPointOfInterestFilter? = nil,
informationVisibility: MapInformationVisibility = .default,
interactionModes: MapInteractionModes = .all,
userTrackingMode: Binding<MapUserTrackingMode>?,
userTrackingMode: Binding<MKUserTrackingMode>?,
annotationItems: AnnotationItems,
@MapAnnotationBuilder annotationContent: @escaping (AnnotationItems.Element) -> MapAnnotation,
overlays: [MKOverlay] = [],
Expand Down Expand Up @@ -538,7 +538,7 @@ extension Map where OverlayItems == [IdentifiableObject<MKOverlay>] {
pointOfInterestFilter: MKPointOfInterestFilter? = nil,
informationVisibility: MapInformationVisibility = .default,
interactionModes: MapInteractionModes = .all,
userTrackingMode: Binding<MapUserTrackingMode>?,
userTrackingMode: Binding<MKUserTrackingMode>?,
annotationItems: AnnotationItems,
@MapAnnotationBuilder annotationContent: @escaping (AnnotationItems.Element) -> MapAnnotation,
overlays: [MKOverlay] = [],
Expand Down Expand Up @@ -575,7 +575,7 @@ extension Map where OverlayItems == [IdentifiableObject<MKOverlay>] {
pointOfInterestFilter: MKPointOfInterestFilter? = nil,
informationVisibility: MapInformationVisibility = .default,
interactionModes: MapInteractionModes = .all,
userTrackingMode: Binding<MapUserTrackingMode>? = nil,
userTrackingMode: Binding<MKUserTrackingMode>? = nil,
annotationItems: AnnotationItems,
@MapAnnotationBuilder annotationContent: @escaping (AnnotationItems.Element) -> MapAnnotation,
overlays: [MKOverlay] = [],
Expand Down Expand Up @@ -606,7 +606,7 @@ extension Map where OverlayItems == [IdentifiableObject<MKOverlay>] {
pointOfInterestFilter: MKPointOfInterestFilter? = nil,
informationVisibility: MapInformationVisibility = .default,
interactionModes: MapInteractionModes = .all,
userTrackingMode: Binding<MapUserTrackingMode>? = nil,
userTrackingMode: Binding<MKUserTrackingMode>? = nil,
annotationItems: AnnotationItems,
@MapAnnotationBuilder annotationContent: @escaping (AnnotationItems.Element) -> MapAnnotation,
overlays: [MKOverlay] = [],
Expand Down Expand Up @@ -715,7 +715,7 @@ extension Map where AnnotationItems == [IdentifiableObject<MKAnnotation>], Overl
pointOfInterestFilter: MKPointOfInterestFilter? = nil,
informationVisibility: MapInformationVisibility = .default,
interactionModes: MapInteractionModes = .all,
userTrackingMode: Binding<MapUserTrackingMode>?,
userTrackingMode: Binding<MKUserTrackingMode>?,
annotations: [MKAnnotation] = [],
@MapAnnotationBuilder annotationContent: @escaping (MKAnnotation) -> MapAnnotation = { annotation in
assertionFailure("Please provide an `annotationContent` closure for the values in `annotations`.")
Expand Down Expand Up @@ -750,7 +750,7 @@ extension Map where AnnotationItems == [IdentifiableObject<MKAnnotation>], Overl
pointOfInterestFilter: MKPointOfInterestFilter? = nil,
informationVisibility: MapInformationVisibility = .default,
interactionModes: MapInteractionModes = .all,
userTrackingMode: Binding<MapUserTrackingMode>?,
userTrackingMode: Binding<MKUserTrackingMode>?,
annotations: [MKAnnotation] = [],
@MapAnnotationBuilder annotationContent: @escaping (MKAnnotation) -> MapAnnotation = { annotation in
assertionFailure("Please provide an `annotationContent` closure for the values in `annotations`.")
Expand Down Expand Up @@ -792,7 +792,7 @@ extension Map
pointOfInterestFilter: MKPointOfInterestFilter? = nil,
informationVisibility: MapInformationVisibility = .default,
interactionModes: MapInteractionModes = .all,
userTrackingMode: Binding<MapUserTrackingMode>? = nil,
userTrackingMode: Binding<MKUserTrackingMode>? = nil,
annotations: [MKAnnotation] = [],
@MapAnnotationBuilder annotationContent: @escaping (MKAnnotation) -> MapAnnotation = { annotation in
assertionFailure("Please provide an `annotationContent` closure for the values in `annotations`.")
Expand Down Expand Up @@ -826,7 +826,7 @@ extension Map
pointOfInterestFilter: MKPointOfInterestFilter? = nil,
informationVisibility: MapInformationVisibility = .default,
interactionModes: MapInteractionModes = .all,
userTrackingMode: Binding<MapUserTrackingMode>? = nil,
userTrackingMode: Binding<MKUserTrackingMode>? = nil,
annotations: [MKAnnotation] = [],
@MapAnnotationBuilder annotationContent: @escaping (MKAnnotation) -> MapAnnotation = { annotation in
assertionFailure("Please provide an `annotationContent` closure for the values in `annotations`.")
Expand Down
45 changes: 45 additions & 0 deletions Sources/Views/MapUserTrackingButton.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//
// MapUserTrackingButton.swift
// Map
//
// Created by Paul Kraft on 03.07.22.
//

#if canImport(UIKit) && !os(watchOS)

import SwiftUI
import MapKit

public struct MapUserTrackingButton {

// MARK: Stored Properties

private let key: AnyHashable

// MARK: Initialization

public init<Key: Hashable>(key: Key) {
self.key = key
}

}

// MARK: - UIViewRepresentable

extension MapUserTrackingButton: UIViewRepresentable {

public func makeUIView(context: Context) -> MKUserTrackingButton {
let view = MKUserTrackingButton(mapView: MapRegistry[key])
updateUIView(view, context: context)
return view
}

public func updateUIView(_ view: MKUserTrackingButton, context: Context) {
if let mapView = MapRegistry[key], mapView != view.mapView {
view.mapView = mapView
}
}

}

#endif

0 comments on commit cf20314

Please sign in to comment.