Skip to content

Commit

Permalink
[Buttons] Add a boolean for when observing cornerRadius so that we wo…
Browse files Browse the repository at this point in the history
…n't try to add 2 observers or remove a non-existent observer

PiperOrigin-RevId: 314722144
  • Loading branch information
yarneo authored and material-automation committed Jun 4, 2020
1 parent 52ee290 commit 3d5c27f
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions components/Buttons/src/MDCButton.m
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ @interface MDCButton () {
NSString *_accessibilityLabelExplicitValue;

BOOL _mdc_adjustsFontForContentSizeCategory;
BOOL _cornerRadiusObserverAdded;
}
@property(nonatomic, strong, readonly, nonnull) MDCStatefulRippleView *rippleView;
@property(nonatomic, strong) MDCInkView *inkView;
Expand Down Expand Up @@ -269,7 +270,7 @@ - (void)commonMDCButtonInit {
- (void)dealloc {
[self removeTarget:self action:NULL forControlEvents:UIControlEventAllEvents];

if (!UIEdgeInsetsEqualToEdgeInsets(self.visibleAreaInsets, UIEdgeInsetsZero)) {
if (_cornerRadiusObserverAdded) {
[self.layer removeObserver:self
forKeyPath:NSStringFromSelector(@selector(cornerRadius))
context:kKVOContextCornerRadius];
Expand Down Expand Up @@ -1048,21 +1049,31 @@ - (void)setUnderlyingColor:(UIColor *)underlyingColor {
#pragma mark - Visible area

- (void)setVisibleAreaInsets:(UIEdgeInsets)visibleAreaInsets {
if (UIEdgeInsetsEqualToEdgeInsets(visibleAreaInsets, _visibleAreaInsets)) {
return;
}

_visibleAreaInsets = visibleAreaInsets;

if (UIEdgeInsetsEqualToEdgeInsets(visibleAreaInsets, UIEdgeInsetsZero)) {
self.shapeGenerator = nil;

[self.layer removeObserver:self
forKeyPath:NSStringFromSelector(@selector(cornerRadius))
context:kKVOContextCornerRadius];
if (_cornerRadiusObserverAdded) {
[self.layer removeObserver:self
forKeyPath:NSStringFromSelector(@selector(cornerRadius))
context:kKVOContextCornerRadius];
_cornerRadiusObserverAdded = NO;
}
} else {
self.shapeGenerator = [self generateShapeWithCornerRadius:self.layer.cornerRadius];

[self.layer addObserver:self
forKeyPath:NSStringFromSelector(@selector(cornerRadius))
options:NSKeyValueObservingOptionNew
context:kKVOContextCornerRadius];
if (!_cornerRadiusObserverAdded) {
[self.layer addObserver:self
forKeyPath:NSStringFromSelector(@selector(cornerRadius))
options:NSKeyValueObservingOptionNew
context:kKVOContextCornerRadius];
_cornerRadiusObserverAdded = YES;
}
}
}

Expand Down

0 comments on commit 3d5c27f

Please sign in to comment.