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

Side by Side Diff: ios/chrome/browser/ui/bookmarks/bookmark_home_view_controller.mm

Issue 2929993002: Refactoring bookmark viewcontrollers (Closed)
Patch Set: Created 3 years, 6 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #import "ios/chrome/browser/ui/bookmarks/bookmark_home_view_controller.h"
6
7 #include "components/bookmarks/browser/base_bookmark_model_observer.h"
8 #include "components/bookmarks/browser/bookmark_model.h"
9 #include "ios/chrome/browser/bookmarks/bookmark_model_factory.h"
10 #include "ios/chrome/browser/browser_state/chrome_browser_state.h"
11 #import "ios/chrome/browser/ui/bookmarks/bookmark_collection_view.h"
12 #import "ios/chrome/browser/ui/url_loader.h"
13
14 #if !defined(__has_feature) || !__has_feature(objc_arc)
15 #error "This file requires ARC support."
16 #endif
17
18 using bookmarks::BookmarkNode;
19
20 @interface BookmarkHomeViewController ()
21
22 // Redefined to be readwrite.
23 @property(nonatomic, strong, readwrite) NSMutableArray* editIndexPaths;
24
25 // Returns the parent, if all the bookmarks are siblings.
26 // Otherwise returns the mobile_node.
27 + (const BookmarkNode*)
28 defaultMoveFolderFromBookmarks:(const std::set<const BookmarkNode*>&)bookmarks
29 model:(bookmarks::BookmarkModel*)model;
30 @end
31
32 @implementation BookmarkHomeViewController
33 @synthesize delegate = _delegate;
34 @synthesize editIndexPaths = _editIndexPaths;
35 @synthesize editing = _editing;
36 @synthesize bookmarks = _bookmarks;
37 @synthesize loader = _loader;
38 @synthesize browserState = _browserState;
39
40 - (instancetype)initWithLoader:(id<UrlLoader>)loader
41 browserState:(ios::ChromeBrowserState*)browserState {
42 DCHECK(browserState);
43 self = [super initWithNibName:nil bundle:nil];
44 if (self) {
45 _browserState = browserState->GetOriginalChromeBrowserState();
46 _loader = loader;
47
48 _bookmarks = ios::BookmarkModelFactory::GetForBrowserState(_browserState);
49 _editIndexPaths = [[NSMutableArray alloc] init];
50
51 [self resetEditNodes];
52 }
53 return self;
54 }
55
56 - (void)loadView {
57 CGRect frame = [[UIScreen mainScreen] bounds];
58 self.view = [[UIView alloc] initWithFrame:frame];
59 }
60
61 - (void)resetEditNodes {
62 _editNodes = std::set<const BookmarkNode*>();
63 _editNodesOrdered = std::vector<const BookmarkNode*>();
64 [self.editIndexPaths removeAllObjects];
65 }
66
67 - (void)insertEditNode:(const BookmarkNode*)node
68 atIndexPath:(NSIndexPath*)indexPath {
69 if (_editNodes.find(node) != _editNodes.end())
70 return;
71 _editNodes.insert(node);
72 _editNodesOrdered.push_back(node);
73 if (indexPath) {
74 [self.editIndexPaths addObject:indexPath];
75 } else {
76 // We insert null if we don't have the cell to keep the index valid.
77 [self.editIndexPaths addObject:[NSNull null]];
78 }
79 }
80
81 - (void)removeEditNode:(const BookmarkNode*)node
82 atIndexPath:(NSIndexPath*)indexPath {
83 if (_editNodes.find(node) == _editNodes.end())
84 return;
85
86 _editNodes.erase(node);
87 std::vector<const BookmarkNode*>::iterator it =
88 std::find(_editNodesOrdered.begin(), _editNodesOrdered.end(), node);
89 DCHECK(it != _editNodesOrdered.end());
90 _editNodesOrdered.erase(it);
91 if (indexPath) {
92 [self.editIndexPaths removeObject:indexPath];
93 } else {
94 // If we don't have the cell, we remove it by using its index.
95 const NSUInteger index = std::distance(_editNodesOrdered.begin(), it);
96 if (index < self.editIndexPaths.count) {
97 [self.editIndexPaths removeObjectAtIndex:index];
98 }
99 }
100 }
101
102 - (void)setEditing:(BOOL)editing animated:(BOOL)animated {
103 if (_editing == editing)
104 return;
105
106 _editing = editing;
107
108 // Only reset the editing state when leaving edit mode. This allows subclasses
109 // to add nodes for editing before entering edit mode.
110 if (!editing)
111 [self resetEditNodes];
112 }
113
114 - (void)dismissModals:(BOOL)animated {
115 // Do nothing in base class.
116 }
117
118 + (const BookmarkNode*)
119 defaultMoveFolderFromBookmarks:(const std::set<const BookmarkNode*>&)bookmarks
120 model:(bookmarks::BookmarkModel*)model {
121 if (bookmarks.size() == 0)
122 return model->mobile_node();
123 const BookmarkNode* firstParent = (*(bookmarks.begin()))->parent();
124 for (const BookmarkNode* node : bookmarks) {
125 if (node->parent() != firstParent)
126 return model->mobile_node();
127 }
128
129 return firstParent;
130 }
131
132 @end
133
134 @implementation BookmarkHomeViewController (PrivateAPIExposedForTesting)
135
136 - (const std::set<const BookmarkNode*>&)editNodes {
137 return _editNodes;
138 }
139
140 - (void)setEditNodes:(const std::set<const BookmarkNode*>&)editNodes {
141 _editNodes = editNodes;
142 }
143
144 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698