From aba24a68dc4175a39885b438464d0a555969ea8a Mon Sep 17 00:00:00 2001 From: Ilya Laktyushin Date: Thu, 21 Dec 2023 02:46:22 +0400 Subject: [PATCH] Various fixes --- .../Sources/DrawingMessageRenderer.swift | 32 +++--- .../Sources/MediaEditorUtils.swift | 104 ++++++++++++++---- .../Sources/MediaEditorScreen.swift | 9 +- 3 files changed, 110 insertions(+), 35 deletions(-) diff --git a/submodules/TelegramUI/Components/MediaEditor/Sources/DrawingMessageRenderer.swift b/submodules/TelegramUI/Components/MediaEditor/Sources/DrawingMessageRenderer.swift index 095f03133a..c39d2d363c 100644 --- a/submodules/TelegramUI/Components/MediaEditor/Sources/DrawingMessageRenderer.swift +++ b/submodules/TelegramUI/Components/MediaEditor/Sources/DrawingMessageRenderer.swift @@ -11,28 +11,28 @@ import WallpaperBackgroundNode public final class DrawingWallpaperRenderer { private let context: AccountContext - private let customDayWallpaper: TelegramWallpaper? - private let customNightWallpaper: TelegramWallpaper? + private let dayWallpaper: TelegramWallpaper? + private let nightWallpaper: TelegramWallpaper? private let wallpaperBackgroundNode: WallpaperBackgroundNode private let darkWallpaperBackgroundNode: WallpaperBackgroundNode - public init (context: AccountContext, customDayWallpaper: TelegramWallpaper?, customNightWallpaper: TelegramWallpaper?) { + public init (context: AccountContext, dayWallpaper: TelegramWallpaper?, nightWallpaper: TelegramWallpaper?) { self.context = context - self.customDayWallpaper = customDayWallpaper - self.customNightWallpaper = customNightWallpaper + self.dayWallpaper = dayWallpaper + self.nightWallpaper = nightWallpaper self.wallpaperBackgroundNode = createWallpaperBackgroundNode(context: context, forChatDisplay: true, useSharedAnimationPhase: false) self.wallpaperBackgroundNode.displaysAsynchronously = false - let wallpaper = self.customDayWallpaper ?? context.sharedContext.currentPresentationData.with { $0 }.chatWallpaper + let wallpaper = self.dayWallpaper ?? context.sharedContext.currentPresentationData.with { $0 }.chatWallpaper self.wallpaperBackgroundNode.update(wallpaper: wallpaper, animated: false) self.darkWallpaperBackgroundNode = createWallpaperBackgroundNode(context: context, forChatDisplay: true, useSharedAnimationPhase: false) self.darkWallpaperBackgroundNode.displaysAsynchronously = false let darkTheme = defaultDarkColorPresentationTheme - let darkWallpaper = self.customNightWallpaper ?? darkTheme.chat.defaultWallpaper + let darkWallpaper = self.nightWallpaper ?? darkTheme.chat.defaultWallpaper self.darkWallpaperBackgroundNode.update(wallpaper: darkWallpaper, animated: false) } @@ -40,13 +40,15 @@ public final class DrawingWallpaperRenderer { self.updateLayout(size: CGSize(width: 360.0, height: 640.0)) let resultSize = CGSize(width: 1080, height: 1920) - self.generate(view: self.wallpaperBackgroundNode.view) { dayImage in - if self.customDayWallpaper != nil && self.customNightWallpaper == nil { - completion(resultSize, dayImage, nil, nil) - } else { - Queue.mainQueue().justDispatch { - self.generate(view: self.darkWallpaperBackgroundNode.view) { nightImage in - completion(resultSize, dayImage, nightImage, nil) + Queue.mainQueue().justDispatch { + self.generate(view: self.wallpaperBackgroundNode.view) { dayImage in + if self.dayWallpaper != nil && self.nightWallpaper == nil { + completion(resultSize, dayImage, nil, nil) + } else { + Queue.mainQueue().justDispatch { + self.generate(view: self.darkWallpaperBackgroundNode.view) { nightImage in + completion(resultSize, dayImage, nightImage, nil) + } } } } @@ -129,7 +131,7 @@ public final class DrawingMessageRenderer { let layout = ContainerViewLayout(size: CGSize(width: 360.0, height: 640.0), metrics: LayoutMetrics(widthClass: .compact, heightClass: .compact, orientation: .portrait), deviceMetrics: .iPhoneX, intrinsicInsets: .zero, safeInsets: .zero, additionalInsets: .zero, statusBarHeight: 0.0, inputHeight: nil, inputHeightIsInteractivellyChanging: false, inVoiceOver: false) let size = self.updateMessagesLayout(layout: layout, presentationData: mockPresentationData) - Queue.mainQueue().after(0.03, { + Queue.mainQueue().after(0.05, { self.generate(size: size) { image in completion(size, image) } diff --git a/submodules/TelegramUI/Components/MediaEditor/Sources/MediaEditorUtils.swift b/submodules/TelegramUI/Components/MediaEditor/Sources/MediaEditorUtils.swift index aa265ace01..0270ec0a7e 100644 --- a/submodules/TelegramUI/Components/MediaEditor/Sources/MediaEditorUtils.swift +++ b/submodules/TelegramUI/Components/MediaEditor/Sources/MediaEditorUtils.swift @@ -4,6 +4,8 @@ import AVFoundation import SwiftSignalKit import TelegramCore import AccountContext +import TelegramUIPreferences +import TelegramPresentationData extension AVPlayer { func fadeVolume(from: Float, to: Float, duration: Float, completion: (() -> Void)? = nil) -> SwiftSignalKit.Timer? { @@ -133,39 +135,103 @@ func getTextureImage(device: MTLDevice, texture: MTLTexture, mirror: Bool = fals } public func getChatWallpaperImage(context: AccountContext, messageId: EngineMessage.Id) -> Signal<(CGSize, UIImage?, UIImage?), NoError> { - return context.account.postbox.transaction { transaction -> TelegramWallpaper? in + let themeSettings = context.sharedContext.accountManager.sharedData(keys: [ApplicationSpecificSharedDataKeys.presentationThemeSettings]) + |> map { sharedData -> PresentationThemeSettings in + let themeSettings: PresentationThemeSettings + if let current = sharedData.entries[ApplicationSpecificSharedDataKeys.presentationThemeSettings]?.get(PresentationThemeSettings.self) { + themeSettings = current + } else { + themeSettings = PresentationThemeSettings.defaultSettings + } + return themeSettings + } + + let peerWallpaper = context.account.postbox.transaction { transaction -> TelegramWallpaper? in return (transaction.getPeerCachedData(peerId: messageId.peerId) as? CachedChannelData)?.wallpaper } - |> mapToSignal { wallpaper -> Signal<(TelegramWallpaper?, TelegramWallpaper?), NoError> in - if let wallpaper, case let .emoticon(emoticon) = wallpaper { - return context.engine.themes.getChatThemes(accountManager: context.sharedContext.accountManager) - |> map { themes -> (TelegramWallpaper?, TelegramWallpaper?) in - if let theme = themes.first(where: { $0.emoticon?.strippedEmoji == emoticon.strippedEmoji }) { - if let dayMatch = theme.settings?.first(where: { $0.baseTheme == .classic || $0.baseTheme == .day }) { - if let dayWallpaper = dayMatch.wallpaper { - var nightWallpaper: TelegramWallpaper? - if let nightMatch = theme.settings?.first(where: { $0.baseTheme == .night || $0.baseTheme == .tinted }) { - nightWallpaper = nightMatch.wallpaper + + return combineLatest(themeSettings, peerWallpaper) + |> mapToSignal { themeSettings, peerWallpaper -> Signal<(TelegramWallpaper?, TelegramWallpaper?), NoError> in + var currentColors = themeSettings.themeSpecificAccentColors[themeSettings.theme.index] + if let colors = currentColors, colors.baseColor == .theme { + currentColors = nil + } + + let themeSpecificWallpaper = (themeSettings.themeSpecificChatWallpapers[coloredThemeIndex(reference: themeSettings.theme, accentColor: currentColors)] ?? themeSettings.themeSpecificChatWallpapers[themeSettings.theme.index]) + + let dayWallpaper: TelegramWallpaper + if let themeSpecificWallpaper = themeSpecificWallpaper { + dayWallpaper = themeSpecificWallpaper + } else { + let theme = makePresentationTheme(mediaBox: context.sharedContext.accountManager.mediaBox, themeReference: themeSettings.theme, accentColor: currentColors?.color, bubbleColors: currentColors?.customBubbleColors ?? [], wallpaper: currentColors?.wallpaper, baseColor: currentColors?.baseColor, preview: true) ?? defaultPresentationTheme + dayWallpaper = theme.chat.defaultWallpaper + } + + var nightWallpaper: TelegramWallpaper? + + let automaticTheme = themeSettings.automaticThemeSwitchSetting.theme + let effectiveColors = themeSettings.themeSpecificAccentColors[automaticTheme.index] + let nightThemeSpecificWallpaper = (themeSettings.themeSpecificChatWallpapers[coloredThemeIndex(reference: automaticTheme, accentColor: effectiveColors)] ?? themeSettings.themeSpecificChatWallpapers[automaticTheme.index]) + + var preferredBaseTheme: TelegramBaseTheme? + if let baseTheme = themeSettings.themePreferredBaseTheme[automaticTheme.index], [.night, .tinted].contains(baseTheme) { + preferredBaseTheme = baseTheme + } else { + preferredBaseTheme = .night + } + + let darkTheme = makePresentationTheme(mediaBox: context.sharedContext.accountManager.mediaBox, themeReference: automaticTheme, baseTheme: preferredBaseTheme, accentColor: effectiveColors?.color, bubbleColors: effectiveColors?.customBubbleColors ?? [], wallpaper: effectiveColors?.wallpaper, baseColor: effectiveColors?.baseColor, serviceBackgroundColor: defaultServiceBackgroundColor) ?? defaultPresentationTheme + + if let nightThemeSpecificWallpaper = nightThemeSpecificWallpaper { + nightWallpaper = nightThemeSpecificWallpaper + } else { + switch dayWallpaper { + case .builtin, .color, .gradient: + nightWallpaper = darkTheme.chat.defaultWallpaper + case .file: + if dayWallpaper.isPattern { + nightWallpaper = darkTheme.chat.defaultWallpaper + } else { + nightWallpaper = nil + } + default: + nightWallpaper = nil + } + } + + if let peerWallpaper { + if case let .emoticon(emoticon) = peerWallpaper { + return context.engine.themes.getChatThemes(accountManager: context.sharedContext.accountManager) + |> map { themes -> (TelegramWallpaper?, TelegramWallpaper?) in + if let theme = themes.first(where: { $0.emoticon?.strippedEmoji == emoticon.strippedEmoji }) { + if let dayMatch = theme.settings?.first(where: { $0.baseTheme == .classic || $0.baseTheme == .day }) { + if let peerDayWallpaper = dayMatch.wallpaper { + var peerNightWallpaper: TelegramWallpaper? + if let nightMatch = theme.settings?.first(where: { $0.baseTheme == .night || $0.baseTheme == .tinted }) { + peerNightWallpaper = nightMatch.wallpaper + } + return (peerDayWallpaper, peerNightWallpaper) + } else { + return (dayWallpaper, nightWallpaper) } - return (dayWallpaper, nightWallpaper) } else { - return (nil, nil) + return (dayWallpaper, nightWallpaper) } } else { - return (nil, nil) + return (dayWallpaper, nightWallpaper) } - } else { - return (nil, nil) } + } else { + return .single((peerWallpaper, nil)) } } else { - return .single((wallpaper, nil)) + return .single((dayWallpaper, nightWallpaper)) } } - |> mapToSignal { customDayWallpaper, customNightWallpaper -> Signal<(CGSize, UIImage?, UIImage?), NoError> in + |> mapToSignal { dayWallpaper, nightWallpaper -> Signal<(CGSize, UIImage?, UIImage?), NoError> in return Signal { subscriber in Queue.mainQueue().async { - let wallpaperRenderer = DrawingWallpaperRenderer(context: context, customDayWallpaper: customDayWallpaper, customNightWallpaper: customNightWallpaper) + let wallpaperRenderer = DrawingWallpaperRenderer(context: context, dayWallpaper: dayWallpaper, nightWallpaper: nightWallpaper) wallpaperRenderer.render { size, image, darkImage, mediaRect in subscriber.putNext((size, image, darkImage)) subscriber.putCompletion() diff --git a/submodules/TelegramUI/Components/MediaEditorScreen/Sources/MediaEditorScreen.swift b/submodules/TelegramUI/Components/MediaEditorScreen/Sources/MediaEditorScreen.swift index 990144081d..e9a09fad9f 100644 --- a/submodules/TelegramUI/Components/MediaEditorScreen/Sources/MediaEditorScreen.swift +++ b/submodules/TelegramUI/Components/MediaEditorScreen/Sources/MediaEditorScreen.swift @@ -2372,6 +2372,9 @@ public final class MediaEditorScreen: ViewController, UIDropInteractionDelegate if let initialVideoPosition = controller.initialVideoPosition { mediaEditor.seek(initialVideoPosition, andPlay: true) } + if case .message = subject, self.context.sharedContext.currentPresentationData.with({$0}).autoNightModeTriggered { + mediaEditor.setNightTheme(true) + } mediaEditor.attachPreviewView(self.previewView) mediaEditor.valuesUpdated = { [weak self] values in if let self, let controller = self.controller, values.gradientColors != nil, controller.previousSavedValues != values { @@ -2468,7 +2471,11 @@ public final class MediaEditorScreen: ViewController, UIDropInteractionDelegate let fraction = max(size.width, size.height) / 353.0 messageEntity.scale = min(6.0, 3.3 * fraction) - self.entitiesView.add(messageEntity, announce: false) + if let entityView = self.entitiesView.add(messageEntity, announce: false) as? DrawingStickerEntityView { + if isNightTheme { + entityView.isNightTheme = true + } + } } self.readyValue.set(.single(true))