From a5043cbd69d40144aca83a3c197fd13e13e37c2e Mon Sep 17 00:00:00 2001 From: Stefan Ceriu Date: Mon, 18 Dec 2023 16:02:24 +0200 Subject: [PATCH] Fixes #2259 - Fix flipped notification placeholder avatars on iOS 17.2+ --- .../Extensions/UNNotificationContent.swift | 49 ++++++++++++------- changelog.d/2259.bugfix | 1 + 2 files changed, 33 insertions(+), 17 deletions(-) create mode 100644 changelog.d/2259.bugfix diff --git a/ElementX/Sources/Other/Extensions/UNNotificationContent.swift b/ElementX/Sources/Other/Extensions/UNNotificationContent.swift index df21c19f0..b67ffac46 100644 --- a/ElementX/Sources/Other/Extensions/UNNotificationContent.swift +++ b/ElementX/Sources/Other/Extensions/UNNotificationContent.swift @@ -189,10 +189,11 @@ extension UNMutableNotificationContent { return updatedContent.mutableCopy() as! UNMutableNotificationContent } + @MainActor private func getPlaceholderAvatarImageData(name: String, id: String) async -> Data? { // The version value is used in case the design of the placeholder is updated to force a replacement - let isIOS17Available = isIOS17Available() - let prefix = "notification_placeholder\(isIOS17Available ? "V3" : "V2")" + let shouldFlipAvatar = shouldFlipAvatar() + let prefix = "notification_placeholder\(shouldFlipAvatar ? "V5" : "V4")" let fileName = "\(prefix)_\(name)_\(id).png" if let data = try? Data(contentsOf: URL.temporaryDirectory.appendingPathComponent(fileName)) { MXLog.info("Found existing notification icon placeholder") @@ -204,25 +205,25 @@ extension UNMutableNotificationContent { contentID: id) .clipShape(Circle()) .frame(width: 50, height: 50) - let renderer = await ImageRenderer(content: image) - guard let image = await renderer.uiImage else { + let renderer = ImageRenderer(content: image) + + // Specify the scale so the image is rendered correctly. We don't have access to the screen + // here so a hardcoded 3.0 will have to do + renderer.scale = 3.0 + + guard let image = renderer.uiImage else { MXLog.info("Generating notification icon placeholder failed") return nil } - + let data: Data? - // On simulator and macOS the image is rendered correctly - // But on other devices before iOS 17 is rendered upside down so we need to flip it - #if targetEnvironment(simulator) - data = image.pngData() - #else - if ProcessInfo.processInfo.isiOSAppOnMac || isIOS17Available { - data = image.pngData() - } else { + + if shouldFlipAvatar { data = image.flippedVertically().pngData() + } else { + data = image.pngData() } - #endif - + if let data { do { // cache image data @@ -235,12 +236,26 @@ extension UNMutableNotificationContent { return data } - private func isIOS17Available() -> Bool { + /// On simulators and macOS the image is rendered correctly + /// On devices before iOS 17 and after iOS 17.2 it's rendered upside down and needs to be flipped + private func shouldFlipAvatar() -> Bool { + #if targetEnvironment(simulator) + return false + #else + if ProcessInfo.processInfo.isiOSAppOnMac { + return false + } + guard let version = Version(UIDevice.current.systemVersion) else { return false } - return version.major >= 17 + if version >= Version(17, 0, 0), version < Version(17, 2, 0) { + return false + } + + return true + #endif } } diff --git a/changelog.d/2259.bugfix b/changelog.d/2259.bugfix new file mode 100644 index 000000000..4b292a697 --- /dev/null +++ b/changelog.d/2259.bugfix @@ -0,0 +1 @@ +Fix flipped notification placeholder avatars on iOS 17.2+ \ No newline at end of file