mirror of
https://github.com/TelegramMessenger/Telegram-iOS.git
synced 2026-07-06 03:33:41 +02:00
Refactor TelegramNotices, TelegramPermissions, PhotoResources, ImageTransparency, TinyThumbnail
This commit is contained in:
parent
1a1a1a890a
commit
55b0df2ff7
121 changed files with 3449 additions and 249 deletions
|
|
@ -13,6 +13,8 @@
|
|||
D084FA1222F436D6004874CE /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D084FA1122F436D6004874CE /* Foundation.framework */; };
|
||||
D084FA1422F436DA004874CE /* Postbox.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D084FA1322F436DA004874CE /* Postbox.framework */; };
|
||||
D084FA1622F436DE004874CE /* SwiftSignalKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D084FA1522F436DE004874CE /* SwiftSignalKit.framework */; };
|
||||
D0C9C0D322FE3E7500FAB518 /* MapResources.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0C9C0D222FE3E7500FAB518 /* MapResources.swift */; };
|
||||
D0C9C0D522FE3E8200FAB518 /* MapKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D0C9C0D422FE3E8200FAB518 /* MapKit.framework */; };
|
||||
/* End PBXBuildFile section */
|
||||
|
||||
/* Begin PBXFileReference section */
|
||||
|
|
@ -24,6 +26,8 @@
|
|||
D084FA1122F436D6004874CE /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
|
||||
D084FA1322F436DA004874CE /* Postbox.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = Postbox.framework; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
D084FA1522F436DE004874CE /* SwiftSignalKit.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = SwiftSignalKit.framework; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
D0C9C0D222FE3E7500FAB518 /* MapResources.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MapResources.swift; sourceTree = "<group>"; };
|
||||
D0C9C0D422FE3E8200FAB518 /* MapKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = MapKit.framework; path = System/Library/Frameworks/MapKit.framework; sourceTree = SDKROOT; };
|
||||
/* End PBXFileReference section */
|
||||
|
||||
/* Begin PBXFrameworksBuildPhase section */
|
||||
|
|
@ -31,6 +35,7 @@
|
|||
isa = PBXFrameworksBuildPhase;
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
D0C9C0D522FE3E8200FAB518 /* MapKit.framework in Frameworks */,
|
||||
D084FA1622F436DE004874CE /* SwiftSignalKit.framework in Frameworks */,
|
||||
D084FA1422F436DA004874CE /* Postbox.framework in Frameworks */,
|
||||
D084FA1222F436D6004874CE /* Foundation.framework in Frameworks */,
|
||||
|
|
@ -62,6 +67,7 @@
|
|||
D084F9FF22F435FD004874CE /* Sources */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
D0C9C0D222FE3E7500FAB518 /* MapResources.swift */,
|
||||
D084FA0C22F436C9004874CE /* CachedResourceRepresentations.swift */,
|
||||
D084FA0022F435FD004874CE /* MediaResources.h */,
|
||||
);
|
||||
|
|
@ -71,6 +77,7 @@
|
|||
D084FA0E22F436D2004874CE /* Frameworks */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
D0C9C0D422FE3E8200FAB518 /* MapKit.framework */,
|
||||
D084FA1522F436DE004874CE /* SwiftSignalKit.framework */,
|
||||
D084FA1322F436DA004874CE /* Postbox.framework */,
|
||||
D084FA1122F436D6004874CE /* Foundation.framework */,
|
||||
|
|
@ -159,6 +166,7 @@
|
|||
isa = PBXSourcesBuildPhase;
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
D0C9C0D322FE3E7500FAB518 /* MapResources.swift in Sources */,
|
||||
D084FA0D22F436C9004874CE /* CachedResourceRepresentations.swift in Sources */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
|
|
|
|||
117
submodules/MediaResources/Sources/MapResources.swift
Normal file
117
submodules/MediaResources/Sources/MapResources.swift
Normal file
|
|
@ -0,0 +1,117 @@
|
|||
import Foundation
|
||||
import UIKit
|
||||
import Postbox
|
||||
import TelegramCore
|
||||
import MapKit
|
||||
import SwiftSignalKit
|
||||
|
||||
public struct MapSnapshotMediaResourceId: MediaResourceId {
|
||||
public let latitude: Double
|
||||
public let longitude: Double
|
||||
public let width: Int32
|
||||
public let height: Int32
|
||||
|
||||
public var uniqueId: String {
|
||||
return "map-\(latitude)-\(longitude)-\(width)x\(height)"
|
||||
}
|
||||
|
||||
public var hashValue: Int {
|
||||
return self.uniqueId.hashValue
|
||||
}
|
||||
|
||||
public func isEqual(to: MediaResourceId) -> Bool {
|
||||
if let to = to as? MapSnapshotMediaResourceId {
|
||||
return self.latitude == to.latitude && self.longitude == to.longitude && self.width == to.width && self.height == to.height
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class MapSnapshotMediaResource: TelegramMediaResource {
|
||||
public let latitude: Double
|
||||
public let longitude: Double
|
||||
public let width: Int32
|
||||
public let height: Int32
|
||||
|
||||
public init(latitude: Double, longitude: Double, width: Int32, height: Int32) {
|
||||
self.latitude = latitude
|
||||
self.longitude = longitude
|
||||
self.width = width
|
||||
self.height = height
|
||||
}
|
||||
|
||||
public required init(decoder: PostboxDecoder) {
|
||||
self.latitude = decoder.decodeDoubleForKey("lt", orElse: 0.0)
|
||||
self.longitude = decoder.decodeDoubleForKey("ln", orElse: 0.0)
|
||||
self.width = decoder.decodeInt32ForKey("w", orElse: 0)
|
||||
self.height = decoder.decodeInt32ForKey("h", orElse: 0)
|
||||
}
|
||||
|
||||
public func encode(_ encoder: PostboxEncoder) {
|
||||
encoder.encodeDouble(self.latitude, forKey: "lt")
|
||||
encoder.encodeDouble(self.longitude, forKey: "ln")
|
||||
encoder.encodeInt32(self.width, forKey: "w")
|
||||
encoder.encodeInt32(self.height, forKey: "h")
|
||||
}
|
||||
|
||||
public var id: MediaResourceId {
|
||||
return MapSnapshotMediaResourceId(latitude: self.latitude, longitude: self.longitude, width: self.width, height: self.height)
|
||||
}
|
||||
|
||||
public func isEqual(to: MediaResource) -> Bool {
|
||||
if let to = to as? MapSnapshotMediaResource {
|
||||
return self.latitude == to.latitude && self.longitude == to.longitude && self.width == to.width && self.height == to.height
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let TGGoogleMapsOffset: Int = 268435456
|
||||
let TGGoogleMapsRadius = Double(TGGoogleMapsOffset) / Double.pi
|
||||
|
||||
private func yToLatitude(_ y: Int) -> Double {
|
||||
return ((Double.pi / 2.0) - 2 * atan(exp((Double(y - TGGoogleMapsOffset)) / TGGoogleMapsRadius))) * 180.0 / Double.pi;
|
||||
}
|
||||
|
||||
private func latitudeToY(_ latitude: Double) -> Int {
|
||||
return Int(round(Double(TGGoogleMapsOffset) - TGGoogleMapsRadius * log((1.0 + sin(latitude * Double.pi / 180.0)) / (1.0 - sin(latitude * Double.pi / 180.0))) / 2.0))
|
||||
}
|
||||
|
||||
private func adjustGMapLatitude(_ latitude: Double, offset: Int, zoom: Int) -> Double {
|
||||
let t: Int = (offset << (21 - zoom))
|
||||
return yToLatitude(latitudeToY(latitude) + t)
|
||||
}
|
||||
|
||||
public func fetchMapSnapshotResource(resource: MapSnapshotMediaResource) -> Signal<MediaResourceDataFetchResult, MediaResourceDataFetchError> {
|
||||
return Signal { subscriber in
|
||||
let disposable = MetaDisposable()
|
||||
|
||||
Queue.concurrentDefaultQueue().async {
|
||||
let options = MKMapSnapshotter.Options()
|
||||
let latitude = adjustGMapLatitude(resource.latitude, offset: -10, zoom: 15)
|
||||
options.region = MKCoordinateRegion(center: CLLocationCoordinate2DMake(latitude, resource.longitude), span: MKCoordinateSpan(latitudeDelta: 0.003, longitudeDelta: 0.003))
|
||||
options.mapType = .standard
|
||||
options.showsPointsOfInterest = false
|
||||
options.showsBuildings = true
|
||||
options.size = CGSize(width: CGFloat(resource.width + 1), height: CGFloat(resource.height + 24))
|
||||
options.scale = 2.0
|
||||
let snapshotter = MKMapSnapshotter(options: options)
|
||||
snapshotter.start(with: DispatchQueue.global(), completionHandler: { result, error in
|
||||
if let image = result?.image {
|
||||
if let data = image.jpegData(compressionQuality: 0.9) {
|
||||
subscriber.putNext(MediaResourceDataFetchResult.dataPart(resourceOffset: 0, data: data, range: 0 ..< data.count, complete: true))
|
||||
subscriber.putCompletion()
|
||||
}
|
||||
}
|
||||
})
|
||||
disposable.set(ActionDisposable {
|
||||
snapshotter.cancel()
|
||||
})
|
||||
}
|
||||
|
||||
return disposable
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue