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

Side by Side Diff: ios/showcase/core/showcase_view_controller.mm

Issue 2592593003: Upstream ios/showcase source code. (Closed)
Patch Set: Created 4 years 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
« no previous file with comments | « ios/showcase/core/showcase_view_controller.h ('k') | ios/showcase/settings/BUILD.gn » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 #import "ios/showcase/core/showcase_view_controller.h"
6
7 #import "base/logging.h"
8 #import "ios/showcase/common/coordinator.h"
9
10 #if !defined(__has_feature) || !__has_feature(objc_arc)
11 #error "This file requires ARC support."
12 #endif
13
14 namespace showcase {
15
16 NSString* const kClassForDisplayKey = @"classForDisplay";
17 NSString* const kClassForInstantiationKey = @"classForInstantiation";
18 NSString* const kUseCaseKey = @"useCase";
19
20 } // namespace showcase
21
22 @interface ShowcaseViewController ()<UITableViewDataSource,
23 UITableViewDelegate,
24 UISearchResultsUpdating>
25
26 // Search controller that contains search bar.
27 @property(nonatomic, strong) UISearchController* searchController;
28
29 // Full data set displayed when tableView is not filtered.
30 @property(nonatomic, strong) NSArray<showcase::ModelRow*>* allRows;
31
32 // Displayed rows in tableView.
33 @property(nonatomic, strong) NSArray<showcase::ModelRow*>* displayedRows;
34
35 // Selected coordinator.
36 @property(nonatomic, strong) id<Coordinator> activeCoordinator;
37
38 @end
39
40 @implementation ShowcaseViewController
41 @synthesize searchController = _searchController;
42 @synthesize allRows = _allRows;
43 @synthesize displayedRows = _displayedRows;
44 @synthesize activeCoordinator = _activeCoordinator;
45
46 - (instancetype)initWithRows:(NSArray<showcase::ModelRow*>*)rows {
47 self = [super initWithStyle:UITableViewStylePlain];
48 if (self) {
49 self.allRows = [rows copy];
50 // Default to displaying all rows.
51 self.displayedRows = self.allRows;
52 }
53 return self;
54 }
55
56 #pragma mark - UIViewController
57
58 - (void)viewDidLoad {
59 [super viewDidLoad];
60
61 self.title = @"Showcase";
62 self.tableView.tableFooterView = [[UIView alloc] init];
63 self.tableView.rowHeight = 70.0;
64
65 self.searchController =
66 [[UISearchController alloc] initWithSearchResultsController:nil];
67 self.searchController.searchResultsUpdater = self;
68 self.searchController.dimsBackgroundDuringPresentation = NO;
69 self.tableView.tableHeaderView = self.searchController.searchBar;
70
71 // Presentation of searchController will walk up the view controller hierarchy
72 // until it finds the root view controller or one that defines a presentation
73 // context. Make this class the presentation context so that the search
74 // controller does not present on top of the navigation controller.
75 self.definesPresentationContext = YES;
76 }
77
78 - (void)viewDidAppear:(BOOL)animated {
79 [super viewDidAppear:animated];
80 // Resets the current coordinator whenever the navigation controller pops
81 // back to this view controller.
82 self.activeCoordinator = nil;
83 }
84
85 #pragma mark - Table view data source
86
87 - (NSInteger)tableView:(UITableView*)tableView
88 numberOfRowsInSection:(NSInteger)section {
89 return self.displayedRows.count;
90 }
91
92 - (UITableViewCell*)tableView:(UITableView*)tableView
93 cellForRowAtIndexPath:(NSIndexPath*)indexPath {
94 UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
95 if (!cell) {
96 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
97 reuseIdentifier:@"cell"];
98 cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
99 }
100 showcase::ModelRow* row = self.displayedRows[indexPath.row];
101 cell.textLabel.text = row[showcase::kClassForDisplayKey];
102 cell.detailTextLabel.text = row[showcase::kUseCaseKey];
103 return cell;
104 }
105
106 #pragma mark - Table view delegate
107
108 - (void)tableView:(UITableView*)tableView
109 didSelectRowAtIndexPath:(NSIndexPath*)indexPath {
110 showcase::ModelRow* row = self.displayedRows[indexPath.row];
111 Class classForInstantiation =
112 NSClassFromString(row[showcase::kClassForInstantiationKey]);
113 if ([classForInstantiation isSubclassOfClass:[UIViewController class]]) {
114 UIViewController* viewController = [[classForInstantiation alloc] init];
115 viewController.title = row[showcase::kUseCaseKey];
116 [self.navigationController pushViewController:viewController animated:YES];
117 } else if ([classForInstantiation
118 conformsToProtocol:@protocol(Coordinator)]) {
119 self.activeCoordinator = [[classForInstantiation alloc] init];
120 self.activeCoordinator.baseViewController = self.navigationController;
121 [self.activeCoordinator start];
122 } else {
123 NOTREACHED();
124 }
125 }
126
127 #pragma mark - UISearchResultsUpdating
128
129 - (void)updateSearchResultsForSearchController:
130 (UISearchController*)searchController {
131 [self filterContentForSearchText:searchController.searchBar.text];
132 }
133
134 #pragma mark - Private
135
136 // Filters |allRows| for |searchText| and reloads the tableView with
137 // animation. If |searchText| is empty, tableView will be loaded with |allRows|.
138 - (void)filterContentForSearchText:(NSString*)searchText {
139 NSArray<showcase::ModelRow*>* newFilteredRows = self.allRows;
140
141 if (![self.searchController.searchBar.text isEqualToString:@""]) {
142 // The search is case-insensitive and searches both displayed texts.
143 NSIndexSet* matchingRows =
144 [self.allRows indexesOfObjectsPassingTest:^BOOL(
145 showcase::ModelRow* row, NSUInteger idx, BOOL* stop) {
146 return [row[showcase::kClassForDisplayKey]
147 localizedCaseInsensitiveContainsString:searchText] ||
148 [row[showcase::kUseCaseKey]
149 localizedCaseInsensitiveContainsString:searchText];
150 }];
151 newFilteredRows = [self.allRows objectsAtIndexes:matchingRows];
152 }
153
154 NSArray<NSIndexPath*>* indexPathsToInsert =
155 [self indexPathsToInsertFromArray:self.displayedRows
156 toArray:newFilteredRows
157 indexPathSection:0];
158 NSArray<NSIndexPath*>* indexPathsToDelete =
159 [self indexPathsToDeleteFromArray:self.displayedRows
160 toArray:newFilteredRows
161 indexPathSection:0];
162
163 [self.tableView beginUpdates];
164 [self.tableView insertRowsAtIndexPaths:indexPathsToInsert
165 withRowAnimation:UITableViewRowAnimationFade];
166 [self.tableView deleteRowsAtIndexPaths:indexPathsToDelete
167 withRowAnimation:UITableViewRowAnimationFade];
168 self.displayedRows = newFilteredRows;
169 [self.tableView endUpdates];
170 }
171
172 // Returns indexPaths that need to be inserted into the tableView.
173 - (NSArray<NSIndexPath*>*)indexPathsToInsertFromArray:(NSArray*)fromArray
174 toArray:(NSArray*)toArray
175 indexPathSection:(NSUInteger)section {
176 NSMutableArray<NSIndexPath*>* indexPathsToInsert =
177 [[NSMutableArray alloc] init];
178 for (NSUInteger row = 0; row < toArray.count; row++) {
179 if (![fromArray containsObject:toArray[row]]) {
180 [indexPathsToInsert
181 addObject:[NSIndexPath indexPathForRow:row inSection:section]];
182 }
183 }
184 return indexPathsToInsert;
185 }
186
187 // Returns indexPaths that need to be deleted from the tableView.
188 - (NSArray<NSIndexPath*>*)indexPathsToDeleteFromArray:(NSArray*)fromArray
189 toArray:(NSArray*)toArray
190 indexPathSection:(NSUInteger)section {
191 NSMutableArray<NSIndexPath*>* indexPathsToDelete =
192 [[NSMutableArray alloc] init];
193 for (NSUInteger row = 0; row < fromArray.count; row++) {
194 if (![toArray containsObject:fromArray[row]]) {
195 [indexPathsToDelete
196 addObject:[NSIndexPath indexPathForRow:row inSection:section]];
197 }
198 }
199 return indexPathsToDelete;
200 }
201
202 @end
OLDNEW
« no previous file with comments | « ios/showcase/core/showcase_view_controller.h ('k') | ios/showcase/settings/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698