mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-12-22 22:25:57 +00:00
Various fixes
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
import Contacts
|
import Contacts
|
||||||
import CoreLocation
|
import CoreLocation
|
||||||
|
import MapKit
|
||||||
import SwiftSignalKit
|
import SwiftSignalKit
|
||||||
|
|
||||||
public func geocodeLocation(address: String, locale: Locale? = nil) -> Signal<[CLPlacemark]?, NoError> {
|
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<ReverseGeocodedPlacemark?, NoError> {
|
public func reverseGeocodeLocation(latitude: Double, longitude: Double, locale: Locale? = nil) -> Signal<ReverseGeocodedPlacemark?, NoError> {
|
||||||
return Signal { subscriber in
|
return Signal { subscriber in
|
||||||
let geocoder = CLGeocoder()
|
let geocoder = CLGeocoder()
|
||||||
geocoder.reverseGeocodeLocation(CLLocation(latitude: latitude, longitude: longitude), preferredLocale: locale, completionHandler: { placemarks, _ in
|
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
|
let result: ReverseGeocodedPlacemark
|
||||||
if placemark.thoroughfare == nil && placemark.locality == nil && placemark.country == nil {
|
if placemark.thoroughfare == nil && placemark.locality == nil && placemark.country == nil {
|
||||||
result = ReverseGeocodedPlacemark(name: placemark.name, street: placemark.name, city: nil, country: nil, countryCode: nil)
|
result = ReverseGeocodedPlacemark(name: placemark.name, street: placemark.name, city: nil, country: nil, countryCode: nil)
|
||||||
} else {
|
} else {
|
||||||
if placemark.thoroughfare == nil && placemark.locality == nil, let ocean = placemark.ocean {
|
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 {
|
} 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)
|
subscriber.putNext(result)
|
||||||
|
|||||||
@@ -798,8 +798,12 @@ final class LocationPickerControllerNode: ViewControllerTracingNode, CLLocationM
|
|||||||
var cityName: String?
|
var cityName: String?
|
||||||
var streetName: String?
|
var streetName: String?
|
||||||
let countryCode = placemark?.countryCode
|
let countryCode = placemark?.countryCode
|
||||||
if let city = placemark?.city, let countryCode = placemark?.countryCode {
|
if let city = placemark?.city {
|
||||||
|
if let countryCode = placemark?.countryCode {
|
||||||
cityName = "\(city), \(displayCountryName(countryCode, locale: locale))"
|
cityName = "\(city), \(displayCountryName(countryCode, locale: locale))"
|
||||||
|
} else {
|
||||||
|
cityName = city
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
cityName = ""
|
cityName = ""
|
||||||
}
|
}
|
||||||
@@ -843,8 +847,12 @@ final class LocationPickerControllerNode: ViewControllerTracingNode, CLLocationM
|
|||||||
var cityName: String?
|
var cityName: String?
|
||||||
var streetName: String?
|
var streetName: String?
|
||||||
let countryCode = placemark?.countryCode
|
let countryCode = placemark?.countryCode
|
||||||
if let city = placemark?.city, let countryCode = placemark?.countryCode {
|
if let city = placemark?.city {
|
||||||
|
if let countryCode = placemark?.countryCode {
|
||||||
cityName = "\(city), \(displayCountryName(countryCode, locale: locale))"
|
cityName = "\(city), \(displayCountryName(countryCode, locale: locale))"
|
||||||
|
} else {
|
||||||
|
cityName = city
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
cityName = ""
|
cityName = ""
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -291,6 +291,7 @@ final class LocationViewControllerNode: ViewControllerTracingNode, CLLocationMan
|
|||||||
var eta: Signal<(ExpectedTravelTime, ExpectedTravelTime, ExpectedTravelTime), NoError> = .single((.calculating, .calculating, .calculating))
|
var eta: Signal<(ExpectedTravelTime, ExpectedTravelTime, ExpectedTravelTime), NoError> = .single((.calculating, .calculating, .calculating))
|
||||||
var address: Signal<String?, NoError> = .single(nil)
|
var address: Signal<String?, NoError> = .single(nil)
|
||||||
|
|
||||||
|
let locale = localeWithStrings(presentationData.strings)
|
||||||
if let location = getLocation(from: subject), location.liveBroadcastingTimeout == nil {
|
if let location = getLocation(from: subject), location.liveBroadcastingTimeout == nil {
|
||||||
eta = .single((.calculating, .calculating, .calculating))
|
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))
|
|> 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 {
|
} else {
|
||||||
address = .single(nil)
|
address = .single(nil)
|
||||||
|> then(
|
|> then(
|
||||||
reverseGeocodeLocation(latitude: location.latitude, longitude: location.longitude)
|
reverseGeocodeLocation(latitude: location.latitude, longitude: location.longitude, locale: locale)
|
||||||
|> map { placemark -> String? in
|
|> map { placemark -> String? in
|
||||||
return placemark?.compactDisplayAddress ?? ""
|
return placemark?.compactDisplayAddress ?? ""
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user