@@ -47,60 +47,61 @@ @implementation CSMenuLeftViewController
4747 */
4848- (void )viewDidLoad
4949{
50- [super viewDidLoad ];
51- self.currentUser = [User current ];
52- self.feeds = [[NSMutableSet alloc ] init ];
50+ [super viewDidLoad ];
51+ self.currentUser = [User current ];
52+ self.feeds = [[NSMutableSet alloc ] init ];
5353
54- // Setup the user and search datasources
55- [self setUpDataSources ];
56-
57- // Setup feedSearch API requestor
58- feedSearcher = [[CSFeedSearcher alloc ] init ];
54+ // Setup the user and search datasources
55+ [self setUpDataSources ];
5956
60- // Set tableViewStyle
61- CSEnhancedTableViewStyle *tableViewStyle = [[CSEnhancedTableViewStyleDark alloc ] init ];
62- self.tableView_feeds .tableViewStyle = tableViewStyle;
63-
64- [[self .textField_searchInput superview ] setBackgroundColor: [tableViewStyle tableBackgroundColor ]];
65- [self .textField_searchInput setBackgroundColor: [tableViewStyle headerBackgroundTopColor ]];
66- self.textField_searchInput .textColor = [tableViewStyle headerTitleLabelTextColor ];
67-
68- [self .textField_searchInput addTarget: self action: @selector (searchFieldDidChange )forControlEvents: UIControlEventEditingChanged];
57+ // Setup feedSearch API requestor
58+ feedSearcher = [[CSFeedSearcher alloc ] init ];
6959
70- // Add observer
71- // [self.currentUser addObserver:self forKeyPath:@"feeds" options:NSKeyValueObservingOptionNew context:nil];
72- [self observeRelationship: @keypath (self .currentUser.feeds)
73- changeBlock: ^(__weak CSMenuLeftViewController *self , NSSet *old, NSSet *new) {
74- NSMutableArray *addedFeeds = [[new allObjects ] mutableCopy ];
75- NSMutableArray *removedFeeds = [[old allObjects ] mutableCopy ];
76-
77- [addedFeeds removeObjectsInArray: [old allObjects ]];
78- [removedFeeds removeObjectsInArray: [new allObjects ]];
79-
80- for ( Feed *feed in removedFeeds ){
81- [[self feeds ] removeObject: feed];
82- }
83-
84- for ( Feed *feed in addedFeeds ){
85- [[self feeds ] addObject: feed];
60+ // Set tableViewStyle
61+ CSEnhancedTableViewStyle *tableViewStyle = [[CSEnhancedTableViewStyleDark alloc ] init ];
62+ self.tableView_feeds .tableViewStyle = tableViewStyle;
63+
64+ [[self .textField_searchInput superview ] setBackgroundColor: [tableViewStyle tableBackgroundColor ]];
65+ [self .textField_searchInput setBackgroundColor: [tableViewStyle headerBackgroundTopColor ]];
66+ self.textField_searchInput .textColor = [tableViewStyle headerTitleLabelTextColor ];
67+
68+ // Added search method to the user input field
69+ [self .textField_searchInput addTarget: self action: @selector (searchFieldDidChange )forControlEvents: UIControlEventEditingChanged];
70+
71+ // Add observer
72+ // [self.currentUser addObserver:self forKeyPath:@"feeds" options:NSKeyValueObservingOptionNew context:nil];
73+ [self observeRelationship: @keypath (self .currentUser.feeds)
74+ changeBlock: ^(__weak CSMenuLeftViewController *self , NSSet *old, NSSet *new) {
75+ NSMutableArray *addedFeeds = [[new allObjects ] mutableCopy ];
76+ NSMutableArray *removedFeeds = [[old allObjects ] mutableCopy ];
77+
78+ [addedFeeds removeObjectsInArray: [old allObjects ]];
79+ [removedFeeds removeObjectsInArray: [new allObjects ]];
80+
81+ for ( Feed *feed in removedFeeds ){
82+ [[self feeds ] removeObject: feed];
83+ }
84+
85+ for ( Feed *feed in addedFeeds ){
86+ [[self feeds ] addObject: feed];
87+ }
88+
89+ // Update and switch to the userFeed data source
90+ self.tableView_feeds .dataSource = userFeedDataSource;
91+ [userFeedDataSource updateWithFeeds: self .feeds];
92+
93+ [self .tableView_feeds reloadData ];
8694 }
87-
88- // Update and switch to the userFeed data source
89- self.tableView_feeds .dataSource = userFeedDataSource;
90- [userFeedDataSource updateWithFeeds: self .feeds];
91-
92- [self .tableView_feeds reloadData ];
93- }
94- insertionBlock: nil
95- removalBlock: nil
96- replacementBlock: nil
97- ];
98-
99- [[NSNotificationCenter defaultCenter ] addObserver: self
100- selector: @selector (menuStateEventOccurred: )
101- name: MFSideMenuStateNotificationEvent
102- object: nil ];
103-
95+ insertionBlock: nil
96+ removalBlock: nil
97+ replacementBlock: nil
98+ ];
99+
100+ [[NSNotificationCenter defaultCenter ] addObserver: self
101+ selector: @selector (menuStateEventOccurred: )
102+ name: MFSideMenuStateNotificationEvent
103+ object: nil ];
104+
104105 self.tableView_feeds .delegate = self;
105106
106107 [userFeedDataSource updateWithFeeds: self .feeds];
@@ -117,38 +118,41 @@ - (void)setUpDataSources
117118}
118119
119120- (void )menuStateEventOccurred : (NSNotification *)notification {
120- MFSideMenuStateEvent event = [[[notification userInfo ] objectForKey: @" eventType" ] intValue ];
121-
122- __weak CSMenuLeftViewController *weakSelf = self;
123-
121+ MFSideMenuStateEvent event = [[[notification userInfo ] objectForKey: @" eventType" ] intValue ];
122+
123+ __weak CSMenuLeftViewController *weakSelf = self;
124+
124125 switch (event) {
125- case MFSideMenuStateEventMenuWillOpen:
126- // the menu will open
127- break ;
128- case MFSideMenuStateEventMenuDidOpen:
129- // the menu finished opening
130- [weakSelf.tableView_feeds reloadData ];
131- break ;
132- case MFSideMenuStateEventMenuWillClose:
133- // the menu will close
134- [self .textField_searchInput endEditing: YES ];
135- break ;
136- case MFSideMenuStateEventMenuDidClose:
137- weakSelf.textField_searchInput .text = @" " ;
138- [weakSelf.tableView_feeds setEditing: NO animated: YES ];
139- weakSelf.menuContainerViewController .panMode = MFSideMenuPanModeNone;
126+ case MFSideMenuStateEventMenuWillOpen:
127+ // the menu will open
128+ break ;
129+ case MFSideMenuStateEventMenuDidOpen:
130+ // the menu finished opening
131+ [weakSelf.tableView_feeds reloadData ];
132+ break ;
133+ case MFSideMenuStateEventMenuWillClose:
134+ // the menu will close
135+ [self .textField_searchInput endEditing: YES ];
136+ break ;
137+ case MFSideMenuStateEventMenuDidClose:
138+ weakSelf.textField_searchInput .text = @" " ;
139+ [weakSelf.tableView_feeds setEditing: NO animated: YES ];
140+ weakSelf.menuContainerViewController .panMode = MFSideMenuPanModeNone;
140141
141- // Reset to the users feeds
142- weakSelf.tableView_feeds .dataSource = userFeedDataSource;
143- [weakSelf.tableView_feeds reloadData ];
144- break ;
142+ // Reset to the users feeds
143+ weakSelf.tableView_feeds .dataSource = userFeedDataSource;
144+ [weakSelf.tableView_feeds reloadData ];
145+ break ;
145146 }
146147}
147148
149+ /* *
150+ * Update the feeds in the menu when a user begins or ends a search
151+ */
148152- (void )searchFieldDidChange
149153{
150154 // If the searchInput has text
151- if (self.textField_searchInput .text && self.textField_searchInput .text .length > 0 ){
155+ if (self.textField_searchInput .text && self.textField_searchInput .text .length > 0 ) {
152156
153157 // Create empty searchFeed set
154158 NSMutableSet *searchedFeeds = [[NSMutableSet alloc ] init ];
@@ -158,28 +162,26 @@ - (void)searchFieldDidChange
158162 self.tableView_feeds .dataSource = searchFeedDataSource;
159163
160164 // If the user is typing a url
161- if ([self .textField_searchInput.text hasPrefix: @" http" ]){
165+ if ([self .textField_searchInput.text hasPrefix: @" http" ]) {
162166 // Get the local context
163167 NSManagedObjectContext *localContext = [NSManagedObjectContext MR_contextForCurrentThread ];
164168
165169 // Create a new Feed in the current thread context
166170 Feed *customFeed = [Feed MR_createInContext: localContext];
167171 customFeed.name = self.textField_searchInput .text ;
168172 customFeed.url = self.textField_searchInput .text ;
169-
173+
170174 // Add custom feed to the searchFeeds
171175 [searchedFeeds addObject: customFeed];
172- }
173- else {
176+ } else {
174177 // Return feeds from the API similar to user input
175178 // Add these feeds to the searchFeed datasource
176179 [feedSearcher feedsLike: self .textField_searchInput.text];
177180 }
178-
181+
179182 // Reload the table with new searchFeeds
180183 [self .tableView_feeds reloadData ];
181- }
182- else {
184+ } else {
183185 // Switch to the userFeeds datasource
184186 self.tableView_feeds .dataSource = userFeedDataSource;
185187 [self .tableView_feeds reloadData ];
0 commit comments