mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-06-16 05:55:20 +00:00
359 lines
17 KiB
Swift
359 lines
17 KiB
Swift
import Foundation
|
|
import Postbox
|
|
import Display
|
|
import SwiftSignalKit
|
|
import WebKit
|
|
import TelegramCore
|
|
|
|
class ChatDocumentGalleryItem: GalleryItem {
|
|
let account: Account
|
|
let theme: PresentationTheme
|
|
let strings: PresentationStrings
|
|
let message: Message
|
|
let location: MessageHistoryEntryLocation?
|
|
|
|
init(account: Account, theme: PresentationTheme, strings: PresentationStrings, message: Message, location: MessageHistoryEntryLocation?) {
|
|
self.account = account
|
|
self.theme = theme
|
|
self.strings = strings
|
|
self.message = message
|
|
self.location = location
|
|
}
|
|
|
|
func node() -> GalleryItemNode {
|
|
let node = ChatDocumentGalleryItemNode(account: self.account, theme: self.theme, strings: self.strings)
|
|
|
|
for media in self.message.media {
|
|
if let file = media as? TelegramMediaFile {
|
|
node.setFile(account: account, file: file)
|
|
break
|
|
} else if let webpage = media as? TelegramMediaWebpage, case let .Loaded(content) = webpage.content {
|
|
if let file = content.file {
|
|
node.setFile(account: account, file: file)
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
if let location = self.location {
|
|
node._title.set(.single("\(location.index + 1) of \(location.count)"))
|
|
}
|
|
node.setMessage(self.message)
|
|
|
|
return node
|
|
}
|
|
|
|
func updateNode(node: GalleryItemNode) {
|
|
if let node = node as? ChatDocumentGalleryItemNode, let location = self.location {
|
|
node._title.set(.single("\(location.index + 1) of \(location.count)"))
|
|
node.setMessage(self.message)
|
|
}
|
|
}
|
|
|
|
func thumbnailItem() -> (Int64, GalleryThumbnailItem)? {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
private let registeredURLProtocol: Void = {
|
|
URLProtocol.registerClass(ChatDocumentURLProtocol.self)
|
|
}()
|
|
|
|
private final class ChatDocumentURLProtocol: URLProtocol {
|
|
override class func canonicalRequest(for request: URLRequest) -> URLRequest {
|
|
return request
|
|
}
|
|
|
|
override class func canInit(with request: URLRequest) -> Bool {
|
|
if let mainDocumentURL = request.mainDocumentURL {
|
|
if mainDocumentURL.scheme == "file" && request.url != mainDocumentURL {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
override class func requestIsCacheEquivalent(_ a: URLRequest, to b: URLRequest) -> Bool {
|
|
return super.requestIsCacheEquivalent(a, to: b)
|
|
}
|
|
|
|
override func startLoading() {
|
|
}
|
|
|
|
override func stopLoading() {
|
|
}
|
|
}
|
|
|
|
class ChatDocumentGalleryItemNode: GalleryItemNode, WKNavigationDelegate {
|
|
fileprivate let _title = Promise<String>()
|
|
|
|
private let statusNodeContainer: HighlightableButtonNode
|
|
private let statusNode: RadialStatusNode
|
|
|
|
private let webView: UIView
|
|
|
|
private var accountAndFile: (Account, TelegramMediaFile)?
|
|
private let dataDisposable = MetaDisposable()
|
|
|
|
private var itemIsVisible = false
|
|
|
|
private var message: Message?
|
|
|
|
private let footerContentNode: ChatItemGalleryFooterContentNode
|
|
|
|
private var fetchDisposable = MetaDisposable()
|
|
private let statusDisposable = MetaDisposable()
|
|
private var status: MediaResourceStatus?
|
|
|
|
init(account: Account, theme: PresentationTheme, strings: PresentationStrings) {
|
|
/*if #available(iOS 9.0, *) {
|
|
let preferences = WKPreferences()
|
|
preferences.javaScriptEnabled = false
|
|
let configuration = WKWebViewConfiguration()
|
|
configuration.preferences = preferences
|
|
let webView = WKWebView(frame: CGRect(), configuration: configuration)
|
|
webView.allowsLinkPreview = false
|
|
webView.allowsBackForwardNavigationGestures = false
|
|
self.webView = webView
|
|
} else {*/
|
|
let _ = registeredURLProtocol
|
|
let webView = UIWebView()
|
|
|
|
webView.scalesPageToFit = true
|
|
self.webView = webView
|
|
//}
|
|
self.footerContentNode = ChatItemGalleryFooterContentNode(account: account, theme: theme, strings: strings)
|
|
|
|
self.statusNodeContainer = HighlightableButtonNode()
|
|
self.statusNode = RadialStatusNode(backgroundNodeColor: UIColor(white: 0.0, alpha: 0.5))
|
|
self.statusNode.isHidden = true
|
|
|
|
super.init()
|
|
|
|
if let webView = self.webView as? WKWebView {
|
|
//webView.navigationDelegate = self
|
|
}
|
|
|
|
self.view.addSubview(self.webView)
|
|
|
|
self.statusNodeContainer.addSubnode(self.statusNode)
|
|
self.addSubnode(self.statusNodeContainer)
|
|
|
|
self.statusNodeContainer.addTarget(self, action: #selector(self.statusPressed), forControlEvents: .touchUpInside)
|
|
|
|
self.statusNodeContainer.isUserInteractionEnabled = false
|
|
}
|
|
|
|
deinit {
|
|
self.dataDisposable.dispose()
|
|
self.fetchDisposable.dispose()
|
|
self.statusDisposable.dispose()
|
|
}
|
|
|
|
override func containerLayoutUpdated(_ layout: ContainerViewLayout, navigationBarHeight: CGFloat, transition: ContainedViewLayoutTransition) {
|
|
super.containerLayoutUpdated(layout, navigationBarHeight: navigationBarHeight, transition: transition)
|
|
|
|
self.webView.frame = CGRect(origin: CGPoint(x: 0.0, y: navigationBarHeight), size: CGSize(width: layout.size.width, height: layout.size.height - navigationBarHeight - 44.0 - layout.insets(options: []).bottom))
|
|
|
|
let statusSize = CGSize(width: 50.0, height: 50.0)
|
|
transition.updateFrame(node: self.statusNodeContainer, frame: CGRect(origin: CGPoint(x: floor((layout.size.width - statusSize.width) / 2.0), y: floor((layout.size.height - statusSize.height) / 2.0)), size: statusSize))
|
|
transition.updateFrame(node: self.statusNode, frame: CGRect(origin: CGPoint(), size: statusSize))
|
|
}
|
|
|
|
fileprivate func setMessage(_ message: Message) {
|
|
self.footerContentNode.setMessage(message)
|
|
}
|
|
|
|
override func navigationStyle() -> Signal<GalleryItemNodeNavigationStyle, NoError> {
|
|
return .single(.dark)
|
|
}
|
|
|
|
func setFile(account: Account, file: TelegramMediaFile) {
|
|
let updateFile = self.accountAndFile?.1 != file
|
|
self.accountAndFile = (account, file)
|
|
if updateFile {
|
|
self.maybeLoadContent()
|
|
self.setupStatus(account: account, resource: file.resource)
|
|
}
|
|
}
|
|
|
|
private func setupStatus(account: Account, resource: MediaResource) {
|
|
self.statusDisposable.set((account.postbox.mediaBox.resourceStatus(resource)
|
|
|> deliverOnMainQueue).start(next: { [weak self] status in
|
|
if let strongSelf = self {
|
|
let previousStatus = strongSelf.status
|
|
strongSelf.status = status
|
|
switch status {
|
|
case .Remote:
|
|
strongSelf.statusNode.isHidden = false
|
|
strongSelf.statusNode.alpha = 1.0
|
|
strongSelf.statusNodeContainer.isUserInteractionEnabled = true
|
|
strongSelf.statusNode.transitionToState(.download(.white), completion: {})
|
|
case let .Fetching(isActive, progress):
|
|
strongSelf.statusNode.isHidden = false
|
|
strongSelf.statusNode.alpha = 1.0
|
|
strongSelf.statusNodeContainer.isUserInteractionEnabled = true
|
|
var actualProgress = progress
|
|
if isActive {
|
|
actualProgress = max(actualProgress, 0.027)
|
|
}
|
|
strongSelf.statusNode.transitionToState(.progress(color: .white, value: CGFloat(actualProgress), cancelEnabled: true), completion: {})
|
|
case .Local:
|
|
if let previousStatus = previousStatus, case .Fetching = previousStatus {
|
|
strongSelf.statusNode.transitionToState(.progress(color: .white, value: 1.0, cancelEnabled: true), completion: {
|
|
if let strongSelf = self {
|
|
strongSelf.statusNode.alpha = 0.0
|
|
strongSelf.statusNodeContainer.isUserInteractionEnabled = false
|
|
strongSelf.statusNode.layer.animateAlpha(from: 1.0, to: 0.0, duration: 0.2, completion: { _ in
|
|
if let strongSelf = self {
|
|
strongSelf.statusNode.transitionToState(.none, animated: false, completion: {})
|
|
}
|
|
})
|
|
}
|
|
})
|
|
} else if !strongSelf.statusNode.isHidden && !strongSelf.statusNode.alpha.isZero {
|
|
strongSelf.statusNode.alpha = 0.0
|
|
strongSelf.statusNodeContainer.isUserInteractionEnabled = false
|
|
strongSelf.statusNode.layer.animateAlpha(from: 1.0, to: 0.0, duration: 0.2, completion: { _ in
|
|
if let strongSelf = self {
|
|
strongSelf.statusNode.transitionToState(.none, animated: false, completion: {})
|
|
}
|
|
})
|
|
}
|
|
}
|
|
}
|
|
}))
|
|
}
|
|
|
|
private func maybeLoadContent() {
|
|
if let (account, file) = self.accountAndFile {
|
|
var pathExtension: String?
|
|
if let fileName = file.fileName {
|
|
pathExtension = (fileName as NSString).pathExtension
|
|
}
|
|
let data = account.postbox.mediaBox.resourceData(file.resource, pathExtension: pathExtension, option: .complete(waitUntilFetchStatus: false))
|
|
|> deliverOnMainQueue
|
|
self.dataDisposable.set(data.start(next: { [weak self] data in
|
|
if let strongSelf = self {
|
|
if data.complete {
|
|
if let webView = strongSelf.webView as? WKWebView {
|
|
if #available(iOS 9.0, *) {
|
|
webView.loadFileURL(URL(fileURLWithPath: data.path), allowingReadAccessTo: URL(fileURLWithPath: data.path))
|
|
}
|
|
} else if let webView = strongSelf.webView as? UIWebView {
|
|
webView.loadRequest(URLRequest(url: URL(fileURLWithPath: data.path)))
|
|
}
|
|
}
|
|
}
|
|
}))
|
|
}
|
|
}
|
|
|
|
/*private func unloadContent() {
|
|
self.dataDisposable.set(nil)
|
|
|
|
self.webView.stopLoading()
|
|
self.webView.loadHTMLString("<html></html>", baseURL: nil)
|
|
}*/
|
|
|
|
override func visibilityUpdated(isVisible: Bool) {
|
|
super.visibilityUpdated(isVisible: isVisible)
|
|
|
|
if self.itemIsVisible != isVisible {
|
|
self.itemIsVisible = isVisible
|
|
|
|
if isVisible {
|
|
} else {
|
|
self.fetchDisposable.set(nil)
|
|
}
|
|
}
|
|
}
|
|
|
|
override func title() -> Signal<String, NoError> {
|
|
return self._title.get()
|
|
}
|
|
|
|
override func animateIn(from node: (ASDisplayNode, () -> UIView?), addToTransitionSurface: (UIView) -> Void) {
|
|
var transformedFrame = node.0.view.convert(node.0.view.bounds, to: self.webView)
|
|
let transformedSuperFrame = node.0.view.convert(node.0.view.bounds, to: self.webView.superview)
|
|
|
|
self.webView.layer.animatePosition(from: CGPoint(x: transformedSuperFrame.midX, y: transformedSuperFrame.midY), to: self.webView.layer.position, duration: 0.25, timingFunction: kCAMediaTimingFunctionSpring)
|
|
|
|
transformedFrame.origin = CGPoint()
|
|
|
|
let transform = CATransform3DScale(self.webView.layer.transform, transformedFrame.size.width / self.webView.layer.bounds.size.width, transformedFrame.size.height / self.webView.layer.bounds.size.height, 1.0)
|
|
self.webView.layer.animate(from: NSValue(caTransform3D: transform), to: NSValue(caTransform3D: self.webView.layer.transform), keyPath: "transform", timingFunction: kCAMediaTimingFunctionSpring, duration: 0.25)
|
|
|
|
self.statusNodeContainer.layer.animatePosition(from: CGPoint(x: transformedSuperFrame.midX, y: transformedSuperFrame.midY), to: self.statusNodeContainer.position, duration: 0.25, timingFunction: kCAMediaTimingFunctionSpring)
|
|
self.statusNodeContainer.layer.animateAlpha(from: 0.0, to: 1.0, duration: 0.25, timingFunction: kCAMediaTimingFunctionSpring)
|
|
self.statusNodeContainer.layer.animateScale(from: 0.5, to: 1.0, duration: 0.25, timingFunction: kCAMediaTimingFunctionSpring)
|
|
}
|
|
|
|
override func animateOut(to node: (ASDisplayNode, () -> UIView?), addToTransitionSurface: (UIView) -> Void, completion: @escaping () -> Void) {
|
|
var transformedFrame = node.0.view.convert(node.0.view.bounds, to: self.webView)
|
|
let transformedSuperFrame = node.0.view.convert(node.0.view.bounds, to: self.webView.superview)
|
|
let transformedSelfFrame = node.0.view.convert(node.0.view.bounds, to: self.view)
|
|
let transformedCopyViewInitialFrame = self.webView.convert(self.webView.bounds, to: self.view)
|
|
|
|
var positionCompleted = false
|
|
var boundsCompleted = false
|
|
var copyCompleted = false
|
|
|
|
let copyView = node.1()!
|
|
|
|
self.view.insertSubview(copyView, belowSubview: self.webView)
|
|
copyView.frame = transformedSelfFrame
|
|
|
|
let intermediateCompletion = { [weak copyView] in
|
|
if positionCompleted && boundsCompleted && copyCompleted {
|
|
copyView?.removeFromSuperview()
|
|
completion()
|
|
}
|
|
}
|
|
|
|
copyView.layer.animateAlpha(from: 0.0, to: 1.0, duration: 0.1, removeOnCompletion: false)
|
|
|
|
copyView.layer.animatePosition(from: CGPoint(x: transformedCopyViewInitialFrame.midX, y: transformedCopyViewInitialFrame.midY), to: CGPoint(x: transformedSelfFrame.midX, y: transformedSelfFrame.midY), duration: 0.25, timingFunction: kCAMediaTimingFunctionSpring, removeOnCompletion: false)
|
|
let scale = CGSize(width: transformedCopyViewInitialFrame.size.width / transformedSelfFrame.size.width, height: transformedCopyViewInitialFrame.size.height / transformedSelfFrame.size.height)
|
|
copyView.layer.animate(from: NSValue(caTransform3D: CATransform3DMakeScale(scale.width, scale.height, 1.0)), to: NSValue(caTransform3D: CATransform3DIdentity), keyPath: "transform", timingFunction: kCAMediaTimingFunctionSpring, duration: 0.25, removeOnCompletion: false, completion: { _ in
|
|
copyCompleted = true
|
|
intermediateCompletion()
|
|
})
|
|
|
|
self.webView.layer.animatePosition(from: self.webView.layer.position, to: CGPoint(x: transformedSuperFrame.midX, y: transformedSuperFrame.midY), duration: 0.25, timingFunction: kCAMediaTimingFunctionSpring, removeOnCompletion: false, completion: { _ in
|
|
positionCompleted = true
|
|
intermediateCompletion()
|
|
})
|
|
|
|
self.webView.layer.animateAlpha(from: 1.0, to: 0.0, duration: 0.25, removeOnCompletion: false)
|
|
|
|
transformedFrame.origin = CGPoint()
|
|
|
|
let transform = CATransform3DScale(self.webView.layer.transform, transformedFrame.size.width / self.webView.layer.bounds.size.width, transformedFrame.size.height / self.webView.layer.bounds.size.height, 1.0)
|
|
self.webView.layer.animate(from: NSValue(caTransform3D: self.webView.layer.transform), to: NSValue(caTransform3D: transform), keyPath: "transform", timingFunction: kCAMediaTimingFunctionSpring, duration: 0.25, removeOnCompletion: false, completion: { _ in
|
|
boundsCompleted = true
|
|
intermediateCompletion()
|
|
})
|
|
|
|
self.statusNodeContainer.layer.animatePosition(from: self.statusNodeContainer.position, to: CGPoint(x: transformedSuperFrame.midX, y: transformedSuperFrame.midY), duration: 0.25, timingFunction: kCAMediaTimingFunctionSpring, removeOnCompletion: false)
|
|
self.statusNodeContainer.layer.animateAlpha(from: 1.0, to: 0.0, duration: 0.15, timingFunction: kCAMediaTimingFunctionEaseIn, removeOnCompletion: false)
|
|
}
|
|
|
|
override func footerContent() -> Signal<GalleryFooterContentNode?, NoError> {
|
|
return .single(self.footerContentNode)
|
|
}
|
|
|
|
@objc func statusPressed() {
|
|
if let (account, file) = self.accountAndFile, let status = self.status {
|
|
switch status {
|
|
case .Fetching:
|
|
account.postbox.mediaBox.cancelInteractiveResourceFetch(file.resource)
|
|
case .Remote:
|
|
self.fetchDisposable.set(account.postbox.mediaBox.fetchedResource(file.resource, tag: TelegramMediaResourceFetchTag(statsCategory: .generic)).start())
|
|
default:
|
|
break
|
|
}
|
|
}
|
|
}
|
|
}
|