Commit 7850d05e authored by Chris Lu's avatar Chris Lu Committed by Commit Bot

[ios] Create new TableViewAccessoryItem

Creates tableviewcellaccessoryitem and tableviewcellaccessorycell. Implements it for "show full history" cell in Recent Tabs.

Screenshot: https://drive.google.com/open?id=1yYsY1BMOgqLV1jz6G8x-oOlOHTlsAEih

Change-Id: I60a911a12fe95f24d706c669f85ef08691ad911e
Reviewed-on: https://chromium-review.googlesource.com/1053351
Commit-Queue: Chris Lu <thegreenfrog@chromium.org>
Reviewed-by: default avatarSergio Collazos <sczs@chromium.org>
Cr-Commit-Position: refs/heads/master@{#558856}
parent c4d9d4f5
......@@ -32,6 +32,7 @@
#include "ios/chrome/browser/ui/ntp/recent_tabs/synced_sessions.h"
#import "ios/chrome/browser/ui/settings/sync_utils/sync_presenter.h"
#import "ios/chrome/browser/ui/signin_interaction/public/signin_presenter.h"
#import "ios/chrome/browser/ui/table_view/cells/table_view_accessory_item.h"
#import "ios/chrome/browser/ui/table_view/cells/table_view_activity_indicator_header_footer_item.h"
#import "ios/chrome/browser/ui/table_view/cells/table_view_cells_constants.h"
#import "ios/chrome/browser/ui/table_view/cells/table_view_disclosure_header_footer_item.h"
......@@ -201,10 +202,10 @@ const int kRelativeTimeMaxHours = 4;
[self addRecentlyClosedTabItems];
// Add show full history item last.
TableViewURLItem* historyItem =
[[TableViewURLItem alloc] initWithType:ItemTypeShowFullHistory];
TableViewAccessoryItem* historyItem =
[[TableViewAccessoryItem alloc] initWithType:ItemTypeShowFullHistory];
historyItem.title = l10n_util::GetNSString(IDS_HISTORY_SHOWFULLHISTORY_LINK);
historyItem.favicon = [UIImage imageNamed:@"show_history"];
// historyItem.image = [UIImage imageNamed:@"show_history"];
[model addItem:historyItem
toSectionWithIdentifier:SectionIdentifierRecentlyClosedTabs];
}
......
......@@ -4,6 +4,7 @@
#import "ios/chrome/browser/ui/settings/table_cell_catalog_view_controller.h"
#import "ios/chrome/browser/ui/table_view/cells/table_view_accessory_item.h"
#import "ios/chrome/browser/ui/table_view/cells/table_view_text_header_footer_item.h"
#import "ios/chrome/browser/ui/table_view/cells/table_view_text_item.h"
#import "ios/chrome/browser/ui/table_view/cells/table_view_url_item.h"
......@@ -25,6 +26,8 @@ typedef NS_ENUM(NSInteger, ItemType) {
ItemTypeTextHeader,
ItemTypeTextFooter,
ItemTypeURLNoMetadata,
ItemTypeTextAccessoryImage,
ItemTypeTextAccessoryNoImage,
ItemTypeURLWithTimestamp,
ItemTypeURLWithSize,
};
......@@ -72,6 +75,19 @@ typedef NS_ENUM(NSInteger, ItemType) {
textItem.textColor = TextItemColorBlack;
[model addItem:textItem toSectionWithIdentifier:SectionIdentifierText];
TableViewAccessoryItem* textAccessoryItem =
[[TableViewAccessoryItem alloc] initWithType:ItemTypeTextAccessoryImage];
textAccessoryItem.title = @"Text Accessory with History Image";
textAccessoryItem.image = [UIImage imageNamed:@"show_history"];
[model addItem:textAccessoryItem
toSectionWithIdentifier:SectionIdentifierText];
textAccessoryItem = [[TableViewAccessoryItem alloc]
initWithType:ItemTypeTextAccessoryNoImage];
textAccessoryItem.title = @"Text Accessory No Image";
[model addItem:textAccessoryItem
toSectionWithIdentifier:SectionIdentifierText];
TableViewTextItem* textItemDefault =
[[TableViewTextItem alloc] initWithType:ItemTypeText];
textItemDefault.text = @"Simple Text Cell with Defaults";
......
......@@ -4,6 +4,8 @@
source_set("cells") {
sources = [
"table_view_accessory_item.h",
"table_view_accessory_item.mm",
"table_view_activity_indicator_header_footer_item.h",
"table_view_activity_indicator_header_footer_item.mm",
"table_view_cells_constants.h",
......@@ -42,6 +44,7 @@ source_set("cells") {
source_set("unit_tests") {
testonly = true
sources = [
"table_view_accessory_item_unittest.mm",
"table_view_header_footer_item_unittest.mm",
"table_view_item_unittest.mm",
"table_view_text_header_footer_item_unittest.mm",
......
// Copyright 2018 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_TABLE_VIEW_CELLS_TABLE_VIEW_ACCESSORY_ITEM_H_
#define IOS_CHROME_BROWSER_UI_TABLE_VIEW_CELLS_TABLE_VIEW_ACCESSORY_ITEM_H_
#import <UIKit/UIKit.h>
#import "ios/chrome/browser/ui/table_view/cells/table_view_item.h"
// TableViewAccessoryItem contains the model data for a TableViewAccessoryCell.
@interface TableViewAccessoryItem : TableViewItem
// The image in the cell. If nil, won't be added to the view hierarchy.
@property(nonatomic, readwrite, strong) UIImage* image;
// The title label in the cell.
@property(nonatomic, readwrite, copy) NSString* title;
@end
// TableViewAccessoryCell contains a favicon, a title, and an accessory.
@interface TableViewAccessoryCell : UITableViewCell
// The cell favicon imageView.
@property(nonatomic, readonly, strong) UIImageView* imageView;
// The cell title.
@property(nonatomic, readonly, strong) UILabel* titleLabel;
@end
#endif // IOS_CHROME_BROWSER_UI_TABLE_VIEW_CELLS_TABLE_VIEW_ACCESSORY_ITEM_H_
// Copyright 2018 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.
#import "ios/chrome/browser/ui/table_view/cells/table_view_accessory_item.h"
#include "base/mac/foundation_util.h"
#import "ios/chrome/browser/ui/table_view/cells/table_view_cells_constants.h"
#import "ios/chrome/browser/ui/table_view/chrome_table_view_styler.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
namespace {
// The width and height of the ImageView.
const float kImageWidth = 28.0f;
}
@implementation TableViewAccessoryItem
@synthesize image = _image;
@synthesize title = _title;
- (instancetype)initWithType:(NSInteger)type {
self = [super initWithType:type];
if (self) {
self.cellClass = [TableViewAccessoryCell class];
}
return self;
}
- (void)configureCell:(UITableViewCell*)tableCell
withStyler:(ChromeTableViewStyler*)styler {
[super configureCell:tableCell withStyler:styler];
TableViewAccessoryCell* cell =
base::mac::ObjCCastStrict<TableViewAccessoryCell>(tableCell);
if (self.image) {
cell.imageView.hidden = NO;
cell.imageView.image = self.image;
} else {
// No image. Hide imageView.
cell.imageView.hidden = YES;
}
cell.titleLabel.text = self.title;
cell.imageView.backgroundColor = styler.tableViewBackgroundColor;
cell.titleLabel.backgroundColor = styler.tableViewBackgroundColor;
}
@end
@implementation TableViewAccessoryCell
@synthesize imageView = _imageView;
@synthesize titleLabel = _titleLabel;
- (instancetype)initWithStyle:(UITableViewCellStyle)style
reuseIdentifier:(NSString*)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
_imageView = [[UIImageView alloc] init];
_titleLabel = [[UILabel alloc] init];
// The favicon image is smaller than its UIImageView's bounds, so center
// it.
_imageView.contentMode = UIViewContentModeCenter;
// Set font size using dynamic type.
_titleLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
_titleLabel.adjustsFontForContentSizeCategory = YES;
// Disclosure ImageView.
UIImageView* disclosureImageView = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:@"table_view_cell_chevron"]];
[disclosureImageView
setContentHuggingPriority:UILayoutPriorityDefaultHigh
forAxis:UILayoutConstraintAxisHorizontal];
// Horizontal stack view holds favicon, title, and disclosureView.
UIStackView* horizontalStack =
[[UIStackView alloc] initWithArrangedSubviews:@[
_imageView, _titleLabel, disclosureImageView
]];
horizontalStack.axis = UILayoutConstraintAxisHorizontal;
horizontalStack.spacing = kTableViewSubViewHorizontalSpacing;
horizontalStack.distribution = UIStackViewDistributionFill;
horizontalStack.alignment = UIStackViewAlignmentCenter;
horizontalStack.translatesAutoresizingMaskIntoConstraints = NO;
[self.contentView addSubview:horizontalStack];
[NSLayoutConstraint activateConstraints:@[
// The favicon view is a fixed size.
[_imageView.heightAnchor constraintEqualToConstant:kImageWidth],
[_imageView.widthAnchor constraintEqualToConstant:kImageWidth],
// Horizontal Stack constraints.
[horizontalStack.leadingAnchor
constraintEqualToAnchor:self.contentView.leadingAnchor
constant:kTableViewHorizontalSpacing],
[horizontalStack.trailingAnchor
constraintEqualToAnchor:self.contentView.trailingAnchor
constant:-kTableViewHorizontalSpacing],
[horizontalStack.topAnchor
constraintEqualToAnchor:self.contentView.topAnchor
constant:kTableViewVerticalSpacing],
[horizontalStack.bottomAnchor
constraintEqualToAnchor:self.contentView.bottomAnchor
constant:-kTableViewVerticalSpacing],
]];
}
return self;
}
@end
// Copyright 2018 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.
#import "ios/chrome/browser/ui/table_view/cells/table_view_accessory_item.h"
#include "base/mac/foundation_util.h"
#import "ios/chrome/browser/ui/table_view/chrome_table_view_styler.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/gtest_mac.h"
#include "testing/platform_test.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
namespace {
using TableViewAccessoryItemTest = PlatformTest;
}
// Tests that the UILabel is set properly after a call to
// |configureCell:| and the image is visible.
TEST_F(TableViewAccessoryItemTest, ItemProperties) {
NSString* text = @"Cell text";
TableViewAccessoryItem* item =
[[TableViewAccessoryItem alloc] initWithType:0];
item.title = text;
item.image = [[UIImage alloc] init];
id cell = [[[item cellClass] alloc] init];
ASSERT_TRUE([cell isMemberOfClass:[TableViewAccessoryCell class]]);
TableViewAccessoryCell* accessoryCell =
base::mac::ObjCCastStrict<TableViewAccessoryCell>(cell);
EXPECT_FALSE(accessoryCell.textLabel.text);
EXPECT_FALSE(accessoryCell.imageView.image);
[item configureCell:cell withStyler:[[ChromeTableViewStyler alloc] init]];
EXPECT_NSEQ(text, accessoryCell.titleLabel.text);
EXPECT_FALSE(accessoryCell.imageView.isHidden);
}
// Tests that the imageView is not visible if no image is set.
TEST_F(TableViewAccessoryItemTest, ItemImageViewHidden) {
NSString* text = @"Cell text";
TableViewAccessoryItem* item =
[[TableViewAccessoryItem alloc] initWithType:0];
item.title = text;
id cell = [[[item cellClass] alloc] init];
ASSERT_TRUE([cell isMemberOfClass:[TableViewAccessoryCell class]]);
TableViewAccessoryCell* accessoryCell =
base::mac::ObjCCastStrict<TableViewAccessoryCell>(cell);
EXPECT_FALSE(item.image);
[item configureCell:cell withStyler:[[ChromeTableViewStyler alloc] init]];
EXPECT_FALSE(item.image);
EXPECT_TRUE(accessoryCell.imageView.isHidden);
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment