From 800b9d90238e385df8bb69f5e94887e2d0e7a695 Mon Sep 17 00:00:00 2001 From: Joe Mattiello Date: Sun, 7 Feb 2021 19:37:08 -0500 Subject: [PATCH] Redo of Enables application extension API only #557 (#681) * Enables application extension API only * Replace UIApp...leftToRight with Locale extension For use in Extensions that don't support UIApp Co-authored-by: Lukas Schmidt --- Examples/SwiftUIMatchExample.swift | 2 +- Hero.xcodeproj/project.pbxproj | 10 ++++++++ Sources/Extensions/Locale+Hero.swift | 24 +++++++++++++++++++ .../Extensions/UIViewController+Hero.swift | 2 ++ .../Preprocessors/CascadePreprocessor.swift | 4 ++-- .../DefaultAnimationPreprocessor.swift | 12 ++++++++-- .../Transition/HeroTransition+Complete.swift | 2 ++ 7 files changed, 51 insertions(+), 5 deletions(-) create mode 100644 Sources/Extensions/Locale+Hero.swift diff --git a/Examples/SwiftUIMatchExample.swift b/Examples/SwiftUIMatchExample.swift index fb3f9a3a..7f3dc870 100644 --- a/Examples/SwiftUIMatchExample.swift +++ b/Examples/SwiftUIMatchExample.swift @@ -41,7 +41,7 @@ struct ImageInfo: Identifiable { struct ImagesTableView: View { var dismiss: (() -> Void)? var onTapRow: ((ImageInfo)->())? - + @State var images = (0...9).map{ ImageInfo(id: $0, name: "Unsplash\($0)") } var body: some View { diff --git a/Hero.xcodeproj/project.pbxproj b/Hero.xcodeproj/project.pbxproj index 7e7d5360..2c168106 100644 --- a/Hero.xcodeproj/project.pbxproj +++ b/Hero.xcodeproj/project.pbxproj @@ -128,6 +128,8 @@ B1D816EC1EF5A720007B9776 /* HeroTransition+Animate.swift in Sources */ = {isa = PBXBuildFile; fileRef = B1D816EA1EF5A720007B9776 /* HeroTransition+Animate.swift */; }; B1D834041F02E7C0009E1E36 /* ConditionalPreprocessor.swift in Sources */ = {isa = PBXBuildFile; fileRef = B1D834031F02E7C0009E1E36 /* ConditionalPreprocessor.swift */; }; B1D834051F02E7C0009E1E36 /* ConditionalPreprocessor.swift in Sources */ = {isa = PBXBuildFile; fileRef = B1D834031F02E7C0009E1E36 /* ConditionalPreprocessor.swift */; }; + B35264CE2454FEF300D33861 /* Locale+Hero.swift in Sources */ = {isa = PBXBuildFile; fileRef = B35264CD2454FEF300D33861 /* Locale+Hero.swift */; }; + B35264CF2454FEF300D33861 /* Locale+Hero.swift in Sources */ = {isa = PBXBuildFile; fileRef = B35264CD2454FEF300D33861 /* Locale+Hero.swift */; }; DBA05BB41A704A4A17967918 /* Pods_HeroTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 841FFA357ACB279D3F74CDEE /* Pods_HeroTests.framework */; }; F482F0BE235D7808002E97ED /* UIColor+HexString.swift in Sources */ = {isa = PBXBuildFile; fileRef = F482F0BD235D7808002E97ED /* UIColor+HexString.swift */; }; F482F0BF235D7808002E97ED /* UIColor+HexString.swift in Sources */ = {isa = PBXBuildFile; fileRef = F482F0BD235D7808002E97ED /* UIColor+HexString.swift */; }; @@ -285,6 +287,7 @@ B1D816E71EF5A6FE007B9776 /* HeroTransition+Complete.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "HeroTransition+Complete.swift"; sourceTree = ""; }; B1D816EA1EF5A720007B9776 /* HeroTransition+Animate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "HeroTransition+Animate.swift"; sourceTree = ""; }; B1D834031F02E7C0009E1E36 /* ConditionalPreprocessor.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ConditionalPreprocessor.swift; sourceTree = ""; }; + B35264CD2454FEF300D33861 /* Locale+Hero.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Locale+Hero.swift"; sourceTree = ""; }; C377744CBFF1E24426E80F55 /* Pods-HeroExamples.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-HeroExamples.release.xcconfig"; path = "Pods/Target Support Files/Pods-HeroExamples/Pods-HeroExamples.release.xcconfig"; sourceTree = ""; }; C51A6465EC2CB38D82F28B93 /* Pods-HeroTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-HeroTests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-HeroTests/Pods-HeroTests.debug.xcconfig"; sourceTree = ""; }; EEE340F89FF0A49DD23A5A6E /* Pods_HeroExamples.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_HeroExamples.framework; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -355,6 +358,7 @@ A37D7B471E2C0C9300AC1959 /* CAMediaTimingFunction+Hero.swift */, A37D7B481E2C0C9300AC1959 /* CG+Hero.swift */, A37D7B491E2C0C9300AC1959 /* DispatchQueue+Hero.swift */, + B35264CD2454FEF300D33861 /* Locale+Hero.swift */, A37D7B4A1E2C0C9300AC1959 /* UIKit+Hero.swift */, B101B2C91E561408007E7112 /* UIView+Hero.swift */, B101B2CD1E561421007E7112 /* UIViewController+Hero.swift */, @@ -981,6 +985,7 @@ 2D1F7FD61E49DD18004D944B /* DispatchQueue+Hero.swift in Sources */, 2D1F7FCB1E49DD08004D944B /* HeroTargetState.swift in Sources */, B17D888D1EF5A5500048D3E8 /* HeroTransition+UIViewControllerTransitioningDelegate.swift in Sources */, + B35264CF2454FEF300D33861 /* Locale+Hero.swift in Sources */, B1D816E01EF5A5DF007B9776 /* HeroViewControllerDelegate.swift in Sources */, B1193ABA1E525D12005B1C87 /* HeroAnimatorViewContext.swift in Sources */, 2D1F7FCA1E49DD08004D944B /* HeroPlugin.swift in Sources */, @@ -1050,6 +1055,7 @@ A37D7B4D1E2C0C9300AC1959 /* CAMediaTimingFunction+Hero.swift in Sources */, A37D7B5D1E2C0CBD00AC1959 /* HeroModifier.swift in Sources */, B17D888C1EF5A5500048D3E8 /* HeroTransition+UIViewControllerTransitioningDelegate.swift in Sources */, + B35264CE2454FEF300D33861 /* Locale+Hero.swift in Sources */, B1D816DF1EF5A5DF007B9776 /* HeroViewControllerDelegate.swift in Sources */, B1193AB91E525D12005B1C87 /* HeroAnimatorViewContext.swift in Sources */, A37D7B5B1E2C0CBD00AC1959 /* HeroDefaultAnimator.swift in Sources */, @@ -1175,6 +1181,7 @@ 2D1F7FC41E49DCB5004D944B /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { + APPLICATION_EXTENSION_API_ONLY = YES; CODE_SIGN_IDENTITY = ""; "CODE_SIGN_IDENTITY[sdk=appletvos*]" = ""; CURRENT_PROJECT_VERSION = 1; @@ -1203,6 +1210,7 @@ 2D1F7FC51E49DCB5004D944B /* Release */ = { isa = XCBuildConfiguration; buildSettings = { + APPLICATION_EXTENSION_API_ONLY = YES; CODE_SIGN_IDENTITY = ""; CURRENT_PROJECT_VERSION = 1; DEFINES_MODULE = YES; @@ -1270,6 +1278,7 @@ A306D3BC1E1C7A2E00B6C23A /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { + APPLICATION_EXTENSION_API_ONLY = YES; CLANG_ENABLE_MODULES = YES; CODE_SIGN_IDENTITY = ""; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = ""; @@ -1298,6 +1307,7 @@ A306D3BD1E1C7A2E00B6C23A /* Release */ = { isa = XCBuildConfiguration; buildSettings = { + APPLICATION_EXTENSION_API_ONLY = YES; CLANG_ENABLE_MODULES = YES; CODE_SIGN_IDENTITY = ""; CURRENT_PROJECT_VERSION = 1; diff --git a/Sources/Extensions/Locale+Hero.swift b/Sources/Extensions/Locale+Hero.swift new file mode 100644 index 00000000..3f5ea75d --- /dev/null +++ b/Sources/Extensions/Locale+Hero.swift @@ -0,0 +1,24 @@ +// +// Locale+Hero.swift +// Hero +// +// Created by Joseph Mattiello on 4/25/20. +// Copyright © 2020 Luke Zhao. All rights reserved. +// + +import Foundation + +internal extension Locale { + internal static var isDeviceLanguageRightToLeft: Bool { + let currentLocale: Locale = Locale.current + guard let code: String = currentLocale.languageCode else { + return false + } + let direction: Locale.LanguageDirection = Locale.characterDirection(forLanguage: code) + return (direction == .rightToLeft) + } + + internal static var isDeviceLanguageLeftToRight: Bool { + return !isDeviceLanguageRightToLeft + } +} diff --git a/Sources/Extensions/UIViewController+Hero.swift b/Sources/Extensions/UIViewController+Hero.swift index 0fe09443..ed792477 100644 --- a/Sources/Extensions/UIViewController+Hero.swift +++ b/Sources/Extensions/UIViewController+Hero.swift @@ -326,7 +326,9 @@ public extension HeroExtension where Base: UIViewController { parentVC.present(next, animated: false, completion: completion) } } else { + #if TARGET_IS_EXTENSION UIApplication.shared.keyWindow?.rootViewController = next + #endif } } } diff --git a/Sources/Preprocessors/CascadePreprocessor.swift b/Sources/Preprocessors/CascadePreprocessor.swift index 22f9f694..3c8b17ff 100644 --- a/Sources/Preprocessors/CascadePreprocessor.swift +++ b/Sources/Preprocessors/CascadePreprocessor.swift @@ -74,11 +74,11 @@ public enum CascadeDirection { } public static var leadingToTrailing: CascadeDirection { - return UIApplication.shared.userInterfaceLayoutDirection == .leftToRight ? .leftToRight : .rightToLeft + return !Locale.isDeviceLanguageRightToLeft ? .leftToRight : .rightToLeft } public static var trailingToLeading: CascadeDirection { - return UIApplication.shared.userInterfaceLayoutDirection == .leftToRight ? .rightToLeft : .leftToRight + return !Locale.isDeviceLanguageRightToLeft ? .rightToLeft : .leftToRight } private func topToBottomComperator(lhs: UIView, rhs: UIView) -> Bool { diff --git a/Sources/Preprocessors/DefaultAnimationPreprocessor.swift b/Sources/Preprocessors/DefaultAnimationPreprocessor.swift index 06feb0f9..f254beb5 100644 --- a/Sources/Preprocessors/DefaultAnimationPreprocessor.swift +++ b/Sources/Preprocessors/DefaultAnimationPreprocessor.swift @@ -39,12 +39,20 @@ public enum HeroDefaultAnimationType { } } + public static var leadingToTrailing: CascadeDirection { + return !Locale.isDeviceLanguageRightToLeft ? .leftToRight : .rightToLeft + } + + public static var trailingToLeading: CascadeDirection { + return !Locale.isDeviceLanguageRightToLeft ? .rightToLeft : .leftToRight + } + public static var leading: Direction { - return UIApplication.shared.userInterfaceLayoutDirection == .leftToRight ? .left : .right + return !Locale.isDeviceLanguageRightToLeft ? .left : .right } public static var trailing: Direction { - return UIApplication.shared.userInterfaceLayoutDirection == .leftToRight ? .right : .left + return !Locale.isDeviceLanguageRightToLeft ? .right : .left } } diff --git a/Sources/Transition/HeroTransition+Complete.swift b/Sources/Transition/HeroTransition+Complete.swift index cbc2e978..208b97b5 100644 --- a/Sources/Transition/HeroTransition+Complete.swift +++ b/Sources/Transition/HeroTransition+Complete.swift @@ -89,7 +89,9 @@ extension HeroTransition { if isPresenting != finished, !inContainerController, transitionContext != nil { // only happens when present a .overFullScreen VC // bug: http://openradar.appspot.com/radar?id=5320103646199808 + #if TARGET_IS_EXTENSION UIApplication.shared.keyWindow?.addSubview(isPresenting ? fromView : toView) + #endif } }