Browse Source

Frontpage loading working

Thomas Buck 11 years ago
parent
commit
6f1379bd07
4 changed files with 52 additions and 9 deletions
  1. 21
    2
      RedditBar/AppDelegate.m
  2. 1
    1
      RedditBar/Base.lproj/Prefs.xib
  3. 29
    5
      RedditBar/Reddit.m
  4. 1
    1
      RedditBar/RedditBar-Info.plist

+ 21
- 2
RedditBar/AppDelegate.m View File

60
 -(void)reloadListWithOptions {
60
 -(void)reloadListWithOptions {
61
     if ([currentState.modhash isEqualToString:@""]) {
61
     if ([currentState.modhash isEqualToString:@""]) {
62
         [firstMenuItem setTitle:@"Not logged in!"];
62
         [firstMenuItem setTitle:@"Not logged in!"];
63
+        [self clearMenuItems];
64
+        [firstMenuItem setHidden:NO];
65
+        [self showPreferences:nil];
63
         return;
66
         return;
64
     }
67
     }
65
     api = [[Reddit alloc] initWithUsername:currentState.username Modhash:currentState.modhash];
68
     api = [[Reddit alloc] initWithUsername:currentState.username Modhash:currentState.modhash];
66
     NSString *tmp = @"";
69
     NSString *tmp = @"";
67
     if (![api isAuthenticatedNewModhash:&tmp]) {
70
     if (![api isAuthenticatedNewModhash:&tmp]) {
68
-        [firstMenuItem setTitle:@"Not logged in!"];
71
+        [firstMenuItem setTitle:@"Login Error!"];
72
+        [self clearMenuItems];
73
+        [firstMenuItem setHidden:NO];
69
         return;
74
         return;
70
     }
75
     }
71
     
76
     
80
         NSArray *items = [api readFrontpageLength:currentState.length];
85
         NSArray *items = [api readFrontpageLength:currentState.length];
81
         if (items == nil) {
86
         if (items == nil) {
82
             [firstMenuItem setTitle:@"Error reading Frontpage!"];
87
             [firstMenuItem setTitle:@"Error reading Frontpage!"];
88
+            [self clearMenuItems];
89
+            [firstMenuItem setHidden:NO];
83
             return;
90
             return;
84
         }
91
         }
85
         redditItems = items;
92
         redditItems = items;
87
         NSArray *items = [api readSubreddits:currentState.subreddits Length:currentState.length];
94
         NSArray *items = [api readSubreddits:currentState.subreddits Length:currentState.length];
88
         if (items == nil) {
95
         if (items == nil) {
89
             [firstMenuItem setTitle:@"Error reading Subreddits!"];
96
             [firstMenuItem setTitle:@"Error reading Subreddits!"];
97
+            [self clearMenuItems];
98
+            [firstMenuItem setHidden:NO];
90
             return;
99
             return;
91
         }
100
         }
92
         redditItems = items;
101
         redditItems = items;
93
     }
102
     }
103
+    [self clearMenuItems];
104
+    [firstMenuItem setHidden:YES];
94
     [self putItemArrayInMenu:redditItems];
105
     [self putItemArrayInMenu:redditItems];
95
 }
106
 }
96
 
107
 
132
     }
143
     }
133
 }
144
 }
134
 
145
 
146
+-(void)clearMenuItems {
147
+    if (menuItems != nil) {
148
+        for (NSUInteger i = 0; i < [menuItems count]; i++) {
149
+            [statusMenu removeItem:[menuItems objectAtIndex:i]];
150
+        }
151
+        menuItems = nil;
152
+    }
153
+}
154
+
135
 -(void)putItemArrayInMenu:(NSArray *)array {
155
 -(void)putItemArrayInMenu:(NSArray *)array {
136
-    [firstMenuItem setHidden:YES];
137
     NSMutableArray *items = [NSMutableArray arrayWithCapacity:array.count];
156
     NSMutableArray *items = [NSMutableArray arrayWithCapacity:array.count];
138
     for (NSUInteger i = 0; i < [array count]; i++) {
157
     for (NSUInteger i = 0; i < [array count]; i++) {
139
         RedditItem *reddit = [array objectAtIndex:i];
158
         RedditItem *reddit = [array objectAtIndex:i];

+ 1
- 1
RedditBar/Base.lproj/Prefs.xib View File

143
                     <button id="XZM-90-hQ8">
143
                     <button id="XZM-90-hQ8">
144
                         <rect key="frame" x="118" y="234" width="204" height="18"/>
144
                         <rect key="frame" x="118" y="234" width="204" height="18"/>
145
                         <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
145
                         <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
146
-                        <buttonCell key="cell" type="check" title="Subscriptions" bezelStyle="regularSquare" imagePosition="overlaps" alignment="right" state="on" inset="2" id="vW5-Cp-Bm1">
146
+                        <buttonCell key="cell" type="check" title="Subscriptions" bezelStyle="regularSquare" imagePosition="overlaps" alignment="right" enabled="NO" state="on" inset="2" id="vW5-Cp-Bm1">
147
                             <behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
147
                             <behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
148
                             <font key="font" metaFont="system"/>
148
                             <font key="font" metaFont="system"/>
149
                         </buttonCell>
149
                         </buttonCell>

+ 29
- 5
RedditBar/Reddit.m View File

93
 }
93
 }
94
 
94
 
95
 -(NSArray *)readFrontpageLength:(NSInteger)length {
95
 -(NSArray *)readFrontpageLength:(NSInteger)length {
96
-    // TODO read frontpage
96
+    NSHTTPURLResponse *response;
97
+    NSString *url = [NSString stringWithFormat:@"hot.json?limit=%ld", (long)length];
98
+    NSData *data = [self queryAPI:url withResponse:&response];
99
+    if ((data == nil) || ([response statusCode] != 200)) {
100
+        return nil;
101
+    }
102
+    NSError *error;
103
+    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
104
+    NSDictionary *dat = [json valueForKey:@"data"];
105
+    if (dat == nil)
106
+        return nil;
107
+    NSArray *children = [dat valueForKey:@"children"];
108
+    if (children == nil)
109
+        return nil;
97
     
110
     
98
-    RedditItem *a = [RedditItem itemWithName:@"Test 1" Link:@"http://google.de" Comments:@"http://google.de" Self:NO];
99
-    RedditItem *b = [RedditItem itemWithName:@"Test 2" Link:@"http://reddit.com" Comments:@"http://reddit.com" Self:NO];
100
-    RedditItem *c = [RedditItem itemWithName:@"Test 3" Link:@"http://google.de" Comments:nil Self:YES];
101
-    NSMutableArray *array = [NSMutableArray arrayWithObjects:a, b, c, nil];
111
+    NSMutableArray *array = [NSMutableArray arrayWithCapacity:[children count]];
112
+    for (NSUInteger i = 0; i < [children count]; i++) {
113
+        NSDictionary *child = [children objectAtIndex:i];
114
+        NSDictionary *current = [child valueForKey:@"data"];
115
+        NSString *name = [current valueForKey:@"title"];
116
+        NSString *link = [current valueForKey:@"url"];
117
+        NSString *comments = nil;
118
+        NSNumber *num = [current valueForKey:@"is_self"];
119
+        BOOL isSelf = [num boolValue];
120
+        if (!isSelf) {
121
+            comments = [NSString stringWithFormat:@"http://www.reddit.com%@", [current valueForKey:@"permalink"]];
122
+        }
123
+        RedditItem *r = [RedditItem itemWithName:name Link:link Comments:comments Self:isSelf];
124
+        [array insertObject:r atIndex:i];
125
+    }
102
     return array;
126
     return array;
103
 }
127
 }
104
 
128
 

+ 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>127</string>
24
+	<string>132</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