| OLD | NEW | 
|     1 // Copyright 2014 The Chromium Authors. All rights reserved. |     1 // Copyright 2014 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/bookmarks/bookmark_home_handset_view_controller.h
      " |     5 #import "ios/chrome/browser/ui/bookmarks/bookmark_home_handset_view_controller.h
      " | 
|     6  |     6  | 
|     7 #include <memory> |     7 #include <memory> | 
|     8  |     8  | 
|     9 #include "base/logging.h" |     9 #include "base/logging.h" | 
|    10 #include "base/metrics/user_metrics_action.h" |    10 #include "base/metrics/user_metrics_action.h" | 
|    11 #include "base/strings/sys_string_conversions.h" |    11 #include "base/strings/sys_string_conversions.h" | 
|    12 #include "components/bookmarks/browser/bookmark_model.h" |    12 #include "components/bookmarks/browser/bookmark_model.h" | 
|    13 #include "components/strings/grit/components_strings.h" |    13 #include "components/strings/grit/components_strings.h" | 
|    14 #include "google_apis/gaia/google_service_auth_error.h" |    14 #include "google_apis/gaia/google_service_auth_error.h" | 
 |    15 #include "ios/chrome/browser/bookmarks/bookmark_model_factory.h" | 
|    15 #include "ios/chrome/browser/bookmarks/bookmarks_utils.h" |    16 #include "ios/chrome/browser/bookmarks/bookmarks_utils.h" | 
|    16 #include "ios/chrome/browser/browser_state/chrome_browser_state.h" |    17 #include "ios/chrome/browser/browser_state/chrome_browser_state.h" | 
|    17 #import "ios/chrome/browser/ui/alert_coordinator/action_sheet_coordinator.h" |    18 #import "ios/chrome/browser/ui/alert_coordinator/action_sheet_coordinator.h" | 
|    18 #import "ios/chrome/browser/ui/bookmarks/bars/bookmark_editing_bar.h" |    19 #import "ios/chrome/browser/ui/bookmarks/bars/bookmark_editing_bar.h" | 
|    19 #import "ios/chrome/browser/ui/bookmarks/bars/bookmark_navigation_bar.h" |    20 #import "ios/chrome/browser/ui/bookmarks/bars/bookmark_navigation_bar.h" | 
|    20 #import "ios/chrome/browser/ui/bookmarks/bookmark_collection_cells.h" |    21 #import "ios/chrome/browser/ui/bookmarks/bookmark_collection_cells.h" | 
|    21 #import "ios/chrome/browser/ui/bookmarks/bookmark_edit_view_controller.h" |    22 #import "ios/chrome/browser/ui/bookmarks/bookmark_edit_view_controller.h" | 
|    22 #import "ios/chrome/browser/ui/bookmarks/bookmark_folder_collection_view.h" |    23 #import "ios/chrome/browser/ui/bookmarks/bookmark_folder_collection_view.h" | 
|    23 #import "ios/chrome/browser/ui/bookmarks/bookmark_folder_editor_view_controller.
      h" |    24 #import "ios/chrome/browser/ui/bookmarks/bookmark_folder_editor_view_controller.
      h" | 
|    24 #import "ios/chrome/browser/ui/bookmarks/bookmark_folder_view_controller.h" |    25 #import "ios/chrome/browser/ui/bookmarks/bookmark_folder_view_controller.h" | 
| (...skipping 29 matching lines...) Expand all  Loading... | 
|    54     BookmarkFolderEditorViewControllerDelegate, |    55     BookmarkFolderEditorViewControllerDelegate, | 
|    55     BookmarkFolderViewControllerDelegate, |    56     BookmarkFolderViewControllerDelegate, | 
|    56     BookmarkMenuViewDelegate, |    57     BookmarkMenuViewDelegate, | 
|    57     BookmarkModelBridgeObserver, |    58     BookmarkModelBridgeObserver, | 
|    58     BookmarkPanelViewDelegate, |    59     BookmarkPanelViewDelegate, | 
|    59     BookmarkPromoControllerDelegate> { |    60     BookmarkPromoControllerDelegate> { | 
|    60   // Bridge to register for bookmark changes. |    61   // Bridge to register for bookmark changes. | 
|    61   std::unique_ptr<bookmarks::BookmarkModelBridge> _bridge; |    62   std::unique_ptr<bookmarks::BookmarkModelBridge> _bridge; | 
|    62 } |    63 } | 
|    63  |    64  | 
 |    65 // Redefined to be readwrite. | 
 |    66 @property(nonatomic, strong, readwrite) NSMutableArray* editIndexPaths; | 
 |    67  | 
 |    68 // Returns the parent, if all the bookmarks are siblings. | 
 |    69 // Otherwise returns the mobile_node. | 
 |    70 + (const BookmarkNode*) | 
 |    71 defaultMoveFolderFromBookmarks:(const std::set<const BookmarkNode*>&)bookmarks | 
 |    72                          model:(bookmarks::BookmarkModel*)model; | 
 |    73  | 
|    64 // This views holds the primary content of this view controller. At any point in |    74 // This views holds the primary content of this view controller. At any point in | 
|    65 // time, it contains exactly one of the BookmarkCollectionView subclasses. |    75 // time, it contains exactly one of the BookmarkCollectionView subclasses. | 
|    66 @property(nonatomic, strong) UIView* contentView; |    76 @property(nonatomic, strong) UIView* contentView; | 
|    67 // The possible views that can be shown from the menu. |    77 // The possible views that can be shown from the menu. | 
|    68 @property(nonatomic, strong) BookmarkFolderCollectionView* folderView; |    78 @property(nonatomic, strong) BookmarkFolderCollectionView* folderView; | 
|    69 // This view is created and used if the model is not fully loaded yet by the |    79 // This view is created and used if the model is not fully loaded yet by the | 
|    70 // time this controller starts. |    80 // time this controller starts. | 
|    71 @property(nonatomic, strong) BookmarkHomeWaitingView* waitForModelView; |    81 @property(nonatomic, strong) BookmarkHomeWaitingView* waitForModelView; | 
|    72  |    82  | 
|    73 // The menu with all the folders and special entries. |    83 // The menu with all the folders and special entries. | 
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|   226  |   236  | 
|   227 @synthesize actionSheetCoordinator = _actionSheetCoordinator; |   237 @synthesize actionSheetCoordinator = _actionSheetCoordinator; | 
|   228 @synthesize editViewController = _editViewController; |   238 @synthesize editViewController = _editViewController; | 
|   229 @synthesize folderSelector = _folderSelector; |   239 @synthesize folderSelector = _folderSelector; | 
|   230 @synthesize folderEditor = _folderEditor; |   240 @synthesize folderEditor = _folderEditor; | 
|   231  |   241  | 
|   232 @synthesize panelView = _panelView; |   242 @synthesize panelView = _panelView; | 
|   233 @synthesize scrollingMenuToTop = _scrollingMenuToTop; |   243 @synthesize scrollingMenuToTop = _scrollingMenuToTop; | 
|   234 @synthesize bookmarkPromoController = _bookmarkPromoController; |   244 @synthesize bookmarkPromoController = _bookmarkPromoController; | 
|   235  |   245  | 
 |   246 @synthesize delegate = _delegate; | 
 |   247 @synthesize editIndexPaths = _editIndexPaths; | 
 |   248 @synthesize editing = _editing; | 
 |   249 @synthesize bookmarks = _bookmarks; | 
 |   250 @synthesize loader = _loader; | 
 |   251 @synthesize browserState = _browserState; | 
 |   252  | 
|   236 - (instancetype)initWithLoader:(id<UrlLoader>)loader |   253 - (instancetype)initWithLoader:(id<UrlLoader>)loader | 
|   237                   browserState:(ios::ChromeBrowserState*)browserState { |   254                   browserState:(ios::ChromeBrowserState*)browserState { | 
|   238   self = [super initWithLoader:loader browserState:browserState]; |   255   DCHECK(browserState); | 
 |   256   self = [super initWithNibName:nil bundle:nil]; | 
|   239   if (self) { |   257   if (self) { | 
 |   258     _browserState = browserState->GetOriginalChromeBrowserState(); | 
 |   259     _loader = loader; | 
 |   260  | 
 |   261     _bookmarks = ios::BookmarkModelFactory::GetForBrowserState(_browserState); | 
 |   262     _editIndexPaths = [[NSMutableArray alloc] init]; | 
 |   263  | 
 |   264     [self resetEditNodes]; | 
 |   265  | 
|   240     _bridge.reset(new bookmarks::BookmarkModelBridge(self, self.bookmarks)); |   266     _bridge.reset(new bookmarks::BookmarkModelBridge(self, self.bookmarks)); | 
|   241     // It is important to initialize the promo controller with the browser state |   267     // It is important to initialize the promo controller with the browser state | 
|   242     // passed in, as it could be incognito. |   268     // passed in, as it could be incognito. | 
|   243     _bookmarkPromoController = |   269     _bookmarkPromoController = | 
|   244         [[BookmarkPromoController alloc] initWithBrowserState:browserState |   270         [[BookmarkPromoController alloc] initWithBrowserState:browserState | 
|   245                                                      delegate:self]; |   271                                                      delegate:self]; | 
|   246   } |   272   } | 
|   247   return self; |   273   return self; | 
|   248 } |   274 } | 
|   249  |   275  | 
|   250 - (void)dealloc { |   276 - (void)dealloc { | 
|   251   _folderView.delegate = nil; |   277   _folderView.delegate = nil; | 
|   252  |   278  | 
|   253   _menuView.delegate = nil; |   279   _menuView.delegate = nil; | 
|   254  |   280  | 
|   255   _editViewController.delegate = nil; |   281   _editViewController.delegate = nil; | 
|   256   _folderSelector.delegate = nil; |   282   _folderSelector.delegate = nil; | 
|   257  |   283  | 
|   258   _panelView.delegate = nil; |   284   _panelView.delegate = nil; | 
|   259 } |   285 } | 
|   260  |   286  | 
 |   287 - (void)loadView { | 
 |   288   CGRect frame = [[UIScreen mainScreen] bounds]; | 
 |   289   self.view = [[UIView alloc] initWithFrame:frame]; | 
 |   290 } | 
 |   291  | 
 |   292 - (void)resetEditNodes { | 
 |   293   _editNodes = std::set<const BookmarkNode*>(); | 
 |   294   _editNodesOrdered = std::vector<const BookmarkNode*>(); | 
 |   295   [self.editIndexPaths removeAllObjects]; | 
 |   296 } | 
 |   297  | 
 |   298 - (void)insertEditNode:(const BookmarkNode*)node | 
 |   299            atIndexPath:(NSIndexPath*)indexPath { | 
 |   300   if (_editNodes.find(node) != _editNodes.end()) | 
 |   301     return; | 
 |   302   _editNodes.insert(node); | 
 |   303   _editNodesOrdered.push_back(node); | 
 |   304   if (indexPath) { | 
 |   305     [self.editIndexPaths addObject:indexPath]; | 
 |   306   } else { | 
 |   307     // We insert null if we don't have the cell to keep the index valid. | 
 |   308     [self.editIndexPaths addObject:[NSNull null]]; | 
 |   309   } | 
 |   310 } | 
 |   311  | 
 |   312 - (void)removeEditNode:(const BookmarkNode*)node | 
 |   313            atIndexPath:(NSIndexPath*)indexPath { | 
 |   314   if (_editNodes.find(node) == _editNodes.end()) | 
 |   315     return; | 
 |   316  | 
 |   317   _editNodes.erase(node); | 
 |   318   std::vector<const BookmarkNode*>::iterator it = | 
 |   319       std::find(_editNodesOrdered.begin(), _editNodesOrdered.end(), node); | 
 |   320   DCHECK(it != _editNodesOrdered.end()); | 
 |   321   _editNodesOrdered.erase(it); | 
 |   322   if (indexPath) { | 
 |   323     [self.editIndexPaths removeObject:indexPath]; | 
 |   324   } else { | 
 |   325     // If we don't have the cell, we remove it by using its index. | 
 |   326     const NSUInteger index = std::distance(_editNodesOrdered.begin(), it); | 
 |   327     if (index < self.editIndexPaths.count) { | 
 |   328       [self.editIndexPaths removeObjectAtIndex:index]; | 
 |   329     } | 
 |   330   } | 
 |   331 } | 
 |   332  | 
|   261 - (void)removeEditNode:(const BookmarkNode*)node |   333 - (void)removeEditNode:(const BookmarkNode*)node | 
|   262                   cell:(UICollectionViewCell*)cell { |   334                   cell:(UICollectionViewCell*)cell { | 
|   263   [super removeEditNode:node atIndexPath:[self indexPathForCell:cell]]; |   335   [self removeEditNode:node atIndexPath:[self indexPathForCell:cell]]; | 
 |   336  | 
|   264   if (_editNodes.size() == 0) |   337   if (_editNodes.size() == 0) | 
|   265     [self setEditing:NO animated:YES]; |   338     [self setEditing:NO animated:YES]; | 
|   266   else |   339   else | 
|   267     [self updateEditingStateAnimated:YES]; |   340     [self updateEditingStateAnimated:YES]; | 
|   268 } |   341 } | 
|   269  |   342  | 
|   270 #pragma mark - UIViewController methods |   343 #pragma mark - UIViewController methods | 
|   271  |   344  | 
|   272 - (void)viewDidLoad { |   345 - (void)viewDidLoad { | 
|   273   [super viewDidLoad]; |   346   [super viewDidLoad]; | 
| (...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|   451   } |   524   } | 
|   452   [self.editingBar updateUIWithBookmarkCount:bookmarkCount |   525   [self.editingBar updateUIWithBookmarkCount:bookmarkCount | 
|   453                                  folderCount:folderCount]; |   526                                  folderCount:folderCount]; | 
|   454  |   527  | 
|   455   [self showEditingBarAnimated:animated]; |   528   [self showEditingBarAnimated:animated]; | 
|   456   [self updateEditBarShadow]; |   529   [self updateEditBarShadow]; | 
|   457   [self.panelView enableSideSwiping:NO]; |   530   [self.panelView enableSideSwiping:NO]; | 
|   458 } |   531 } | 
|   459  |   532  | 
|   460 - (void)setEditing:(BOOL)editing animated:(BOOL)animated { |   533 - (void)setEditing:(BOOL)editing animated:(BOOL)animated { | 
|   461   [super setEditing:editing animated:animated]; |   534   if (_editing == editing) | 
 |   535     return; | 
 |   536  | 
 |   537   _editing = editing; | 
 |   538  | 
 |   539   // Only reset the editing state when leaving edit mode. This allows subclasses | 
 |   540   // to add nodes for editing before entering edit mode. | 
 |   541   if (!editing) | 
 |   542     [self resetEditNodes]; | 
|   462  |   543  | 
|   463   [self updateEditingStateAnimated:animated]; |   544   [self updateEditingStateAnimated:animated]; | 
|   464   if ([[self primaryMenuItem] supportsEditing]) |   545   if ([[self primaryMenuItem] supportsEditing]) | 
|   465     [[self primaryView] setEditing:editing animated:animated]; |   546     [[self primaryView] setEditing:editing animated:animated]; | 
|   466  |   547  | 
|   467   if (editing) |   548   if (editing) | 
|   468     self.bookmarkPromoController.promoState = NO; |   549     self.bookmarkPromoController.promoState = NO; | 
|   469   else |   550   else | 
|   470     [self.bookmarkPromoController updatePromoState]; |   551     [self.bookmarkPromoController updatePromoState]; | 
|   471 } |   552 } | 
| (...skipping 482 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|   954   [self.menuView setScrollsToTop:NO]; |  1035   [self.menuView setScrollsToTop:NO]; | 
|   955   [[self primaryView] setScrollsToTop:YES]; |  1036   [[self primaryView] setScrollsToTop:YES]; | 
|   956   self.scrollingMenuToTop = NO; |  1037   self.scrollingMenuToTop = NO; | 
|   957   [self.panelView hideMenuAnimated:animated]; |  1038   [self.panelView hideMenuAnimated:animated]; | 
|   958   if (update) { |  1039   if (update) { | 
|   959     UIInterfaceOrientation orient = GetInterfaceOrientation(); |  1040     UIInterfaceOrientation orient = GetInterfaceOrientation(); | 
|   960     [self updateNavigationBarAnimated:animated orientation:orient]; |  1041     [self updateNavigationBarAnimated:animated orientation:orient]; | 
|   961   } |  1042   } | 
|   962 } |  1043 } | 
|   963  |  1044  | 
|   964 #pragma mark - BookmarkHomeViewController |  | 
|   965  |  | 
|   966 - (void)dismissModals:(BOOL)animated { |  1045 - (void)dismissModals:(BOOL)animated { | 
|   967   [self.actionSheetCoordinator stop]; |  1046   [self.actionSheetCoordinator stop]; | 
|   968   self.actionSheetCoordinator = nil; |  1047   self.actionSheetCoordinator = nil; | 
|   969 } |  1048 } | 
|   970  |  1049  | 
|   971 #pragma mark - BookmarkPanelViewDelegate |  1050 #pragma mark - BookmarkPanelViewDelegate | 
|   972  |  1051  | 
|   973 - (void)bookmarkPanelView:(BookmarkPanelView*)view |  1052 - (void)bookmarkPanelView:(BookmarkPanelView*)view | 
|   974              willShowMenu:(BOOL)showMenu |  1053              willShowMenu:(BOOL)showMenu | 
|   975     withAnimationDuration:(CGFloat)duration { |  1054     withAnimationDuration:(CGFloat)duration { | 
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  1095 } |  1174 } | 
|  1096  |  1175  | 
|  1097 #pragma mark - Internal non-UI Methods |  1176 #pragma mark - Internal non-UI Methods | 
|  1098  |  1177  | 
|  1099 - (void)delegateDismiss:(const GURL&)url { |  1178 - (void)delegateDismiss:(const GURL&)url { | 
|  1100   if ([self primaryView]) { |  1179   if ([self primaryView]) { | 
|  1101     bookmark_utils_ios::CachePosition( |  1180     bookmark_utils_ios::CachePosition( | 
|  1102         [[self primaryView] contentPositionInPortraitOrientation], |  1181         [[self primaryView] contentPositionInPortraitOrientation], | 
|  1103         [self primaryMenuItem]); |  1182         [self primaryMenuItem]); | 
|  1104   } |  1183   } | 
|  1105   [self.delegate bookmarkHomeViewControllerWantsDismissal:self |  1184   [self.delegate bookmarkHomeHandsetViewControllerWantsDismissal:self | 
|  1106                                           navigationToUrl:url]; |  1185                                                  navigationToUrl:url]; | 
|  1107 } |  1186 } | 
|  1108  |  1187  | 
|  1109 #pragma mark - BookmarkPromoControllerDelegate |  1188 #pragma mark - BookmarkPromoControllerDelegate | 
|  1110  |  1189  | 
|  1111 - (void)promoStateChanged:(BOOL)promoEnabled { |  1190 - (void)promoStateChanged:(BOOL)promoEnabled { | 
|  1112   [self.folderView |  1191   [self.folderView | 
|  1113       promoStateChangedAnimated:self.folderView == [self primaryView]]; |  1192       promoStateChangedAnimated:self.folderView == [self primaryView]]; | 
|  1114 } |  1193 } | 
|  1115  |  1194  | 
|  1116 - (NSIndexPath*)indexPathForCell:(UICollectionViewCell*)cell { |  1195 - (NSIndexPath*)indexPathForCell:(UICollectionViewCell*)cell { | 
|  1117   DCHECK([self primaryView].collectionView); |  1196   DCHECK([self primaryView].collectionView); | 
|  1118   NSIndexPath* indexPath = |  1197   NSIndexPath* indexPath = | 
|  1119       [[self primaryView].collectionView indexPathForCell:cell]; |  1198       [[self primaryView].collectionView indexPathForCell:cell]; | 
|  1120   return indexPath; |  1199   return indexPath; | 
|  1121 } |  1200 } | 
|  1122  |  1201  | 
 |  1202 + (const BookmarkNode*) | 
 |  1203 defaultMoveFolderFromBookmarks:(const std::set<const BookmarkNode*>&)bookmarks | 
 |  1204                          model:(bookmarks::BookmarkModel*)model { | 
 |  1205   if (bookmarks.size() == 0) | 
 |  1206     return model->mobile_node(); | 
 |  1207   const BookmarkNode* firstParent = (*(bookmarks.begin()))->parent(); | 
 |  1208   for (const BookmarkNode* node : bookmarks) { | 
 |  1209     if (node->parent() != firstParent) | 
 |  1210       return model->mobile_node(); | 
 |  1211   } | 
 |  1212  | 
 |  1213   return firstParent; | 
 |  1214 } | 
 |  1215  | 
|  1123 @end |  1216 @end | 
 |  1217  | 
 |  1218 @implementation BookmarkHomeHandsetViewController (ExposedForTesting) | 
 |  1219  | 
 |  1220 - (void)ensureAllViewExists { | 
 |  1221   // Do nothing. | 
 |  1222 } | 
 |  1223  | 
 |  1224 - (const std::set<const BookmarkNode*>&)editNodes { | 
 |  1225   return _editNodes; | 
 |  1226 } | 
 |  1227  | 
 |  1228 - (void)setEditNodes:(const std::set<const BookmarkNode*>&)editNodes { | 
 |  1229   _editNodes = editNodes; | 
 |  1230 } | 
 |  1231  | 
 |  1232 @end | 
| OLD | NEW |