Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(252)

Side by Side Diff: ios/chrome/browser/ui/content_suggestions/content_suggestions_collection_updater.mm

Issue 2739153002: Reload all ContentSuggestions on notification (Closed)
Patch Set: Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/content_suggestions/content_suggestions_collectio n_updater.h" 5 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_collectio n_updater.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/mac/foundation_util.h" 8 #include "base/mac/foundation_util.h"
9 #include "base/time/time.h" 9 #include "base/time/time.h"
10 #import "ios/chrome/browser/ui/collection_view/collection_view_controller.h" 10 #import "ios/chrome/browser/ui/collection_view/collection_view_controller.h"
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
80 80
81 @interface ContentSuggestionsCollectionUpdater ()< 81 @interface ContentSuggestionsCollectionUpdater ()<
82 ContentSuggestionsArticleItemDelegate, 82 ContentSuggestionsArticleItemDelegate,
83 ContentSuggestionsDataSink> 83 ContentSuggestionsDataSink>
84 84
85 @property(nonatomic, weak) id<ContentSuggestionsDataSource> dataSource; 85 @property(nonatomic, weak) id<ContentSuggestionsDataSource> dataSource;
86 @property(nonatomic, strong) 86 @property(nonatomic, strong)
87 NSMutableDictionary<NSNumber*, ContentSuggestionsSectionInformation*>* 87 NSMutableDictionary<NSNumber*, ContentSuggestionsSectionInformation*>*
88 sectionInfoBySectionIdentifier; 88 sectionInfoBySectionIdentifier;
89 89
90 // Reloads all the data from the data source, deleting all the current items.
91 - (void)reloadData;
92 // Adds a new section if needed and returns the section identifier. 90 // Adds a new section if needed and returns the section identifier.
93 - (NSInteger)addSectionIfNeeded: 91 - (NSInteger)addSectionIfNeeded:
94 (ContentSuggestionsSectionInformation*)sectionInformation; 92 (ContentSuggestionsSectionInformation*)sectionInformation;
95 // Resets the models, removing the current CollectionViewItem and the 93 // Resets the models, removing the current CollectionViewItem and the
96 // SectionInfo. 94 // SectionInfo.
97 - (void)resetModels; 95 - (void)resetModels;
98 96
99 @end 97 @end
100 98
101 @implementation ContentSuggestionsCollectionUpdater 99 @implementation ContentSuggestionsCollectionUpdater
(...skipping 11 matching lines...) Expand all
113 } 111 }
114 return self; 112 return self;
115 } 113 }
116 114
117 #pragma mark - Properties 115 #pragma mark - Properties
118 116
119 - (void)setCollectionViewController: 117 - (void)setCollectionViewController:
120 (ContentSuggestionsViewController*)collectionViewController { 118 (ContentSuggestionsViewController*)collectionViewController {
121 _collectionViewController = collectionViewController; 119 _collectionViewController = collectionViewController;
122 120
123 [self reloadData]; 121 [self reloadAllData];
124 } 122 }
125 123
126 #pragma mark - ContentSuggestionsDataSink 124 #pragma mark - ContentSuggestionsDataSink
127 125
128 - (void)dataAvailable { 126 - (void)dataAvailable {
129 [self reloadData]; 127 [self reloadAllData];
130 } 128 }
131 129
132 - (void)clearSuggestion:(ContentSuggestionIdentifier*)suggestionIdentifier { 130 - (void)clearSuggestion:(ContentSuggestionIdentifier*)suggestionIdentifier {
133 SectionIdentifier sectionIdentifier = 131 SectionIdentifier sectionIdentifier =
134 SectionIdentifierForInfo(suggestionIdentifier.sectionInfo); 132 SectionIdentifierForInfo(suggestionIdentifier.sectionInfo);
135 if (![self.collectionViewController.collectionViewModel 133 if (![self.collectionViewController.collectionViewModel
136 hasSectionForSectionIdentifier:sectionIdentifier]) { 134 hasSectionForSectionIdentifier:sectionIdentifier]) {
137 return; 135 return;
138 } 136 }
139 137
(...skipping 12 matching lines...) Expand all
152 150
153 if (!correspondingItem) 151 if (!correspondingItem)
154 return; 152 return;
155 153
156 NSIndexPath* indexPath = [self.collectionViewController.collectionViewModel 154 NSIndexPath* indexPath = [self.collectionViewController.collectionViewModel
157 indexPathForItem:correspondingItem 155 indexPathForItem:correspondingItem
158 inSectionWithIdentifier:sectionIdentifier]; 156 inSectionWithIdentifier:sectionIdentifier];
159 [self.collectionViewController dismissEntryAtIndexPath:indexPath]; 157 [self.collectionViewController dismissEntryAtIndexPath:indexPath];
160 } 158 }
161 159
160 - (void)reloadAllData {
stkhapugin 2017/03/09 14:12:05 This was just renamed and moved up, right?
gambard 2017/03/09 14:12:52 Yes!
161 [self resetModels];
162 CollectionViewModel* model =
163 self.collectionViewController.collectionViewModel;
164
165 NSArray<ContentSuggestion*>* suggestions = [self.dataSource allSuggestions];
166
167 for (ContentSuggestion* suggestion in suggestions) {
168 NSInteger sectionIdentifier =
169 [self addSectionIfNeeded:suggestion.suggestionIdentifier.sectionInfo];
170 ContentSuggestionsArticleItem* articleItem =
171 [[ContentSuggestionsArticleItem alloc]
172 initWithType:ItemTypeForContentSuggestionType(suggestion.type)
173 title:suggestion.title
174 subtitle:suggestion.text
175 delegate:self
176 url:suggestion.url];
177
178 articleItem.publisher = suggestion.publisher;
179 articleItem.publishDate = suggestion.publishDate;
180
181 articleItem.suggestionIdentifier = suggestion.suggestionIdentifier;
182
183 [model addItem:articleItem toSectionWithIdentifier:sectionIdentifier];
184 }
185
186 if ([self.collectionViewController isViewLoaded]) {
187 [self.collectionViewController.collectionView reloadData];
188 }
189 }
190
162 #pragma mark - Public methods 191 #pragma mark - Public methods
163 192
164 - (BOOL)shouldUseCustomStyleForSection:(NSInteger)section { 193 - (BOOL)shouldUseCustomStyleForSection:(NSInteger)section {
165 NSNumber* identifier = @([self.collectionViewController.collectionViewModel 194 NSNumber* identifier = @([self.collectionViewController.collectionViewModel
166 sectionIdentifierForSection:section]); 195 sectionIdentifierForSection:section]);
167 ContentSuggestionsSectionInformation* sectionInformation = 196 ContentSuggestionsSectionInformation* sectionInformation =
168 self.sectionInfoBySectionIdentifier[identifier]; 197 self.sectionInfoBySectionIdentifier[identifier];
169 return sectionInformation.layout == ContentSuggestionsSectionLayoutCustom; 198 return sectionInformation.layout == ContentSuggestionsSectionLayoutCustom;
170 } 199 }
171 200
(...skipping 27 matching lines...) Expand all
199 inSectionWithIdentifier:sectionIdentifier]; 228 inSectionWithIdentifier:sectionIdentifier];
200 }; 229 };
201 230
202 [self.dataSource.imageFetcher 231 [self.dataSource.imageFetcher
203 fetchImageForSuggestion:articleItem.suggestionIdentifier 232 fetchImageForSuggestion:articleItem.suggestionIdentifier
204 callback:imageFetchedCallback]; 233 callback:imageFetchedCallback];
205 } 234 }
206 235
207 #pragma mark - Private methods 236 #pragma mark - Private methods
208 237
209 - (void)reloadData {
210 [self resetModels];
211 CollectionViewModel* model =
212 self.collectionViewController.collectionViewModel;
213
214 NSArray<ContentSuggestion*>* suggestions = [self.dataSource allSuggestions];
215
216 for (ContentSuggestion* suggestion in suggestions) {
217 NSInteger sectionIdentifier =
218 [self addSectionIfNeeded:suggestion.suggestionIdentifier.sectionInfo];
219 ContentSuggestionsArticleItem* articleItem =
220 [[ContentSuggestionsArticleItem alloc]
221 initWithType:ItemTypeForContentSuggestionType(suggestion.type)
222 title:suggestion.title
223 subtitle:suggestion.text
224 delegate:self
225 url:suggestion.url];
226
227 articleItem.publisher = suggestion.publisher;
228 articleItem.publishDate = suggestion.publishDate;
229
230 articleItem.suggestionIdentifier = suggestion.suggestionIdentifier;
231
232 [model addItem:articleItem toSectionWithIdentifier:sectionIdentifier];
233 }
234
235 if ([self.collectionViewController isViewLoaded]) {
236 [self.collectionViewController.collectionView reloadData];
237 }
238 }
239
240 - (NSInteger)addSectionIfNeeded: 238 - (NSInteger)addSectionIfNeeded:
241 (ContentSuggestionsSectionInformation*)sectionInformation { 239 (ContentSuggestionsSectionInformation*)sectionInformation {
242 NSInteger sectionIdentifier = SectionIdentifierForInfo(sectionInformation); 240 NSInteger sectionIdentifier = SectionIdentifierForInfo(sectionInformation);
243 241
244 CollectionViewModel* model = 242 CollectionViewModel* model =
245 self.collectionViewController.collectionViewModel; 243 self.collectionViewController.collectionViewModel;
246 if (![model hasSectionForSectionIdentifier:sectionIdentifier]) { 244 if (![model hasSectionForSectionIdentifier:sectionIdentifier]) {
247 [model addSectionWithIdentifier:sectionIdentifier]; 245 [model addSectionWithIdentifier:sectionIdentifier];
248 self.sectionInfoBySectionIdentifier[@(sectionIdentifier)] = 246 self.sectionInfoBySectionIdentifier[@(sectionIdentifier)] =
249 sectionInformation; 247 sectionInformation;
250 [self.sectionInfoBySectionIdentifier setObject:sectionInformation 248 [self.sectionInfoBySectionIdentifier setObject:sectionInformation
251 forKey:@(sectionIdentifier)]; 249 forKey:@(sectionIdentifier)];
252 } 250 }
253 return sectionIdentifier; 251 return sectionIdentifier;
254 } 252 }
255 253
256 - (void)resetModels { 254 - (void)resetModels {
257 [self.collectionViewController loadModel]; 255 [self.collectionViewController loadModel];
258 self.sectionInfoBySectionIdentifier = [[NSMutableDictionary alloc] init]; 256 self.sectionInfoBySectionIdentifier = [[NSMutableDictionary alloc] init];
259 } 257 }
260 258
261 @end 259 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698