Skip to content
Permalink
Browse files

[Ripple] Add traitCollectionDidChange block (#8062)

Adds a traitCollectionDidChangeBlock to MDCRippleView and its subclass, called when its trait collection changes.

Closes #8043
  • Loading branch information
codeman7 committed Jul 24, 2019
1 parent 6190a33 commit 8f6338e0b2d91e31b3df276b66eb6b8ca216a92d
@@ -73,6 +73,13 @@ typedef NS_ENUM(NSInteger, MDCRippleStyle) {
*/
@property(nonatomic, strong, nonnull) UIColor *activeRippleColor;

/**
A block that is invoked when the @c MDCRippleView receives a call to @c
traitCollectionDidChange:. The block is called after the call to the superclass.
*/
@property(nonatomic, copy, nullable) void (^traitCollectionDidChangeBlock)
(MDCRippleView *_Nonnull ripple, UITraitCollection *_Nullable previousTraitCollection);

/**
Cancels all the existing ripples.
@@ -121,6 +128,7 @@ typedef NS_ENUM(NSInteger, MDCRippleStyle) {
*/
- (void)beginRippleTouchUpAnimated:(BOOL)animated
completion:(nullable MDCRippleCompletionBlock)completion;

@end

/**
@@ -78,6 +78,14 @@ - (void)layoutSubviews {
self.activeRippleLayer.fillColor = self.activeRippleColor.CGColor;
}

- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
[super traitCollectionDidChange:previousTraitCollection];

if (self.traitCollectionDidChangeBlock) {
self.traitCollectionDidChangeBlock(self, previousTraitCollection);
}
}

- (void)layoutSublayersOfLayer:(CALayer *)layer {
[super layoutSublayersOfLayer:layer];
for (CALayer *sublayer in self.layer.sublayers) {
@@ -249,4 +249,28 @@ - (void)testMaximumRippleRadiusImpactsUnboundedRipple {
XCTAssertEqual(rippleView.activeRippleLayer.maximumRadius, fakeRippleRadius);
}

- (void)testTraitCollectionDidChangeBlockCalledWithExpectedParameters {
// Given
MDCRippleView *testRippleView = [[MDCRippleView alloc] init];
XCTestExpectation *expectation =
[[XCTestExpectation alloc] initWithDescription:@"traitCollection"];
__block UITraitCollection *passedTraitCollection = nil;
__block MDCRippleView *passedRippleView = nil;
testRippleView.traitCollectionDidChangeBlock =
^(MDCRippleView *_Nonnull ripple, UITraitCollection *_Nullable previousTraitCollection) {
passedTraitCollection = previousTraitCollection;
passedRippleView = ripple;
[expectation fulfill];
};
UITraitCollection *fakeTraitCollection = [UITraitCollection traitCollectionWithDisplayScale:7];

// When
[testRippleView traitCollectionDidChange:fakeTraitCollection];

// Then
[self waitForExpectations:@[ expectation ] timeout:1];
XCTAssertEqual(passedRippleView, testRippleView);
XCTAssertEqual(passedTraitCollection, fakeTraitCollection);
}

@end
@@ -149,4 +149,28 @@ - (void)testRippleHighlighted {
XCTAssertTrue(rippleView.rippleHighlighted);
}

- (void)testTraitCollectionDidChangeBlockCalledWithExpectedParameters {
// Given
MDCStatefulRippleView *testRippleView = [[MDCStatefulRippleView alloc] init];
XCTestExpectation *expectation =
[[XCTestExpectation alloc] initWithDescription:@"traitCollection"];
__block UITraitCollection *passedTraitCollection = nil;
__block MDCRippleView *passedRippleView = nil;
testRippleView.traitCollectionDidChangeBlock =
^(MDCRippleView *_Nonnull ripple, UITraitCollection *_Nullable previousTraitCollection) {
passedTraitCollection = previousTraitCollection;
passedRippleView = ripple;
[expectation fulfill];
};
UITraitCollection *fakeTraitCollection = [UITraitCollection traitCollectionWithDisplayScale:7];

// When
[testRippleView traitCollectionDidChange:fakeTraitCollection];

// Then
[self waitForExpectations:@[ expectation ] timeout:1];
XCTAssertEqual(passedRippleView, testRippleView);
XCTAssertEqual(passedTraitCollection, fakeTraitCollection);
}

@end

0 comments on commit 8f6338e

Please sign in to comment.
You can’t perform that action at this time.