Skip to content

Commit

Permalink
Safety Check iOS: Modify settings_password_check_cell for safety check
Browse files Browse the repository at this point in the history
The custom cell for password check is similar to the one needed for
safety check.  It is being renamed to settings_check_cell for more
general use.

The main difference is the whether or not an image is used on the
leading side of the cell. This CL adds an optional image to the leading
side of the cell for compatibility with both checks.

Bug: 1078782
Change-Id: I825bdcb53f624ce2e567a68dc900bddc310dc270
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2273162
Commit-Queue: Sean Harrison <harrisonsean@chromium.org>
Reviewed-by: Gauthier Ambard <gambard@chromium.org>
Reviewed-by: Javier Ernesto Flores Robles <javierrobles@chromium.org>
Cr-Commit-Position: refs/heads/master@{#785836}
  • Loading branch information
harrisonsean authored and Commit Bot committed Jul 7, 2020
1 parent a7d2baf commit f776805
Show file tree
Hide file tree
Showing 11 changed files with 314 additions and 203 deletions.
10 changes: 5 additions & 5 deletions ios/chrome/browser/ui/settings/cells/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ source_set("cells") {
"passphrase_error_item.mm",
"search_engine_item.h",
"search_engine_item.mm",
"settings_check_cell.h",
"settings_check_cell.mm",
"settings_check_item.h",
"settings_check_item.mm",
"settings_image_detail_text_cell.h",
"settings_image_detail_text_cell.mm",
"settings_image_detail_text_item.h",
"settings_image_detail_text_item.mm",
"settings_multiline_detail_item.h",
"settings_multiline_detail_item.mm",
"settings_password_check_cell.h",
"settings_password_check_cell.mm",
"settings_password_check_item.h",
"settings_password_check_item.mm",
"settings_switch_cell.h",
"settings_switch_cell.mm",
"settings_switch_item.h",
Expand Down Expand Up @@ -85,8 +85,8 @@ source_set("unit_tests") {
"copied_to_chrome_item_unittest.mm",
"passphrase_error_item_unittest.mm",
"search_engine_item_unittest.mm",
"settings_check_item_unittest.mm",
"settings_multiline_detail_item_unittest.mm",
"settings_password_check_item_unittest.mm",
"version_item_unittest.mm",
]

Expand Down
44 changes: 44 additions & 0 deletions ios/chrome/browser/ui/settings/cells/settings_check_cell.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef IOS_CHROME_BROWSER_UI_SETTINGS_CELLS_SETTINGS_CHECK_CELL_H_
#define IOS_CHROME_BROWSER_UI_SETTINGS_CELLS_SETTINGS_CHECK_CELL_H_

#import <UIKit/UIKit.h>

#import "ios/chrome/browser/ui/table_view/cells/table_view_cell.h"

// Cell representation for SettingsCheckItem.
// +---------------------------------------------------------+
// | +--------+ |
// | +--------+ |trailing| |
// | | leading| One line title |image or| |
// | | image | Multiline detail text |spinner | |
// | +--------+ +--------+ |
// +---------------------------------------------------------+
@interface SettingsCheckCell : TableViewCell

// Shows |activityIndicator| and starts animation. It will hide |imageView| if
// it was shown.
- (void)showActivityIndicator;

// Hides |activityIndicator| and stops animation.
- (void)hideActivityIndicator;

// Sets the |trailingImage| and tint |trailingColor| for it that should be
// displayed at the trailing edge of the cell. If set to nil, |trailingImage|
// will be hidden, otherwise |imageView| will be shown and |activityIndicator|
// will be hidden.
- (void)setTrailingImage:(UIImage*)trailingImage
withTintColor:(UIColor*)tintColor;

// Sets the [leadingImage] and tint [leadingColor] for it that should be
// displayed at the leading edge of the cell. If set to nil, the image is
// hidden.
- (void)setLeadingImage:(UIImage*)leadingImage
withTintColor:(UIColor*)tintColor;

@end

#endif // IOS_CHROME_BROWSER_UI_SETTINGS_CELLS_SETTINGS_CHECK_CELL_H_
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import "ios/chrome/browser/ui/settings/cells/settings_password_check_cell.h"
#import "ios/chrome/browser/ui/settings/cells/settings_check_cell.h"

#include "base/check.h"
#include "ios/chrome/browser/ui/table_view/cells/table_view_cells_constants.h"
Expand All @@ -16,59 +16,73 @@

namespace {

// Padding used between the icon and the text labels.
const CGFloat kIconLeadingPadding = 12;
// Padding used between the image and the text labels.
const CGFloat kTrailingImagePadding = 12;

// Size of the icon image.
// Size of the icon images.
const CGFloat kIconImageSize = 28;

} // namespace

@interface SettingsPasswordCheckCell ()
@interface SettingsCheckCell ()

// The image view for the trailing icon.
@property(nonatomic, strong) UIImageView* imageView;
// The image view for the trailing image.
@property(nonatomic, strong) UIImageView* trailingImageView;

// UIActivityIndicatorView spinning while check is running.
@property(nonatomic, readonly, strong)
UIActivityIndicatorView* activityIndicator;

// Constraint that is used to show only text without |imageView| or
// |activityIndicator|.
@property(nonatomic, strong) NSLayoutConstraint* onlyTextShownConstraint;
// Image view for the leading image.
@property(nonatomic, strong) UIImageView* leadingImageView;

// Constraint that is used to show either |imageView| or |activityIndicator|.
// Constraint that is used to define trailing text constraint without
// |trailingImageView| or |activityIndicator|.
@property(nonatomic, strong)
NSLayoutConstraint* iconOrIndicatorVisibleConstraint;
NSLayoutConstraint* textNoTrailingContentsConstraint;

// Constraint that is used to define trailing text constraint with either
// |trailingImageView| or |activityIndicator| showing.
@property(nonatomic, strong)
NSLayoutConstraint* textWithTrailingContentsConstraint;

// Constraint used for leading text constraint without |leadingImage|.
@property(nonatomic, strong) NSLayoutConstraint* textNoLeadingImageConstraint;

// Constraint used for leading text constraint with |leadingImage| showing.
@property(nonatomic, strong) NSLayoutConstraint* textWithLeadingImageConstraint;

@end

@implementation SettingsPasswordCheckCell
@implementation SettingsCheckCell

@synthesize textLabel = _textLabel;
@synthesize detailTextLabel = _detailTextLabel;
@synthesize activityIndicator = _activityIndicator;
@synthesize imageView = _imageView;

- (instancetype)initWithStyle:(UITableViewCellStyle)style
reuseIdentifier:(NSString*)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
UIView* contentView = self.contentView;

_imageView = [[UIImageView alloc] init];
_imageView.translatesAutoresizingMaskIntoConstraints = NO;
_imageView.tintColor = UIColor.cr_labelColor;
_imageView.hidden = YES;
[contentView addSubview:_imageView];
// Attributes of row contents in order or appearance (if present).

// |_leadingImageView| attributes
_leadingImageView = [[UIImageView alloc] init];
_leadingImageView.translatesAutoresizingMaskIntoConstraints = NO;
_leadingImageView.tintColor = UIColor.cr_labelColor;
_leadingImageView.hidden = NO;
[contentView addSubview:_leadingImageView];

// Text attributes.
// |_textLabel| attributes.
_textLabel = [[UILabel alloc] init];
_textLabel.translatesAutoresizingMaskIntoConstraints = NO;
_textLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
_textLabel.adjustsFontForContentSizeCategory = YES;
_textLabel.textColor = UIColor.cr_labelColor;
[contentView addSubview:_textLabel];

// |detailText| attributes.
_detailTextLabel = [[UILabel alloc] init];
_detailTextLabel.numberOfLines = 0;
_detailTextLabel.translatesAutoresizingMaskIntoConstraints = NO;
Expand All @@ -78,6 +92,14 @@ - (instancetype)initWithStyle:(UITableViewCellStyle)style
_detailTextLabel.textColor = UIColor.cr_secondaryLabelColor;
[contentView addSubview:_detailTextLabel];

// Only |_trailingImageView| or |_activityIndicator| is shown, not both at
// once. |trailingImage| attributes.
_trailingImageView = [[UIImageView alloc] init];
_trailingImageView.translatesAutoresizingMaskIntoConstraints = NO;
_trailingImageView.tintColor = UIColor.cr_labelColor;
_trailingImageView.hidden = YES;
[contentView addSubview:_trailingImageView];
// |activityIndictor| attributes.
_activityIndicator = [[UIActivityIndicatorView alloc] init];
_activityIndicator.translatesAutoresizingMaskIntoConstraints = NO;
_activityIndicator.hidden = YES;
Expand All @@ -87,13 +109,21 @@ - (instancetype)initWithStyle:(UITableViewCellStyle)style
UILayoutGuide* textLayoutGuide = [[UILayoutGuide alloc] init];
[self.contentView addLayoutGuide:textLayoutGuide];

_onlyTextShownConstraint = [textLayoutGuide.trailingAnchor
_textNoTrailingContentsConstraint = [textLayoutGuide.trailingAnchor
constraintEqualToAnchor:contentView.trailingAnchor
constant:-kTableViewHorizontalSpacing];

_iconOrIndicatorVisibleConstraint = [textLayoutGuide.trailingAnchor
constraintEqualToAnchor:_imageView.leadingAnchor
constant:-kIconLeadingPadding];
_textWithTrailingContentsConstraint = [textLayoutGuide.trailingAnchor
constraintEqualToAnchor:_trailingImageView.leadingAnchor
constant:-kTrailingImagePadding];

_textNoLeadingImageConstraint = [textLayoutGuide.leadingAnchor
constraintEqualToAnchor:contentView.leadingAnchor
constant:kTableViewHorizontalSpacing];

_textWithLeadingImageConstraint = [textLayoutGuide.leadingAnchor
constraintEqualToAnchor:_leadingImageView.trailingAnchor
constant:kTableViewHorizontalSpacing];

NSLayoutConstraint* heightConstraint = [self.contentView.heightAnchor
constraintGreaterThanOrEqualToConstant:kChromeTableViewCellHeight];
Expand All @@ -102,18 +132,23 @@ - (instancetype)initWithStyle:(UITableViewCellStyle)style
// indicator are hidden.
[NSLayoutConstraint activateConstraints:@[
heightConstraint,
_onlyTextShownConstraint,
_textNoTrailingContentsConstraint,

[_imageView.trailingAnchor
// Constraints for |_trailingImageView| (same position as
// |_activityIndictor|).
[_trailingImageView.trailingAnchor
constraintEqualToAnchor:self.contentView.trailingAnchor
constant:-kTableViewHorizontalSpacing],
[_imageView.widthAnchor constraintEqualToConstant:kIconImageSize],
[_imageView.heightAnchor constraintEqualToConstant:kIconImageSize],
[_imageView.centerYAnchor
[_trailingImageView.widthAnchor constraintEqualToConstant:kIconImageSize],
[_trailingImageView.heightAnchor
constraintEqualToConstant:kIconImageSize],
[_trailingImageView.centerYAnchor
constraintEqualToAnchor:textLayoutGuide.centerYAnchor],
[_imageView.leadingAnchor
[_trailingImageView.leadingAnchor
constraintEqualToAnchor:_activityIndicator.leadingAnchor],

// Constraints for |_activityIndictor| (same position as
// |_trailingImageView|).
[_activityIndicator.trailingAnchor
constraintEqualToAnchor:self.contentView.trailingAnchor
constant:-kTableViewHorizontalSpacing],
Expand All @@ -123,12 +158,18 @@ - (instancetype)initWithStyle:(UITableViewCellStyle)style
[_activityIndicator.centerYAnchor
constraintEqualToAnchor:textLayoutGuide.centerYAnchor],

[textLayoutGuide.leadingAnchor
// Constraints for |_leadingImageView|.
[_leadingImageView.leadingAnchor
constraintEqualToAnchor:self.contentView.leadingAnchor
constant:kTableViewHorizontalSpacing],
[_leadingImageView.widthAnchor constraintEqualToConstant:kIconImageSize],
[_leadingImageView.heightAnchor constraintEqualToConstant:kIconImageSize],
[_leadingImageView.centerYAnchor
constraintEqualToAnchor:textLayoutGuide.centerYAnchor],

// Constraints for |_textLabel| and |_detailTextLabel|.
[textLayoutGuide.centerYAnchor
constraintEqualToAnchor:self.contentView.centerYAnchor],

[textLayoutGuide.leadingAnchor
constraintEqualToAnchor:_textLabel.leadingAnchor],
[textLayoutGuide.leadingAnchor
Expand All @@ -154,10 +195,10 @@ - (void)showActivityIndicator {
if (!self.activityIndicator.hidden)
return;

self.imageView.hidden = YES;
self.trailingImageView.hidden = YES;
self.activityIndicator.hidden = NO;
[self.activityIndicator startAnimating];
[self updateTextConstraints];
[self updateTrailingImageTextConstraints];
}

- (void)hideActivityIndicator {
Expand All @@ -166,35 +207,52 @@ - (void)hideActivityIndicator {

[self.activityIndicator stopAnimating];
self.activityIndicator.hidden = YES;
[self updateTextConstraints];
[self updateTrailingImageTextConstraints];
}

- (void)setIconImage:(UIImage*)image withTintColor:(UIColor*)color {
if (color) {
self.imageView.tintColor = color;
}
BOOL hidden = !image;
self.imageView.image = image;
if (hidden == self.imageView.hidden)
- (void)setTrailingImage:(UIImage*)trailingImage
withTintColor:(UIColor*)trailingImageColor {
self.trailingImageView.tintColor = trailingImageColor;
BOOL hidden = !trailingImage;
self.trailingImageView.image = trailingImage;
if (hidden == self.trailingImageView.hidden)
return;
self.imageView.hidden = hidden;
self.trailingImageView.hidden = hidden;
if (!hidden) {
[self hideActivityIndicator];
}
[self updateTextConstraints];
[self updateTrailingImageTextConstraints];
}

- (void)setLeadingImage:(UIImage*)leadingImage
withTintColor:(UIColor*)leadingImageColor {
self.leadingImageView.tintColor = leadingImageColor;
BOOL hidden = !leadingImage;
self.leadingImageView.image = leadingImage;
self.leadingImageView.hidden = hidden;
// Update the leading text constraint based on |image| being provided.
if (hidden) {
_textWithLeadingImageConstraint.active = NO;
_textNoLeadingImageConstraint.active = YES;
} else {
_textNoLeadingImageConstraint.active = NO;
_textWithLeadingImageConstraint.active = YES;
}
}

#pragma mark - Private Methods

- (void)updateTextConstraints {
// Active proper |textLayoutGuide| trailing constraint to show |imageView| or
// |activityIndicator|.
if (self.activityIndicator.hidden && self.imageView.hidden) {
_iconOrIndicatorVisibleConstraint.active = NO;
_onlyTextShownConstraint.active = YES;
// Updates the constraints around the trailing image for when |trailingImage| or
// |activityIndicator| is shown or hidden.
- (void)updateTrailingImageTextConstraints {
// Active proper |textLayoutGuide| trailing constraint to show
// |trailingImageView| or |activityIndicator|.
if (self.activityIndicator.hidden && self.trailingImageView.hidden) {
_textWithTrailingContentsConstraint.active = NO;
_textNoTrailingContentsConstraint.active = YES;
} else {
_onlyTextShownConstraint.active = NO;
_iconOrIndicatorVisibleConstraint.active = YES;
_textNoTrailingContentsConstraint.active = NO;
_textWithTrailingContentsConstraint.active = YES;
}
}

Expand All @@ -206,7 +264,8 @@ - (void)prepareForReuse {
self.textLabel.text = nil;
self.detailTextLabel.text = nil;
self.accessibilityTraits = UIAccessibilityTraitNone;
[self setIconImage:nil withTintColor:nil];
[self setTrailingImage:nil withTintColor:nil];
[self setLeadingImage:nil withTintColor:nil];
[self hideActivityIndicator];
}

Expand Down

0 comments on commit f776805

Please sign in to comment.