Skip to content

Commit

Permalink
tvOS will just geocode hand-generated strings (#752)
Browse files Browse the repository at this point in the history
- Since it cannot use the Contacts framework.
  • Loading branch information
bolsinga authored Dec 5, 2023
1 parent cb192af commit d98ab44
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 12 deletions.
4 changes: 4 additions & 0 deletions Sources/Site/Music/Atlas.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ extension Logger {
static let atlas = Logger(category: "atlas")
}

enum GeocodeError: Error {
case noPlacemark
}

protocol Geocodable {
func geocode() async throws -> CLPlacemark
}
Expand Down
6 changes: 0 additions & 6 deletions Sources/Site/Music/CNPostalAddress+Geocode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 15 additions & 4 deletions Sources/Site/Music/Location+Contacts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
3 changes: 1 addition & 2 deletions Sources/Site/Music/Location+Geocode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ extension Location: AtlasGeocodable {
#if canImport(Contacts)
try await postalAddress.geocode()
#else
// Temporary...
MKMapItem.forCurrentLocation().placemark
try await addressString.geocode()
#endif
}
}
17 changes: 17 additions & 0 deletions Sources/Site/Music/String+Geocode.swift
Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit d98ab44

Please sign in to comment.