OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef IOS_CHROME_BROWSER_UI_COLLECTION_VIEW_COLLECTION_VIEW_MODEL_H_ |
| 6 #define IOS_CHROME_BROWSER_UI_COLLECTION_VIEW_COLLECTION_VIEW_MODEL_H_ |
| 7 |
| 8 #include <UIKit/UIKit.h> |
| 9 |
| 10 @class CollectionViewItem; |
| 11 |
| 12 // Use these as the starting value for section identifier and item type enums. |
| 13 // These are provided to help not mix between indexPath's section/item and the |
| 14 // model section identifier / item type. |
| 15 // |
| 16 // For example: |
| 17 // typedef NS_ENUM(NSInteger, SectionIdentifier) { |
| 18 // SectionIdentifierFoo = kSectionIdentifierEnumZero, |
| 19 // SectionIdentifierBar, |
| 20 // }; |
| 21 // |
| 22 // typedef NS_ENUM(NSInteger, ItemType) { |
| 23 // ItemTypeBaz = kItemTypeEnumZero, |
| 24 // ItemTypeQux, |
| 25 // }; |
| 26 // |
| 27 // These values are chosen to try to prevent overlapping. |
| 28 const NSInteger kSectionIdentifierEnumZero = 10; |
| 29 const NSInteger kItemTypeEnumZero = 100; |
| 30 |
| 31 // CollectionViewModel acts as a model class for collection view controllers. |
| 32 // It provides methods to map from collection view index paths (aka collection |
| 33 // view section and collection view item) to model coordinates. These are |
| 34 // useful when the contents of a collection view are dynamic (for example, based |
| 35 // on experimental flags), so that there is not a static mapping between index |
| 36 // paths and section items. |
| 37 // Model coordinates have 3 dimensions: section identifier, item type and index |
| 38 // in item type. |
| 39 // Disclaimer: CollectionViewModel doesn't support a batch update logic. All |
| 40 // changes are immediately processed (contrary to the reload/delete/add order of |
| 41 // UICollectionView's |performBatchUpdates:completion:|). |
| 42 |
| 43 @interface CollectionViewModel : NSObject |
| 44 |
| 45 #pragma mark Modification methods |
| 46 |
| 47 // Adds a new section tagged with the given identifier. This method must not be |
| 48 // called multiple times with the same identifier. |
| 49 - (void)addSectionWithIdentifier:(NSInteger)sectionIdentifier; |
| 50 |
| 51 // Inserts a new section tagged with the given identifier at the given index. |
| 52 // This method must not be called multiple times with the same identifier. |
| 53 - (void)insertSectionWithIdentifier:(NSInteger)sectionIdentifier |
| 54 atIndex:(NSUInteger)index; |
| 55 |
| 56 // Adds an item to the section with the given identifier. Adding several times |
| 57 // the same item is undefined behavior. |
| 58 - (void)addItem:(CollectionViewItem*)item |
| 59 toSectionWithIdentifier:(NSInteger)sectionIdentifier; |
| 60 |
| 61 // Inserts an item to the section with the given identifier at the given index. |
| 62 // |index| must not be greater than the count of elements in the section. |
| 63 - (void)insertItem:(CollectionViewItem*)item |
| 64 inSectionWithIdentifier:(NSInteger)sectionIdentifier |
| 65 atIndex:(NSUInteger)index; |
| 66 |
| 67 // Removes the item for |itemType| from the section for |sectionIdentifier|. |
| 68 // If there are multiple entries with the same item type, this will remove the |
| 69 // first occurrence, but to selectively delete an item for a given index, use |
| 70 // -removeItemWithType:fromSectionWithIdentifier:atIndex:. |
| 71 - (void)removeItemWithType:(NSInteger)itemType |
| 72 fromSectionWithIdentifier:(NSInteger)sectionIdentifier; |
| 73 |
| 74 // Removes the item for |itemType| from the section for |sectionIdentifier| at |
| 75 // |index|. |
| 76 - (void)removeItemWithType:(NSInteger)itemType |
| 77 fromSectionWithIdentifier:(NSInteger)sectionIdentifier |
| 78 atIndex:(NSUInteger)index; |
| 79 |
| 80 // Removes the section for |sectionIdentifier|. If there are still items left in |
| 81 // the section, they are removed. |
| 82 - (void)removeSectionWithIdentifier:(NSInteger)sectionIdentifier; |
| 83 |
| 84 // Sets the header item for the section with the given |sectionIdentifier|. |
| 85 - (void)setHeader:(CollectionViewItem*)header |
| 86 forSectionWithIdentifier:(NSInteger)sectionIdentifier; |
| 87 |
| 88 // Sets the footer item for the section with the given |sectionIdentifier|. |
| 89 - (void)setFooter:(CollectionViewItem*)footer |
| 90 forSectionWithIdentifier:(NSInteger)sectionIdentifier; |
| 91 |
| 92 #pragma mark Query model coordinates from index paths |
| 93 |
| 94 // Returns the section identifier for the given section. |
| 95 - (NSInteger)sectionIdentifierForSection:(NSInteger)section; |
| 96 |
| 97 // Returns the item type for the given index path. |
| 98 - (NSInteger)itemTypeForIndexPath:(NSIndexPath*)indexPath; |
| 99 |
| 100 // Returns the index in item type for the given index path. |
| 101 // It corresponds to the index of the item at index path, among all the items if |
| 102 // the same type in the given section. |
| 103 // For example, let [A, B, B, B] a section, where A and B are item identifiers. |
| 104 // -indexInItemTypeForIndexPath:{0, 0} would return 0. |
| 105 // -indexInItemTypeForIndexPath:{0, 1} would return 0. |
| 106 // -indexInItemTypeForIndexPath:{0, 3} would return 2. |
| 107 - (NSUInteger)indexInItemTypeForIndexPath:(NSIndexPath*)indexPath; |
| 108 |
| 109 #pragma mark Query items from index paths |
| 110 |
| 111 // Returns whether there is an item at the given index path. |
| 112 - (BOOL)hasItemAtIndexPath:(NSIndexPath*)indexPath; |
| 113 |
| 114 // Returns the item at the given index path. |
| 115 - (CollectionViewItem*)itemAtIndexPath:(NSIndexPath*)indexPath; |
| 116 |
| 117 // Returns the header for the given |section|. |
| 118 - (CollectionViewItem*)headerForSection:(NSInteger)section; |
| 119 |
| 120 // Returns the footer for the given |section|. |
| 121 - (CollectionViewItem*)footerForSection:(NSInteger)section; |
| 122 |
| 123 // Returns an array of items in the section with the given identifier. |
| 124 - (NSArray*)itemsInSectionWithIdentifier:(NSInteger)sectionIdentifier; |
| 125 |
| 126 // Returns the header for the section with the given |sectionIdentifier|. |
| 127 - (CollectionViewItem*)headerForSectionWithIdentifier: |
| 128 (NSInteger)sectionIdentifier; |
| 129 |
| 130 // Returns the footer for the section with the given |sectionIdentifier|. |
| 131 - (CollectionViewItem*)footerForSectionWithIdentifier: |
| 132 (NSInteger)sectionIdentifier; |
| 133 |
| 134 #pragma mark Query index paths from model coordinates |
| 135 |
| 136 // Returns whether there is a section at the given section identifier. |
| 137 - (BOOL)hasSectionForSectionIdentifier:(NSInteger)sectionIdentifier; |
| 138 |
| 139 // Returns the index path's section for the given section identifier. |
| 140 - (NSInteger)sectionForSectionIdentifier:(NSInteger)sectionIdentifier; |
| 141 |
| 142 // Returns whether there is an item of type |itemType| at the given |
| 143 // |sectionIdentifier|. |
| 144 - (BOOL)hasItemForItemType:(NSInteger)itemType |
| 145 sectionIdentifier:(NSInteger)sectionIdentifier; |
| 146 |
| 147 // Returns the index path for |itemType| in the section for |sectionIdentifier|. |
| 148 // If there are multiple entries with the same item type, use |
| 149 // -indexPathForItemType:sectionIdentifier:atIndex:. |
| 150 - (NSIndexPath*)indexPathForItemType:(NSInteger)itemType |
| 151 sectionIdentifier:(NSInteger)sectionIdentifier; |
| 152 |
| 153 // Returns whether there is an item of type |itemType| at the given |
| 154 // |sectionIdentifier| and |index|. |
| 155 - (BOOL)hasItemForItemType:(NSInteger)itemType |
| 156 sectionIdentifier:(NSInteger)sectionIdentifier |
| 157 atIndex:(NSUInteger)index; |
| 158 |
| 159 // Returns the index path for |itemType| in the section for |sectionIdentifier| |
| 160 // at |index|. |
| 161 - (NSIndexPath*)indexPathForItemType:(NSInteger)itemType |
| 162 sectionIdentifier:(NSInteger)sectionIdentifier |
| 163 atIndex:(NSUInteger)index; |
| 164 |
| 165 #pragma mark Query index paths from items |
| 166 |
| 167 // Returns whether |item| exists in section for |sectionIdentifier|. |
| 168 - (BOOL)hasItem:(CollectionViewItem*)item |
| 169 inSectionWithIdentifier:(NSInteger)sectionIdentifier; |
| 170 |
| 171 // Returns the index path corresponding to the given |item|. |
| 172 - (NSIndexPath*)indexPathForItem:(CollectionViewItem*)itemType |
| 173 inSectionWithIdentifier:(NSInteger)sectionIdentifier; |
| 174 |
| 175 #pragma mark UICollectionView data sourcing |
| 176 |
| 177 // Returns the number of collection view sections. |
| 178 - (NSInteger)numberOfSections; |
| 179 |
| 180 // Returns the number of collection view items in the given section. |
| 181 - (NSInteger)numberOfItemsInSection:(NSInteger)section; |
| 182 |
| 183 @end |
| 184 |
| 185 #endif // IOS_CHROME_BROWSER_UI_COLLECTION_VIEW_COLLECTION_VIEW_MODEL_H_ |
OLD | NEW |