diff --git a/TelegramUI/ChatMessageInteractiveMediaNode.swift b/TelegramUI/ChatMessageInteractiveMediaNode.swift index 85567a070f..a910baf55c 100644 --- a/TelegramUI/ChatMessageInteractiveMediaNode.swift +++ b/TelegramUI/ChatMessageInteractiveMediaNode.swift @@ -474,7 +474,7 @@ final class ChatMessageInteractiveMediaNode: ASDisplayNode { case let .file(file, _): let representations: [ImageRepresentationWithReference] = file.previewRepresentations.map({ ImageRepresentationWithReference(representation: $0, reference: AnyMediaReference.message(message: MessageReference(message), media: file).resourceReference($0.resource)) }) if file.mimeType == "image/png" { - return patternWallpaperImage(account: context.account, representations: representations, mode: .thumbnail) + return patternWallpaperImage(account: context.account, accountManager: context.sharedContext.accountManager, representations: representations, mode: .thumbnail) } else { return wallpaperImage(account: context.account, accountManager: context.sharedContext.accountManager, fileReference: FileMediaReference.message(message: MessageReference(message), media: file), representations: representations, alwaysShowThumbnailFirst: false, thumbnail: true, autoFetchFullSize: true) } diff --git a/TelegramUI/FetchVideoMediaResource.swift b/TelegramUI/FetchVideoMediaResource.swift index 91c9f80745..d2054514c1 100644 --- a/TelegramUI/FetchVideoMediaResource.swift +++ b/TelegramUI/FetchVideoMediaResource.swift @@ -143,9 +143,9 @@ public func fetchVideoLibraryMediaResource(postbox: Postbox, resource: VideoLibr if let result = next as? TGMediaVideoConversionResult { var value = stat() if stat(result.fileURL.path, &value) == 0 { - if config.remuxToFMp4 { + if false && config.remuxToFMp4 { let tempFile = TempBox.shared.tempFile(fileName: "video.mp4") - if false && FFMpegRemuxer.remux(result.fileURL.path, to: tempFile.path) { + if FFMpegRemuxer.remux(result.fileURL.path, to: tempFile.path) { let _ = try? FileManager.default.removeItem(atPath: result.fileURL.path) subscriber.putNext(.moveTempFile(file: tempFile)) } else { @@ -228,9 +228,9 @@ func fetchLocalFileVideoMediaResource(postbox: Postbox, resource: LocalFileVideo if let result = next as? TGMediaVideoConversionResult { var value = stat() if stat(result.fileURL.path, &value) == 0 { - if config.remuxToFMp4 { + if false && config.remuxToFMp4 { let tempFile = TempBox.shared.tempFile(fileName: "video.mp4") - if false && FFMpegRemuxer.remux(result.fileURL.path, to: tempFile.path) { + if FFMpegRemuxer.remux(result.fileURL.path, to: tempFile.path) { let _ = try? FileManager.default.removeItem(atPath: result.fileURL.path) subscriber.putNext(.moveTempFile(file: tempFile)) } else { diff --git a/TelegramUI/SettingsThemeWallpaperNode.swift b/TelegramUI/SettingsThemeWallpaperNode.swift index c5f79cc336..d3d3c612c8 100644 --- a/TelegramUI/SettingsThemeWallpaperNode.swift +++ b/TelegramUI/SettingsThemeWallpaperNode.swift @@ -121,7 +121,7 @@ final class SettingsThemeWallpaperNode: ASDisplayNode { } self.backgroundNode.backgroundColor = patternColor self.color = patternColor - imageSignal = patternWallpaperImage(account: context.account, representations: convertedRepresentations, mode: .thumbnail, autoFetchFullSize: true) + imageSignal = patternWallpaperImage(account: context.account, accountManager: context.sharedContext.accountManager, representations: convertedRepresentations, mode: .thumbnail, autoFetchFullSize: true) } else { self.backgroundNode.isHidden = true diff --git a/TelegramUI/ThemeGridController.swift b/TelegramUI/ThemeGridController.swift index 1c92801a23..1ac0b89042 100644 --- a/TelegramUI/ThemeGridController.swift +++ b/TelegramUI/ThemeGridController.swift @@ -457,10 +457,9 @@ final class ThemeGridController: ViewController { } if mode.contains(.blur) { - let _ = self.context.sharedContext.accountManager.mediaBox.cachedResourceRepresentation(resource, representation: CachedBlurredWallpaperRepresentation(), complete: true, fetch: true).start(completed: { - apply() - }) - let _ = self.context.account.postbox.mediaBox.cachedResourceRepresentation(resource, representation: CachedBlurredWallpaperRepresentation(), complete: true, fetch: true).start(completed: { + let representation = CachedBlurredWallpaperRepresentation() + let _ = self.context.account.postbox.mediaBox.cachedResourceRepresentation(resource, representation: representation, complete: true, fetch: true).start() + let _ = self.context.sharedContext.accountManager.mediaBox.cachedResourceRepresentation(resource, representation: representation, complete: true, fetch: true).start(completed: { apply() }) } else { diff --git a/TelegramUI/WallpaperGalleryController.swift b/TelegramUI/WallpaperGalleryController.swift index e7c72f386d..353289a319 100644 --- a/TelegramUI/WallpaperGalleryController.swift +++ b/TelegramUI/WallpaperGalleryController.swift @@ -382,8 +382,11 @@ class WallpaperGalleryController: ViewController { self?.dismiss(forceAway: true) }) - if case .wallpaper = strongSelf.source { - let _ = saveWallpaper(account: strongSelf.context.account, wallpaper: wallpaper).start() + switch strongSelf.source { + case .wallpaper, .slug: + let _ = saveWallpaper(account: strongSelf.context.account, wallpaper: wallpaper).start() + default: + break } let _ = installWallpaper(account: strongSelf.context.account, wallpaper: wallpaper).start() } @@ -391,18 +394,33 @@ class WallpaperGalleryController: ViewController { let applyWallpaper: (TelegramWallpaper) -> Void = { wallpaper in if options.contains(.blur) { if let resource = resource { - let _ = strongSelf.context.account.postbox.mediaBox.cachedResourceRepresentation(resource, representation: CachedBlurredWallpaperRepresentation(), complete: true, fetch: true).start(completed: { + let representation = CachedBlurredWallpaperRepresentation() + let _ = strongSelf.context.account.postbox.mediaBox.cachedResourceRepresentation(resource, representation: representation, complete: true, fetch: true).start() + + if let path = strongSelf.context.account.postbox.mediaBox.completedResourcePath(resource), let data = try? Data(contentsOf: URL(fileURLWithPath: path), options: .mappedRead) { + strongSelf.context.sharedContext.accountManager.mediaBox.storeResourceData(resource.id, data: data) + let _ = strongSelf.context.sharedContext.accountManager.mediaBox.cachedResourceRepresentation(resource, representation: representation, complete: true, fetch: true).start(completed: { + completion(wallpaper) + }) + } + } + } else if case let .file(file) = wallpaper { + if file.isPattern, let color = file.settings.color, let intensity = file.settings.intensity { + let representation = CachedPatternWallpaperRepresentation(color: color, intensity: intensity) + let _ = strongSelf.context.account.postbox.mediaBox.cachedResourceRepresentation(file.file.resource, representation: representation, complete: true, fetch: true).start() + + if let path = strongSelf.context.account.postbox.mediaBox.completedResourcePath(file.file.resource), let data = try? Data(contentsOf: URL(fileURLWithPath: path), options: .mappedRead) { + strongSelf.context.sharedContext.accountManager.mediaBox.storeResourceData(file.file.resource.id, data: data) + let _ = strongSelf.context.sharedContext.accountManager.mediaBox.cachedResourceRepresentation(file.file.resource, representation: representation, complete: true, fetch: true).start(completed: { + completion(wallpaper) + }) + } + } else if let path = strongSelf.context.account.postbox.mediaBox.completedResourcePath(file.file.resource), let data = try? Data(contentsOf: URL(fileURLWithPath: path), options: .mappedRead) { + strongSelf.context.sharedContext.accountManager.mediaBox.storeResourceData(file.file.resource.id, data: data) completion(wallpaper) - }) } } else { - if case let .file(file) = wallpaper, file.isPattern, let color = file.settings.color, let intensity = file.settings.intensity { - let _ = strongSelf.context.account.postbox.mediaBox.cachedResourceRepresentation(file.file.resource, representation: CachedPatternWallpaperRepresentation(color: color, intensity: intensity), complete: true, fetch: true).start(completed: { - completion(wallpaper) - }) - } else { - completion(wallpaper) - } + completion(wallpaper) } } diff --git a/TelegramUI/WallpaperGalleryItem.swift b/TelegramUI/WallpaperGalleryItem.swift index 0207afa837..ac7afec5af 100644 --- a/TelegramUI/WallpaperGalleryItem.swift +++ b/TelegramUI/WallpaperGalleryItem.swift @@ -265,7 +265,7 @@ final class WallpaperGalleryItemNode: GalleryItemNode { self.colorPreview = self.arguments.colorPreview - signal = patternWallpaperImage(account: context.account, representations: convertedRepresentations, mode: self.arguments.colorPreview ? .fastScreen : .screen, autoFetchFullSize: true) + signal = patternWallpaperImage(account: context.account, accountManager: context.sharedContext.accountManager, representations: convertedRepresentations, mode: self.arguments.colorPreview ? .fastScreen : .screen, autoFetchFullSize: true) colorSignal = chatServiceBackgroundColor(wallpaper: wallpaper, mediaBox: context.account.postbox.mediaBox) isBlurrable = false diff --git a/TelegramUI/WallpaperResources.swift b/TelegramUI/WallpaperResources.swift index a3d0602724..d4f1400f88 100644 --- a/TelegramUI/WallpaperResources.swift +++ b/TelegramUI/WallpaperResources.swift @@ -11,15 +11,18 @@ private func wallpaperDatas(account: Account, accountManager: AccountManager, fi let maybeFullSize: Signal if thumbnail, let file = fileReference?.media { maybeFullSize = combineLatest(accountManager.mediaBox.cachedResourceRepresentation(file.resource, representation: CachedScaledImageRepresentation(size: CGSize(width: 720.0, height: 720.0), mode: .aspectFit), complete: false, fetch: false, attemptSynchronously: synchronousLoad), account.postbox.mediaBox.cachedResourceRepresentation(file.resource, representation: CachedScaledImageRepresentation(size: CGSize(width: 720.0, height: 720.0), mode: .aspectFit), complete: false, fetch: false, attemptSynchronously: synchronousLoad)) - |> mapToSignal { meybeSharedData, maybeData -> Signal in - if meybeSharedData.complete { - return .single(meybeSharedData) + |> mapToSignal { maybeSharedData, maybeData -> Signal in + if maybeSharedData.complete { + return .single(maybeSharedData) } else if maybeData.complete { return .single(maybeData) } else { - return account.postbox.mediaBox.resourceData(file.resource, attemptSynchronously: synchronousLoad) - |> mapToSignal { maybeData -> Signal in - if maybeData.complete { + return combineLatest(accountManager.mediaBox.resourceData(file.resource), account.postbox.mediaBox.resourceData(file.resource)) + |> mapToSignal { maybeSharedData, maybeData -> Signal in + if maybeSharedData.complete { + return accountManager.mediaBox.cachedResourceRepresentation(file.resource, representation: CachedScaledImageRepresentation(size: CGSize(width: 720.0, height: 720.0), mode: .aspectFit), complete: false, fetch: true) + } + else if maybeData.complete { return account.postbox.mediaBox.cachedResourceRepresentation(file.resource, representation: CachedScaledImageRepresentation(size: CGSize(width: 720.0, height: 720.0), mode: .aspectFit), complete: false, fetch: true) } else { return .single(maybeData) @@ -275,7 +278,7 @@ enum PatternWallpaperDrawMode { case screen } -private func patternWallpaperDatas(account: Account, representations: [ImageRepresentationWithReference], mode: PatternWallpaperDrawMode, autoFetchFullSize: Bool = false) -> Signal<(Data?, Data?, Bool), NoError> { +private func patternWallpaperDatas(account: Account, accountManager: AccountManager, representations: [ImageRepresentationWithReference], mode: PatternWallpaperDrawMode, autoFetchFullSize: Bool = false) -> Signal<(Data?, Data?, Bool), NoError> { if let smallestRepresentation = smallestImageRepresentation(representations.map({ $0.representation })), let largestRepresentation = largestImageRepresentation(representations.map({ $0.representation })), let smallestIndex = representations.index(where: { $0.representation == smallestRepresentation }), let largestIndex = representations.index(where: { $0.representation == largestRepresentation }) { let size: CGSize? @@ -287,12 +290,15 @@ private func patternWallpaperDatas(account: Account, representations: [ImageRepr default: size = nil } - let maybeFullSize = account.postbox.mediaBox.cachedResourceRepresentation(largestRepresentation.resource, representation: CachedPatternWallpaperMaskRepresentation(size: size), complete: false, fetch: false) + let maybeFullSize = combineLatest(accountManager.mediaBox.cachedResourceRepresentation(largestRepresentation.resource, representation: CachedPatternWallpaperMaskRepresentation(size: size), complete: false, fetch: false), account.postbox.mediaBox.cachedResourceRepresentation(largestRepresentation.resource, representation: CachedPatternWallpaperMaskRepresentation(size: size), complete: false, fetch: false)) let signal = maybeFullSize |> take(1) - |> mapToSignal { maybeData -> Signal<(Data?, Data?, Bool), NoError> in - if maybeData.complete { + |> mapToSignal { maybeSharedData, maybeData -> Signal<(Data?, Data?, Bool), NoError> in + if maybeSharedData.complete { + let loadedData: Data? = try? Data(contentsOf: URL(fileURLWithPath: maybeSharedData.path), options: []) + return .single((nil, loadedData, true)) + } else if maybeData.complete { let loadedData: Data? = try? Data(contentsOf: URL(fileURLWithPath: maybeData.path), options: []) return .single((nil, loadedData, true)) } else { @@ -303,6 +309,11 @@ private func patternWallpaperDatas(account: Account, representations: [ImageRepr let fetchedDisposable = fetchedThumbnail.start() let thumbnailDisposable = account.postbox.mediaBox.cachedResourceRepresentation(representations[smallestIndex].representation.resource, representation: CachedPatternWallpaperMaskRepresentation(size: size), complete: false, fetch: true).start(next: { next in subscriber.putNext(next.size == 0 ? nil : try? Data(contentsOf: URL(fileURLWithPath: next.path), options: [])) + + if next.complete, let data = try? Data(contentsOf: URL(fileURLWithPath: next.path), options: .mappedRead) { + accountManager.mediaBox.storeResourceData(representations[smallestIndex].representation.resource.id, data: data) + let _ = accountManager.mediaBox.cachedResourceRepresentation(representations[smallestIndex].representation.resource, representation: CachedPatternWallpaperMaskRepresentation(size: size), complete: false, fetch: true).start() + } }, error: subscriber.putError, completed: subscriber.putCompletion) return ActionDisposable { @@ -315,6 +326,11 @@ private func patternWallpaperDatas(account: Account, representations: [ImageRepr let fetchedFullSizeDisposable = fetchedFullSize.start() let fullSizeDisposable = account.postbox.mediaBox.cachedResourceRepresentation(representations[largestIndex].representation.resource, representation: CachedPatternWallpaperMaskRepresentation(size: size), complete: false, fetch: true).start(next: { next in subscriber.putNext((next.size == 0 ? nil : try? Data(contentsOf: URL(fileURLWithPath: next.path), options: []), next.complete)) + + if next.complete, let data = try? Data(contentsOf: URL(fileURLWithPath: next.path), options: .mappedRead) { + accountManager.mediaBox.storeResourceData(representations[largestIndex].representation.resource.id, data: data) + let _ = accountManager.mediaBox.cachedResourceRepresentation(representations[largestIndex].representation.resource, representation: CachedPatternWallpaperMaskRepresentation(size: size), complete: false, fetch: true).start() + } }, error: subscriber.putError, completed: subscriber.putCompletion) return ActionDisposable { @@ -337,9 +353,8 @@ private func patternWallpaperDatas(account: Account, representations: [ImageRepr } } -func patternWallpaperImage(account: Account, representations: [ImageRepresentationWithReference], mode: PatternWallpaperDrawMode, autoFetchFullSize: Bool = false) -> Signal<(TransformImageArguments) -> DrawingContext?, NoError> { - - return patternWallpaperDatas(account: account, representations: representations, mode: mode, autoFetchFullSize: autoFetchFullSize) +func patternWallpaperImage(account: Account, accountManager: AccountManager, representations: [ImageRepresentationWithReference], mode: PatternWallpaperDrawMode, autoFetchFullSize: Bool = false) -> Signal<(TransformImageArguments) -> DrawingContext?, NoError> { + return patternWallpaperDatas(account: account, accountManager: accountManager, representations: representations, mode: mode, autoFetchFullSize: autoFetchFullSize) |> mapToSignal { (thumbnailData, fullSizeData, fullSizeComplete) in return patternWallpaperImageInternal(thumbnailData: thumbnailData, fullSizeData: fullSizeData, fullSizeComplete: fullSizeComplete, mode: mode) }