From d98ab44ddacfe1eab115a9df80608101faa6f09c Mon Sep 17 00:00:00 2001 From: Greg Bolsinga Date: Mon, 4 Dec 2023 16:32:21 -0800 Subject: [PATCH] tvOS will just geocode hand-generated strings (#752) - Since it cannot use the Contacts framework. --- Sources/Site/Music/Atlas.swift | 4 ++++ .../Site/Music/CNPostalAddress+Geocode.swift | 6 ------ Sources/Site/Music/Location+Contacts.swift | 19 +++++++++++++++---- Sources/Site/Music/Location+Geocode.swift | 3 +-- Sources/Site/Music/String+Geocode.swift | 17 +++++++++++++++++ 5 files changed, 37 insertions(+), 12 deletions(-) create mode 100644 Sources/Site/Music/String+Geocode.swift diff --git a/Sources/Site/Music/Atlas.swift b/Sources/Site/Music/Atlas.swift index 32af798b..eaa06604 100644 --- a/Sources/Site/Music/Atlas.swift +++ b/Sources/Site/Music/Atlas.swift @@ -13,6 +13,10 @@ extension Logger { static let atlas = Logger(category: "atlas") } +enum GeocodeError: Error { + case noPlacemark +} + protocol Geocodable { func geocode() async throws -> CLPlacemark } diff --git a/Sources/Site/Music/CNPostalAddress+Geocode.swift b/Sources/Site/Music/CNPostalAddress+Geocode.swift index d2e52d5e..a5996051 100644 --- a/Sources/Site/Music/CNPostalAddress+Geocode.swift +++ b/Sources/Site/Music/CNPostalAddress+Geocode.swift @@ -9,14 +9,8 @@ import CoreLocation #if canImport(Contacts) import Contacts -#endif -#if canImport(Contacts) extension CNPostalAddress: Geocodable { - private enum GeocodeError: Error { - case noPlacemark - } - func geocode() async throws -> CLPlacemark { guard let placemark = try await CLGeocoder().geocodePostalAddress(self).first else { throw GeocodeError.noPlacemark diff --git a/Sources/Site/Music/Location+Contacts.swift b/Sources/Site/Music/Location+Contacts.swift index 1ccf90c6..0455a5d6 100644 --- a/Sources/Site/Music/Location+Contacts.swift +++ b/Sources/Site/Music/Location+Contacts.swift @@ -5,11 +5,14 @@ // Created by Greg Bolsinga on 2/27/23. // +import Foundation + #if canImport(Contacts) import Contacts - import Foundation +#endif - extension Location { +extension Location { + #if canImport(Contacts) var postalAddress: CNPostalAddress { let pAddress = CNMutablePostalAddress() pAddress.city = city @@ -24,5 +27,13 @@ // Note this requests access to Contacts, despite this not reading any contacts. CNPostalAddressFormatter().string(from: postalAddress) } - } -#endif + #else + var addressString: String { + let cityState = "\(city) \(state)" + if let street { + return "\(street)\n\(cityState)" + } + return cityState + } + #endif +} diff --git a/Sources/Site/Music/Location+Geocode.swift b/Sources/Site/Music/Location+Geocode.swift index 65ed11ed..ba9fc499 100644 --- a/Sources/Site/Music/Location+Geocode.swift +++ b/Sources/Site/Music/Location+Geocode.swift @@ -17,8 +17,7 @@ extension Location: AtlasGeocodable { #if canImport(Contacts) try await postalAddress.geocode() #else - // Temporary... - MKMapItem.forCurrentLocation().placemark + try await addressString.geocode() #endif } } diff --git a/Sources/Site/Music/String+Geocode.swift b/Sources/Site/Music/String+Geocode.swift new file mode 100644 index 00000000..07a72bed --- /dev/null +++ b/Sources/Site/Music/String+Geocode.swift @@ -0,0 +1,17 @@ +// +// String+Geocode.swift +// +// +// Created by Greg Bolsinga on 12/4/23. +// + +import CoreLocation + +extension String: Geocodable { + func geocode() async throws -> CLPlacemark { + guard let placemark = try await CLGeocoder().geocodeAddressString(self).first else { + throw GeocodeError.noPlacemark + } + return placemark + } +}