Skip to content
Permalink
Browse files

[Buttons] [Ripple] fix button to update ripple states correctly (#7337)

Fixing programmatic interface of buttons to correctly set the Ripple highlighted and select state.

### Description

Buttons do not change the ripple state correctly when their selected or highlighted states are updated programmatically. To fix this issue, we're updating the RippleViews's highlighted and selected states to match the button's highlighted and selected states.

Steps to reproduce the highlighted state issued:
1. set: button.isHighlighted = true 
Expected: button is highlighted (background is light blue)
Outcome: button is not highlighted (background is white)

Steps to reproduce the selected state issued:
1. set: button.isSelected = true 
Expected: button is selected (background is light blue)
Outcome: button is not selected (background is white)

Before: 
![image](https://user-images.githubusercontent.com/2329102/57167435-c60c6300-6dcb-11e9-8e54-3aa96b0f8eb8.png)

After:
![image](https://user-images.githubusercontent.com/2329102/57167455-dd4b5080-6dcb-11e9-864e-cd4d960df464.png)

### Issue
b/128908676
Issue: #7342
  • Loading branch information
galiak11 committed May 19, 2019
1 parent 2744b6c commit 7c481c023ef2fa01e8ab0824a58e3ae6bc648303
Showing with 25 additions and 20 deletions.
  1. +5 −9 components/Buttons/src/MDCButton.m
  2. +20 −11 components/Buttons/tests/unit/ButtonRippleTests.m
@@ -321,9 +321,7 @@ - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
}
[super touchesBegan:touches withEvent:event];

if (self.enableRippleBehavior) {
self.rippleView.rippleHighlighted = YES;
} else {
if (!self.enableRippleBehavior) {
[self handleBeginTouches:touches];
}
}
@@ -343,9 +341,7 @@ - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
}
[super touchesEnded:touches withEvent:event];

if (self.enableRippleBehavior) {
self.rippleView.rippleHighlighted = NO;
} else {
if (!self.enableRippleBehavior) {
CGPoint location = [self locationFromTouches:touches];
[_inkView startTouchEndedAnimationAtPoint:location completion:nil];
}
@@ -358,9 +354,7 @@ - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
}
[super touchesCancelled:touches withEvent:event];

if (self.enableRippleBehavior) {
self.rippleView.rippleHighlighted = NO;
} else {
if (!self.enableRippleBehavior) {
[self evaporateInkToPoint:[self locationFromTouches:touches]];
}
}
@@ -380,12 +374,14 @@ - (void)setEnabled:(BOOL)enabled animated:(BOOL)animated {
- (void)setHighlighted:(BOOL)highlighted {
[super setHighlighted:highlighted];

self.rippleView.rippleHighlighted = highlighted;
[self updateAfterStateChange:NO];
}

- (void)setSelected:(BOOL)selected {
[super setSelected:selected];

self.rippleView.selected = selected;
[self updateAfterStateChange:NO];
}

@@ -127,39 +127,48 @@ - (void)testSetInkStyleBoundedUpdatesRippleView {

#pragma mark - Touch tests

- (void)testTouchesBeganSetsRippleHighlightedToYES {
- (void)testSettingHighlightedUpdatesRippleTheming {
// Given
self.button.enableRippleBehavior = YES;

// When
[self.button touchesBegan:[NSSet setWithObject:[[UITouch alloc] init]] withEvent:nil];
self.button.highlighted = YES;

// Then
XCTAssertTrue(self.button.rippleView.isRippleHighlighted);

// And When
self.button.highlighted = NO;

// Then
XCTAssertFalse(self.button.rippleView.isRippleHighlighted);
}

- (void)testTouchesCancelledSetsRippleHighlightedToNO {
- (void)testSettingSelectedWhenNotAllowedDoesntUpdateRippleTheming {
// Given
self.button.enableRippleBehavior = YES;
self.button.rippleView.rippleHighlighted = YES;

// When
[self.button touchesCancelled:[NSSet setWithObject:[[UITouch alloc] init]] withEvent:nil];
self.button.selected = YES;

// Then
XCTAssertFalse(self.button.rippleView.isRippleHighlighted);
XCTAssertFalse(self.button.rippleView.isSelected);
}

- (void)testTouchesEndedSetsRippleHighlightedToNO {
- (void)testSettingSelectedUpdatesRippleTheming {
// Given
self.button.enableRippleBehavior = YES;
self.button.rippleView.rippleHighlighted = YES;
self.button.rippleView.allowsSelection = YES;
self.button.selected = YES;

// When
[self.button touchesEnded:[NSSet setWithObject:[[UITouch alloc] init]] withEvent:nil];
// Then
XCTAssertTrue(self.button.rippleView.isSelected);

// And When
self.button.selected = NO;

// Then
XCTAssertFalse(self.button.rippleView.isRippleHighlighted);
XCTAssertFalse(self.button.rippleView.isSelected);
}

@end

0 comments on commit 7c481c0

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