Chromium Code Reviews| Index: ios/chrome/browser/ui/util/label_observer.mm |
| diff --git a/ios/chrome/browser/ui/util/label_observer.mm b/ios/chrome/browser/ui/util/label_observer.mm |
| index 4fa5c8477581690d2d482fee10afa4b06c17458c..c9a71df7f2ae55cba02f667a0626fb87df1c8da8 100644 |
| --- a/ios/chrome/browser/ui/util/label_observer.mm |
| +++ b/ios/chrome/browser/ui/util/label_observer.mm |
| @@ -38,9 +38,19 @@ NSString* GetStringValue(id value) { |
| // property on |_label|. |
| @property(nonatomic, assign, getter=isRespondingToKVO) BOOL respondingToKVO; |
| +// The number of times this observer has been asked to observe the label. When |
| +// reaching zero, the label stops being observed. |
| +@property(nonatomic, assign) NSInteger observingCount; |
| + |
| // Initializes a LabelObserver that observes |label|. |
| - (instancetype)initWithLabel:(UILabel*)label NS_DESIGNATED_INITIALIZER; |
| +// Adds |self| as observer for the |_label|. |
| +- (void)registerAsObserver; |
| + |
| +// Removes |self| as observer for the |_label|. |
| +- (void)removeObserver; |
| + |
| // Performs all LabelObserverActions in |actions|. |
| - (void)performActions:(NSArray*)actions; |
| @@ -62,6 +72,7 @@ static NSSet* textKeys; |
| @implementation LabelObserver |
| @synthesize respondingToKVO = _respondingToKVO; |
| +@synthesize observingCount = _observingCount; |
| + (void)initialize { |
| if (self == [LabelObserver class]) { |
| @@ -79,27 +90,11 @@ static NSSet* textKeys; |
| if ((self = [super init])) { |
| DCHECK(label); |
| _label.reset(label); |
| - for (NSSet* keySet in @[ styleKeys, layoutKeys, textKeys ]) { |
| - for (NSString* key in keySet) { |
| - [_label addObserver:self |
| - forKeyPath:key |
| - options:NSKeyValueObservingOptionNew |
| - context:nullptr]; |
| - } |
| - } |
| [self resetLabelAttributes]; |
| } |
| return self; |
| } |
| -- (void)dealloc { |
| - for (NSSet* keySet in @[ styleKeys, layoutKeys, textKeys ]) { |
| - for (NSString* key in keySet) { |
| - [_label removeObserver:self forKeyPath:key]; |
| - } |
| - } |
| - [super dealloc]; |
| -} |
| #pragma mark - Public interface |
| @@ -110,12 +105,27 @@ static NSSet* textKeys; |
| if (!observer) { |
| observer = [[LabelObserver alloc] initWithLabel:label]; |
| objc_setAssociatedObject(label, kLabelObserverKey, observer, |
| - OBJC_ASSOCIATION_RETAIN_NONATOMIC); |
| - [observer release]; |
| + OBJC_ASSOCIATION_ASSIGN); |
| + [observer autorelease]; |
| } |
| return observer; |
| } |
| +- (void)startObserving { |
| + if (self.observingCount++ == 0) { |
|
stkhapugin
2017/04/19 16:39:40
Please don't put two operations - increment and co
pkl (ping after 24h if needed)
2017/04/19 16:48:03
Readability aside, do the two implementations do t
gambard
2017/04/20 08:30:18
No, the increment should be outside the if.
Done.
|
| + [self registerAsObserver]; |
| + } |
| +} |
| + |
| +- (void)stopObserving { |
| + if (self.observingCount == 0) |
| + return; |
| + |
| + if (self.observingCount-- == 1) { |
| + [self removeObserver]; |
| + } |
| +} |
| + |
| - (void)addStyleChangedAction:(LabelObserverAction)action { |
| DCHECK(action); |
| if (!_styleActions) |
| @@ -142,6 +152,25 @@ static NSSet* textKeys; |
| #pragma mark - |
| +- (void)registerAsObserver { |
| + for (NSSet* keySet in @[ styleKeys, layoutKeys, textKeys ]) { |
| + for (NSString* key in keySet) { |
| + [_label addObserver:self |
| + forKeyPath:key |
| + options:NSKeyValueObservingOptionNew |
| + context:nullptr]; |
| + } |
| + } |
| +} |
| + |
| +- (void)removeObserver { |
| + for (NSSet* keySet in @[ styleKeys, layoutKeys, textKeys ]) { |
| + for (NSString* key in keySet) { |
| + [_label removeObserver:self forKeyPath:key]; |
| + } |
| + }; |
| +} |
| + |
| - (void)performActions:(NSArray*)actions { |
| for (LabelObserverAction action in actions) |
| action(_label); |