From 2afc1ff3dab83eae317c0770c3cc124780cfb71a Mon Sep 17 00:00:00 2001 From: Ilya Laktyushin Date: Fri, 3 Jan 2020 23:12:20 +0300 Subject: [PATCH] Fix wallpaper preview --- .../Themes/ThemeSettingsChatPreviewItem.swift | 2 +- .../ChatControllerBackgroundNode.swift | 42 +++++++++++++++---- .../Sources/WallpaperCache.swift | 4 +- 3 files changed, 37 insertions(+), 11 deletions(-) diff --git a/submodules/SettingsUI/Sources/Themes/ThemeSettingsChatPreviewItem.swift b/submodules/SettingsUI/Sources/Themes/ThemeSettingsChatPreviewItem.swift index 0ababb20c5..4a2a1361ad 100644 --- a/submodules/SettingsUI/Sources/Themes/ThemeSettingsChatPreviewItem.swift +++ b/submodules/SettingsUI/Sources/Themes/ThemeSettingsChatPreviewItem.swift @@ -143,7 +143,7 @@ class ThemeSettingsChatPreviewItemNode: ListViewItemNode { return { item, params, neighbors in var updatedBackgroundSignal: Signal<(UIImage?, Bool)?, NoError>? if currentItem?.wallpaper != item.wallpaper { - updatedBackgroundSignal = chatControllerBackgroundImageSignal(wallpaper: item.wallpaper, mediaBox: item.context.sharedContext.accountManager.mediaBox) + updatedBackgroundSignal = chatControllerBackgroundImageSignal(wallpaper: item.wallpaper, mediaBox: item.context.sharedContext.accountManager.mediaBox, accountMediaBox: item.context.account.postbox.mediaBox) } let insets: UIEdgeInsets diff --git a/submodules/TelegramPresentationData/Sources/ChatControllerBackgroundNode.swift b/submodules/TelegramPresentationData/Sources/ChatControllerBackgroundNode.swift index ea05ba574d..f72ccd56f1 100644 --- a/submodules/TelegramPresentationData/Sources/ChatControllerBackgroundNode.swift +++ b/submodules/TelegramPresentationData/Sources/ChatControllerBackgroundNode.swift @@ -106,7 +106,7 @@ public func chatControllerBackgroundImage(theme: PresentationTheme?, wallpaper i private var signalBackgroundImageForWallpaper: (TelegramWallpaper, Bool, UIImage)? -public func chatControllerBackgroundImageSignal(wallpaper: TelegramWallpaper, mediaBox: MediaBox) -> Signal<(UIImage?, Bool)?, NoError> { +public func chatControllerBackgroundImageSignal(wallpaper: TelegramWallpaper, mediaBox: MediaBox, accountMediaBox: MediaBox) -> Signal<(UIImage?, Bool)?, NoError> { var backgroundImage: UIImage? if wallpaper == signalBackgroundImageForWallpaper?.0, (wallpaper.settings?.blur ?? false) == signalBackgroundImageForWallpaper?.1, let image = signalBackgroundImageForWallpaper?.2 { return .single((image, true)) @@ -175,7 +175,16 @@ public func chatControllerBackgroundImageSignal(wallpaper: TelegramWallpaper, me } case let .file(file): if wallpaper.isPattern, let color = file.settings.color, let intensity = file.settings.intensity { - return mediaBox.cachedResourceRepresentation(file.file.resource, representation: CachedPatternWallpaperRepresentation(color: color, bottomColor: file.settings.bottomColor, intensity: intensity, rotation: file.settings.rotation), complete: true, fetch: true, attemptSynchronously: true) + let representation = CachedPatternWallpaperRepresentation(color: color, bottomColor: file.settings.bottomColor, intensity: intensity, rotation: file.settings.rotation) + + 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) |> take(1) |> mapToSignal { data -> Signal<(UIImage?, Bool)?, NoError> in if data.complete { @@ -205,7 +214,7 @@ public func chatControllerBackgroundImageSignal(wallpaper: TelegramWallpaper, me } }) - return .single((interrimImage, false)) |> then(mediaBox.cachedResourceRepresentation(file.file.resource, representation: CachedPatternWallpaperRepresentation(color: color, bottomColor: file.settings.bottomColor, intensity: intensity, rotation: file.settings.rotation), complete: true, fetch: true, attemptSynchronously: false) + return .single((interrimImage, false)) |> then(effectiveMediaBox.cachedResourceRepresentation(file.file.resource, representation: CachedPatternWallpaperRepresentation(color: color, bottomColor: file.settings.bottomColor, intensity: intensity, rotation: file.settings.rotation), complete: true, fetch: true, attemptSynchronously: false) |> map { data -> (UIImage?, Bool)? in return (UIImage(contentsOfFile: data.path)?.precomposed(), true) }) @@ -216,7 +225,16 @@ public func chatControllerBackgroundImageSignal(wallpaper: TelegramWallpaper, me } } else { if file.settings.blur { - return mediaBox.cachedResourceRepresentation(file.file.resource, representation: CachedBlurredWallpaperRepresentation(), complete: true, fetch: true, attemptSynchronously: true) + 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) @@ -227,10 +245,18 @@ public func chatControllerBackgroundImageSignal(wallpaper: TelegramWallpaper, me |> afterNext { image in cacheWallpaper(image?.0) } - } else if let path = mediaBox.completedResourcePath(file.file.resource) { - return .single((UIImage(contentsOfFile: path)?.precomposed(), true)) - |> afterNext { image in - cacheWallpaper(image?.0) + } else { + var path: String? + if let maybePath = mediaBox.completedResourcePath(file.file.resource) { + path = maybePath + } else if let maybePath = accountMediaBox.completedResourcePath(file.file.resource) { + path = maybePath + } + if let path = path { + return .single((UIImage(contentsOfFile: path)?.precomposed(), true)) + |> afterNext { image in + cacheWallpaper(image?.0) + } } } } diff --git a/submodules/WallpaperResources/Sources/WallpaperCache.swift b/submodules/WallpaperResources/Sources/WallpaperCache.swift index c21875181e..5d01c54c3d 100644 --- a/submodules/WallpaperResources/Sources/WallpaperCache.swift +++ b/submodules/WallpaperResources/Sources/WallpaperCache.swift @@ -26,11 +26,11 @@ public final class CachedWallpaper: PostboxCoding { private let collectionSpec = ItemCacheCollectionSpec(lowWaterItemCount: 10000, highWaterItemCount: 20000) -public func cachedWallpaper(account: Account, slug: String, settings: WallpaperSettings?) -> Signal { +public func cachedWallpaper(account: Account, slug: String, settings: WallpaperSettings?, update: Bool = false) -> Signal { return account.postbox.transaction { transaction -> Signal in let key = ValueBoxKey(length: 8) key.setInt64(0, value: Int64(bitPattern: slug.persistentHashValue)) - if let entry = transaction.retrieveItemCacheEntry(id: ItemCacheEntryId(collectionId: ApplicationSpecificItemCacheCollectionId.cachedWallpapers, key: key)) as? CachedWallpaper { + if !update, let entry = transaction.retrieveItemCacheEntry(id: ItemCacheEntryId(collectionId: ApplicationSpecificItemCacheCollectionId.cachedWallpapers, key: key)) as? CachedWallpaper { if let settings = settings { return .single(CachedWallpaper(wallpaper: entry.wallpaper.withUpdatedSettings(settings))) } else {