mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-06-16 05:55:20 +00:00
128 lines
5.1 KiB
Swift
128 lines
5.1 KiB
Swift
import Foundation
|
|
import LegacyComponents
|
|
import Postbox
|
|
import TelegramCore
|
|
import SwiftSignalKit
|
|
import Display
|
|
|
|
private let sharedImageCache = TGMemoryImageCache(softMemoryLimit: 2 * 1024 * 1024, hardMemoryLimit: 3 * 1024 * 1024)!
|
|
|
|
private let placeholderImage = generateFilledCircleImage(diameter: 40.0, color: UIColor(rgb: 0xf2f2f2))
|
|
|
|
private final class LegacyLocationVenueIconTask: NSObject {
|
|
private let disposable = DisposableSet()
|
|
|
|
init(account: Account, url: String, completion: @escaping (Data?) -> Void) {
|
|
super.init()
|
|
|
|
let resource = HttpReferenceMediaResource(url: url, size: nil)
|
|
self.disposable.add(account.postbox.mediaBox.resourceData(resource).start(next: { data in
|
|
if data.complete {
|
|
if let loadedData = try? Data(contentsOf: URL(fileURLWithPath: data.path)) {
|
|
completion(loadedData)
|
|
}
|
|
}
|
|
}))
|
|
self.disposable.add(account.postbox.mediaBox.fetchedResource(resource, tag: nil).start())
|
|
}
|
|
|
|
deinit {
|
|
self.disposable.dispose()
|
|
}
|
|
|
|
func cancel() {
|
|
self.disposable.dispose()
|
|
}
|
|
}
|
|
|
|
final class LegacyLocationVenueIconDataSource: TGImageDataSource {
|
|
private let account: () -> Account?
|
|
|
|
init(account: @escaping () -> Account?) {
|
|
self.account = account
|
|
|
|
super.init()
|
|
}
|
|
|
|
override func canHandleUri(_ uri: String!) -> Bool {
|
|
if let uri = uri {
|
|
if uri.hasPrefix("location-venue-icon://") {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
override func loadAttributeSync(forUri uri: String!, attribute: String!) -> Any! {
|
|
if attribute == "placeholder" {
|
|
return placeholderImage
|
|
}
|
|
return nil
|
|
}
|
|
|
|
override func loadDataSync(withUri uri: String!, canWait: Bool, acceptPartialData: Bool, asyncTaskId: AutoreleasingUnsafeMutablePointer<AnyObject?>!, progress: ((Float) -> Void)!, partialCompletion: ((TGDataResource?) -> Void)!, completion: ((TGDataResource?) -> Void)!) -> TGDataResource! {
|
|
if let image = sharedImageCache.image(forKey: uri, attributes: nil) {
|
|
return TGDataResource(image: image, decoded: true)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
override func loadDataAsync(withUri uri: String!, progress: ((Float) -> Void)!, partialCompletion: ((TGDataResource?) -> Void)!, completion: ((TGDataResource?) -> Void)!) -> Any! {
|
|
if let account = self.account() {
|
|
let args: [AnyHashable : Any]
|
|
let argumentsString = uri.substring(from: uri.index(uri.startIndex, offsetBy: "location-venue-icon://".characters.count))
|
|
args = TGStringUtils.argumentDictionary(inUrlString: argumentsString)!
|
|
|
|
guard let width = Int((args["width"] as! String)), width > 1 else {
|
|
return nil
|
|
}
|
|
guard let height = Int((args["height"] as! String)), height > 1 else {
|
|
return nil
|
|
}
|
|
|
|
guard let url = args["url"] as? String else {
|
|
return nil
|
|
}
|
|
|
|
let size = CGSize(width: CGFloat(width), height: CGFloat(height))
|
|
|
|
return LegacyLocationVenueIconTask(account: account, url: url, completion: { data in
|
|
if let data = data, let iconSourceImage = UIImage(data: data) {
|
|
UIGraphicsBeginImageContextWithOptions(iconSourceImage.size, false, iconSourceImage.scale)
|
|
var context = UIGraphicsGetCurrentContext()!
|
|
iconSourceImage.draw(at: CGPoint())
|
|
context.setBlendMode(.sourceAtop)
|
|
context.setFillColor(UIColor(rgb: 0xa0a0a0).cgColor)
|
|
context.fill(CGRect(origin: CGPoint(), size: iconSourceImage.size))
|
|
|
|
let tintedIconImage = UIGraphicsGetImageFromCurrentImageContext()
|
|
UIGraphicsEndImageContext()
|
|
|
|
UIGraphicsBeginImageContextWithOptions(size, false, 0.0)
|
|
context = UIGraphicsGetCurrentContext()!
|
|
context.setFillColor(UIColor(rgb: 0xf2f2f2).cgColor)
|
|
context.fillEllipse(in: CGRect(origin: CGPoint(), size: size))
|
|
let imageRect = CGRect(x: 4.0, y: 4.0, width: 32.0, height: 32.0)
|
|
tintedIconImage?.draw(in: imageRect)
|
|
|
|
let iconImage = UIGraphicsGetImageFromCurrentImageContext();
|
|
UIGraphicsEndImageContext()
|
|
|
|
if let iconImage = iconImage {
|
|
sharedImageCache.setImage(iconImage, forKey: uri, attributes: nil)
|
|
completion?(TGDataResource(image: iconImage, decoded: true))
|
|
}
|
|
}
|
|
})
|
|
} else {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
override func cancelTask(byId taskId: Any!) {
|
|
if let disposable = taskId as? LegacyLocationVenueIconTask {
|
|
disposable.cancel()
|
|
}
|
|
}
|
|
}
|