[WIP] Modern cache

This commit is contained in:
Ali
2022-12-17 00:17:31 +04:00
parent 792e0091bc
commit 614c74b0b0
189 changed files with 1464 additions and 848 deletions

View File

@@ -12,13 +12,14 @@ import GalleryUI
private struct InstantImageGalleryThumbnailItem: GalleryThumbnailItem {
let account: Account
let userLocation: MediaResourceUserLocation
let mediaReference: AnyMediaReference
func image(synchronous: Bool) -> (Signal<(TransformImageArguments) -> DrawingContext?, NoError>, CGSize) {
if let imageReferene = mediaReference.concrete(TelegramMediaImage.self), let representation = largestImageRepresentation(imageReferene.media.representations) {
return (mediaGridMessagePhoto(account: self.account, photoReference: imageReferene), representation.dimensions.cgSize)
return (mediaGridMessagePhoto(account: self.account, userLocation: self.userLocation, photoReference: imageReferene), representation.dimensions.cgSize)
} else if let fileReference = mediaReference.concrete(TelegramMediaFile.self), let dimensions = fileReference.media.dimensions {
return (mediaGridMessageVideo(postbox: account.postbox, videoReference: fileReference), dimensions.cgSize)
return (mediaGridMessageVideo(postbox: account.postbox, userLocation: self.userLocation, videoReference: fileReference), dimensions.cgSize)
} else {
return (.single({ _ in return nil }), CGSize(width: 128.0, height: 128.0))
}
@@ -42,6 +43,7 @@ class InstantImageGalleryItem: GalleryItem {
let context: AccountContext
let presentationData: PresentationData
let userLocation: MediaResourceUserLocation
let imageReference: ImageMediaReference
let caption: NSAttributedString
let credit: NSAttributedString
@@ -49,8 +51,9 @@ class InstantImageGalleryItem: GalleryItem {
let openUrl: (InstantPageUrlItem) -> Void
let openUrlOptions: (InstantPageUrlItem) -> Void
init(context: AccountContext, presentationData: PresentationData, itemId: AnyHashable, imageReference: ImageMediaReference, caption: NSAttributedString, credit: NSAttributedString, location: InstantPageGalleryEntryLocation?, openUrl: @escaping (InstantPageUrlItem) -> Void, openUrlOptions: @escaping (InstantPageUrlItem) -> Void) {
init(context: AccountContext, presentationData: PresentationData, itemId: AnyHashable, userLocation: MediaResourceUserLocation, imageReference: ImageMediaReference, caption: NSAttributedString, credit: NSAttributedString, location: InstantPageGalleryEntryLocation?, openUrl: @escaping (InstantPageUrlItem) -> Void, openUrlOptions: @escaping (InstantPageUrlItem) -> Void) {
self.itemId = itemId
self.userLocation = userLocation
self.context = context
self.presentationData = presentationData
self.imageReference = imageReference
@@ -64,7 +67,7 @@ class InstantImageGalleryItem: GalleryItem {
func node(synchronous: Bool) -> GalleryItemNode {
let node = InstantImageGalleryItemNode(context: self.context, presentationData: self.presentationData, openUrl: self.openUrl, openUrlOptions: self.openUrlOptions)
node.setImage(imageReference: self.imageReference)
node.setImage(userLocation: self.userLocation, imageReference: self.imageReference)
if let location = self.location {
node._title.set(.single(self.presentationData.strings.Items_NOfM("\(location.position + 1)", "\(location.totalCount)").string))
@@ -86,7 +89,7 @@ class InstantImageGalleryItem: GalleryItem {
}
func thumbnailItem() -> (Int64, GalleryThumbnailItem)? {
return (0, InstantImageGalleryThumbnailItem(account: self.context.account, mediaReference: imageReference.abstract))
return (0, InstantImageGalleryThumbnailItem(account: self.context.account, userLocation: self.userLocation, mediaReference: imageReference.abstract))
}
}
@@ -98,6 +101,7 @@ final class InstantImageGalleryItemNode: ZoomableContentGalleryItemNode {
fileprivate let _title = Promise<String>()
private let footerContentNode: InstantPageGalleryFooterContentNode
private var userLocation: MediaResourceUserLocation?
private var contextAndMedia: (AccountContext, AnyMediaReference)?
private var fetchDisposable = MetaDisposable()
@@ -136,14 +140,16 @@ final class InstantImageGalleryItemNode: ZoomableContentGalleryItemNode {
self.footerContentNode.setCaption(caption, credit: credit)
}
fileprivate func setImage(imageReference: ImageMediaReference) {
fileprivate func setImage(userLocation: MediaResourceUserLocation, imageReference: ImageMediaReference) {
self.userLocation = userLocation
if self.contextAndMedia == nil || !self.contextAndMedia!.1.media.isEqual(to: imageReference.media) {
if let largestSize = largestRepresentationForPhoto(imageReference.media) {
let displaySize = largestSize.dimensions.cgSize.fitted(CGSize(width: 1280.0, height: 1280.0)).dividedByScreenScale().integralFloor
self.imageNode.asyncLayout()(TransformImageArguments(corners: ImageCorners(), imageSize: displaySize, boundingSize: displaySize, intrinsicInsets: UIEdgeInsets(), emptyColor: .black))()
self.imageNode.setSignal(chatMessagePhoto(postbox: self.context.account.postbox, photoReference: imageReference), dispatchOnDisplayLink: false)
self.imageNode.setSignal(chatMessagePhoto(postbox: self.context.account.postbox, userLocation: userLocation, photoReference: imageReference), dispatchOnDisplayLink: false)
self.zoomableContent = (largestSize.dimensions.cgSize, self.imageNode)
self.fetchDisposable.set(fetchedMediaResource(mediaBox: self.context.account.postbox.mediaBox, reference: imageReference.resourceReference(largestSize.resource)).start())
self.fetchDisposable.set(fetchedMediaResource(mediaBox: self.context.account.postbox.mediaBox, userLocation: userLocation, userContentType: .image, reference: imageReference.resourceReference(largestSize.resource)).start())
} else {
self._ready.set(.single(Void()))
}
@@ -152,12 +158,14 @@ final class InstantImageGalleryItemNode: ZoomableContentGalleryItemNode {
self.footerContentNode.setShareMedia(imageReference.abstract)
}
func setFile(context: AccountContext, fileReference: FileMediaReference) {
func setFile(context: AccountContext, userLocation: MediaResourceUserLocation, fileReference: FileMediaReference) {
self.userLocation = userLocation
if self.contextAndMedia == nil || !self.contextAndMedia!.1.media.isEqual(to: fileReference.media) {
if let largestSize = fileReference.media.dimensions {
let displaySize = largestSize.cgSize.dividedByScreenScale()
self.imageNode.asyncLayout()(TransformImageArguments(corners: ImageCorners(), imageSize: displaySize, boundingSize: displaySize, intrinsicInsets: UIEdgeInsets()))()
self.imageNode.setSignal(chatMessageImageFile(account: context.account, fileReference: fileReference, thumbnail: false), dispatchOnDisplayLink: false)
self.imageNode.setSignal(chatMessageImageFile(account: context.account, userLocation: userLocation, fileReference: fileReference, thumbnail: false), dispatchOnDisplayLink: false)
self.zoomableContent = (largestSize.cgSize, self.imageNode)
} else {
self._ready.set(.single(Void()))
@@ -296,7 +304,7 @@ final class InstantImageGalleryItemNode: ZoomableContentGalleryItemNode {
if let (context, media) = self.contextAndMedia, let fileReference = media.concrete(TelegramMediaFile.self) {
if isVisible {
self.fetchDisposable.set(fetchedMediaResource(mediaBox: context.account.postbox.mediaBox, reference: fileReference.resourceReference(fileReference.media.resource)).start())
self.fetchDisposable.set(fetchedMediaResource(mediaBox: context.account.postbox.mediaBox, userLocation: self.userLocation ?? .other, userContentType: .file, reference: fileReference.resourceReference(fileReference.media.resource)).start())
} else {
self.fetchDisposable.set(nil)
}