Skip to content

Commit

Permalink
Rework ASTraitCollection to Fix Warnings and Remove Boilerplate (#1211)
Browse files Browse the repository at this point in the history
* Clean up trait collection to fix a ton of warnings and remove code

* Support Xcode 9 (iOS 11 SDK)

* Hack harder
  • Loading branch information
Adlai-Holler authored Nov 6, 2018
1 parent 5c9815f commit e392f83
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 490 deletions.
4 changes: 0 additions & 4 deletions AsyncDisplayKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@
3917EBD41E9C2FC400D04A01 /* _ASCollectionReusableView.h in Headers */ = {isa = PBXBuildFile; fileRef = 3917EBD21E9C2FC400D04A01 /* _ASCollectionReusableView.h */; settings = {ATTRIBUTES = (Private, ); }; };
3917EBD51E9C2FC400D04A01 /* _ASCollectionReusableView.mm in Sources */ = {isa = PBXBuildFile; fileRef = 3917EBD31E9C2FC400D04A01 /* _ASCollectionReusableView.mm */; };
3C9C128519E616EF00E942A0 /* ASTableViewTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = 3C9C128419E616EF00E942A0 /* ASTableViewTests.mm */; };
4496D0731FA9EA6B001CC8D5 /* ASTraitCollectionTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = 4496D0721FA9EA6B001CC8D5 /* ASTraitCollectionTests.mm */; };
4E9127691F64157600499623 /* ASRunLoopQueueTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = 4E9127681F64157600499623 /* ASRunLoopQueueTests.mm */; };
509E68601B3AED8E009B9150 /* ASScrollDirection.mm in Sources */ = {isa = PBXBuildFile; fileRef = 205F0E111B371BD7007741D0 /* ASScrollDirection.mm */; };
509E68611B3AEDA0009B9150 /* ASAbstractLayoutController.h in Headers */ = {isa = PBXBuildFile; fileRef = 205F0E171B37339C007741D0 /* ASAbstractLayoutController.h */; };
Expand Down Expand Up @@ -672,7 +671,6 @@
3917EBD21E9C2FC400D04A01 /* _ASCollectionReusableView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = _ASCollectionReusableView.h; sourceTree = "<group>"; };
3917EBD31E9C2FC400D04A01 /* _ASCollectionReusableView.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = _ASCollectionReusableView.mm; sourceTree = "<group>"; };
3C9C128419E616EF00E942A0 /* ASTableViewTests.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; lineEnding = 0; path = ASTableViewTests.mm; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objc; };
4496D0721FA9EA6B001CC8D5 /* ASTraitCollectionTests.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = ASTraitCollectionTests.mm; sourceTree = "<group>"; };
464052191A3F83C40061C0BA /* ASDataController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; lineEnding = 0; path = ASDataController.h; sourceTree = "<group>"; };
4640521A1A3F83C40061C0BA /* ASDataController.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; lineEnding = 0; path = ASDataController.mm; sourceTree = "<group>"; };
4640521B1A3F83C40061C0BA /* ASTableLayoutController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ASTableLayoutController.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1359,7 +1357,6 @@
058D0A37195D057000B7D73C /* ASTextNodeWordKernerTests.mm */,
CCE4F9BC1F0ECE5200062E4E /* ASTLayoutFixture.h */,
CCE4F9BD1F0ECE5200062E4E /* ASTLayoutFixture.mm */,
4496D0721FA9EA6B001CC8D5 /* ASTraitCollectionTests.mm */,
CC0AEEA31D66316E005D1C78 /* ASUICollectionViewTests.mm */,
AEEC47E31C21D3D200EC1693 /* ASVideoNodeTests.mm */,
CCA221D21D6FA7EF00AF6A0F /* ASViewControllerTests.mm */,
Expand Down Expand Up @@ -2268,7 +2265,6 @@
CCEDDDD9200C518800FFCD0A /* ASConfigurationTests.mm in Sources */,
AE440175210FB7CF00B36DA2 /* ASTextKitFontSizeAdjusterTests.mm in Sources */,
E51B78BF1F028ABF00E32604 /* ASLayoutFlatteningTests.mm in Sources */,
4496D0731FA9EA6B001CC8D5 /* ASTraitCollectionTests.mm in Sources */,
29CDC2E21AAE70D000833CA4 /* ASBasicImageDownloaderContextTests.mm in Sources */,
CC583AD71EF9BDC100134156 /* NSInvocation+ASTestHelpers.mm in Sources */,
CC051F1F1D7A286A006434CB /* ASCALayerTests.mm in Sources */,
Expand Down
1 change: 1 addition & 0 deletions Source/Base/ASAssert.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
#define ASDisplayNodeCAssertPositiveReal(description, num) ASDisplayNodeCAssert(num >= 0 && num <= CGFLOAT_MAX, @"%@ must be a real positive integer: %f.", description, (CGFloat)num)
#define ASDisplayNodeCAssertInfOrPositiveReal(description, num) ASDisplayNodeCAssert(isinf(num) || (num >= 0 && num <= CGFLOAT_MAX), @"%@ must be infinite or a real positive integer: %f.", description, (CGFloat)num)

#define ASDisplayNodeCAssertPermanent(object) ASDisplayNodeCAssert(CFGetRetainCount((__bridge CFTypeRef)(object)) == CFGetRetainCount(kCFNull), @"Expected %s to be a permanent object.", #object)
#define ASDisplayNodeErrorDomain @"ASDisplayNodeErrorDomain"
#define ASDisplayNodeNonFatalErrorCode 1

Expand Down
12 changes: 12 additions & 0 deletions Source/Base/ASBaseDefines.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@
# define let const __auto_type
#endif

/**
* Hack to support building for iOS with Xcode 9. UIUserInterfaceStyle was previously tvOS-only,
* and it was added to iOS 12. Xcode 9 (iOS 11 SDK) will flat-out refuse to build anything that
* references this enum targeting iOS, even if it's guarded with the right availability macros,
* because it thinks the entire platform isn't compatible with the enum.
*/
#if TARGET_OS_TV || (defined(__IPHONE_12_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_12_0)
#define AS_BUILD_UIUSERINTERFACESTYLE 1
#else
#define AS_BUILD_UIUSERINTERFACESTYLE 0
#endif

#ifdef __GNUC__
# define ASDISPLAYNODE_GNUC(major, minor) \
(__GNUC__ > (major) || (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor)))
Expand Down
107 changes: 19 additions & 88 deletions Source/Details/ASTraitCollection.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,6 @@

NS_ASSUME_NONNULL_BEGIN

#pragma mark - ASPrimitiveContentSizeCategory

/**
* ASPrimitiveContentSizeCategory is a UIContentSizeCategory that can be used inside a struct.
*
* We need an unretained pointer because ARC can't manage struct memory.
*
* WARNING: DO NOT cast UIContentSizeCategory values to ASPrimitiveContentSizeCategory directly.
* Use ASPrimitiveContentSizeCategoryMake(UIContentSizeCategory) instead.
* This is because we make some assumptions about the lifetime of the object it points to.
* Also note that cast from ASPrimitiveContentSizeCategory to UIContentSizeCategory is always safe.
*/
typedef __unsafe_unretained UIContentSizeCategory ASPrimitiveContentSizeCategory;

/**
* Safely casts from UIContentSizeCategory to ASPrimitiveContentSizeCategory.
*
* The UIKit documentation doesn't specify if we can receive a copy of the UIContentSizeCategory constant. While getting
* copies is fine with ARC, usage of unretained pointers requires us to ensure the lifetime of the object it points to.
* Manual retain&release of the UIContentSizeCategory object is not an option because it would require us to do that
* everywhere ASPrimitiveTraitCollection is used. This is error-prone and can lead to crashes and memory leaks. So, we
* explicitly limit possible values of ASPrimitiveContentSizeCategory to the predetermined set of global constants with
* known lifetime.
*
* @return a pointer to one of the UIContentSizeCategory constants.
*/
AS_EXTERN ASPrimitiveContentSizeCategory ASPrimitiveContentSizeCategoryMake(UIContentSizeCategory sizeCategory);

#pragma mark - ASPrimitiveTraitCollection

/**
Expand All @@ -56,7 +28,9 @@ AS_EXTERN ASPrimitiveContentSizeCategory ASPrimitiveContentSizeCategoryMake(UICo
* If you use ASPrimitiveTraitCollection, please do make sure to initialize it with ASPrimitiveTraitCollectionMakeDefault()
* or ASPrimitiveTraitCollectionFromUITraitCollection(UITraitCollection*).
*/
typedef struct ASPrimitiveTraitCollection {
#pragma clang diagnostic push
#pragma clang diagnostic warning "-Wpadded"
typedef struct {
UIUserInterfaceSizeClass horizontalSizeClass;
UIUserInterfaceSizeClass verticalSizeClass;

Expand All @@ -66,14 +40,16 @@ typedef struct ASPrimitiveTraitCollection {
UIUserInterfaceIdiom userInterfaceIdiom;
UIForceTouchCapability forceTouchCapability;
UITraitEnvironmentLayoutDirection layoutDirection API_AVAILABLE(ios(10.0));
#if TARGET_OS_TV
UIUserInterfaceStyle userInterfaceStyle;
#if AS_BUILD_UIUSERINTERFACESTYLE
UIUserInterfaceStyle userInterfaceStyle API_AVAILABLE(tvos(10.0), ios(12.0));
#endif

ASPrimitiveContentSizeCategory preferredContentSizeCategory;
// NOTE: This must be a constant. We will assert.
unowned UIContentSizeCategory preferredContentSizeCategory API_AVAILABLE(ios(10.0));

CGSize containerSize;
} ASPrimitiveTraitCollection;
#pragma clang diagnostic pop

/**
* Creates ASPrimitiveTraitCollection with default values.
Expand Down Expand Up @@ -164,52 +140,21 @@ AS_EXTERN void ASTraitCollectionPropagateDown(id<ASLayoutElement> element, ASPri
AS_SUBCLASSING_RESTRICTED
@interface ASTraitCollection : NSObject

@property (nonatomic, readonly) UIUserInterfaceSizeClass horizontalSizeClass;
@property (nonatomic, readonly) UIUserInterfaceSizeClass verticalSizeClass;
@property (readonly) UIUserInterfaceSizeClass horizontalSizeClass;
@property (readonly) UIUserInterfaceSizeClass verticalSizeClass;

@property (nonatomic, readonly) CGFloat displayScale;
@property (nonatomic, readonly) UIDisplayGamut displayGamut API_AVAILABLE(ios(10.0));
@property (readonly) CGFloat displayScale;
@property (readonly) UIDisplayGamut displayGamut API_AVAILABLE(ios(10.0));

@property (nonatomic, readonly) UIUserInterfaceIdiom userInterfaceIdiom;
@property (nonatomic, readonly) UIForceTouchCapability forceTouchCapability;
@property (nonatomic, readonly) UITraitEnvironmentLayoutDirection layoutDirection API_AVAILABLE(ios(10.0));
#if TARGET_OS_TV
@property (nonatomic, readonly) UIUserInterfaceStyle userInterfaceStyle;
@property (readonly) UIUserInterfaceIdiom userInterfaceIdiom;
@property (readonly) UIForceTouchCapability forceTouchCapability;
@property (readonly) UITraitEnvironmentLayoutDirection layoutDirection API_AVAILABLE(ios(10.0));
#if AS_BUILD_UIUSERINTERFACESTYLE
@property (readonly) UIUserInterfaceStyle userInterfaceStyle API_AVAILABLE(tvos(10.0), ios(12.0));
#endif
@property (readonly) UIContentSizeCategory preferredContentSizeCategory API_AVAILABLE(ios(10.0));

@property (nonatomic, readonly) UIContentSizeCategory preferredContentSizeCategory;

@property (nonatomic, readonly) CGSize containerSize;

+ (ASTraitCollection *)traitCollectionWithUITraitCollection:(UITraitCollection *)traitCollection
containerSize:(CGSize)windowSize NS_RETURNS_RETAINED;

+ (ASTraitCollection *)traitCollectionWithUITraitCollection:(UITraitCollection *)traitCollection
containerSize:(CGSize)windowSize
fallbackContentSizeCategory:(UIContentSizeCategory)fallbackContentSizeCategory NS_RETURNS_RETAINED;

#if TARGET_OS_TV
+ (ASTraitCollection *)traitCollectionWithHorizontalSizeClass:(UIUserInterfaceSizeClass)horizontalSizeClass
verticalSizeClass:(UIUserInterfaceSizeClass)verticalSizeClass
displayScale:(CGFloat)displayScale
displayGamut:(UIDisplayGamut)displayGamut
userInterfaceIdiom:(UIUserInterfaceIdiom)userInterfaceIdiom
forceTouchCapability:(UIForceTouchCapability)forceTouchCapability
layoutDirection:(UITraitEnvironmentLayoutDirection)layoutDirection
userInterfaceStyle:(UIUserInterfaceStyle)userInterfaceStyle
preferredContentSizeCategory:(UIContentSizeCategory)preferredContentSizeCategory
containerSize:(CGSize)windowSize NS_RETURNS_RETAINED;
#else
+ (ASTraitCollection *)traitCollectionWithHorizontalSizeClass:(UIUserInterfaceSizeClass)horizontalSizeClass
verticalSizeClass:(UIUserInterfaceSizeClass)verticalSizeClass
displayScale:(CGFloat)displayScale
displayGamut:(UIDisplayGamut)displayGamut
userInterfaceIdiom:(UIUserInterfaceIdiom)userInterfaceIdiom
forceTouchCapability:(UIForceTouchCapability)forceTouchCapability
layoutDirection:(UITraitEnvironmentLayoutDirection)layoutDirection
preferredContentSizeCategory:(UIContentSizeCategory)preferredContentSizeCategory
containerSize:(CGSize)windowSize NS_RETURNS_RETAINED;
#endif
@property (readonly) CGSize containerSize;

- (BOOL)isEqualToTraitCollection:(ASTraitCollection *)traitCollection;

Expand All @@ -226,18 +171,4 @@ AS_SUBCLASSING_RESTRICTED

@end

@interface ASTraitCollection (Deprecated)

- (instancetype)init ASDISPLAYNODE_DEPRECATED_MSG("The default constructor of this class is going to become unavailable. Use other constructors instead.");

+ (ASTraitCollection *)traitCollectionWithDisplayScale:(CGFloat)displayScale
userInterfaceIdiom:(UIUserInterfaceIdiom)userInterfaceIdiom
horizontalSizeClass:(UIUserInterfaceSizeClass)horizontalSizeClass
verticalSizeClass:(UIUserInterfaceSizeClass)verticalSizeClass
forceTouchCapability:(UIForceTouchCapability)forceTouchCapability
containerSize:(CGSize)windowSize
NS_RETURNS_RETAINED ASDISPLAYNODE_DEPRECATED_MSG("Use full version of this method instead.");

@end

NS_ASSUME_NONNULL_END
Loading

0 comments on commit e392f83

Please sign in to comment.