Skip to content

Commit

Permalink
[Ripple] Add traitCollectionDidChange block (#8062)
Browse files Browse the repository at this point in the history
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 8f6338e
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
8 changes: 8 additions & 0 deletions components/Ripple/src/MDCRippleView.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -121,6 +128,7 @@ typedef NS_ENUM(NSInteger, MDCRippleStyle) {
*/
- (void)beginRippleTouchUpAnimated:(BOOL)animated
completion:(nullable MDCRippleCompletionBlock)completion;

@end

/**
Expand Down
8 changes: 8 additions & 0 deletions components/Ripple/src/MDCRippleView.m
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
24 changes: 24 additions & 0 deletions components/Ripple/tests/unit/MDCRippleViewTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
24 changes: 24 additions & 0 deletions components/Ripple/tests/unit/MDCStatefulRippleViewTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -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.