Browse Source

Removed some duplicated code… Refactored putItemArrayInMenu.

Thomas Buck 11 years ago
parent
commit
47453c6f65
2 changed files with 25 additions and 28 deletions
  1. 24
    27
      RedditBar/AppDelegate.m
  2. 1
    1
      RedditBar/RedditBar-Info.plist

+ 24
- 27
RedditBar/AppDelegate.m View File

85
 }
85
 }
86
 
86
 
87
 -(void)reloadListHasFrontpageCallback:(NSArray *)items {
87
 -(void)reloadListHasFrontpageCallback:(NSArray *)items {
88
-    if (items == nil) {
89
-        [firstMenuItem setTitle:NSLocalizedString(@"Error reading Frontpage!", @"Status api Read error")];
90
-        [self clearMenuItems];
91
-        [firstMenuItem setHidden:NO];
92
-        return;
93
-    }
94
-    lastFullName = [items objectAtIndex:[items count] - 1]; // last link fullname is at end of array
95
-    items = [items subarrayWithRange:NSMakeRange(0, [items count] - 1)]; // Remove last item
96
-    redditItems = items;
97
-    [self clearMenuItems];
98
-    [firstMenuItem setHidden:YES];
99
-    [self putItemArrayInMenu:redditItems];
88
+    [self reloadListHasXCallback:items ErrorMessage:NSLocalizedString(@"Error reading Frontpage!", @"Status api Read error")];
100
 }
89
 }
101
 
90
 
102
 -(void)reloadListHasSubredditsCallback:(NSArray *)items {
91
 -(void)reloadListHasSubredditsCallback:(NSArray *)items {
92
+    [self reloadListHasXCallback:items ErrorMessage:NSLocalizedString(@"Error reading Subreddits!", @"Status api read error")];
93
+}
94
+
95
+-(void)reloadListHasXCallback:(NSArray *)items ErrorMessage:(NSString*)error {
103
     if (items == nil) {
96
     if (items == nil) {
104
-        [firstMenuItem setTitle:NSLocalizedString(@"Error reading Subreddits!", @"Status api read error")];
97
+        [firstMenuItem setTitle:error];
105
         [self clearMenuItems];
98
         [self clearMenuItems];
106
         [firstMenuItem setHidden:NO];
99
         [firstMenuItem setHidden:NO];
107
         return;
100
         return;
198
 -(void)putItemArrayInMenu:(NSArray *)array {
191
 -(void)putItemArrayInMenu:(NSArray *)array {
199
     NSMutableArray *items = [NSMutableArray arrayWithCapacity:array.count];
192
     NSMutableArray *items = [NSMutableArray arrayWithCapacity:array.count];
200
     for (NSUInteger i = 0; i < [array count]; i++) {
193
     for (NSUInteger i = 0; i < [array count]; i++) {
201
-        RedditItem *reddit = [array objectAtIndex:i];
202
-        NSMenuItem *item = [[NSMenuItem alloc] init];
203
-        [item setTitle:reddit.name];
204
-        if (![reddit.name isEqualToString:reddit.fullName])
205
-            [item setToolTip:reddit.fullName];
206
-        if (reddit.isSelf) {
207
-            [item setAction:@selector(linkToOpen:)];
208
-            [item setKeyEquivalent:@""];
209
-        } else {
210
-            NSMenu *submenu = [[NSMenu alloc] init];
211
-            [submenu addItemWithTitle:NSLocalizedString(@"Link...", @"Link item") action:@selector(linkToOpen:) keyEquivalent:@""];
212
-            [submenu addItemWithTitle:NSLocalizedString(@"Comments...", @"comment item") action:@selector(linkToOpen:) keyEquivalent:@""];
213
-            [item setSubmenu:submenu];
214
-        }
194
+        NSMenuItem *item = [self prepareItemForMenu:[array objectAtIndex:i]];
215
         [items addObject:item];
195
         [items addObject:item];
216
         [statusMenu insertItem:item atIndex:i];
196
         [statusMenu insertItem:item atIndex:i];
217
     }
197
     }
218
     menuItems = items;
198
     menuItems = items;
219
 }
199
 }
220
 
200
 
201
+-(NSMenuItem *)prepareItemForMenu:(RedditItem *)reddit {
202
+    NSMenuItem *item = [[NSMenuItem alloc] init];
203
+    [item setTitle:reddit.name];
204
+    if (![reddit.name isEqualToString:reddit.fullName])
205
+        [item setToolTip:reddit.fullName];
206
+    if (reddit.isSelf) {
207
+        [item setAction:@selector(linkToOpen:)];
208
+        [item setKeyEquivalent:@""];
209
+    } else {
210
+        NSMenu *submenu = [[NSMenu alloc] init];
211
+        [submenu addItemWithTitle:NSLocalizedString(@"Link...", @"Link item") action:@selector(linkToOpen:) keyEquivalent:@""];
212
+        [submenu addItemWithTitle:NSLocalizedString(@"Comments...", @"comment item") action:@selector(linkToOpen:) keyEquivalent:@""];
213
+        [item setSubmenu:submenu];
214
+    }
215
+    return item;
216
+}
217
+
221
 -(IBAction)showPreferences:(id)sender {
218
 -(IBAction)showPreferences:(id)sender {
222
     [NSApp activateIgnoringOtherApps:YES];
219
     [NSApp activateIgnoringOtherApps:YES];
223
     prefWindow = [[PrefController alloc] initWithWindowNibName:@"Prefs"];
220
     prefWindow = [[PrefController alloc] initWithWindowNibName:@"Prefs"];

+ 1
- 1
RedditBar/RedditBar-Info.plist View File

21
 	<key>CFBundleSignature</key>
21
 	<key>CFBundleSignature</key>
22
 	<string>????</string>
22
 	<string>????</string>
23
 	<key>CFBundleVersion</key>
23
 	<key>CFBundleVersion</key>
24
-	<string>174</string>
24
+	<string>175</string>
25
 	<key>LSApplicationCategoryType</key>
25
 	<key>LSApplicationCategoryType</key>
26
 	<string>public.app-category.utilities</string>
26
 	<string>public.app-category.utilities</string>
27
 	<key>LSMinimumSystemVersion</key>
27
 	<key>LSMinimumSystemVersion</key>

Loading…
Cancel
Save