From bd782b04029d65fb9802880c142157717a2f0cae Mon Sep 17 00:00:00 2001 From: Ilya Laktyushin Date: Thu, 13 Apr 2023 15:19:38 +0400 Subject: [PATCH] Chat wallpaper fixes --- .../Themes/WallpaperGalleryController.swift | 38 +++++++++++++++++-- .../Sources/Themes/WallpaperGalleryItem.swift | 3 +- .../Sources/TextNodeWithEntities.swift | 2 +- .../TelegramUI/Sources/ChatController.swift | 22 ++++++----- .../Sources/WallpaperBackgroundNode.swift | 2 +- 5 files changed, 50 insertions(+), 17 deletions(-) diff --git a/submodules/SettingsUI/Sources/Themes/WallpaperGalleryController.swift b/submodules/SettingsUI/Sources/Themes/WallpaperGalleryController.swift index 2fa114ef65..ed244c8c5e 100644 --- a/submodules/SettingsUI/Sources/Themes/WallpaperGalleryController.swift +++ b/submodules/SettingsUI/Sources/Themes/WallpaperGalleryController.swift @@ -523,9 +523,42 @@ public class WallpaperGalleryController: ViewController { let options = centralItemNode.options if !strongSelf.entries.isEmpty { let entry = strongSelf.entries[centralItemNode.index] - + let apply = strongSelf.apply if case .peer = strongSelf.mode { - strongSelf.apply?(entry, options, centralItemNode.editedFullSizeImage, centralItemNode.editedCropRect, centralItemNode.brightness) + if case let .wallpaper(wallpaper, _) = entry, options.contains(.blur) { + var resource: MediaResource? + switch wallpaper { + case let .file(file): + resource = file.file.resource + case let .image(representations, _): + if let largestSize = largestImageRepresentation(representations) { + resource = largestSize.resource + } + default: + break + } + if let resource = resource { + let representation = CachedBlurredWallpaperRepresentation() + var data: Data? + if let path = strongSelf.context.account.postbox.mediaBox.completedResourcePath(resource), let maybeData = try? Data(contentsOf: URL(fileURLWithPath: path), options: .mappedRead) { + data = maybeData + } else if let path = strongSelf.context.sharedContext.accountManager.mediaBox.completedResourcePath(resource), let maybeData = try? Data(contentsOf: URL(fileURLWithPath: path), options: .mappedRead) { + data = maybeData + } + + if let data = data { + strongSelf.context.sharedContext.accountManager.mediaBox.storeResourceData(resource.id, data: data, synchronous: true) + let _ = (strongSelf.context.sharedContext.accountManager.mediaBox.cachedResourceRepresentation(resource, representation: representation, complete: true, fetch: true) + |> filter({ $0.complete }) + |> take(1) + |> deliverOnMainQueue).start(next: { _ in + apply?(entry, options, nil, nil, centralItemNode.brightness) + }) + } + } + } else { + apply?(entry, options, centralItemNode.editedFullSizeImage, centralItemNode.editedCropRect, centralItemNode.brightness) + } return } @@ -595,7 +628,6 @@ public class WallpaperGalleryController: ViewController { if options.contains(.blur) { if let resource = resource { let representation = CachedBlurredWallpaperRepresentation() - var data: Data? if let path = strongSelf.context.account.postbox.mediaBox.completedResourcePath(resource), let maybeData = try? Data(contentsOf: URL(fileURLWithPath: path), options: .mappedRead) { data = maybeData diff --git a/submodules/SettingsUI/Sources/Themes/WallpaperGalleryItem.swift b/submodules/SettingsUI/Sources/Themes/WallpaperGalleryItem.swift index e4b08e7db2..edd7fec83b 100644 --- a/submodules/SettingsUI/Sources/Themes/WallpaperGalleryItem.swift +++ b/submodules/SettingsUI/Sources/Themes/WallpaperGalleryItem.swift @@ -640,7 +640,7 @@ final class WallpaperGalleryItemNode: GalleryItemNode { self.nativeNode.isHidden = false self.patternButtonNode.isSelected = file.isPattern - if file.isPattern && file.settings.colors.count >= 3 { + if file.settings.colors.count >= 3 { self.playButtonNode.setIcon(self.playButtonPlayImage) } else { self.playButtonNode.setIcon(self.playButtonRotateImage) @@ -733,7 +733,6 @@ final class WallpaperGalleryItemNode: GalleryItemNode { let dimensions = file.file.dimensions ?? PixelDimensions(width: 2000, height: 4000) contentSize = dimensions.cgSize displaySize = dimensions.cgSize.dividedByScreenScale().integralFloor - var convertedRepresentations: [ImageRepresentationWithReference] = [] for representation in file.file.previewRepresentations { convertedRepresentations.append(ImageRepresentationWithReference(representation: representation, reference: reference(for: representation.resource, media: file.file, message: message, slug: file.slug))) diff --git a/submodules/TelegramUI/Components/TextNodeWithEntities/Sources/TextNodeWithEntities.swift b/submodules/TelegramUI/Components/TextNodeWithEntities/Sources/TextNodeWithEntities.swift index 2471c6ae11..032c38f515 100644 --- a/submodules/TelegramUI/Components/TextNodeWithEntities/Sources/TextNodeWithEntities.swift +++ b/submodules/TelegramUI/Components/TextNodeWithEntities/Sources/TextNodeWithEntities.swift @@ -239,7 +239,7 @@ public final class TextNodeWithEntities { let id = InlineStickerItemLayer.Key(id: stickerItem.emoji.fileId, index: index) validIds.append(id) - let itemSize = floor(stickerItem.fontSize * 24.0 / 17.0) + let itemSize = floorToScreenPixels(stickerItem.fontSize * 24.0 / 17.0) var itemFrame = CGRect(origin: item.rect.offsetBy(dx: textLayout.insets.left, dy: textLayout.insets.top + 1.0).center, size: CGSize()).insetBy(dx: -itemSize / 2.0, dy: -itemSize / 2.0) itemFrame.origin.x = floorToScreenPixels(itemFrame.origin.x) diff --git a/submodules/TelegramUI/Sources/ChatController.swift b/submodules/TelegramUI/Sources/ChatController.swift index 95c45ce24e..1c6a9576ec 100644 --- a/submodules/TelegramUI/Sources/ChatController.swift +++ b/submodules/TelegramUI/Sources/ChatController.swift @@ -858,17 +858,19 @@ public final class ChatControllerImpl: TelegramBaseController, ChatController, G } strongSelf.chatDisplayNode.dismissInput() let wallpaperPreviewController = WallpaperGalleryController(context: strongSelf.context, source: .wallpaper(wallpaper, nil, [], nil, nil, nil), mode: .peer(EnginePeer(peer), true)) - wallpaperPreviewController.apply = { [weak wallpaperPreviewController] entry, options, _, _, _ in - if case let .wallpaper(wallpaper, _) = entry, case let .file(file) = wallpaper, !file.isPattern && options.contains(.blur) { - uploadCustomPeerWallpaper(context: strongSelf.context, wallpaper: entry, mode: options, editedImage: nil, cropRect: nil, brightness: nil, peerId: message.id.peerId, completion: { - wallpaperPreviewController?.dismiss() - }) - } else { - let _ = (strongSelf.context.engine.themes.setExistingChatWallpaper(messageId: message.id, settings: nil) - |> deliverOnMainQueue).start() - Queue.mainQueue().after(0.1) { - wallpaperPreviewController?.dismiss() + wallpaperPreviewController.apply = { [weak wallpaperPreviewController] entry, options, _, _, brightness in + var settings: WallpaperSettings? + if case let .wallpaper(wallpaper, _) = entry, case let .file(file) = wallpaper, !file.isPattern { + var intensity: Int32? + if let brightness { + intensity = max(0, min(100, Int32(brightness * 100.0))) } + settings = WallpaperSettings(blur: options.contains(.blur), motion: options.contains(.motion), intensity: intensity) + } + let _ = (strongSelf.context.engine.themes.setExistingChatWallpaper(messageId: message.id, settings: settings) + |> deliverOnMainQueue).start() + Queue.mainQueue().after(0.1) { + wallpaperPreviewController?.dismiss() } } strongSelf.push(wallpaperPreviewController) diff --git a/submodules/WallpaperBackgroundNode/Sources/WallpaperBackgroundNode.swift b/submodules/WallpaperBackgroundNode/Sources/WallpaperBackgroundNode.swift index 957db88bfb..8a97099ee9 100644 --- a/submodules/WallpaperBackgroundNode/Sources/WallpaperBackgroundNode.swift +++ b/submodules/WallpaperBackgroundNode/Sources/WallpaperBackgroundNode.swift @@ -1058,12 +1058,12 @@ final class WallpaperBackgroundNodeImpl: ASDisplayNode, WallpaperBackgroundNode if let (size, displayMode) = self.validLayout { self.updateLayout(size: size, displayMode: displayMode, transition: .immediate) - self.updateBubbles() if scheduleLoopingEvent { self.animateEvent(transition: .animated(duration: 0.7, curve: .linear), extendAnimation: false) } } + self.updateBubbles() self.updateDimming() }