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

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

Issue 2691593002: Connect ContentSuggestionsMediator to the ContentService (Closed)
Patch Set: Address comments Created 3 years, 10 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 #import "ios/chrome/browser/ui/collection_view/collection_view_controller.h" 9 #import "ios/chrome/browser/ui/collection_view/collection_view_controller.h"
10 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h" 10 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h"
11 #import "ios/chrome/browser/ui/content_suggestions/content_suggestion.h"
11 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_article_i tem.h" 12 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_article_i tem.h"
12 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_data_sink .h" 13 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_data_sink .h"
13 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_data_sour ce.h" 14 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_data_sour ce.h"
14 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_expandabl e_item.h" 15 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_expandabl e_item.h"
15 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_favicon_i tem.h" 16 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_favicon_i tem.h"
17 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_image_upd ater.h"
16 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_item.h" 18 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_item.h"
19 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_section_i nformation.h"
17 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_stack_ite m.h" 20 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_stack_ite m.h"
18 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_view_cont roller.h" 21 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_view_cont roller.h"
19 #include "url/gurl.h" 22 #include "url/gurl.h"
20 23
21 #if !defined(__has_feature) || !__has_feature(objc_arc) 24 #if !defined(__has_feature) || !__has_feature(objc_arc)
22 #error "This file requires ARC support." 25 #error "This file requires ARC support."
23 #endif 26 #endif
24 27
25 @interface ContentSuggestionsCollectionUpdater ()<ContentSuggestionsDataSink> 28 namespace {
29
30 typedef NS_ENUM(NSInteger, SectionIdentifier) {
31 SectionIdentifierBookmarks = kSectionIdentifierEnumZero,
32 SectionIdentifierArticles,
33 SectionIdentifierDefault,
34 };
35
36 // Returns the section identifier corresponding to the section |info|.
37 SectionIdentifier SectionIdentifierForInfo(
38 ContentSuggestionsSectionInformation* info) {
39 switch (info.ID) {
40 case ContentSuggestionsSectionBookmarks:
41 return SectionIdentifierBookmarks;
42
43 case ContentSuggestionsSectionArticles:
44 return SectionIdentifierArticles;
45
46 case ContentSuggestionsSectionCount:
47 return SectionIdentifierDefault;
48 }
49 }
50
51 } // namespace
52
53 @interface ContentSuggestionsCollectionUpdater ()<
54 ContentSuggestionsDataSink,
55 ContentSuggestionsImageUpdaterDelegate>
26 56
27 @property(nonatomic, weak) id<ContentSuggestionsDataSource> dataSource; 57 @property(nonatomic, weak) id<ContentSuggestionsDataSource> dataSource;
58 @property(nonatomic, strong)
59 NSMutableDictionary* sectionInfoBySectionIdentifier;
60 @property(nonatomic, strong) NSMutableSet* imageUpdaterSet;
61
62 // Reloads all the data from the data source, deleting all the current items.
63 - (void)reloadData;
64 // Adds a new section if needed and returns the section identifier.
65 - (NSInteger)addSectionIfNeeded:
66 (ContentSuggestionsSectionInformation*)sectionInformation;
67 // Resets the models and cells.
68 - (void)resetModels;
28 69
29 @end 70 @end
30 71
31 @implementation ContentSuggestionsCollectionUpdater 72 @implementation ContentSuggestionsCollectionUpdater
32 73
33 @synthesize collectionViewController = _collectionViewController; 74 @synthesize collectionViewController = _collectionViewController;
34 @synthesize dataSource = _dataSource; 75 @synthesize dataSource = _dataSource;
76 @synthesize sectionInfoBySectionIdentifier = _sectionInfoBySectionIdentifier;
77 @synthesize imageUpdaterSet = _imageUpdaterSet;
35 78
36 - (instancetype)initWithDataSource: 79 - (instancetype)initWithDataSource:
37 (id<ContentSuggestionsDataSource>)dataSource { 80 (id<ContentSuggestionsDataSource>)dataSource {
38 self = [super init]; 81 self = [super init];
39 if (self) { 82 if (self) {
40 _dataSource = dataSource; 83 _dataSource = dataSource;
41 _dataSource.dataSink = self; 84 _dataSource.dataSink = self;
42 } 85 }
43 return self; 86 return self;
44 } 87 }
45 88
46 #pragma mark - Properties 89 #pragma mark - Properties
47 90
48 - (void)setCollectionViewController: 91 - (void)setCollectionViewController:
49 (ContentSuggestionsViewController*)collectionViewController { 92 (ContentSuggestionsViewController*)collectionViewController {
50 _collectionViewController = collectionViewController; 93 _collectionViewController = collectionViewController;
51 [collectionViewController loadModel];
52 CollectionViewModel* model = collectionViewController.collectionViewModel;
53 94
54 // TODO(crbug.com/686728): Load the data with the dataSource instead of hard 95 [self reloadData];
55 // coded value.
56
57 NSInteger sectionIdentifier = kSectionIdentifierEnumZero;
58
59 // Stack Item.
60 [model addSectionWithIdentifier:sectionIdentifier];
61 [model addItem:[[ContentSuggestionsStackItem alloc]
62 initWithType:ItemTypeStack
63 title:@"The title"
64 subtitle:@"The subtitle"]
65 toSectionWithIdentifier:sectionIdentifier++];
66
67 // Favicon Item.
68 [model addSectionWithIdentifier:sectionIdentifier];
69 ContentSuggestionsFaviconItem* faviconItem =
70 [[ContentSuggestionsFaviconItem alloc] initWithType:ItemTypeFavicon];
71 for (NSInteger i = 0; i < 6; i++) {
72 [faviconItem addFavicon:[UIImage imageNamed:@"bookmark_gray_star_large"]
73 withTitle:@"Super website! Incredible!"];
74 }
75 faviconItem.delegate = _collectionViewController;
76 [model addItem:faviconItem toSectionWithIdentifier:sectionIdentifier++];
77
78 for (NSInteger i = 0; i < 3; i++) {
79 [model addSectionWithIdentifier:sectionIdentifier];
80
81 // Standard Item.
82 [model addItem:[[ContentSuggestionsItem alloc] initWithType:ItemTypeText
83 title:@"The title"
84 subtitle:@"The subtitle"]
85 toSectionWithIdentifier:sectionIdentifier];
86
87 // Article Item.
88 [model addItem:[[ContentSuggestionsArticleItem alloc]
89 initWithType:ItemTypeArticle
90 title:@"Title of an Article"
91 subtitle:@"This is the subtitle of an article, can "
92 @"spawn on multiple lines"
93 image:[UIImage imageNamed:@"distillation_success"]
94 url:GURL()]
95 toSectionWithIdentifier:sectionIdentifier];
96
97 // Expandable Item.
98 SuggestionsExpandableItem* expandableItem =
99 [[SuggestionsExpandableItem alloc]
100 initWithType:ItemTypeExpand
101 title:@"Title of an Expandable Article"
102 subtitle:@"This Article can be expanded to display "
103 @"additional information or interaction "
104 @"options"
105 image:[UIImage imageNamed:@"distillation_fail"]
106 detailText:@"Details shown only when the article is "
107 @"expanded. It can be displayed on "
108 @"multiple lines."];
109 expandableItem.delegate = _collectionViewController;
110 [model addItem:expandableItem toSectionWithIdentifier:sectionIdentifier];
111 sectionIdentifier++;
112 }
113 } 96 }
114 97
115 #pragma mark - ContentSuggestionsDataSink 98 #pragma mark - ContentSuggestionsDataSink
116 99
117 - (void)dataAvailable { 100 - (void)dataAvailable {
118 // TODO(crbug.com/686728): Get the new data from the DataSource. 101 [self reloadData];
119 } 102 }
120 103
121 #pragma mark - Public methods 104 #pragma mark - Public methods
122 105
123 - (void)addTextItem:(NSString*)title 106 - (void)addTextItem:(NSString*)title
124 subtitle:(NSString*)subtitle 107 subtitle:(NSString*)subtitle
125 toSection:(NSInteger)inputSection { 108 toSection:(NSInteger)inputSection {
126 DCHECK(_collectionViewController); 109 DCHECK(self.collectionViewController);
127 ContentSuggestionsItem* item = 110 ContentSuggestionsItem* item = [[ContentSuggestionsItem alloc]
128 [[ContentSuggestionsItem alloc] initWithType:ItemTypeText 111 initWithType:ContentSuggestionsItemTypeText
129 title:title 112 title:title
130 subtitle:subtitle]; 113 subtitle:subtitle];
131 NSInteger sectionIdentifier = kSectionIdentifierEnumZero + inputSection; 114 NSInteger sectionIdentifier = kSectionIdentifierEnumZero + inputSection;
132 NSInteger sectionIndex = inputSection; 115 NSInteger sectionIndex = inputSection;
133 CollectionViewModel* model = _collectionViewController.collectionViewModel; 116 CollectionViewModel* model =
117 self.collectionViewController.collectionViewModel;
134 if ([model numberOfSections] <= inputSection) { 118 if ([model numberOfSections] <= inputSection) {
135 sectionIndex = [model numberOfSections]; 119 sectionIndex = [model numberOfSections];
136 sectionIdentifier = kSectionIdentifierEnumZero + sectionIndex; 120 sectionIdentifier = kSectionIdentifierEnumZero + sectionIndex;
137 [_collectionViewController.collectionView performBatchUpdates:^{ 121 [self.collectionViewController.collectionView performBatchUpdates:^{
138 [_collectionViewController.collectionViewModel 122 [self.collectionViewController.collectionViewModel
139 addSectionWithIdentifier:sectionIdentifier]; 123 addSectionWithIdentifier:sectionIdentifier];
140 [_collectionViewController.collectionView 124 [self.collectionViewController.collectionView
141 insertSections:[NSIndexSet indexSetWithIndex:sectionIndex]]; 125 insertSections:[NSIndexSet indexSetWithIndex:sectionIndex]];
142 } 126 }
143 completion:nil]; 127 completion:nil];
144 } 128 }
145 NSInteger numberOfItemsInSection = 129 NSInteger numberOfItemsInSection =
146 [model numberOfItemsInSection:sectionIndex]; 130 [model numberOfItemsInSection:sectionIndex];
147 [_collectionViewController.collectionViewModel addItem:item 131 [self.collectionViewController.collectionViewModel addItem:item
148 toSectionWithIdentifier:sectionIdentifier]; 132 toSectionWithIdentifier:sectionIdentifier];
149 [_collectionViewController.collectionView performBatchUpdates:^{ 133 [self.collectionViewController.collectionView performBatchUpdates:^{
150 [_collectionViewController.collectionView 134 [self.collectionViewController.collectionView
151 insertItemsAtIndexPaths:@[ [NSIndexPath 135 insertItemsAtIndexPaths:@[ [NSIndexPath
152 indexPathForRow:numberOfItemsInSection 136 indexPathForRow:numberOfItemsInSection
153 inSection:sectionIndex] ]]; 137 inSection:sectionIndex] ]];
154 } 138 }
155 completion:nil]; 139 completion:nil];
156 } 140 }
157 141
158 - (BOOL)shouldUseCustomStyleForSection:(NSInteger)section { 142 - (BOOL)shouldUseCustomStyleForSection:(NSInteger)section {
159 return section == 0 || section == 1; 143 NSInteger identifier = [self.collectionViewController.collectionViewModel
144 sectionIdentifierForSection:section];
145 ContentSuggestionsSectionInformation* sectionInformation =
146 [self.sectionInfoBySectionIdentifier objectForKey:@(identifier)];
147 return sectionInformation.layout == ContentSuggestionsSectionLayoutCustom;
148 }
149
150 #pragma mark - Private methods
151
152 - (void)reloadData {
153 [self resetModels];
154 CollectionViewModel* model =
155 self.collectionViewController.collectionViewModel;
156
157 NSArray<ContentSuggestion*>* suggestions = [self.dataSource allSuggestions];
158
159 for (ContentSuggestion* suggestion in suggestions) {
160 NSInteger sectionIdentifier = [self addSectionIfNeeded:suggestion.section];
161 ContentSuggestionsArticleItem* articleItem =
162 [[ContentSuggestionsArticleItem alloc]
163 initWithType:ContentSuggestionsItemTypeArticle
164 title:suggestion.title
165 subtitle:suggestion.text
166 image:suggestion.image
167 url:suggestion.url];
168
169 ContentSuggestionsImageUpdater* imageUpdater = suggestion.imageUpdater;
170 [self.imageUpdaterSet addObject:suggestion.imageUpdater];
171 imageUpdater.delegate = self;
172 imageUpdater.item = articleItem;
173 imageUpdater.sectionIdentifier = sectionIdentifier;
174
175 [model addItem:articleItem toSectionWithIdentifier:sectionIdentifier];
176 }
177
178 if ([self.collectionViewController isViewLoaded]) {
179 [self.collectionViewController.collectionView reloadData];
180 }
181 }
182
183 - (NSInteger)addSectionIfNeeded:
184 (ContentSuggestionsSectionInformation*)sectionInformation {
185 NSInteger sectionIdentifier = SectionIdentifierForInfo(sectionInformation);
186
187 CollectionViewModel* model =
188 self.collectionViewController.collectionViewModel;
189 if (![model hasSectionForSectionIdentifier:sectionIdentifier]) {
190 [model addSectionWithIdentifier:sectionIdentifier];
191 self.sectionInfoBySectionIdentifier[@(sectionIdentifier)] =
192 sectionInformation;
193 [self.sectionInfoBySectionIdentifier setObject:sectionInformation
194 forKey:@(sectionIdentifier)];
195 }
196 return sectionIdentifier;
197 }
198
199 - (void)resetModels {
200 [self.collectionViewController loadModel];
201 self.sectionInfoBySectionIdentifier = [[NSMutableDictionary alloc] init];
202 self.imageUpdaterSet = [[NSMutableSet alloc] init];
203 }
204
205 #pragma mark - ContentSuggestionsImageUpdaterDelegate
206
207 - (void)contentSuggestionsImageUpdater:
208 (ContentSuggestionsImageUpdater*)imageUpdater
209 receivedImage:(UIImage*)image {
210 if (!imageUpdater.item)
211 return;
212
213 imageUpdater.item.image = image;
214 [self.collectionViewController
215 reconfigureCellsForItems:@[ imageUpdater.item ]
216 inSectionWithIdentifier:imageUpdater.sectionIdentifier];
217 [self.imageUpdaterSet removeObject:imageUpdater];
160 } 218 }
161 219
162 @end 220 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698