diff --git a/submodules/SettingsUI/Sources/Themes/ThemeAccentColorController.swift b/submodules/SettingsUI/Sources/Themes/ThemeAccentColorController.swift index 363c161817..c4b6e1b2f2 100644 --- a/submodules/SettingsUI/Sources/Themes/ThemeAccentColorController.swift +++ b/submodules/SettingsUI/Sources/Themes/ThemeAccentColorController.swift @@ -430,7 +430,13 @@ final class ThemeAccentColorController: ViewController { motion = settings.motion rotation = settings.rotation ?? 0 } else { - backgroundColors = [] + if let image = chatControllerBackgroundImage(theme: nil, wallpaper: wallpaper, mediaBox: strongSelf.context.sharedContext.accountManager.mediaBox, knockoutMode: false) { + backgroundColors = [averageColor(from: image).rgb] + } else if let image = chatControllerBackgroundImage(theme: nil, wallpaper: wallpaper, mediaBox: strongSelf.context.account.postbox.mediaBox, knockoutMode: false) { + backgroundColors = [averageColor(from: image).rgb] + } else { + backgroundColors = [UIColor.gray.rgb] + } } } diff --git a/submodules/SettingsUI/Sources/Themes/WallpaperColorPanelNode.swift b/submodules/SettingsUI/Sources/Themes/WallpaperColorPanelNode.swift index db6370b49b..7d1f6b29c5 100644 --- a/submodules/SettingsUI/Sources/Themes/WallpaperColorPanelNode.swift +++ b/submodules/SettingsUI/Sources/Themes/WallpaperColorPanelNode.swift @@ -525,19 +525,21 @@ final class WallpaperColorPanelNode: ASDisplayNode { if colorWasRemovable != self.multiColorFieldNode.isRemovable { updateLayout = true } + + if let index = self.state.selection { + if self.state.colors.count > index { + self.colorPickerNode.color = UIColor(rgb: self.state.colors[index]) + } + } if updateLayout, let size = self.validLayout { - if let index = self.state.selection { - if self.state.colors.count > index { - self.colorPickerNode.color = UIColor(rgb: self.state.colors[index]) - } - } - self.updateLayout(size: size, transition: animated ? .animated(duration: 0.3, curve: .easeInOut) : .immediate) } if let index = state.selection { - self.multiColorFieldNode.setColor(UIColor(rgb: self.state.colors[index]), update: false) + if self.state.colors.count > index { + self.multiColorFieldNode.setColor(UIColor(rgb: self.state.colors[index]), update: false) + } } for i in 0 ..< state.colors.count { diff --git a/submodules/SyncCore/Sources/TelegramWallpaper.swift b/submodules/SyncCore/Sources/TelegramWallpaper.swift index c7c02cab2e..d9a4161798 100644 --- a/submodules/SyncCore/Sources/TelegramWallpaper.swift +++ b/submodules/SyncCore/Sources/TelegramWallpaper.swift @@ -242,7 +242,7 @@ public enum TelegramWallpaper: OrderedItemListEntryContents, Equatable { case let .image(representations, _): return .image(representations, settings) case let .file(id, accessHash, isCreator, isDefault, isPattern, isDark, slug, file, _): - return .file(id: id, accessHash: accessHash, isCreator: isCreator, isDefault: isDefault, isPattern: settings.colors.isEmpty ? isPattern : true, isDark: isDark, slug: slug, file: file, settings: settings) + return .file(id: id, accessHash: accessHash, isCreator: isCreator, isDefault: isDefault, isPattern: isPattern, isDark: isDark, slug: slug, file: file, settings: settings) } } } diff --git a/submodules/TelegramPresentationData/BUILD b/submodules/TelegramPresentationData/BUILD index 741b28c6b7..424efdb88a 100644 --- a/submodules/TelegramPresentationData/BUILD +++ b/submodules/TelegramPresentationData/BUILD @@ -17,6 +17,7 @@ swift_library( "//submodules/AppBundle:AppBundle", "//submodules/StringPluralization:StringPluralization", "//submodules/Sunrise:Sunrise", + "//submodules/TinyThumbnail:TinyThumbnail", ], visibility = [ "//visibility:public", diff --git a/submodules/TelegramPresentationData/Sources/ChatControllerBackgroundNode.swift b/submodules/TelegramPresentationData/Sources/ChatControllerBackgroundNode.swift index 0f174bb29f..c6f8ef054b 100644 --- a/submodules/TelegramPresentationData/Sources/ChatControllerBackgroundNode.swift +++ b/submodules/TelegramPresentationData/Sources/ChatControllerBackgroundNode.swift @@ -8,6 +8,7 @@ import SwiftSignalKit import Postbox import MediaResources import AppBundle +import TinyThumbnail private var backgroundImageForWallpaper: (TelegramWallpaper, Bool, UIImage)? @@ -225,24 +226,46 @@ public func chatControllerBackgroundImageSignal(wallpaper: TelegramWallpaper, me } else { if file.settings.blur { let representation = CachedBlurredWallpaperRepresentation() - - let effectiveMediaBox: MediaBox + if FileManager.default.fileExists(atPath: mediaBox.cachedRepresentationCompletePath(file.file.resource.id, representation: representation)) { - effectiveMediaBox = mediaBox - } else { - effectiveMediaBox = accountMediaBox - } - - return effectiveMediaBox.cachedResourceRepresentation(file.file.resource, representation: representation, complete: true, fetch: true, attemptSynchronously: true) - |> map { data -> (UIImage?, Bool)? in - if data.complete { - return (UIImage(contentsOfFile: data.path)?.precomposed(), true) - } else { - return nil + let effectiveMediaBox = mediaBox + + return effectiveMediaBox.cachedResourceRepresentation(file.file.resource, representation: representation, complete: true, fetch: true, attemptSynchronously: true) + |> map { data -> (UIImage?, Bool)? in + if data.complete { + return (UIImage(contentsOfFile: data.path)?.precomposed(), true) + } else { + return nil + } + } + |> afterNext { image in + cacheWallpaper(image?.0) + } + } else { + return Signal { subscriber in + let fetch = fetchedMediaResource(mediaBox: accountMediaBox, reference: MediaResourceReference.wallpaper(wallpaper: WallpaperReference.slug(file.slug), resource: file.file.resource)).start() + var didOutputBlurred = false + let data = accountMediaBox.cachedResourceRepresentation(file.file.resource, representation: representation, complete: true, fetch: true, attemptSynchronously: true).start(next: { data in + if data.complete { + if let image = UIImage(contentsOfFile: data.path)?.precomposed() { + mediaBox.copyResourceData(file.file.resource.id, fromTempPath: data.path) + subscriber.putNext((image, true)) + } + } else if !didOutputBlurred { + didOutputBlurred = true + if let immediateThumbnailData = file.file.immediateThumbnailData, let decodedData = decodeTinyThumbnail(data: immediateThumbnailData) { + if let image = UIImage(data: decodedData)?.precomposed() { + subscriber.putNext((image, false)) + } + } + } + }) + + return ActionDisposable { + fetch.dispose() + data.dispose() + } } - } - |> afterNext { image in - cacheWallpaper(image?.0) } } else { var path: String? @@ -259,12 +282,20 @@ public func chatControllerBackgroundImageSignal(wallpaper: TelegramWallpaper, me } else { return Signal { subscriber in let fetch = fetchedMediaResource(mediaBox: accountMediaBox, reference: MediaResourceReference.wallpaper(wallpaper: WallpaperReference.slug(file.slug), resource: file.file.resource)).start() + var didOutputBlurred = false let data = accountMediaBox.resourceData(file.file.resource).start(next: { data in if data.complete { if let image = UIImage(contentsOfFile: data.path)?.precomposed() { mediaBox.copyResourceData(file.file.resource.id, fromTempPath: data.path) subscriber.putNext((image, true)) } + } else if !didOutputBlurred { + didOutputBlurred = true + if let immediateThumbnailData = file.file.immediateThumbnailData, let decodedData = decodeTinyThumbnail(data: immediateThumbnailData) { + if let image = UIImage(data: decodedData)?.precomposed() { + subscriber.putNext((image, false)) + } + } } })