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/autofill/cells/status_item.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #import "ios/chrome/browser/ui/colors/MDCPalette+CrAdditions.h" |
| 9 #include "ios/chrome/grit/ios_theme_resources.h" |
| 10 #import "ios/third_party/material_components_ios/src/components/ActivityIndicato
r/src/MaterialActivityIndicator.h" |
| 11 #import "ios/third_party/material_components_ios/src/components/Palettes/src/Mat
erialPalettes.h" |
| 12 #import "ios/third_party/material_roboto_font_loader_ios/src/src/MaterialRobotoF
ontLoader.h" |
| 13 #include "ui/base/resource/resource_bundle.h" |
| 14 |
| 15 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 16 #error "This file requires ARC support." |
| 17 #endif |
| 18 |
| 19 namespace { |
| 20 // Padding used on the leading and trailing edges of the cell. |
| 21 const CGFloat kHorizontalPadding = 16; |
| 22 } // namespace |
| 23 |
| 24 @interface StatusCell () |
| 25 - (void)configureForState:(StatusItemState)state; |
| 26 @end |
| 27 |
| 28 @implementation StatusItem |
| 29 |
| 30 @synthesize text = _text; |
| 31 @synthesize state = _state; |
| 32 |
| 33 - (instancetype)initWithType:(NSInteger)type { |
| 34 self = [super initWithType:type]; |
| 35 if (self) { |
| 36 self.cellClass = [StatusCell class]; |
| 37 } |
| 38 return self; |
| 39 } |
| 40 |
| 41 #pragma mark CollectionViewItem |
| 42 |
| 43 - (void)configureCell:(StatusCell*)cell { |
| 44 [super configureCell:cell]; |
| 45 cell.textLabel.text = self.text; |
| 46 [cell configureForState:self.state]; |
| 47 } |
| 48 |
| 49 @end |
| 50 |
| 51 @implementation StatusCell |
| 52 |
| 53 @synthesize activityIndicator = _activityIndicator; |
| 54 @synthesize verifiedImageView = _verifiedImageView; |
| 55 @synthesize errorImageView = _errorImageView; |
| 56 @synthesize textLabel = _textLabel; |
| 57 |
| 58 - (instancetype)initWithFrame:(CGRect)frame { |
| 59 self = [super initWithFrame:frame]; |
| 60 if (self) { |
| 61 UIView* contentView = self.contentView; |
| 62 |
| 63 // This view is used to center the graphic and the text label. |
| 64 UIView* verticalCenteringView = [[UIView alloc] init]; |
| 65 verticalCenteringView.translatesAutoresizingMaskIntoConstraints = NO; |
| 66 [contentView addSubview:verticalCenteringView]; |
| 67 |
| 68 _activityIndicator = [[MDCActivityIndicator alloc] init]; |
| 69 _activityIndicator.cycleColors = @[ [[MDCPalette cr_bluePalette] tint500] ]; |
| 70 [_activityIndicator setRadius:10]; |
| 71 _activityIndicator.translatesAutoresizingMaskIntoConstraints = NO; |
| 72 [verticalCenteringView addSubview:_activityIndicator]; |
| 73 |
| 74 _verifiedImageView = [[UIImageView alloc] init]; |
| 75 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| 76 // TODO(crbug.com/508925): Get rid of IDR_IOS_CHECKMARK, use a vector icon |
| 77 // instead. |
| 78 _verifiedImageView.image = |
| 79 rb.GetNativeImageNamed(IDR_IOS_CHECKMARK).ToUIImage(); |
| 80 _verifiedImageView.contentMode = UIViewContentModeScaleAspectFit; |
| 81 _verifiedImageView.hidden = YES; |
| 82 _verifiedImageView.translatesAutoresizingMaskIntoConstraints = NO; |
| 83 [verticalCenteringView addSubview:_verifiedImageView]; |
| 84 |
| 85 _errorImageView = [[UIImageView alloc] init]; |
| 86 _errorImageView.image = rb.GetNativeImageNamed(IDR_IOS_ERROR).ToUIImage(); |
| 87 _errorImageView.contentMode = UIViewContentModeScaleAspectFit; |
| 88 _errorImageView.hidden = YES; |
| 89 _errorImageView.translatesAutoresizingMaskIntoConstraints = NO; |
| 90 [verticalCenteringView addSubview:_errorImageView]; |
| 91 |
| 92 _textLabel = [[UILabel alloc] init]; |
| 93 _textLabel.font = |
| 94 [[MDFRobotoFontLoader sharedInstance] mediumFontOfSize:16]; |
| 95 _textLabel.numberOfLines = 0; |
| 96 _textLabel.lineBreakMode = NSLineBreakByWordWrapping; |
| 97 // The label's position will be centered with Auto Layout but this ensures |
| 98 // that if there are multiple lines of text they will be centered relative |
| 99 // to each other. |
| 100 _textLabel.textAlignment = NSTextAlignmentCenter; |
| 101 _textLabel.translatesAutoresizingMaskIntoConstraints = NO; |
| 102 [verticalCenteringView addSubview:_textLabel]; |
| 103 |
| 104 const CGFloat kVerticalPadding = 16; |
| 105 const CGFloat kSpacing = 12; |
| 106 const CGFloat kActivityIndicatorSize = 24; |
| 107 [NSLayoutConstraint activateConstraints:@[ |
| 108 [verticalCenteringView.centerXAnchor |
| 109 constraintEqualToAnchor:contentView.centerXAnchor], |
| 110 [verticalCenteringView.centerYAnchor |
| 111 constraintEqualToAnchor:contentView.centerYAnchor], |
| 112 [_activityIndicator.centerXAnchor |
| 113 constraintEqualToAnchor:verticalCenteringView.centerXAnchor], |
| 114 [_verifiedImageView.centerXAnchor |
| 115 constraintEqualToAnchor:verticalCenteringView.centerXAnchor], |
| 116 [_errorImageView.centerXAnchor |
| 117 constraintEqualToAnchor:verticalCenteringView.centerXAnchor], |
| 118 [_textLabel.centerXAnchor |
| 119 constraintEqualToAnchor:verticalCenteringView.centerXAnchor], |
| 120 [_textLabel.leadingAnchor |
| 121 constraintEqualToAnchor:contentView.leadingAnchor |
| 122 constant:kHorizontalPadding], |
| 123 [_textLabel.trailingAnchor |
| 124 constraintEqualToAnchor:contentView.trailingAnchor |
| 125 constant:-kHorizontalPadding], |
| 126 [_activityIndicator.bottomAnchor |
| 127 constraintEqualToAnchor:_textLabel.topAnchor |
| 128 constant:-kSpacing], |
| 129 [_verifiedImageView.bottomAnchor |
| 130 constraintEqualToAnchor:_textLabel.topAnchor |
| 131 constant:-kSpacing], |
| 132 [_errorImageView.bottomAnchor constraintEqualToAnchor:_textLabel.topAnchor |
| 133 constant:-kSpacing], |
| 134 [_activityIndicator.heightAnchor |
| 135 constraintEqualToConstant:kActivityIndicatorSize], |
| 136 [verticalCenteringView.topAnchor |
| 137 constraintEqualToAnchor:_activityIndicator.topAnchor], |
| 138 [verticalCenteringView.bottomAnchor |
| 139 constraintEqualToAnchor:_textLabel.bottomAnchor], |
| 140 [verticalCenteringView.topAnchor |
| 141 constraintGreaterThanOrEqualToAnchor:contentView.topAnchor |
| 142 constant:kVerticalPadding], |
| 143 [verticalCenteringView.bottomAnchor |
| 144 constraintLessThanOrEqualToAnchor:contentView.bottomAnchor |
| 145 constant:-kVerticalPadding], |
| 146 ]]; |
| 147 } |
| 148 return self; |
| 149 } |
| 150 |
| 151 - (void)prepareForReuse { |
| 152 [super prepareForReuse]; |
| 153 self.textLabel.text = nil; |
| 154 [self configureForState:StatusItemState::VERIFYING]; |
| 155 } |
| 156 |
| 157 - (void)layoutSubviews { |
| 158 [super layoutSubviews]; |
| 159 |
| 160 // Adjust the text label preferredMaxLayoutWidth when the parent's width |
| 161 // changes, for instance on screen rotation. |
| 162 CGFloat parentWidth = CGRectGetWidth(self.contentView.bounds); |
| 163 self.textLabel.preferredMaxLayoutWidth = parentWidth - 2 * kHorizontalPadding; |
| 164 |
| 165 // Re-layout with the new preferred width to allow the label to adjust its |
| 166 // height. |
| 167 [super layoutSubviews]; |
| 168 } |
| 169 |
| 170 #pragma mark - Private |
| 171 |
| 172 - (void)configureForState:(StatusItemState)state { |
| 173 switch (state) { |
| 174 case StatusItemState::VERIFYING: |
| 175 self.textLabel.textColor = [[MDCPalette cr_bluePalette] tint500]; |
| 176 [self.activityIndicator startAnimating]; |
| 177 self.activityIndicator.hidden = NO; |
| 178 self.verifiedImageView.hidden = YES; |
| 179 self.errorImageView.hidden = YES; |
| 180 break; |
| 181 case StatusItemState::VERIFIED: |
| 182 self.textLabel.textColor = [[MDCPalette cr_bluePalette] tint500]; |
| 183 [self.activityIndicator stopAnimating]; |
| 184 self.activityIndicator.hidden = YES; |
| 185 self.verifiedImageView.hidden = NO; |
| 186 self.errorImageView.hidden = YES; |
| 187 break; |
| 188 case StatusItemState::ERROR: |
| 189 self.textLabel.textColor = [[MDCPalette cr_redPalette] tint500]; |
| 190 [self.activityIndicator stopAnimating]; |
| 191 self.activityIndicator.hidden = YES; |
| 192 self.verifiedImageView.hidden = YES; |
| 193 self.errorImageView.hidden = NO; |
| 194 break; |
| 195 } |
| 196 } |
| 197 |
| 198 @end |
OLD | NEW |