From 4b19b57a868a7271cb35f3ee0d33fbe32e81fea3 Mon Sep 17 00:00:00 2001 From: Ali <> Date: Sun, 13 Jun 2021 02:14:44 +0400 Subject: [PATCH] Fix first time pattern background loading --- .../Themes/SettingsThemeWallpaperNode.swift | 7 ++ .../Themes/ThemeColorsGridController.swift | 119 +++++++++++------- .../Themes/ThemePreviewControllerNode.swift | 7 ++ .../ChatMessageInteractiveMediaNode.swift | 7 ++ .../Sources/WallpaperBackgroundNode.swift | 16 +-- .../Sources/WallpaperResources.swift | 12 +- 6 files changed, 116 insertions(+), 52 deletions(-) diff --git a/submodules/SettingsUI/Sources/Themes/SettingsThemeWallpaperNode.swift b/submodules/SettingsUI/Sources/Themes/SettingsThemeWallpaperNode.swift index 05f37192fc..ee9013013f 100644 --- a/submodules/SettingsUI/Sources/Themes/SettingsThemeWallpaperNode.swift +++ b/submodules/SettingsUI/Sources/Themes/SettingsThemeWallpaperNode.swift @@ -173,6 +173,13 @@ final class SettingsThemeWallpaperNode: ASDisplayNode { self.arguments = PatternWallpaperArguments(colors: [.clear], rotation: nil, customPatternColor: isLight ? .black : .white) } imageSignal = patternWallpaperImage(account: context.account, accountManager: context.sharedContext.accountManager, representations: convertedRepresentations, mode: .thumbnail, autoFetchFullSize: true) + |> mapToSignal { value -> Signal<(TransformImageArguments) -> DrawingContext?, NoError> in + if let value = value { + return .single(value) + } else { + return .complete() + } + } } else { self.imageNode.alpha = 1.0 diff --git a/submodules/SettingsUI/Sources/Themes/ThemeColorsGridController.swift b/submodules/SettingsUI/Sources/Themes/ThemeColorsGridController.swift index 93322d08ab..0c1ebc183c 100644 --- a/submodules/SettingsUI/Sources/Themes/ThemeColorsGridController.swift +++ b/submodules/SettingsUI/Sources/Themes/ThemeColorsGridController.swift @@ -11,47 +11,82 @@ import TelegramPresentationData import TelegramUIPreferences import AccountContext -private func availableColors() -> [UInt32] { - return [ - 0xffffff, - 0xd4dfea, - 0xb3cde1, - 0x6ab7ea, - 0x008dd0, - 0xd3e2da, - 0xc8e6c9, - 0xc5e1a5, - 0x61b06e, - 0xcdcfaf, - 0xa7a895, - 0x7c6f72, - 0xffd7ae, - 0xffb66d, - 0xde8751, - 0xefd5e0, - 0xdba1b9, - 0xffafaf, - 0xf16a60, - 0xe8bcea, - 0x9592ed, - 0xd9bc60, - 0xb17e49, - 0xd5cef7, - 0xdf506b, - 0x8bd2cc, - 0x3c847e, - 0x22612c, - 0x244d7c, - 0x3d3b85, - 0x65717d, - 0x18222d, - 0x000000 - ] -} - -private func randomColor() -> UInt32 { - let colors = availableColors() - return colors[1 ..< colors.count - 1].randomElement() ?? 0x000000 +private func availableColors(theme: PresentationTheme) -> [UInt32] { + if theme.overallDarkAppearance { + return ([ + 0xffffff, + 0xd4dfea, + 0xb3cde1, + 0x6ab7ea, + 0x008dd0, + 0xd3e2da, + 0xc8e6c9, + 0xc5e1a5, + 0x61b06e, + 0xcdcfaf, + 0xa7a895, + 0x7c6f72, + 0xffd7ae, + 0xffb66d, + 0xde8751, + 0xefd5e0, + 0xdba1b9, + 0xffafaf, + 0xf16a60, + 0xe8bcea, + 0x9592ed, + 0xd9bc60, + 0xb17e49, + 0xd5cef7, + 0xdf506b, + 0x8bd2cc, + 0x3c847e, + 0x22612c, + 0x244d7c, + 0x3d3b85, + 0x65717d, + 0x18222d, + 0x000000 + ] as [UInt32]).filter { color in + return UIColor(rgb: color).hsb.b <= 0.4 + } + } else { + return [ + 0xffffff, + 0xd4dfea, + 0xb3cde1, + 0x6ab7ea, + 0x008dd0, + 0xd3e2da, + 0xc8e6c9, + 0xc5e1a5, + 0x61b06e, + 0xcdcfaf, + 0xa7a895, + 0x7c6f72, + 0xffd7ae, + 0xffb66d, + 0xde8751, + 0xefd5e0, + 0xdba1b9, + 0xffafaf, + 0xf16a60, + 0xe8bcea, + 0x9592ed, + 0xd9bc60, + 0xb17e49, + 0xd5cef7, + 0xdf506b, + 0x8bd2cc, + 0x3c847e, + 0x22612c, + 0x244d7c, + 0x3d3b85, + 0x65717d, + 0x18222d, + 0x000000 + ] + } } final class ThemeColorsGridController: ViewController { @@ -120,7 +155,7 @@ final class ThemeColorsGridController: ViewController { } override func loadDisplayNode() { - self.displayNode = ThemeColorsGridControllerNode(context: self.context, presentationData: self.presentationData, colors: availableColors(), present: { [weak self] controller, arguments in + self.displayNode = ThemeColorsGridControllerNode(context: self.context, presentationData: self.presentationData, colors: availableColors(theme: self.presentationData.theme), present: { [weak self] controller, arguments in self?.present(controller, in: .window(.root), with: arguments, blockInteraction: true) }, pop: { [weak self] in if let strongSelf = self, let navigationController = strongSelf.navigationController as? NavigationController { diff --git a/submodules/SettingsUI/Sources/Themes/ThemePreviewControllerNode.swift b/submodules/SettingsUI/Sources/Themes/ThemePreviewControllerNode.swift index 6994daadea..174144f250 100644 --- a/submodules/SettingsUI/Sources/Themes/ThemePreviewControllerNode.swift +++ b/submodules/SettingsUI/Sources/Themes/ThemePreviewControllerNode.swift @@ -251,6 +251,13 @@ final class ThemePreviewControllerNode: ASDisplayNode, UIScrollViewDelegate { let fileReference = FileMediaReference.standalone(media: file.file) if wallpaper.isPattern { signal = patternWallpaperImage(account: context.account, accountManager: context.sharedContext.accountManager, representations: convertedRepresentations, mode: .screen, autoFetchFullSize: false) + |> mapToSignal { value -> Signal<(TransformImageArguments) -> DrawingContext?, NoError> in + if let value = value { + return .single(value) + } else { + return .complete() + } + } } else { signal = .complete() } diff --git a/submodules/TelegramUI/Sources/ChatMessageInteractiveMediaNode.swift b/submodules/TelegramUI/Sources/ChatMessageInteractiveMediaNode.swift index 52b7140037..d5c7784b3e 100644 --- a/submodules/TelegramUI/Sources/ChatMessageInteractiveMediaNode.swift +++ b/submodules/TelegramUI/Sources/ChatMessageInteractiveMediaNode.swift @@ -739,6 +739,13 @@ final class ChatMessageInteractiveMediaNode: ASDisplayNode, GalleryItemTransitio } if ["image/png", "image/svg+xml", "application/x-tgwallpattern"].contains(file.mimeType) { return patternWallpaperImage(account: context.account, accountManager: context.sharedContext.accountManager, representations: representations, mode: .thumbnail) + |> mapToSignal { value -> Signal<(TransformImageArguments) -> DrawingContext?, NoError> in + if let value = value { + return .single(value) + } else { + return .complete() + } + } } 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/submodules/WallpaperBackgroundNode/Sources/WallpaperBackgroundNode.swift b/submodules/WallpaperBackgroundNode/Sources/WallpaperBackgroundNode.swift index 67a2c59fc4..11110e8c4e 100644 --- a/submodules/WallpaperBackgroundNode/Sources/WallpaperBackgroundNode.swift +++ b/submodules/WallpaperBackgroundNode/Sources/WallpaperBackgroundNode.swift @@ -503,18 +503,20 @@ public final class WallpaperBackgroundNode: ASDisplayNode { let dimensions = file.dimensions ?? PixelDimensions(width: 2000, height: 4000) convertedRepresentations.append(ImageRepresentationWithReference(representation: .init(dimensions: dimensions, resource: file.resource, progressiveSizes: [], immediateThumbnailData: nil), reference: reference(for: file.resource, media: file, message: nil))) - let signal = patternWallpaperImage(account: self.context.account, accountManager: self.context.sharedContext.accountManager, representations: convertedRepresentations, mode: .screen, autoFetchFullSize: true) + let signal = patternWallpaperImage(account: self.context.account, accountManager: self.context.sharedContext.accountManager, representations: convertedRepresentations, mode: .screen, autoFetchFullSize: true, onlyFullSize: true) self.patternImageDisposable.set((signal |> deliverOnMainQueue).start(next: { [weak self] generator in guard let strongSelf = self else { return } - strongSelf.validPatternImage = ValidPatternImage(wallpaper: wallpaper, generate: generator) - strongSelf.validPatternGeneratedImage = nil - if let size = strongSelf.validLayout { - strongSelf.loadPatternForSizeIfNeeded(size: size, transition: .immediate) - } else { - strongSelf._isReady.set(true) + if let generator = generator { + strongSelf.validPatternImage = ValidPatternImage(wallpaper: wallpaper, generate: generator) + strongSelf.validPatternGeneratedImage = nil + if let size = strongSelf.validLayout { + strongSelf.loadPatternForSizeIfNeeded(size: size, transition: .immediate) + } else { + strongSelf._isReady.set(true) + } } })) } diff --git a/submodules/WallpaperResources/Sources/WallpaperResources.swift b/submodules/WallpaperResources/Sources/WallpaperResources.swift index 423ebbcbec..28140f4a76 100644 --- a/submodules/WallpaperResources/Sources/WallpaperResources.swift +++ b/submodules/WallpaperResources/Sources/WallpaperResources.swift @@ -401,14 +401,14 @@ private func patternWallpaperDatas(account: Account, accountManager: AccountMana } } -public func patternWallpaperImage(account: Account, accountManager: AccountManager, representations: [ImageRepresentationWithReference], mode: PatternWallpaperDrawMode, autoFetchFullSize: Bool = false) -> Signal<(TransformImageArguments) -> DrawingContext?, NoError> { +public func patternWallpaperImage(account: Account, accountManager: AccountManager, representations: [ImageRepresentationWithReference], mode: PatternWallpaperDrawMode, autoFetchFullSize: Bool = false, onlyFullSize: 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) + return patternWallpaperImageInternal(thumbnailData: thumbnailData, fullSizeData: fullSizeData, fullSizeComplete: fullSizeComplete, mode: mode, onlyFullSize: onlyFullSize) } } -public func patternWallpaperImageInternal(thumbnailData: Data?, fullSizeData: Data?, fullSizeComplete: Bool, mode: PatternWallpaperDrawMode) -> Signal<(TransformImageArguments) -> DrawingContext?, NoError> { +public func patternWallpaperImageInternal(thumbnailData: Data?, fullSizeData: Data?, fullSizeComplete: Bool, mode: PatternWallpaperDrawMode, onlyFullSize: Bool = false) -> Signal<((TransformImageArguments) -> DrawingContext?)?, NoError> { var prominent = false if case .thumbnail = mode { prominent = true @@ -434,6 +434,12 @@ public func patternWallpaperImageInternal(thumbnailData: Data?, fullSizeData: Da } } } + + if onlyFullSize { + if fullSizeData == nil { + return nil + } + } return { arguments in var scale = scale