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

Conversation

ypogribnyi
Copy link
Contributor

In comparison to UITraitCollection, some properties were missing from ASTraitCollection.
These are needed for ASDisplayNodes to conveniently access information about current interface direction, DynamicType size category etc.

* ASTraitCollection now completely reflects UITraitCollection

* Add ASContentSizeCategory enum that corresponds to
  UIContentSizeCategory and can be used inside a struct.
@CLAassistant
Copy link

CLAassistant commented Oct 20, 2017

CLA assistant check
All committers have signed the CLA.

@ghost
Copy link

ghost commented Oct 20, 2017

🚫 CI failed with log

Copy link
Contributor

@maicki maicki left a comment

Choose a reason for hiding this comment

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

Thanks for the PR, really appreciated!

In general looks good to me, would like to have another team member to take a look over it though.


// 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?

@{
// 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)?

Copy link
Member

@Adlai-Holler Adlai-Holler left a comment

Choose a reason for hiding this comment

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

Sorry for the long delay getting a review on this @ypogribnyi and thank you for contributing!

/**
* 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;

@ypogribnyi
Copy link
Contributor Author

Updated ASPrimitiveTraitCollection to use an unretained pointer to UIContentSizeCategory. Thanks for the suggestion.

Copy link
Member

@nguyenhuy nguyenhuy left a comment

Choose a reason for hiding this comment

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

Some small changes and we're good to go!

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.

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.

if (AS_AT_LEAST_IOS10) {
return UIContentSizeCategoryUnspecified;
}
else {
Copy link
Member

Choose a reason for hiding this comment

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

Nit: Merge this line to the previous one.

return UIContentSizeCategoryAccessibilityExtraExtraExtraLarge;
}

if (AS_AT_LEAST_IOS10) {
Copy link
Member

Choose a reason for hiding this comment

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

This fallback logic is used multiple times in this diff. I think we can encapsulate it into a function and use everywhere.


// preferredContentSizeCategory always points to one of UIContentSizeCategory constants.
// Assuming their values do not duplicate, we can simply compare pointers, avoiding string comparison.
lhs.preferredContentSizeCategory == rhs.preferredContentSizeCategory &&
Copy link
Member

Choose a reason for hiding this comment

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

Thank you for the detailed comment. I actually expect -[NSString isEqualToString:] to compare memory addresses early on. Without concrete numbers, I'd consider this optimization unnecessary, also because calling -isEqualToString: gives us better maintainability.

Interesting find: https://gist.github.com/0xced/2275014#file-nsstring-m-L76

… ASTraitCollection-missing-properties

# Conflicts:
#	CHANGELOG.md
#	Source/Details/ASTraitCollection.m
* Restore one of the ASTraitCollection constructors with a deprecation notice.
* Clean up API by the separation of tvOS-specific interfaces.
* Use [NSString -isEqualToString:] for ASPrimitiveContentSizeCategory equality tests for better readability.
* Encapsulate fallback logic for UIContentSizeCategoryUnspecified.
Copy link
Member

@nguyenhuy nguyenhuy left a comment

Choose a reason for hiding this comment

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

Thank you! 🎉

lhs.userInterfaceStyle == rhs.userInterfaceStyle &&
#endif

[leftSizeCategory isEqualToString:rightSizeCategory] && // Simple pointer comparison should be sufficient here
Copy link
Member

Choose a reason for hiding this comment

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

:)

@nguyenhuy nguyenhuy merged commit a3136b0 into TextureGroup:master Jan 16, 2018
@ypogribnyi ypogribnyi deleted the ASTraitCollection-missing-properties branch January 16, 2018 18:21
bernieperez pushed a commit to AtomTickets/Texture that referenced this pull request Apr 25, 2018
…ureGroup#625)

* [ASTraitCollection] Add missing properties to ASTraitCollection

* ASTraitCollection now completely reflects UITraitCollection

* Add ASContentSizeCategory enum that corresponds to
  UIContentSizeCategory and can be used inside a struct.

* * Remove enum ASContentSizeCategory.
* Use __unsafe_unretained UIContentSizeCategory instead of the enum.

* Added ASPrimitiveTraitCollection lifetime test

* Changes requested at code review:
* Restore one of the ASTraitCollection constructors with a deprecation notice.
* Clean up API by the separation of tvOS-specific interfaces.
* Use [NSString -isEqualToString:] for ASPrimitiveContentSizeCategory equality tests for better readability.
* Encapsulate fallback logic for UIContentSizeCategoryUnspecified.

* Fix failing test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants