From b3249c736068519a04145e6eaad7436433d23fd3 Mon Sep 17 00:00:00 2001 From: Jayden <95338403+linjie-firework@users.noreply.github.com> Date: Fri, 18 Aug 2023 20:54:03 +0800 Subject: [PATCH] Bug fix: changeAppLanguage API affects rendering SVG images on iOS (#12) --- .../xcshareddata/swiftpm/Package.resolved | 28 +++++++------- .../AppLanguage/AppLanguageManager.swift | 36 ++++++++++-------- .../Extensions/UIKit/UIView+AppLanguage.swift | 10 ++++- .../Extensions/UIKit/CALayer+LayoutFlip.swift | 37 +++++++------------ .../Extensions/UIKit/UIView+LayoutFlip.swift | 3 ++ 5 files changed, 60 insertions(+), 54 deletions(-) diff --git a/Sample/SampleApp.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved b/Sample/SampleApp.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved index 358a0cc..6710b13 100644 --- a/Sample/SampleApp.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/Sample/SampleApp.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -5,8 +5,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/AgoraIO/AgoraRtcEngine_iOS.git", "state" : { - "revision" : "2e035dbfd39dea92ba9efd6447cd976fba85d5ff", - "version" : "4.2.2" + "revision" : "13f007dff53e2c670ad645fae43e3fcea8f4df6c", + "version" : "4.1.1" } }, { @@ -32,8 +32,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/loopsocial/firework_ios_sdk_agora_support.git", "state" : { - "revision" : "d0ce3b68a30efca753bafd6364a63c0602815459", - "version" : "0.4.2" + "revision" : "4082de71251b8afe6c8fb5a4c536a2745c20eb07", + "version" : "0.4.1" } }, { @@ -77,8 +77,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/google/GoogleAppMeasurement.git", "state" : { - "revision" : "03b9beee1a61f62d32c521e172e192a1663a5e8b", - "version" : "10.13.0" + "revision" : "62e3a0c09a75e2637f5300d46f05a59313f1c286", + "version" : "10.11.0" } }, { @@ -86,8 +86,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/google/GoogleUtilities.git", "state" : { - "revision" : "c38ce365d77b04a9a300c31061c5227589e5597b", - "version" : "7.11.5" + "revision" : "58d03d22beae762eaddbd30cb5a61af90d4b309f", + "version" : "7.11.3" } }, { @@ -104,8 +104,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/google/promises.git", "state" : { - "revision" : "e70e889c0196c76d22759eb50d6a0270ca9f1d9e", - "version" : "2.3.1" + "revision" : "ec957ccddbcc710ccc64c9dcbd4c7006fcf8b73a", + "version" : "2.2.0" } }, { @@ -113,8 +113,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/googleads/swift-package-manager-google-mobile-ads.git", "state" : { - "revision" : "1a6faf6b9b82ddf8780f678745381b8628711077", - "version" : "10.9.0" + "revision" : "a6e24f2167295d95371bfc3049e47381a73a9e43", + "version" : "10.7.0" } }, { @@ -122,8 +122,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/googleads/swift-package-manager-google-user-messaging-platform.git", "state" : { - "revision" : "129fa838520cd02174f890ae0cfe0242e60714ae", - "version" : "2.1.0" + "revision" : "3b924ce3313a5fd2fc6a8dc889a8c38f76890fe3", + "version" : "2.0.0" } } ], diff --git a/Sources/FireworkVideoUI/AppLanguage/AppLanguageManager.swift b/Sources/FireworkVideoUI/AppLanguage/AppLanguageManager.swift index 6665b7a..89cac35 100644 --- a/Sources/FireworkVideoUI/AppLanguage/AppLanguageManager.swift +++ b/Sources/FireworkVideoUI/AppLanguage/AppLanguageManager.swift @@ -8,7 +8,7 @@ import UIKit private let appLanguageStorageKey = "firework_sdk_app_language_storage_key" -enum AppLanguageLayoutDirection: Int { +enum LanguageLayoutDirection: Int { case ltr, rtl, unsupported } @@ -54,7 +54,7 @@ public class AppLanguageManager { return LanguageExtension.getLanguageCode(language) } - var appLanguageLayoutDirection: AppLanguageLayoutDirection? { + var appLanguageLayoutDirection: LanguageLayoutDirection? { guard let languageCode = appLanguageCode else { return nil } @@ -68,24 +68,30 @@ public class AppLanguageManager { return .unsupported } } - - var shouldHorizontalFlip: Bool { - guard let appLanguageCode = appLanguageCode else { - return false + + var systemLanguageLayoutDirection: LanguageLayoutDirection? { + guard let languageCode = systemLanguageCode else { + return nil } - - guard let systemLanguageCode = systemLanguageCode else { - return false + let direction = Locale.characterDirection(forLanguage: languageCode) + switch direction { + case .leftToRight: + return .ltr + case .rightToLeft: + return .rtl + default: + return .unsupported } + } - let appLanguageDirection = Locale.characterDirection(forLanguage: appLanguageCode) - let systemLanguageDirection = Locale.characterDirection(forLanguage: systemLanguageCode) - - if appLanguageDirection == .leftToRight, systemLanguageDirection == .rightToLeft { + var shouldHorizontalFlip: Bool { + if appLanguageLayoutDirection == .rtl, + systemLanguageLayoutDirection == .ltr { return true } - - if appLanguageDirection == .rightToLeft, systemLanguageDirection == .leftToRight { + + if appLanguageLayoutDirection == .ltr, + systemLanguageLayoutDirection == .rtl { return true } diff --git a/Sources/FireworkVideoUI/AppLanguage/Extensions/UIKit/UIView+AppLanguage.swift b/Sources/FireworkVideoUI/AppLanguage/Extensions/UIKit/UIView+AppLanguage.swift index f4f62c5..8893eca 100644 --- a/Sources/FireworkVideoUI/AppLanguage/Extensions/UIKit/UIView+AppLanguage.swift +++ b/Sources/FireworkVideoUI/AppLanguage/Extensions/UIKit/UIView+AppLanguage.swift @@ -41,7 +41,15 @@ extension UIView { @objc func fw_semanticContentAttribute() -> UISemanticContentAttribute { if self.isIOSSDKView, AppLanguageManager.shared.shouldHorizontalFlip { - return .forceLeftToRight + let systemLanguageLayoutDirection = AppLanguageManager.shared.systemLanguageLayoutDirection ?? .unsupported + switch systemLanguageLayoutDirection { + case .ltr: + return .forceLeftToRight + case .rtl: + return .forceRightToLeft + case .unsupported: + return fw_semanticContentAttribute() + } } return fw_semanticContentAttribute() diff --git a/Sources/FireworkVideoUI/LayoutFlip/Extensions/UIKit/CALayer+LayoutFlip.swift b/Sources/FireworkVideoUI/LayoutFlip/Extensions/UIKit/CALayer+LayoutFlip.swift index 252eefa..5d4ffe4 100644 --- a/Sources/FireworkVideoUI/LayoutFlip/Extensions/UIKit/CALayer+LayoutFlip.swift +++ b/Sources/FireworkVideoUI/LayoutFlip/Extensions/UIKit/CALayer+LayoutFlip.swift @@ -6,8 +6,6 @@ import UIKit -private var gHasEnabledHorizontalFlip = false - extension CALayer { private struct AssociatedKeys { static var basicTransform = "basicTransform" @@ -42,18 +40,20 @@ extension CALayer { } set { - if objc_getAssociatedObject(self, &AssociatedKeys.basicTransform) == nil, - CGAffineTransformIsIdentity(newValue) { + let newBasicTransformValue = NSValue(cgAffineTransform: newValue) + if (objc_getAssociatedObject(self, &AssociatedKeys.basicTransform) as? NSValue) + == newBasicTransformValue { return } - + + let currentAffineTransform = self.affineTransform() objc_setAssociatedObject( self, &AssociatedKeys.basicTransform, - NSValue(cgAffineTransform: newValue), + newBasicTransformValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC ) - self.setAffineTransform(self.affineTransform()) + self.setAffineTransform(currentAffineTransform) } } @@ -66,7 +66,7 @@ extension CALayer { set { objc_setAssociatedObject( self, - &AssociatedKeys.basicTransform, + &AssociatedKeys.isRenderStartLayer, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC ) @@ -74,11 +74,7 @@ extension CALayer { } @objc func fw_setAffineTransform(_ affineTransform: CGAffineTransform) { - if !gHasEnabledHorizontalFlip && LayoutFlipManager.shared.enableHorizontalFlip { - gHasEnabledHorizontalFlip = true - } - - if gHasEnabledHorizontalFlip { + if objc_getAssociatedObject(self, &AssociatedKeys.basicTransform) != nil { objc_setAssociatedObject( self, &AssociatedKeys.affineTransform, @@ -92,11 +88,7 @@ extension CALayer { } @objc func fw_affineTransform() -> CGAffineTransform { - if !gHasEnabledHorizontalFlip && LayoutFlipManager.shared.enableHorizontalFlip { - gHasEnabledHorizontalFlip = true - } - - if gHasEnabledHorizontalFlip { + if objc_getAssociatedObject(self, &AssociatedKeys.basicTransform) != nil { if let value = objc_getAssociatedObject(self, &AssociatedKeys.affineTransform) as? NSValue { return value.cgAffineTransformValue } @@ -107,7 +99,8 @@ extension CALayer { } @objc func fw_add(_ anim: CAAnimation, forKey key: String?) { - if let basicAnim = anim as? CABasicAnimation, + if objc_getAssociatedObject(self, &AssociatedKeys.basicTransform) != nil, + let basicAnim = anim as? CABasicAnimation, let keyPath = basicAnim.keyPath, keyPath.starts(with: "transform.scale"), self.basicTransform.a == -1, @@ -121,11 +114,7 @@ extension CALayer { } @objc func fw_render(in ctx: CGContext) { - if !gHasEnabledHorizontalFlip && LayoutFlipManager.shared.enableHorizontalFlip { - gHasEnabledHorizontalFlip = true - } - - if gHasEnabledHorizontalFlip { + if objc_getAssociatedObject(self, &AssociatedKeys.basicTransform) != nil { var isRenderStartLayer = true var allSuperLayerTransform = basicTransform var layer = self diff --git a/Sources/FireworkVideoUI/LayoutFlip/Extensions/UIKit/UIView+LayoutFlip.swift b/Sources/FireworkVideoUI/LayoutFlip/Extensions/UIKit/UIView+LayoutFlip.swift index 5fdc1fd..c23bb00 100644 --- a/Sources/FireworkVideoUI/LayoutFlip/Extensions/UIKit/UIView+LayoutFlip.swift +++ b/Sources/FireworkVideoUI/LayoutFlip/Extensions/UIKit/UIView+LayoutFlip.swift @@ -138,6 +138,9 @@ extension UIView { } func renewLayerTransformForceRecursively(_ forceRecursively: Bool) { + if !isIOSSDKView { + return + } updateCalculatedViewType() let updatedViewType = calculatedViewType let superViewCalculatedViewType = superview?.calculatedViewType ?? .auto