Commit 184a5d74 authored by gambard's avatar gambard Committed by Commit bot

Suggestions UI - Add Data Source and Item

BUG=679369

Review-Url: https://codereview.chromium.org/2619963004
Cr-Commit-Position: refs/heads/master@{#443215}
parent 71fc94ca
......@@ -5,7 +5,11 @@
source_set("suggestions") {
configs += [ "//build/config/compiler:enable_arc" ]
sources = [
"suggestions_collection_updater.h",
"suggestions_collection_updater.mm",
"suggestions_commands.h",
"suggestions_item.h",
"suggestions_item.mm",
"suggestions_item_actions.h",
"suggestions_view_controller.h",
"suggestions_view_controller.mm",
......
// Copyright 2016 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_SUGGESTIONS_SUGGESTIONS_COLLECTION_UPDATER_H_
#define IOS_CHROME_BROWSER_UI_SUGGESTIONS_SUGGESTIONS_COLLECTION_UPDATER_H_
#import <UIKit/UIKit.h>
@class CollectionViewController;
// Updater for a CollectionViewController populating it with some items and
// handling the items addition.
@interface SuggestionsCollectionUpdater : NSObject
// |collectionViewController| this Updater will update.
- (instancetype)initWithCollectionViewController:
(CollectionViewController*)collectionViewController
NS_DESIGNATED_INITIALIZER;
- (instancetype)init NS_UNAVAILABLE;
// Adds a text item with a |title| and a |subtitle| in the section numbered
// |section|. If |section| is greater than the current number of section, it
// will add a new section at the end.
- (void)addTextItem:(NSString*)title
subtitle:(NSString*)subtitle
toSection:(NSInteger)inputSection;
@end
#endif // IOS_CHROME_BROWSER_UI_SUGGESTIONS_SUGGESTIONS_COLLECTION_UPDATER_H_
// Copyright 2016 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/suggestions/suggestions_collection_updater.h"
#import "ios/chrome/browser/ui/collection_view/collection_view_controller.h"
#import "ios/chrome/browser/ui/collection_view/collection_view_model.h"
#import "ios/chrome/browser/ui/suggestions/suggestions_item.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
namespace {
typedef NS_ENUM(NSInteger, ItemType) {
ItemTypeText = kItemTypeEnumZero,
ItemTypeArticle,
ItemTypeExpand,
};
} // namespace
@implementation SuggestionsCollectionUpdater {
CollectionViewController* _collectionViewController;
}
- (instancetype)initWithCollectionViewController:
(CollectionViewController*)collectionViewController {
self = [super init];
if (self) {
_collectionViewController = collectionViewController;
[collectionViewController loadModel];
CollectionViewModel* model = collectionViewController.collectionViewModel;
NSInteger sectionIdentifier = kSectionIdentifierEnumZero;
for (NSInteger i = 0; i < 3; i++) {
[model addSectionWithIdentifier:sectionIdentifier];
[model addItem:[[SuggestionsItem alloc] initWithType:ItemTypeText
title:@"The title"
subtitle:@"The subtitle"]
toSectionWithIdentifier:sectionIdentifier];
sectionIdentifier++;
}
}
return self;
}
#pragma mark - Public methods
- (void)addTextItem:(NSString*)title
subtitle:(NSString*)subtitle
toSection:(NSInteger)inputSection {
SuggestionsItem* item = [[SuggestionsItem alloc] initWithType:ItemTypeText
title:title
subtitle:subtitle];
NSInteger sectionIdentifier = kSectionIdentifierEnumZero + inputSection;
NSInteger sectionIndex = inputSection;
CollectionViewModel* model = _collectionViewController.collectionViewModel;
if ([model numberOfSections] <= inputSection) {
sectionIndex = [model numberOfSections];
sectionIdentifier = kSectionIdentifierEnumZero + sectionIndex;
[_collectionViewController.collectionView performBatchUpdates:^{
[_collectionViewController.collectionViewModel
addSectionWithIdentifier:sectionIdentifier];
[_collectionViewController.collectionView
insertSections:[NSIndexSet indexSetWithIndex:sectionIndex]];
}
completion:nil];
}
NSInteger numberOfItemsInSection =
[model numberOfItemsInSection:sectionIndex];
[_collectionViewController.collectionViewModel addItem:item
toSectionWithIdentifier:sectionIdentifier];
[_collectionViewController.collectionView performBatchUpdates:^{
[_collectionViewController.collectionView
insertItemsAtIndexPaths:@[ [NSIndexPath
indexPathForRow:numberOfItemsInSection
inSection:sectionIndex] ]];
}
completion:nil];
}
@end
// Copyright 2016 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_SUGGESTIONS_SUGGESTIONS_ITEM_H_
#define IOS_CHROME_BROWSER_UI_SUGGESTIONS_SUGGESTIONS_ITEM_H_
#import "ios/chrome/browser/ui/collection_view/cells/collection_view_item.h"
#import "ios/third_party/material_components_ios/src/components/CollectionCells/src/MaterialCollectionCells.h"
// Item for a suggestions item with a title and subtitle.
@interface SuggestionsItem : CollectionViewItem
// Init a suggestions item with a |title| and a |subtitle|. |type| is the type
// of the item.
- (instancetype)initWithType:(NSInteger)type
title:(NSString*)title
subtitle:(NSString*)subtitle NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithType:(NSInteger)type NS_UNAVAILABLE;
@end
// Corresponding cell for a suggestion item.
@interface SuggestionsCell : MDCCollectionViewCell
@property(nonatomic, readonly, strong) UIButton* titleButton;
@property(nonatomic, readonly, strong) UILabel* detailTextLabel;
@end
#endif // IOS_CHROME_BROWSER_UI_SUGGESTIONS_SUGGESTIONS_ITEM_H_
// Copyright 2016 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/suggestions/suggestions_item.h"
#import "ios/chrome/browser/ui/suggestions/suggestions_item_actions.h"
#import "ios/third_party/material_components_ios/src/components/Palettes/src/MaterialPalettes.h"
#import "ios/third_party/material_roboto_font_loader_ios/src/src/MaterialRobotoFontLoader.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
@interface SuggestionsItem ()
@property(nonatomic, copy) NSString* title;
@property(nonatomic, copy) NSString* subtitle;
@end
@implementation SuggestionsItem
@synthesize title = _title;
@synthesize subtitle = _subtitle;
- (instancetype)initWithType:(NSInteger)type
title:(NSString*)title
subtitle:(NSString*)subtitle {
self = [super initWithType:type];
if (self) {
self.cellClass = [SuggestionsCell class];
_title = [title copy];
_subtitle = [subtitle copy];
}
return self;
}
#pragma mark - CollectionViewItem
- (void)configureCell:(SuggestionsCell*)cell {
[super configureCell:cell];
[cell.titleButton setTitle:self.title forState:UIControlStateNormal];
cell.detailTextLabel.text = self.subtitle;
}
@end
#pragma mark - SuggestionsCell
@implementation SuggestionsCell
@synthesize titleButton = _titleButton;
@synthesize detailTextLabel = _detailTextLabel;
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
MDFRobotoFontLoader* fontLoader = [MDFRobotoFontLoader sharedInstance];
_titleButton = [UIButton buttonWithType:UIButtonTypeSystem];
_titleButton.titleLabel.font = [fontLoader mediumFontOfSize:16];
_titleButton.titleLabel.textColor = [[MDCPalette greyPalette] tint900];
[_titleButton addTarget:nil
action:@selector(addNewItem:)
forControlEvents:UIControlEventTouchUpInside];
_detailTextLabel = [[UILabel alloc] init];
_detailTextLabel.font = [fontLoader mediumFontOfSize:14];
_detailTextLabel.textColor = [[MDCPalette greyPalette] tint500];
UIStackView* labelsStack = [[UIStackView alloc]
initWithArrangedSubviews:@[ _titleButton, _detailTextLabel ]];
labelsStack.axis = UILayoutConstraintAxisVertical;
[self.contentView addSubview:labelsStack];
labelsStack.layoutMarginsRelativeArrangement = YES;
labelsStack.layoutMargins = UIEdgeInsetsMake(16, 16, 16, 16);
labelsStack.alignment = UIStackViewAlignmentCenter;
labelsStack.translatesAutoresizingMaskIntoConstraints = NO;
[NSLayoutConstraint activateConstraints:@[
[self.contentView.leadingAnchor
constraintEqualToAnchor:labelsStack.leadingAnchor],
[self.contentView.trailingAnchor
constraintEqualToAnchor:labelsStack.trailingAnchor],
[self.contentView.topAnchor
constraintEqualToAnchor:labelsStack.topAnchor],
[self.contentView.bottomAnchor
constraintEqualToAnchor:labelsStack.bottomAnchor]
]];
self.backgroundColor = [UIColor whiteColor];
}
return self;
}
@end
......@@ -17,6 +17,13 @@
// Handler for the commands sent by the SuggestionsViewController.
@property(nonatomic, weak) id<SuggestionsCommands> suggestionCommandHandler;
// Adds a text item with a |title| and a |subtitle| in the section numbered
// |section|. If |section| is greater than the current number of section, it
// will add a new section at the end.
- (void)addTextItem:(NSString*)title
subtitle:(NSString*)subtitle
toSection:(NSInteger)inputSection;
@end
#endif // IOS_CHROME_BROWSER_UI_SUGGESTIONS_SUGGESTIONS_VIEW_CONTROLLER_H_
......@@ -7,6 +7,7 @@
#import "ios/chrome/browser/ui/collection_view/cells/MDCCollectionViewCell+Chrome.h"
#import "ios/chrome/browser/ui/collection_view/cells/collection_view_item.h"
#import "ios/chrome/browser/ui/collection_view/collection_view_model.h"
#import "ios/chrome/browser/ui/suggestions/suggestions_collection_updater.h"
#import "ios/chrome/browser/ui/suggestions/suggestions_commands.h"
#import "ios/chrome/browser/ui/suggestions/suggestions_item_actions.h"
......@@ -16,17 +17,23 @@
@interface SuggestionsViewController ()<SuggestionsItemActions>
@property(nonatomic, strong) SuggestionsCollectionUpdater* collectionUpdater;
@end
@implementation SuggestionsViewController
@synthesize suggestionCommandHandler = _suggestionCommandHandler;
@synthesize collectionUpdater = _collectionUpdater;
#pragma mark - UIViewController
- (void)viewDidLoad {
[super viewDidLoad];
_collectionUpdater = [[SuggestionsCollectionUpdater alloc]
initWithCollectionViewController:self];
self.collectionView.delegate = self;
self.styler.cellStyle = MDCCollectionViewCellStyleCard;
}
......@@ -53,4 +60,14 @@
[self.suggestionCommandHandler addEmptyItem];
}
#pragma mark - SuggestionsCollectionUpdater forwarding
- (void)addTextItem:(NSString*)title
subtitle:(NSString*)subtitle
toSection:(NSInteger)inputSection {
[self.collectionUpdater addTextItem:title
subtitle:subtitle
toSection:inputSection];
}
@end
......@@ -13,26 +13,34 @@
@interface SCSuggestionsCoordinator ()<SuggestionsCommands>
@property(nonatomic, strong)
SuggestionsViewController* suggestionViewController;
@end
@implementation SCSuggestionsCoordinator
@synthesize baseViewController;
@synthesize suggestionViewController = _suggestionViewController;
#pragma mark - Coordinator
- (void)start {
SuggestionsViewController* suggestion = [[SuggestionsViewController alloc]
_suggestionViewController = [[SuggestionsViewController alloc]
initWithStyle:CollectionViewControllerStyleDefault];
suggestion.suggestionCommandHandler = self;
_suggestionViewController.suggestionCommandHandler = self;
[self.baseViewController pushViewController:suggestion animated:YES];
[self.baseViewController pushViewController:_suggestionViewController
animated:YES];
}
#pragma mark - SuggestionsCommands
- (void)addEmptyItem {
[self.suggestionViewController addTextItem:@"Button clicked"
subtitle:@"Item Added!"
toSection:5];
}
@end
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