Chat wallpaper fixes

This commit is contained in:
Ilya Laktyushin 2023-04-13 22:29:58 +04:00
parent 790343efa8
commit 724a303c9c
9 changed files with 93 additions and 25 deletions

View File

@ -285,10 +285,6 @@ public func uploadCustomPeerWallpaper(context: AccountContext, wallpaper: Wallpa
} }
croppedImage = TGPhotoEditorCrop(image, nil, .up, 0.0, finalCropRect, false, CGSize(width: 1440.0, height: 2960.0), image.size, true) 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 thumbnailDimensions = finalCropRect.size.fitted(CGSize(width: 320.0, height: 320.0))
let thumbnailImage = generateScaledImage(image: croppedImage, size: thumbnailDimensions, scale: 1.0) let thumbnailImage = generateScaledImage(image: croppedImage, size: thumbnailDimensions, scale: 1.0)

View File

@ -142,18 +142,28 @@ public final class ThemeGridController: ViewController {
} }
}, presentGallery: { [weak self] in }, presentGallery: { [weak self] in
if let strongSelf = self { 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)) 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 controller.customSelection = { [weak self] asset in
guard let strongSelf = self else { guard let strongSelf = self else {
return return
} }
let controller = WallpaperGalleryController(context: strongSelf.context, source: .asset(asset)) let controller = WallpaperGalleryController(context: strongSelf.context, source: .asset(asset))
controller.apply = { [weak self, weak controller] wallpaper, options, editedImage, cropRect, brightness in controller.apply = { [weak self] wallpaper, options, editedImage, cropRect, brightness in
if let strongSelf = self, let controller = controller { if let strongSelf = self {
uploadCustomWallpaper(context: strongSelf.context, wallpaper: wallpaper, mode: options, editedImage: editedImage, cropRect: cropRect, brightness: brightness, completion: { [weak controller] in uploadCustomWallpaper(context: strongSelf.context, wallpaper: wallpaper, mode: options, editedImage: editedImage, cropRect: cropRect, brightness: brightness, completion: {
if let controller = controller { dismissControllers()
controller.dismiss(forceAway: true)
}
}) })
} }
} }

View File

@ -668,6 +668,20 @@ final class WallpaperGalleryItemNode: GalleryItemNode {
self.patternButtonNode.isSelected = false self.patternButtonNode.isSelected = false
self.playButtonNode.setIcon(self.playButtonRotateImage) 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: case .asset:
self.nativeNode._internalUpdateIsSettingUpWallpaper() self.nativeNode._internalUpdateIsSettingUpWallpaper()
self.nativeNode.isHidden = true self.nativeNode.isHidden = true

View File

@ -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) 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))) return .single(.content(PendingMessageUploadedContentAndReuploadInfo(content: .media(inputPoll, text), reuploadInfo: nil, cacheReferenceKey: nil)))
} else if let media = media as? TelegramMediaDice { } else if let media = media as? TelegramMediaDice {
let input = Api.InputMedia.inputMediaDice(emoticon: media.emoji) let inputDice = Api.InputMedia.inputMediaDice(emoticon: media.emoji)
return .single(.content(PendingMessageUploadedContentAndReuploadInfo(content: .media(input, text), reuploadInfo: nil, cacheReferenceKey: nil))) return .single(.content(PendingMessageUploadedContentAndReuploadInfo(content: .media(inputDice, text), reuploadInfo: nil, cacheReferenceKey: nil)))
} else { } else {
return nil return nil
} }

View File

@ -11,6 +11,7 @@ public final class PeerMediaUploadingItem: Equatable {
public enum Error { public enum Error {
case generic case generic
case flood
} }
public enum Content: Equatable { 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) 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 |> map { updates -> PeerMediaUploadingItem.ProgressValue in
return .done(updates) return .done(updates)
} }
@ -78,7 +86,14 @@ private func uploadPeerMedia(postbox: Postbox, network: Network, stateManager: A
} }
} else { } else {
return _internal_setChatWallpaper(postbox: postbox, network: network, stateManager: stateManager, peerId: peerId, wallpaper: wallpaper, applyUpdates: false) 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 |> map { updates -> PeerMediaUploadingItem.ProgressValue in
return .done(updates) return .done(updates)
} }
@ -265,7 +280,15 @@ private final class PendingPeerMediaUploadManagerImpl {
} }
if let context = strongSelf.contexts[peerId], context === initialContext { if let context = strongSelf.contexts[peerId], context === initialContext {
strongSelf.contexts.removeValue(forKey: peerId) 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() strongSelf.updateValues()
} }
})) }))

View File

@ -118,13 +118,19 @@ func managedChatThemesUpdates(accountManager: AccountManager<TelegramAccountMana
return (poll |> then(.complete() |> suspendAwareDelay(1.0 * 60.0 * 60.0, queue: Queue.concurrentDefaultQueue()))) |> restart return (poll |> 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<Api.Updates, NoError> { public enum SetChatWallpaperError {
case generic
case flood
}
func _internal_setChatWallpaper(postbox: Postbox, network: Network, stateManager: AccountStateManager, peerId: PeerId, wallpaper: TelegramWallpaper?, applyUpdates: Bool = true) -> Signal<Api.Updates, SetChatWallpaperError> {
return postbox.loadedPeerWithId(peerId) return postbox.loadedPeerWithId(peerId)
|> castError(SetChatWallpaperError.self)
|> mapToSignal { peer in |> mapToSignal { peer in
guard let inputPeer = apiInputPeer(peer) else { guard let inputPeer = apiInputPeer(peer) else {
return .complete() return .complete()
} }
return postbox.transaction { transaction -> Signal<Api.Updates, NoError> in return postbox.transaction { transaction -> Signal<Api.Updates, SetChatWallpaperError> in
transaction.updatePeerCachedData(peerIds: Set([peerId]), update: { _, current in transaction.updatePeerCachedData(peerIds: Set([peerId]), update: { _, current in
if let current = current as? CachedUserData { if let current = current as? CachedUserData {
return current.withUpdatedWallpaper(wallpaper) return current.withUpdatedWallpaper(wallpaper)
@ -143,16 +149,22 @@ func _internal_setChatWallpaper(postbox: Postbox, network: Network, stateManager
inputSettings = inputWallpaperAndInputSettings.1 inputSettings = inputWallpaperAndInputSettings.1
} }
return network.request(Api.functions.messages.setChatWallPaper(flags: flags, peer: inputPeer, wallpaper: inputWallpaper, settings: inputSettings, id: nil), automaticFloodWait: false) return network.request(Api.functions.messages.setChatWallPaper(flags: flags, peer: inputPeer, wallpaper: inputWallpaper, settings: inputSettings, id: nil), automaticFloodWait: false)
|> `catch` { error in |> mapError { error -> SetChatWallpaperError in
return .complete() if error.errorDescription.hasPrefix("FLOOD_WAIT") {
return .flood
} else {
return .generic
}
} }
|> mapToSignal { updates -> Signal<Api.Updates, NoError> in |> mapToSignal { updates -> Signal<Api.Updates, SetChatWallpaperError> in
if applyUpdates { if applyUpdates {
stateManager.addUpdates(updates) stateManager.addUpdates(updates)
} }
return .single(updates) return .single(updates)
} }
} |> switchToLatest }
|> castError(SetChatWallpaperError.self)
|> switchToLatest
} }
} }

View File

@ -17,7 +17,7 @@ public extension TelegramEngine {
return _internal_setChatTheme(account: self.account, peerId: peerId, emoticon: emoticon) return _internal_setChatTheme(account: self.account, peerId: peerId, emoticon: emoticon)
} }
public func setChatWallpaper(peerId: PeerId, wallpaper: TelegramWallpaper?) -> Signal<Never, NoError> { public func setChatWallpaper(peerId: PeerId, wallpaper: TelegramWallpaper?) -> Signal<Never, SetChatWallpaperError> {
return _internal_setChatWallpaper(postbox: self.account.postbox, network: self.account.network, stateManager: self.account.stateManager, peerId: peerId, wallpaper: wallpaper) return _internal_setChatWallpaper(postbox: self.account.postbox, network: self.account.network, stateManager: self.account.stateManager, peerId: peerId, wallpaper: wallpaper)
|> ignoreValues |> ignoreValues
} }

View File

@ -857,7 +857,20 @@ public final class ChatControllerImpl: TelegramBaseController, ChatController, G
return true return true
} }
strongSelf.chatDisplayNode.dismissInput() 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 wallpaperPreviewController.apply = { [weak wallpaperPreviewController] entry, options, _, _, brightness in
var settings: WallpaperSettings? var settings: WallpaperSettings?
if case let .wallpaper(wallpaper, _) = entry { if case let .wallpaper(wallpaper, _) = entry {

View File

@ -328,7 +328,7 @@ class ChatMessageWallpaperBubbleContentNode: ChatMessageBubbleContentNode {
if let dimensions = file.dimensions?.cgSize { if let dimensions = file.dimensions?.cgSize {
imageSize = dimensions.aspectFilled(boundingSize) 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): case let .image(representations):
if let dimensions = representations.last?.dimensions.cgSize { if let dimensions = representations.last?.dimensions.cgSize {