OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h" | 5 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/mac/scoped_nsobject.h" | |
9 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_item.h" | 8 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_item.h" |
10 | 9 |
| 10 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 11 #error "This file requires ARC support." |
| 12 #endif |
| 13 |
11 namespace { | 14 namespace { |
12 typedef NSMutableArray<CollectionViewItem*> SectionItems; | 15 typedef NSMutableArray<CollectionViewItem*> SectionItems; |
13 } | 16 } |
14 | 17 |
15 @implementation CollectionViewModel { | 18 @implementation CollectionViewModel { |
16 // Ordered list of section identifiers, one per section in the model. | 19 // Ordered list of section identifiers, one per section in the model. |
17 base::scoped_nsobject<NSMutableArray<NSNumber*>> _sectionIdentifiers; | 20 NSMutableArray<NSNumber*>* _sectionIdentifiers; |
18 | 21 |
19 // The lists of section items, one per section. | 22 // The lists of section items, one per section. |
20 base::scoped_nsobject<NSMutableArray<SectionItems*>> _sections; | 23 NSMutableArray<SectionItems*>* _sections; |
21 | 24 |
22 // Maps from section identifier to header and footer. | 25 // Maps from section identifier to header and footer. |
23 base::scoped_nsobject<NSMutableDictionary<NSNumber*, CollectionViewItem*>> | 26 NSMutableDictionary<NSNumber*, CollectionViewItem*>* _headers; |
24 _headers; | 27 NSMutableDictionary<NSNumber*, CollectionViewItem*>* _footers; |
25 base::scoped_nsobject<NSMutableDictionary<NSNumber*, CollectionViewItem*>> | |
26 _footers; | |
27 } | 28 } |
28 | 29 |
29 - (instancetype)init { | 30 - (instancetype)init { |
30 if ((self = [super init])) { | 31 if ((self = [super init])) { |
31 _sectionIdentifiers.reset([[NSMutableArray alloc] init]); | 32 _sectionIdentifiers = [[NSMutableArray alloc] init]; |
32 _sections.reset([[NSMutableArray alloc] init]); | 33 _sections = [[NSMutableArray alloc] init]; |
33 _headers.reset([[NSMutableDictionary alloc] init]); | 34 _headers = [[NSMutableDictionary alloc] init]; |
34 _footers.reset([[NSMutableDictionary alloc] init]); | 35 _footers = [[NSMutableDictionary alloc] init]; |
35 } | 36 } |
36 return self; | 37 return self; |
37 } | 38 } |
38 | 39 |
39 #pragma mark Modification methods | 40 #pragma mark Modification methods |
40 | 41 |
41 - (void)addSectionWithIdentifier:(NSInteger)sectionIdentifier { | 42 - (void)addSectionWithIdentifier:(NSInteger)sectionIdentifier { |
42 DCHECK_GE(sectionIdentifier, kSectionIdentifierEnumZero); | 43 DCHECK_GE(sectionIdentifier, kSectionIdentifierEnumZero); |
43 DCHECK_EQ(static_cast<NSUInteger>(NSNotFound), | 44 DCHECK_EQ(static_cast<NSUInteger>(NSNotFound), |
44 [self internalSectionForIdentifier:sectionIdentifier]); | 45 [self internalSectionForIdentifier:sectionIdentifier]); |
45 [_sectionIdentifiers addObject:@(sectionIdentifier)]; | 46 [_sectionIdentifiers addObject:@(sectionIdentifier)]; |
46 | 47 |
47 base::scoped_nsobject<SectionItems> section([[SectionItems alloc] init]); | 48 SectionItems* section = [[SectionItems alloc] init]; |
48 [_sections addObject:section]; | 49 [_sections addObject:section]; |
49 } | 50 } |
50 | 51 |
51 - (void)insertSectionWithIdentifier:(NSInteger)sectionIdentifier | 52 - (void)insertSectionWithIdentifier:(NSInteger)sectionIdentifier |
52 atIndex:(NSUInteger)index { | 53 atIndex:(NSUInteger)index { |
53 DCHECK_GE(sectionIdentifier, kSectionIdentifierEnumZero); | 54 DCHECK_GE(sectionIdentifier, kSectionIdentifierEnumZero); |
54 DCHECK_EQ(static_cast<NSUInteger>(NSNotFound), | 55 DCHECK_EQ(static_cast<NSUInteger>(NSNotFound), |
55 [self internalSectionForIdentifier:sectionIdentifier]); | 56 [self internalSectionForIdentifier:sectionIdentifier]); |
56 DCHECK_LE(index, [_sections count]); | 57 DCHECK_LE(index, [_sections count]); |
57 | 58 |
58 [_sectionIdentifiers insertObject:@(sectionIdentifier) atIndex:index]; | 59 [_sectionIdentifiers insertObject:@(sectionIdentifier) atIndex:index]; |
59 | 60 |
60 base::scoped_nsobject<SectionItems> section([[SectionItems alloc] init]); | 61 SectionItems* section = [[SectionItems alloc] init]; |
61 [_sections insertObject:section atIndex:index]; | 62 [_sections insertObject:section atIndex:index]; |
62 } | 63 } |
63 | 64 |
64 - (void)addItem:(CollectionViewItem*)item | 65 - (void)addItem:(CollectionViewItem*)item |
65 toSectionWithIdentifier:(NSInteger)sectionIdentifier { | 66 toSectionWithIdentifier:(NSInteger)sectionIdentifier { |
66 DCHECK_GE(item.type, kItemTypeEnumZero); | 67 DCHECK_GE(item.type, kItemTypeEnumZero); |
67 NSInteger section = [self sectionForSectionIdentifier:sectionIdentifier]; | 68 NSInteger section = [self sectionForSectionIdentifier:sectionIdentifier]; |
68 SectionItems* items = [_sections objectAtIndex:section]; | 69 SectionItems* items = [_sections objectAtIndex:section]; |
69 [items addObject:item]; | 70 [items addObject:item]; |
70 } | 71 } |
(...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
321 } | 322 } |
322 if (sectionItem.type == item.type) { | 323 if (sectionItem.type == item.type) { |
323 indexInItemType++; | 324 indexInItemType++; |
324 } | 325 } |
325 } | 326 } |
326 DCHECK(found); | 327 DCHECK(found); |
327 return indexInItemType; | 328 return indexInItemType; |
328 } | 329 } |
329 | 330 |
330 @end | 331 @end |
OLD | NEW |