From 5884fdff66c79219ea587650503de82d14487d69 Mon Sep 17 00:00:00 2001 From: Ilya Laktyushin Date: Sun, 13 Aug 2023 03:50:44 +0200 Subject: [PATCH] Various fixes --- submodules/Geocoding/Sources/Geocoding.swift | 35 +++++++++++++++++-- .../LocationPickerControllerNode.swift | 16 ++++++--- .../Sources/LocationViewControllerNode.swift | 3 +- 3 files changed, 46 insertions(+), 8 deletions(-) diff --git a/submodules/Geocoding/Sources/Geocoding.swift b/submodules/Geocoding/Sources/Geocoding.swift index c0745657f3..c2ca6f0ba5 100644 --- a/submodules/Geocoding/Sources/Geocoding.swift +++ b/submodules/Geocoding/Sources/Geocoding.swift @@ -1,6 +1,7 @@ import Foundation import Contacts import CoreLocation +import MapKit import SwiftSignalKit public func geocodeLocation(address: String, locale: Locale? = nil) -> Signal<[CLPlacemark]?, NoError> { @@ -69,19 +70,47 @@ public struct ReverseGeocodedPlacemark { } } + +private let regions = [ + ( + CLLocationCoordinate2D(latitude: 46.046331, longitude: 32.398307), + CLLocationCoordinate2D(latitude: 44.326515, longitude: 36.613495) + ) +] + +private func shouldDisplayActualCountryName(latitude: Double, longitude: Double) -> Bool { + let coordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude) + let point = MKMapPoint(coordinate) + for region in regions { + let p1 = MKMapPoint(region.0) + let p2 = MKMapPoint(region.1) + let rect = MKMapRect(x: min(p1.x, p2.x), y: min(p1.y, p2.y), width: abs(p1.x - p2.x), height: abs(p1.y - p2.y)) + if rect.contains(point) { + return false + } + } + return true +} + public func reverseGeocodeLocation(latitude: Double, longitude: Double, locale: Locale? = nil) -> Signal { return Signal { subscriber in let geocoder = CLGeocoder() geocoder.reverseGeocodeLocation(CLLocation(latitude: latitude, longitude: longitude), preferredLocale: locale, completionHandler: { placemarks, _ in - if let placemarks = placemarks, let placemark = placemarks.first { + if let placemarks, let placemark = placemarks.first { + var countryName = placemark.country + var countryCode = placemark.isoCountryCode + if !shouldDisplayActualCountryName(latitude: latitude, longitude: longitude) { + countryName = nil + countryCode = nil + } let result: ReverseGeocodedPlacemark if placemark.thoroughfare == nil && placemark.locality == nil && placemark.country == nil { result = ReverseGeocodedPlacemark(name: placemark.name, street: placemark.name, city: nil, country: nil, countryCode: nil) } else { if placemark.thoroughfare == nil && placemark.locality == nil, let ocean = placemark.ocean { - result = ReverseGeocodedPlacemark(name: ocean, street: nil, city: nil, country: placemark.country, countryCode: placemark.isoCountryCode) + result = ReverseGeocodedPlacemark(name: ocean, street: nil, city: nil, country: countryName, countryCode: countryCode) } else { - result = ReverseGeocodedPlacemark(name: nil, street: placemark.thoroughfare, city: placemark.locality, country: placemark.country, countryCode: placemark.isoCountryCode) + result = ReverseGeocodedPlacemark(name: nil, street: placemark.thoroughfare, city: placemark.locality, country: countryName, countryCode: countryCode) } } subscriber.putNext(result) diff --git a/submodules/LocationUI/Sources/LocationPickerControllerNode.swift b/submodules/LocationUI/Sources/LocationPickerControllerNode.swift index de57185159..7b2a393180 100644 --- a/submodules/LocationUI/Sources/LocationPickerControllerNode.swift +++ b/submodules/LocationUI/Sources/LocationPickerControllerNode.swift @@ -798,8 +798,12 @@ final class LocationPickerControllerNode: ViewControllerTracingNode, CLLocationM var cityName: String? var streetName: String? let countryCode = placemark?.countryCode - if let city = placemark?.city, let countryCode = placemark?.countryCode { - cityName = "\(city), \(displayCountryName(countryCode, locale: locale))" + if let city = placemark?.city { + if let countryCode = placemark?.countryCode { + cityName = "\(city), \(displayCountryName(countryCode, locale: locale))" + } else { + cityName = city + } } else { cityName = "" } @@ -843,8 +847,12 @@ final class LocationPickerControllerNode: ViewControllerTracingNode, CLLocationM var cityName: String? var streetName: String? let countryCode = placemark?.countryCode - if let city = placemark?.city, let countryCode = placemark?.countryCode { - cityName = "\(city), \(displayCountryName(countryCode, locale: locale))" + if let city = placemark?.city { + if let countryCode = placemark?.countryCode { + cityName = "\(city), \(displayCountryName(countryCode, locale: locale))" + } else { + cityName = city + } } else { cityName = "" } diff --git a/submodules/LocationUI/Sources/LocationViewControllerNode.swift b/submodules/LocationUI/Sources/LocationViewControllerNode.swift index 578cd30ff3..9f13a79efd 100644 --- a/submodules/LocationUI/Sources/LocationViewControllerNode.swift +++ b/submodules/LocationUI/Sources/LocationViewControllerNode.swift @@ -291,6 +291,7 @@ final class LocationViewControllerNode: ViewControllerTracingNode, CLLocationMan var eta: Signal<(ExpectedTravelTime, ExpectedTravelTime, ExpectedTravelTime), NoError> = .single((.calculating, .calculating, .calculating)) var address: Signal = .single(nil) + let locale = localeWithStrings(presentationData.strings) if let location = getLocation(from: subject), location.liveBroadcastingTimeout == nil { eta = .single((.calculating, .calculating, .calculating)) |> then(combineLatest(queue: Queue.mainQueue(), getExpectedTravelTime(coordinate: location.coordinate, transportType: .automobile), getExpectedTravelTime(coordinate: location.coordinate, transportType: .transit), getExpectedTravelTime(coordinate: location.coordinate, transportType: .walking)) @@ -313,7 +314,7 @@ final class LocationViewControllerNode: ViewControllerTracingNode, CLLocationMan } else { address = .single(nil) |> then( - reverseGeocodeLocation(latitude: location.latitude, longitude: location.longitude) + reverseGeocodeLocation(latitude: location.latitude, longitude: location.longitude, locale: locale) |> map { placemark -> String? in return placemark?.compactDisplayAddress ?? "" }