From 724a303c9c19265d1fe8d95b8506aae476aa5ce1 Mon Sep 17 00:00:00 2001 From: Ilya Laktyushin Date: Thu, 13 Apr 2023 22:29:58 +0400 Subject: [PATCH] Chat wallpaper fixes --- .../Themes/CustomWallpaperPicker.swift | 6 +--- .../Sources/Themes/ThemeGridController.swift | 22 ++++++++++---- .../Sources/Themes/WallpaperGalleryItem.swift | 14 +++++++++ .../PendingMessageUploadedContent.swift | 4 +-- .../PendingPeerMediaUploadManager.swift | 29 +++++++++++++++++-- .../TelegramEngine/Themes/ChatThemes.swift | 24 +++++++++++---- .../Themes/TelegramEngineThemes.swift | 2 +- .../TelegramUI/Sources/ChatController.swift | 15 +++++++++- ...hatMessageWallpaperBubbleContentNode.swift | 2 +- 9 files changed, 93 insertions(+), 25 deletions(-) diff --git a/submodules/SettingsUI/Sources/Themes/CustomWallpaperPicker.swift b/submodules/SettingsUI/Sources/Themes/CustomWallpaperPicker.swift index d938dd0358..47ef6d0b8f 100644 --- a/submodules/SettingsUI/Sources/Themes/CustomWallpaperPicker.swift +++ b/submodules/SettingsUI/Sources/Themes/CustomWallpaperPicker.swift @@ -284,11 +284,7 @@ public func uploadCustomPeerWallpaper(context: AccountContext, wallpaper: Wallpa finalCropRect = CGRect(x: (image.size.width - fittedSize.width) / 2.0, y: (image.size.height - fittedSize.height) / 2.0, width: fittedSize.width, height: fittedSize.height) } croppedImage = TGPhotoEditorCrop(image, nil, .up, 0.0, finalCropRect, false, CGSize(width: 1440.0, height: 2960.0), image.size, true) - - if mode.contains(.blur) { - croppedImage = blurredImage(croppedImage, radius: 30.0)! - } - + let thumbnailDimensions = finalCropRect.size.fitted(CGSize(width: 320.0, height: 320.0)) let thumbnailImage = generateScaledImage(image: croppedImage, size: thumbnailDimensions, scale: 1.0) diff --git a/submodules/SettingsUI/Sources/Themes/ThemeGridController.swift b/submodules/SettingsUI/Sources/Themes/ThemeGridController.swift index 048a0af79e..8bf500a949 100644 --- a/submodules/SettingsUI/Sources/Themes/ThemeGridController.swift +++ b/submodules/SettingsUI/Sources/Themes/ThemeGridController.swift @@ -142,18 +142,28 @@ public final class ThemeGridController: ViewController { } }, presentGallery: { [weak self] in if let strongSelf = self { + let dismissControllers = { [weak self] in + if let self, let navigationController = self.navigationController as? NavigationController { + let controllers = navigationController.viewControllers.filter({ controller in + if controller is WallpaperGalleryController || controller is MediaPickerScreen { + return false + } + return true + }) + navigationController.setViewControllers(controllers, animated: true) + } + } + let controller = MediaPickerScreen(context: strongSelf.context, peer: nil, threadTitle: nil, chatLocation: nil, bannedSendPhotos: nil, bannedSendVideos: nil, subject: .assets(nil, .wallpaper)) controller.customSelection = { [weak self] asset in guard let strongSelf = self else { return } let controller = WallpaperGalleryController(context: strongSelf.context, source: .asset(asset)) - controller.apply = { [weak self, weak controller] wallpaper, options, editedImage, cropRect, brightness in - if let strongSelf = self, let controller = controller { - uploadCustomWallpaper(context: strongSelf.context, wallpaper: wallpaper, mode: options, editedImage: editedImage, cropRect: cropRect, brightness: brightness, completion: { [weak controller] in - if let controller = controller { - controller.dismiss(forceAway: true) - } + controller.apply = { [weak self] wallpaper, options, editedImage, cropRect, brightness in + if let strongSelf = self { + uploadCustomWallpaper(context: strongSelf.context, wallpaper: wallpaper, mode: options, editedImage: editedImage, cropRect: cropRect, brightness: brightness, completion: { + dismissControllers() }) } } diff --git a/submodules/SettingsUI/Sources/Themes/WallpaperGalleryItem.swift b/submodules/SettingsUI/Sources/Themes/WallpaperGalleryItem.swift index edd7fec83b..51ccf0e2d1 100644 --- a/submodules/SettingsUI/Sources/Themes/WallpaperGalleryItem.swift +++ b/submodules/SettingsUI/Sources/Themes/WallpaperGalleryItem.swift @@ -668,6 +668,20 @@ final class WallpaperGalleryItemNode: GalleryItemNode { self.patternButtonNode.isSelected = false self.playButtonNode.setIcon(self.playButtonRotateImage) } + + if let settings = wallpaper.settings { + if settings.blur { + self.blurButtonNode.setSelected(true, animated: false) + self.setBlurEnabled(true, animated: false) + } + if settings.motion { + self.motionButtonNode.setSelected(true, animated: false) + self.setMotionEnabled(true, animated: false) + } + if case let .file(file) = wallpaper, !file.isPattern, let intensity = file.settings.intensity { + self.sliderNode.value = (1.0 - CGFloat(intensity) / 100.0) + } + } case .asset: self.nativeNode._internalUpdateIsSettingUpWallpaper() self.nativeNode.isHidden = true diff --git a/submodules/TelegramCore/Sources/PendingMessages/PendingMessageUploadedContent.swift b/submodules/TelegramCore/Sources/PendingMessages/PendingMessageUploadedContent.swift index 29d6a27614..595b020200 100644 --- a/submodules/TelegramCore/Sources/PendingMessages/PendingMessageUploadedContent.swift +++ b/submodules/TelegramCore/Sources/PendingMessages/PendingMessageUploadedContent.swift @@ -212,8 +212,8 @@ func mediaContentToUpload(network: Network, postbox: Postbox, auxiliaryMethods: let inputPoll = Api.InputMedia.inputMediaPoll(flags: pollMediaFlags, poll: Api.Poll.poll(id: 0, flags: pollFlags, question: poll.text, answers: poll.options.map({ $0.apiOption }), closePeriod: poll.deadlineTimeout, closeDate: nil), correctAnswers: correctAnswers, solution: mappedSolution, solutionEntities: mappedSolutionEntities) return .single(.content(PendingMessageUploadedContentAndReuploadInfo(content: .media(inputPoll, text), reuploadInfo: nil, cacheReferenceKey: nil))) } else if let media = media as? TelegramMediaDice { - let input = Api.InputMedia.inputMediaDice(emoticon: media.emoji) - return .single(.content(PendingMessageUploadedContentAndReuploadInfo(content: .media(input, text), reuploadInfo: nil, cacheReferenceKey: nil))) + let inputDice = Api.InputMedia.inputMediaDice(emoticon: media.emoji) + return .single(.content(PendingMessageUploadedContentAndReuploadInfo(content: .media(inputDice, text), reuploadInfo: nil, cacheReferenceKey: nil))) } else { return nil } diff --git a/submodules/TelegramCore/Sources/PendingMessages/PendingPeerMediaUploadManager.swift b/submodules/TelegramCore/Sources/PendingMessages/PendingPeerMediaUploadManager.swift index 2e6468064e..a1938e5310 100644 --- a/submodules/TelegramCore/Sources/PendingMessages/PendingPeerMediaUploadManager.swift +++ b/submodules/TelegramCore/Sources/PendingMessages/PendingPeerMediaUploadManager.swift @@ -11,6 +11,7 @@ public final class PeerMediaUploadingItem: Equatable { public enum Error { case generic + case flood } public enum Content: Equatable { @@ -69,7 +70,14 @@ private func uploadPeerMedia(postbox: Postbox, network: Network, stateManager: A } } return _internal_setChatWallpaper(postbox: postbox, network: network, stateManager: stateManager, peerId: peerId, wallpaper: result, applyUpdates: false) - |> castError(PeerMediaUploadingItem.Error.self) + |> mapError { error -> PeerMediaUploadingItem.Error in + switch error { + case .generic: + return .generic + case .flood: + return .flood + } + } |> map { updates -> PeerMediaUploadingItem.ProgressValue in return .done(updates) } @@ -78,7 +86,14 @@ private func uploadPeerMedia(postbox: Postbox, network: Network, stateManager: A } } else { return _internal_setChatWallpaper(postbox: postbox, network: network, stateManager: stateManager, peerId: peerId, wallpaper: wallpaper, applyUpdates: false) - |> castError(PeerMediaUploadingItem.Error.self) + |> mapError { error -> PeerMediaUploadingItem.Error in + switch error { + case .generic: + return .generic + case .flood: + return .flood + } + } |> map { updates -> PeerMediaUploadingItem.ProgressValue in return .done(updates) } @@ -265,7 +280,15 @@ private final class PendingPeerMediaUploadManagerImpl { } if let context = strongSelf.contexts[peerId], context === initialContext { strongSelf.contexts.removeValue(forKey: peerId) - context.disposable.dispose() + + if let messageId = context.value.messageId { + context.disposable.set(strongSelf.postbox.transaction({ transaction in + transaction.deleteMessages([messageId], forEachMedia: nil) + }).start()) + } else { + context.disposable.dispose() + } + strongSelf.updateValues() } })) diff --git a/submodules/TelegramCore/Sources/TelegramEngine/Themes/ChatThemes.swift b/submodules/TelegramCore/Sources/TelegramEngine/Themes/ChatThemes.swift index 8e97221e71..437097df2d 100644 --- a/submodules/TelegramCore/Sources/TelegramEngine/Themes/ChatThemes.swift +++ b/submodules/TelegramCore/Sources/TelegramEngine/Themes/ChatThemes.swift @@ -118,13 +118,19 @@ func managedChatThemesUpdates(accountManager: AccountManager then(.complete() |> suspendAwareDelay(1.0 * 60.0 * 60.0, queue: Queue.concurrentDefaultQueue()))) |> restart } -func _internal_setChatWallpaper(postbox: Postbox, network: Network, stateManager: AccountStateManager, peerId: PeerId, wallpaper: TelegramWallpaper?, applyUpdates: Bool = true) -> Signal { +public enum SetChatWallpaperError { + case generic + case flood +} + +func _internal_setChatWallpaper(postbox: Postbox, network: Network, stateManager: AccountStateManager, peerId: PeerId, wallpaper: TelegramWallpaper?, applyUpdates: Bool = true) -> Signal { return postbox.loadedPeerWithId(peerId) + |> castError(SetChatWallpaperError.self) |> mapToSignal { peer in guard let inputPeer = apiInputPeer(peer) else { return .complete() } - return postbox.transaction { transaction -> Signal in + return postbox.transaction { transaction -> Signal in transaction.updatePeerCachedData(peerIds: Set([peerId]), update: { _, current in if let current = current as? CachedUserData { return current.withUpdatedWallpaper(wallpaper) @@ -143,16 +149,22 @@ func _internal_setChatWallpaper(postbox: Postbox, network: Network, stateManager inputSettings = inputWallpaperAndInputSettings.1 } return network.request(Api.functions.messages.setChatWallPaper(flags: flags, peer: inputPeer, wallpaper: inputWallpaper, settings: inputSettings, id: nil), automaticFloodWait: false) - |> `catch` { error in - return .complete() + |> mapError { error -> SetChatWallpaperError in + if error.errorDescription.hasPrefix("FLOOD_WAIT") { + return .flood + } else { + return .generic + } } - |> mapToSignal { updates -> Signal in + |> mapToSignal { updates -> Signal in if applyUpdates { stateManager.addUpdates(updates) } return .single(updates) } - } |> switchToLatest + } + |> castError(SetChatWallpaperError.self) + |> switchToLatest } } diff --git a/submodules/TelegramCore/Sources/TelegramEngine/Themes/TelegramEngineThemes.swift b/submodules/TelegramCore/Sources/TelegramEngine/Themes/TelegramEngineThemes.swift index 623efabd58..e189e3cbaa 100644 --- a/submodules/TelegramCore/Sources/TelegramEngine/Themes/TelegramEngineThemes.swift +++ b/submodules/TelegramCore/Sources/TelegramEngine/Themes/TelegramEngineThemes.swift @@ -17,7 +17,7 @@ public extension TelegramEngine { return _internal_setChatTheme(account: self.account, peerId: peerId, emoticon: emoticon) } - public func setChatWallpaper(peerId: PeerId, wallpaper: TelegramWallpaper?) -> Signal { + public func setChatWallpaper(peerId: PeerId, wallpaper: TelegramWallpaper?) -> Signal { return _internal_setChatWallpaper(postbox: self.account.postbox, network: self.account.network, stateManager: self.account.stateManager, peerId: peerId, wallpaper: wallpaper) |> ignoreValues } diff --git a/submodules/TelegramUI/Sources/ChatController.swift b/submodules/TelegramUI/Sources/ChatController.swift index c399cfad06..3733d0799d 100644 --- a/submodules/TelegramUI/Sources/ChatController.swift +++ b/submodules/TelegramUI/Sources/ChatController.swift @@ -857,7 +857,20 @@ public final class ChatControllerImpl: TelegramBaseController, ChatController, G return true } strongSelf.chatDisplayNode.dismissInput() - let wallpaperPreviewController = WallpaperGalleryController(context: strongSelf.context, source: .wallpaper(wallpaper, nil, [], nil, nil, nil), mode: .peer(EnginePeer(peer), true)) + var options = WallpaperPresentationOptions() + var intensity: Int32? + if let settings = wallpaper.settings { + if settings.blur { + options.insert(.blur) + } + if settings.motion { + options.insert(.motion) + } + if case let .file(file) = wallpaper, !file.isPattern { + intensity = settings.intensity + } + } + let wallpaperPreviewController = WallpaperGalleryController(context: strongSelf.context, source: .wallpaper(wallpaper, options, [], intensity, nil, nil), mode: .peer(EnginePeer(peer), true)) wallpaperPreviewController.apply = { [weak wallpaperPreviewController] entry, options, _, _, brightness in var settings: WallpaperSettings? if case let .wallpaper(wallpaper, _) = entry { diff --git a/submodules/TelegramUI/Sources/ChatMessageWallpaperBubbleContentNode.swift b/submodules/TelegramUI/Sources/ChatMessageWallpaperBubbleContentNode.swift index 853caa832f..6624a49de8 100644 --- a/submodules/TelegramUI/Sources/ChatMessageWallpaperBubbleContentNode.swift +++ b/submodules/TelegramUI/Sources/ChatMessageWallpaperBubbleContentNode.swift @@ -328,7 +328,7 @@ class ChatMessageWallpaperBubbleContentNode: ChatMessageBubbleContentNode { if let dimensions = file.dimensions?.cgSize { imageSize = dimensions.aspectFilled(boundingSize) } - updateImageSignal = wallpaperImage(account: item.context.account, accountManager: item.context.sharedContext.accountManager, fileReference: FileMediaReference.message(message: MessageReference(item.message), media: file), representations: representations, alwaysShowThumbnailFirst: true, thumbnail: true, autoFetchFullSize: true) + updateImageSignal = wallpaperImage(account: item.context.account, accountManager: item.context.sharedContext.accountManager, fileReference: FileMediaReference.message(message: MessageReference(item.message), media: file), representations: representations, alwaysShowThumbnailFirst: true, thumbnail: true, autoFetchFullSize: true, blurred: wallpaper?.settings?.blur == true) } case let .image(representations): if let dimensions = representations.last?.dimensions.cgSize {