OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #import "ios/chrome/browser/ui/reading_list/number_badge_view.h" |
| 6 |
| 7 #import <Foundation/Foundation.h> |
| 8 |
| 9 #include "base/format_macros.h" |
| 10 #import "ios/chrome/common/material_timing.h" |
| 11 #import "ios/third_party/material_roboto_font_loader_ios/src/src/MaterialRobotoF
ontLoader.h" |
| 12 |
| 13 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 14 #error "This file requires ARC support." |
| 15 #endif |
| 16 |
| 17 namespace { |
| 18 const CGFloat kAnimationDuration = ios::material::kDuration3; |
| 19 const CGFloat labelMargin = 2.5f; |
| 20 } // namespace |
| 21 |
| 22 @interface NumberBadgeView () |
| 23 @property(nonatomic, readonly, weak) UILabel* label; |
| 24 // This is a pill-shaped (rounded corners) view used in the background of the |
| 25 // badge. |
| 26 @property(nonatomic, readonly, weak) UIView* backgroundCircleView; |
| 27 @property(nonatomic, assign) NSInteger displayNumber; |
| 28 @end |
| 29 |
| 30 @implementation NumberBadgeView |
| 31 #pragma mark - properties |
| 32 @synthesize label = _label; |
| 33 @synthesize backgroundCircleView = _backgroundCircleView; |
| 34 @synthesize displayNumber = _displayNumber; |
| 35 |
| 36 #pragma mark - lifecycle |
| 37 - (instancetype)initWithFrame:(CGRect)frame { |
| 38 self = [super initWithFrame:frame]; |
| 39 if (self) { |
| 40 self.backgroundColor = [UIColor clearColor]; |
| 41 |
| 42 UIView* backgroundCircleView = [[UIView alloc] initWithFrame:CGRectZero]; |
| 43 _backgroundCircleView = backgroundCircleView; |
| 44 [backgroundCircleView setTranslatesAutoresizingMaskIntoConstraints:NO]; |
| 45 [self addSubview:backgroundCircleView]; |
| 46 |
| 47 UILabel* label = [[UILabel alloc] initWithFrame:CGRectZero]; |
| 48 _label = label; |
| 49 [label setFont:[[MDFRobotoFontLoader sharedInstance] boldFontOfSize:10]]; |
| 50 [label setTranslatesAutoresizingMaskIntoConstraints:NO]; |
| 51 [label setTextColor:[UIColor whiteColor]]; |
| 52 [self addSubview:label]; |
| 53 |
| 54 [NSLayoutConstraint activateConstraints:@[ |
| 55 // Position bubble. |
| 56 [backgroundCircleView.trailingAnchor |
| 57 constraintEqualToAnchor:self.trailingAnchor], |
| 58 [backgroundCircleView.centerYAnchor |
| 59 constraintEqualToAnchor:self.centerYAnchor], |
| 60 |
| 61 // Position label on bubble. |
| 62 [label.centerXAnchor |
| 63 constraintEqualToAnchor:backgroundCircleView.centerXAnchor], |
| 64 [label.centerYAnchor |
| 65 constraintEqualToAnchor:backgroundCircleView.centerYAnchor], |
| 66 |
| 67 // Make bubble fit label. |
| 68 [backgroundCircleView.heightAnchor |
| 69 constraintEqualToAnchor:label.heightAnchor |
| 70 constant:labelMargin * 2], |
| 71 [backgroundCircleView.widthAnchor |
| 72 constraintGreaterThanOrEqualToAnchor:backgroundCircleView |
| 73 .heightAnchor], |
| 74 [backgroundCircleView.widthAnchor |
| 75 constraintGreaterThanOrEqualToAnchor:label.widthAnchor |
| 76 constant:labelMargin * 2] |
| 77 ]]; |
| 78 |
| 79 // Start hidden. |
| 80 self.alpha = 0.0; |
| 81 } |
| 82 return self; |
| 83 } |
| 84 |
| 85 #pragma mark - UIView |
| 86 - (void)layoutSubviews { |
| 87 [super layoutSubviews]; |
| 88 self.backgroundCircleView.layer.cornerRadius = |
| 89 self.backgroundCircleView.bounds.size.height / 2.0f; |
| 90 } |
| 91 |
| 92 #pragma mark - public |
| 93 - (void)setNumber:(NSInteger)number animated:(BOOL)animated { |
| 94 // If the previous number and current number match, do nothing. |
| 95 if (self.displayNumber != number) { |
| 96 self.displayNumber = number; |
| 97 if (animated) { |
| 98 [UIView animateWithDuration:kAnimationDuration |
| 99 animations:^{ |
| 100 if (number > 0) { |
| 101 self.alpha = 1.0; |
| 102 // Only setting when > 0 as this makes the animation |
| 103 // look better than switching to 0 then fading out. |
| 104 self.label.text = |
| 105 [NSString stringWithFormat:@"%" PRIdNS, number]; |
| 106 } else { |
| 107 self.alpha = 0.0; |
| 108 } |
| 109 [self setNeedsLayout]; |
| 110 [self layoutIfNeeded]; |
| 111 }]; |
| 112 } else { |
| 113 self.label.text = [NSString stringWithFormat:@"%" PRIdNS, number]; |
| 114 } |
| 115 } |
| 116 } |
| 117 |
| 118 - (void)setBackgroundColor:(UIColor*)backgroundColor animated:(BOOL)animated { |
| 119 if (animated) { |
| 120 [UIView animateWithDuration:kAnimationDuration |
| 121 animations:^{ |
| 122 [self.backgroundCircleView |
| 123 setBackgroundColor:backgroundColor]; |
| 124 }]; |
| 125 } else { |
| 126 [self.backgroundCircleView setBackgroundColor:backgroundColor]; |
| 127 } |
| 128 } |
| 129 |
| 130 @end |
OLD | NEW |