Commit 1ec4e039 authored by Jérôme Lebel's avatar Jérôme Lebel Committed by Commit Bot

[iOS] Adding -ListMode.collapsableMode

Adding collapsableMode property in ListMode which can have 2 values:
 + ListModelCollapsableModeHeader
 + ListModelCollapsableModeFirstCell

The header mode is used to when the header to collapse a section. When
collapsed, a section contains no item.

The first cell mode is used to when the first cell to collapse a
section. When collapsed, a section contains the first item.


Cq-Include-Trybots: luci.chromium.try:ios-simulator-full-configs;master.tryserver.chromium.mac:ios-simulator-cronet
Change-Id: If46f85d5c412b1b799099635fefe7a8c8d11bbf2
Reviewed-on: https://chromium-review.googlesource.com/1101558
Commit-Queue: Jérôme Lebel <jlebel@chromium.org>
Reviewed-by: default avatarSergio Collazos <sczs@chromium.org>
Cr-Commit-Position: refs/heads/master@{#568495}
parent f22976c5
......@@ -9,6 +9,15 @@
@class ListItem;
// Collapsable mode to use either the header cell or the first cell to collapse
// sections in ListMode.
typedef NS_ENUM(NSInteger, ListModelCollapsableMode) {
// When a section is collapsed, all items are hidden.
ListModelCollapsableModeHeader = 0,
// When a section is collapsed, all items except the first one are hidden.
ListModelCollapsableModeFirstCell,
};
// Key for saving collapsed state in the NSUserDefaults.
extern NSString* const kListModelCollapsedKey;
......@@ -189,6 +198,9 @@ const NSInteger kItemTypeEnumZero = 100;
#pragma mark Collapsing methods.
// The default value is ListModelCollapsableModeHeader.
@property(nonatomic, assign) ListModelCollapsableMode collapsableMode;
// Sets an existing |sectionIdentifier| |collapsedKey| to be used when
// collapsing or expanding a section. |collapsedKey| is a unique identifier for
// each section that will be used for persisting information about the collapsed
......
......@@ -34,6 +34,8 @@ typedef NSMutableArray<ListItem*> SectionItems;
NSMutableDictionary<NSNumber*, NSString*>* _collapsedKeys;
}
@synthesize collapsableMode = _collapsableMode;
- (instancetype)init {
if ((self = [super init])) {
_sectionIdentifiers = [[NSMutableArray alloc] init];
......@@ -293,9 +295,17 @@ typedef NSMutableArray<ListItem*> SectionItems;
- (NSInteger)numberOfItemsInSection:(NSInteger)section {
DCHECK_LT(base::checked_cast<NSUInteger>(section), [_sections count]);
NSInteger sectionIdentifier = [self sectionIdentifierForSection:section];
if ([self sectionIsCollapsed:sectionIdentifier])
return 0;
SectionItems* items = [_sections objectAtIndex:section];
if ([self sectionIsCollapsed:sectionIdentifier]) {
switch (self.collapsableMode) {
case ListModelCollapsableModeHeader:
return 0;
case ListModelCollapsableModeFirstCell:
DCHECK_LT(0ul, items.count);
return 1;
}
NOTREACHED();
}
return items.count;
}
......
......@@ -174,4 +174,27 @@ TEST_F(ListModelCollapseTest, PersistCollapsedSections) {
EXPECT_TRUE([model sectionIsCollapsed:SectionIdentifierBar]);
}
TEST_F(ListModelCollapseTest, CollapsedSectionMode) {
model.collapsableMode = ListModelCollapsableModeFirstCell;
[model setSection:SectionIdentifierFoo collapsed:YES];
[model setSection:SectionIdentifierBar collapsed:YES];
// SectionIdentifierFoo
EXPECT_EQ(1, [model numberOfItemsInSection:0]);
EXPECT_TRUE([model sectionIsCollapsed:SectionIdentifierFoo]);
// SectionIdentifierBar
EXPECT_EQ(1, [model numberOfItemsInSection:1]);
EXPECT_TRUE([model sectionIsCollapsed:SectionIdentifierBar]);
[model setSection:SectionIdentifierFoo collapsed:NO];
[model setSection:SectionIdentifierBar collapsed:NO];
// SectionIdentifierFoo
EXPECT_EQ(1, [model numberOfItemsInSection:0]);
EXPECT_FALSE([model sectionIsCollapsed:SectionIdentifierFoo]);
// SectionIdentifierBar
EXPECT_EQ(2, [model numberOfItemsInSection:1]);
EXPECT_FALSE([model sectionIsCollapsed:SectionIdentifierBar]);
}
} // namespace
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