Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ASTraitCollection] Add missing properties to ASTraitCollection #625

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions AsyncDisplayKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@
3917EBD41E9C2FC400D04A01 /* _ASCollectionReusableView.h in Headers */ = {isa = PBXBuildFile; fileRef = 3917EBD21E9C2FC400D04A01 /* _ASCollectionReusableView.h */; settings = {ATTRIBUTES = (Private, ); }; };
3917EBD51E9C2FC400D04A01 /* _ASCollectionReusableView.m in Sources */ = {isa = PBXBuildFile; fileRef = 3917EBD31E9C2FC400D04A01 /* _ASCollectionReusableView.m */; };
3C9C128519E616EF00E942A0 /* ASTableViewTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = 3C9C128419E616EF00E942A0 /* ASTableViewTests.mm */; };
44A442581F9A1FEC007B9B1A /* ASContentSizeCategory.m in Sources */ = {isa = PBXBuildFile; fileRef = 44A442571F9A1FEC007B9B1A /* ASContentSizeCategory.m */; };
44A442591F9A2080007B9B1A /* ASContentSizeCategory.h in Headers */ = {isa = PBXBuildFile; fileRef = 44A442561F9A1D93007B9B1A /* ASContentSizeCategory.h */; settings = {ATTRIBUTES = (Public, ); }; };
4E9127691F64157600499623 /* ASRunLoopQueueTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 4E9127681F64157600499623 /* ASRunLoopQueueTests.m */; };
509E68601B3AED8E009B9150 /* ASScrollDirection.m in Sources */ = {isa = PBXBuildFile; fileRef = 205F0E111B371BD7007741D0 /* ASScrollDirection.m */; };
509E68611B3AEDA0009B9150 /* ASAbstractLayoutController.h in Headers */ = {isa = PBXBuildFile; fileRef = 205F0E171B37339C007741D0 /* ASAbstractLayoutController.h */; };
Expand Down Expand Up @@ -620,6 +622,8 @@
3917EBD21E9C2FC400D04A01 /* _ASCollectionReusableView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = _ASCollectionReusableView.h; sourceTree = "<group>"; };
3917EBD31E9C2FC400D04A01 /* _ASCollectionReusableView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = _ASCollectionReusableView.m; sourceTree = "<group>"; };
3C9C128419E616EF00E942A0 /* ASTableViewTests.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; lineEnding = 0; path = ASTableViewTests.mm; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objc; };
44A442561F9A1D93007B9B1A /* ASContentSizeCategory.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ASContentSizeCategory.h; sourceTree = "<group>"; };
44A442571F9A1FEC007B9B1A /* ASContentSizeCategory.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ASContentSizeCategory.m; 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 @@ -1263,6 +1267,8 @@
68C215571DE10D330019C4BC /* ASCollectionViewLayoutInspector.m */,
205F0E1B1B373A2C007741D0 /* ASCollectionViewLayoutController.h */,
205F0E1C1B373A2C007741D0 /* ASCollectionViewLayoutController.m */,
44A442561F9A1D93007B9B1A /* ASContentSizeCategory.h */,
44A442571F9A1FEC007B9B1A /* ASContentSizeCategory.m */,
696F01EA1DD2AF450049FBD5 /* ASEventLog.h */,
696F01EB1DD2AF450049FBD5 /* ASEventLog.mm */,
E5B225271F1790B5001E1431 /* ASHashing.h */,
Expand Down Expand Up @@ -1823,6 +1829,7 @@
E5775B021F16759300CAC9BC /* ASCollectionLayoutCache.h in Headers */,
E5775B001F13D25400CAC9BC /* ASCollectionLayoutState+Private.h in Headers */,
E5667E8C1F33871300FA6FC0 /* _ASCollectionGalleryLayoutInfo.h in Headers */,
44A442591F9A2080007B9B1A /* ASContentSizeCategory.h in Headers */,
E5775AFC1F13CE9F00CAC9BC /* _ASCollectionGalleryLayoutItem.h in Headers */,
E5855DF01EBB4D83003639AE /* ASCollectionLayoutDefines.h in Headers */,
E5B5B9D11E9BAD9800A6B726 /* ASCollectionLayoutContext+Private.h in Headers */,
Expand Down Expand Up @@ -2197,6 +2204,7 @@
CCCCCCE41EC3EF060087FE10 /* NSParagraphStyle+ASText.m in Sources */,
8BBBAB8D1CEBAF1E00107FC6 /* ASDefaultPlaybackButton.m in Sources */,
B30BF6541C59D889004FCD53 /* ASLayoutManager.m in Sources */,
44A442581F9A1FEC007B9B1A /* ASContentSizeCategory.m in Sources */,
92DD2FE71BF4D0850074C9DD /* ASMapNode.mm in Sources */,
CCA282B91E9EA8E40037E8B7 /* AsyncDisplayKit+Tips.m in Sources */,
636EA1A51C7FF4EF00EE152F /* ASDefaultPlayButton.m in Sources */,
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## master

* Add your own contributions to the next release on the line below this with your name.
- [ASTraitCollection] Add new properties of UITraitCollection to ASTraitCollection. [Yevgen Pogribnyi](https://github.com/ypogribnyi)
- [ASCollectionView] Improve index space translation of Flow Layout Delegate methods. [Scott Goodson](https://github.com/appleguy)
- [ASVideoNode] Fix unreleased time observer. [Flo Vouin](https://github.com/flovouin)
- [PINCache] Set a default .byteLimit to reduce disk usage and startup time. [#595](https://github.com/TextureGroup/Texture/pull/595) [Scott Goodson](https://github.com/appleguy)
Expand Down
49 changes: 49 additions & 0 deletions Source/Details/ASContentSizeCategory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//
// ASContentSizeCategory.h
// Texture
//
// Copyright (c) 2017-present, Pinterest, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//

#import <UIKit/UIKit.h>
#import <AsyncDisplayKit/ASBaseDefines.h>

NS_ASSUME_NONNULL_BEGIN

/**
* ASContentSizeCategory is a UIContentSizeCategory that can be used in a struct.
*/
typedef NS_ENUM(NSInteger, ASContentSizeCategory) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's do something clever, and use __unsafe_unretained UIContentSizeCategory as a struct member:

typedef struct {
    // unretained is required because ARC can't manage struct memory
    __unsafe_unretained UIContentSizeCategory category;
    BOOL flag;
} ASTestStruct;

ASContentSizeCategoryUnspecified,
ASContentSizeCategoryExtraSmall,
ASContentSizeCategorySmall,
ASContentSizeCategoryMedium,
ASContentSizeCategoryLarge,
ASContentSizeCategoryExtraLarge,
ASContentSizeCategoryExtraExtraLarge,
ASContentSizeCategoryExtraExtraExtraLarge,

// Accessibility sizes
ASContentSizeCategoryAccessibilityMedium,
ASContentSizeCategoryAccessibilityLarge,
ASContentSizeCategoryAccessibilityExtraLarge,
ASContentSizeCategoryAccessibilityExtraExtraLarge,
ASContentSizeCategoryAccessibilityExtraExtraExtraLarge
};

/**
* Mapping from UIContentSizeCategory
*/
extern ASContentSizeCategory ASContentSizeCategoryFromUIContentSizeCategory(UIContentSizeCategory contentSizeCategory);

/**
* Mapping to UIContentSizeCategory
*/
extern UIContentSizeCategory UIContentSizeCategoryFromASContentSizeCategory(ASContentSizeCategory contentSizeCategory);

NS_ASSUME_NONNULL_END
76 changes: 76 additions & 0 deletions Source/Details/ASContentSizeCategory.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//
// ASContentSizeCategory.m
// Texture
//
// Copyright (c) 2017-present, Pinterest, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//

#import <Foundation/Foundation.h>

#import <AsyncDisplayKit/ASContentSizeCategory.h>
#import <AsyncDisplayKit/ASAvailability.h>

// UIContentSizeCategoryUnspecified is available only in iOS 10.0 and later.
// This constant is used as a fallback for older iOS versions.
UIContentSizeCategory const AS_UIContentSizeCategoryUnspecified = @"_UICTContentSizeCategoryUnspecified";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make this static?


/**
* Defines a dictionary of pairs of corresponding ASContentSizeCategory and UIContentSizeCategory values.
*/
ASDISPLAYNODE_INLINE NSDictionary<NSNumber *, UIContentSizeCategory> *_as_contentSizeCategory_correspondingValues() {
static NSDictionary<NSNumber *, UIContentSizeCategory> *correspondingValues;

static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
correspondingValues =
@{
// We will fallback to ASContentSizeCategoryUnspecified so there is no need to include it in this dictionary.

[NSNumber numberWithInteger:ASContentSizeCategoryExtraSmall]: UIContentSizeCategoryExtraSmall,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use object literals in this case? @(ASContentSizeCategoryExtraSmall)?

[NSNumber numberWithInteger:ASContentSizeCategorySmall]: UIContentSizeCategorySmall,
[NSNumber numberWithInteger:ASContentSizeCategoryMedium]: UIContentSizeCategoryMedium,
[NSNumber numberWithInteger:ASContentSizeCategoryLarge]: UIContentSizeCategoryLarge,
[NSNumber numberWithInteger:ASContentSizeCategoryExtraLarge]: UIContentSizeCategoryExtraLarge,
[NSNumber numberWithInteger:ASContentSizeCategoryExtraExtraLarge]: UIContentSizeCategoryExtraExtraLarge,
[NSNumber numberWithInteger:ASContentSizeCategoryExtraExtraExtraLarge]: UIContentSizeCategoryExtraExtraExtraLarge,

[NSNumber numberWithInteger:ASContentSizeCategoryAccessibilityMedium]: UIContentSizeCategoryAccessibilityMedium,
[NSNumber numberWithInteger:ASContentSizeCategoryAccessibilityLarge]: UIContentSizeCategoryAccessibilityLarge,
[NSNumber numberWithInteger:ASContentSizeCategoryAccessibilityExtraLarge]: UIContentSizeCategoryAccessibilityExtraLarge,
[NSNumber numberWithInteger:ASContentSizeCategoryAccessibilityExtraExtraLarge]: UIContentSizeCategoryAccessibilityExtraExtraLarge,
[NSNumber numberWithInteger:ASContentSizeCategoryAccessibilityExtraExtraExtraLarge]: UIContentSizeCategoryAccessibilityExtraExtraExtraLarge,
};
});

return correspondingValues;
}

ASContentSizeCategory ASContentSizeCategoryFromUIContentSizeCategory(UIContentSizeCategory contentSizeCategory) {
if (!contentSizeCategory) {
return ASContentSizeCategoryUnspecified;
}

NSNumber *key = [[_as_contentSizeCategory_correspondingValues() allKeysForObject:contentSizeCategory] firstObject];
if (key) {
return key.integerValue;
}
else {
return ASContentSizeCategoryUnspecified;
}
}

UIContentSizeCategory UIContentSizeCategoryFromASContentSizeCategory(ASContentSizeCategory contentSizeCategory) {
UIContentSizeCategory result = _as_contentSizeCategory_correspondingValues()[[NSNumber numberWithInteger:contentSizeCategory]];
if (result) {
return result;
} else if (AS_AT_LEAST_IOS10) {
return UIContentSizeCategoryUnspecified;
} else {
return AS_UIContentSizeCategoryUnspecified;
}
}
53 changes: 42 additions & 11 deletions Source/Details/ASTraitCollection.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@


#import <UIKit/UIKit.h>

#import <AsyncDisplayKit/ASContentSizeCategory.h>
#import <AsyncDisplayKit/ASBaseDefines.h>

@class ASTraitCollection;
Expand All @@ -30,11 +32,20 @@ ASDISPLAYNODE_EXTERN_C_BEGIN
#pragma mark - ASPrimitiveTraitCollection

typedef struct ASPrimitiveTraitCollection {
CGFloat displayScale;
UIUserInterfaceSizeClass horizontalSizeClass;
UIUserInterfaceIdiom userInterfaceIdiom;
UIUserInterfaceSizeClass verticalSizeClass;

CGFloat displayScale;
UIDisplayGamut displayGamut;

UIUserInterfaceIdiom userInterfaceIdiom;
UIForceTouchCapability forceTouchCapability;
UITraitEnvironmentLayoutDirection layoutDirection;
#if TARGET_OS_TV
UIUserInterfaceStyle userInterfaceStyle;
#endif

ASContentSizeCategory preferredContentSizeCategory;

CGSize containerSize;
} ASPrimitiveTraitCollection;
Expand Down Expand Up @@ -124,25 +135,45 @@ ASDISPLAYNODE_EXTERN_C_END
AS_SUBCLASSING_RESTRICTED
@interface ASTraitCollection : NSObject

@property (nonatomic, assign, readonly) CGFloat displayScale;
@property (nonatomic, assign, readonly) UIUserInterfaceSizeClass horizontalSizeClass;
@property (nonatomic, assign, readonly) UIUserInterfaceIdiom userInterfaceIdiom;
@property (nonatomic, assign, readonly) UIUserInterfaceSizeClass verticalSizeClass;

@property (nonatomic, assign, readonly) CGFloat displayScale;
@property (nonatomic, assign, readonly) UIDisplayGamut displayGamut;

@property (nonatomic, assign, readonly) UIUserInterfaceIdiom userInterfaceIdiom;
@property (nonatomic, assign, readonly) UIForceTouchCapability forceTouchCapability;
@property (nonatomic, assign, readonly) UITraitEnvironmentLayoutDirection layoutDirection;
#if TARGET_OS_TV
@property (nonatomic, assign, readonly) UIUserInterfaceStyle userInterfaceStyle;
#endif

@property (nonatomic, assign, readonly) UIContentSizeCategory preferredContentSizeCategory;

@property (nonatomic, assign, readonly) CGSize containerSize;

+ (ASTraitCollection *)traitCollectionWithASPrimitiveTraitCollection:(ASPrimitiveTraitCollection)traits;

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


+ (ASTraitCollection *)traitCollectionWithDisplayScale:(CGFloat)displayScale
userInterfaceIdiom:(UIUserInterfaceIdiom)userInterfaceIdiom
horizontalSizeClass:(UIUserInterfaceSizeClass)horizontalSizeClass
verticalSizeClass:(UIUserInterfaceSizeClass)verticalSizeClass
forceTouchCapability:(UIForceTouchCapability)forceTouchCapability
containerSize:(CGSize)windowSize;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be considered a breaking API change. Instead, I think we should preserve this API and call the full version internally with default values. We can also deprecate it, but that's as far as we can go for now, IMHO.

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


+ (ASTraitCollection *)traitCollectionWithHorizontalSizeClass:(UIUserInterfaceSizeClass)horizontalSizeClass
verticalSizeClass:(UIUserInterfaceSizeClass)verticalSizeClass
displayScale:(CGFloat)displayScale
displayGamut:(UIDisplayGamut)displayGamut
userInterfaceIdiom:(UIUserInterfaceIdiom)userInterfaceIdiom
forceTouchCapability:(UIForceTouchCapability)forceTouchCapability
layoutDirection:(UITraitEnvironmentLayoutDirection)layoutDirection
#if TARGET_OS_TV
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand your reasoning here. However, for maintainability, I think it's best to declare a separate API for tvOS with this extra param.

userInterfaceStyle:(UIUserInterfaceStyle)userInterfaceStyle
#endif
preferredContentSizeCategory:(UIContentSizeCategory)preferredContentSizeCategory
containerSize:(CGSize)windowSize;


- (ASPrimitiveTraitCollection)primitiveTraitCollection;
Expand Down
Loading