Skip to content

Commit 99569ae

Browse files
committed
Add (currently broken) support for persisting objects to disk.
1 parent 29d4e74 commit 99569ae

File tree

13 files changed

+369
-129
lines changed

13 files changed

+369
-129
lines changed

Classes/HNKit/HNAPISearch.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ - (void)handleResponse {
6363
NSDictionary *item = [self itemFromRaw:[result objectForKey:@"item"]];
6464
HNEntry *entry = [HNEntry entryWithIdentifier:[item objectForKey:@"identifier"]];
6565

66-
[entry loadFromDictionary:item];
66+
[entry loadContentsDictionary:item];
6767
[entries addObject:entry];
6868
}
6969
[responseString release];

Classes/HNKit/HNContainer.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#import "HNShared.h"
1010
#import "HNObject.h"
1111

12-
#define kHNContainerLoadingStateLoadingMore 0x00010000
12+
#define kHNContainerLoadingStateLoadingMore (0x1 << 16)
1313

1414
@interface HNContainer : HNObject {
1515
NSArray *entries;
@@ -25,7 +25,6 @@
2525
- (BOOL)isLoadingMore;
2626
- (void)cancelLoadingMore;
2727

28-
- (void)loadFromDictionary:(NSDictionary *)response;
29-
- (void)loadFromDictionary:(NSDictionary *)response entries:(NSArray **)outEntries;
28+
- (void)loadContentsDictionary:(NSDictionary *)contents entries:(NSArray **)outEntries;
3029

3130
@end

Classes/HNKit/HNContainer.m

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,43 @@ - (void)beginLoadingMore {
2828
[moreRequest performRequestWithPath:@"x" parameters:parameters];
2929
}
3030

31-
- (void)loadFromDictionary:(NSDictionary *)response entries:(NSArray **)outEntries {
31+
- (void)loadContentsDictionary:(NSDictionary *)contents entries:(NSArray **)outEntries {
3232
return;
3333
}
3434

35-
- (void)loadFromDictionary:(NSDictionary *)response {
36-
[self loadFromDictionary:response entries:NULL];
37-
}
38-
39-
- (void)loadFromDictionary:(NSDictionary *)response append:(BOOL)append {
35+
- (void)loadContentsDictionary:(NSDictionary *)contents {
4036
NSArray *children = nil;
41-
[self loadFromDictionary:response entries:&children];
42-
43-
if (append) {
37+
[self loadContentsDictionary:contents entries:&children];
38+
39+
if ([[contents objectForKey:@"append"] boolValue]) {
4440
[self setEntries:[entries arrayByAddingObjectsFromArray:children]];
4541
} else {
4642
[self setEntries:children];
4743
}
48-
49-
[self setMoreToken:[response objectForKey:@"more"]];
44+
45+
[self setMoreToken:[contents objectForKey:@"more"]];
46+
47+
[super loadContentsDictionary:contents];
48+
}
49+
50+
- (void)loadContentsDictionary:(NSDictionary *)contents append:(BOOL)append {
51+
NSMutableDictionary *mutableContents = [[contents mutableCopy] autorelease];
52+
[mutableContents setObject:[NSNumber numberWithBool:append] forKey:@"append"];
53+
[self loadContentsDictionary:mutableContents];
54+
}
55+
56+
- (NSDictionary *)contentsDictionary {
57+
NSMutableDictionary *dictionary = [[[super contentsDictionary] mutableCopy] autorelease];
58+
59+
if (moreToken != nil) [dictionary setObject:moreToken forKey:@"more"];
60+
61+
NSMutableArray *children = [NSMutableArray array];
62+
for (HNEntry *child in entries) {
63+
[children addObject:[child contentsDictionary]];
64+
}
65+
[dictionary setObject:children forKey:@"children"];
66+
67+
return dictionary;
5068
}
5169

5270
- (void)cancelLoadingMore {
@@ -67,7 +85,7 @@ - (void)moreRequest:(HNAPIRequest *)request completedWithResponse:(NSDictionary
6785
moreRequest = nil;
6886

6987
if (error == nil) {
70-
[self loadFromDictionary:response append:YES];
88+
[self loadContentsDictionary:response append:YES];
7189

7290
[[NSNotificationCenter defaultCenter] postNotificationName:kHNObjectFinishedLoadingNotification object:self];
7391
} else {
@@ -77,7 +95,7 @@ - (void)moreRequest:(HNAPIRequest *)request completedWithResponse:(NSDictionary
7795

7896
- (void)finishLoadingWithResponse:(NSDictionary *)response error:(NSError *)error {
7997
if (error == nil) {
80-
[self loadFromDictionary:response append:NO];
98+
[self loadContentsDictionary:response append:NO];
8199
}
82100
}
83101

Classes/HNKit/HNEntry.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
@property (nonatomic, readonly) HNEntryBodyRenderer *renderer;
4343
#endif
4444

45-
- (void)loadFromDictionary:(NSDictionary *)response entries:(NSArray **)outEntries withSubmission:(HNEntry *)submission_;
45+
- (void)loadContentsDictionary:(NSDictionary *)response entries:(NSArray **)outEntries withSubmission:(HNEntry *)submission_;
4646

4747
- (BOOL)isComment;
4848
- (BOOL)isSubmission;

Classes/HNKit/HNEntry.m

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,16 @@ - (BOOL)isComment {
5353
}
5454

5555
- (BOOL)isSubmission {
56-
// Checking submission rather than something like title since this will be set
57-
// even when the entry hasn't been loaded.
56+
// This is counterintuitive, but we are essentially checking for a parent.
5857
return [self submission] == nil;
5958
}
6059

61-
- (void)loadFromDictionary:(NSDictionary *)response entries:(NSArray **)outEntries {
62-
if ([response objectForKey:@"submission"]) {
63-
[self setSubmission:[HNEntry entryWithIdentifier:[response objectForKey:@"submission"]]];
60+
- (void)loadContentsDictionary:(NSDictionary *)contents entries:(NSArray **)outEntries {
61+
if ([contents objectForKey:@"submission"]) {
62+
[self setSubmission:[HNEntry entryWithIdentifier:[contents objectForKey:@"submission"]]];
6463
}
6564

66-
id parentId = [response objectForKey:@"parent"];
65+
id parentId = [contents objectForKey:@"parent"];
6766
if (parentId) {
6867
HNEntry *parent_ = [HNEntry entryWithIdentifier:parentId];
6968

@@ -76,27 +75,24 @@ - (void)loadFromDictionary:(NSDictionary *)response entries:(NSArray **)outEntri
7675
[self setParent:parent_];
7776
}
7877

79-
[self loadFromDictionary:response entries:outEntries withSubmission:[self submission] ?: self];
78+
[self loadContentsDictionary:contents entries:outEntries withSubmission:[self submission] ?: self];
8079
}
8180

82-
- (void)loadChildrenFromDictionary:(NSDictionary *)response {
83-
}
84-
85-
- (void)loadFromDictionary:(NSDictionary *)response entries:(NSArray **)outEntries withSubmission:(HNEntry *)submission_ {
86-
if ([response objectForKey:@"url"] != nil) [self setDestination:[NSURL URLWithString:[response objectForKey:@"url"]]];
87-
if ([response objectForKey:@"user"] != nil) [self setSubmitter:[HNUser userWithIdentifier:[response objectForKey:@"user"]]];
88-
if ([response objectForKey:@"body"] != nil) [self setBody:[response objectForKey:@"body"]];
89-
if ([response objectForKey:@"date"] != nil) [self setPosted:[response objectForKey:@"date"]];
90-
if ([response objectForKey:@"title"] != nil) [self setTitle:[response objectForKey:@"title"]];
91-
if ([response objectForKey:@"points"] != nil) [self setPoints:[[response objectForKey:@"points"] intValue]];
81+
- (void)loadContentsDictionary:(NSDictionary *)contents entries:(NSArray **)outEntries withSubmission:(HNEntry *)submission_ {
82+
if ([contents objectForKey:@"url"] != nil) [self setDestination:[NSURL URLWithString:[contents objectForKey:@"url"]]];
83+
if ([contents objectForKey:@"user"] != nil) [self setSubmitter:[HNUser userWithIdentifier:[contents objectForKey:@"user"]]];
84+
if ([contents objectForKey:@"body"] != nil) [self setBody:[contents objectForKey:@"body"]];
85+
if ([contents objectForKey:@"date"] != nil) [self setPosted:[contents objectForKey:@"date"]];
86+
if ([contents objectForKey:@"title"] != nil) [self setTitle:[contents objectForKey:@"title"]];
87+
if ([contents objectForKey:@"points"] != nil) [self setPoints:[[contents objectForKey:@"points"] intValue]];
9288

9389
NSMutableArray *comments = [NSMutableArray array];
94-
if ([response objectForKey:@"children"] != nil) {
95-
for (NSDictionary *child in [response objectForKey:@"children"]) {
90+
if ([contents objectForKey:@"children"] != nil) {
91+
for (NSDictionary *child in [contents objectForKey:@"children"]) {
9692
HNEntry *childEntry = [HNEntry entryWithIdentifier:[child objectForKey:@"identifier"]];
9793
NSArray *childEntries = nil;
9894

99-
[childEntry loadFromDictionary:child entries:&childEntries withSubmission:submission_];
95+
[childEntry loadContentsDictionary:child entries:&childEntries withSubmission:submission_];
10096
[childEntry setEntries:childEntries];
10197
[childEntry setParent:self];
10298
[childEntry setSubmission:submission_];
@@ -115,8 +111,8 @@ - (void)loadFromDictionary:(NSDictionary *)response entries:(NSArray **)outEntri
115111
}
116112
}
117113

118-
if ([response objectForKey:@"numchildren"] != nil) {
119-
int count = [[response objectForKey:@"numchildren"] intValue];
114+
if ([contents objectForKey:@"numchildren"] != nil) {
115+
int count = [[contents objectForKey:@"numchildren"] intValue];
120116
[self setChildren:count];
121117
} else {
122118
int count = [comments count];
@@ -126,4 +122,18 @@ - (void)loadFromDictionary:(NSDictionary *)response entries:(NSArray **)outEntri
126122
}
127123
}
128124

125+
- (NSDictionary *)contentsDictionary {
126+
NSMutableDictionary *dictionary = [[[super contentsDictionary] mutableCopy] autorelease];
127+
128+
if (destination != nil) [dictionary setObject:destination forKey:@"url"];
129+
if (submitter != nil) [dictionary setObject:[submitter identifier] forKey:@"user"];
130+
if (body != nil) [dictionary setObject:body forKey:@"body"];
131+
if (posted != nil) [dictionary setObject:posted forKey:@"date"];
132+
if (title != nil) [dictionary setObject:title forKey:@"title"];
133+
[dictionary setObject:[NSNumber numberWithInt:points] forKey:@"points"];
134+
[dictionary setObject:[NSNumber numberWithInt:children] forKey:@"numchildren"];
135+
136+
return dictionary;
137+
}
138+
129139
@end

Classes/HNKit/HNEntryList.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
typedef NSString *HNEntryListIdentifier;
1414

15+
#define kHNContainerLoadingStatePartial (0x1 << 17)
16+
1517
#define kHNEntryListIdentifierSubmissions @"news"
1618
#define kHNEntryListIdentifierNewSubmissions @"newest"
1719
#define kHNEntryListIdentifierBestSubmissions @"best"

Classes/HNKit/HNEntryList.m

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,21 +73,26 @@ - (NSDictionary *)infoDictionary {
7373
}
7474
}
7575

76-
- (void)loadFromDictionary:(NSDictionary *)response entries:(NSArray **)outEntries {
76+
- (void)loadContentsDictionary:(NSDictionary *)contents entries:(NSArray **)outEntries {
7777
NSMutableArray *children = [NSMutableArray array];
7878

79-
for (NSDictionary *entryDictionary in [response objectForKey:@"children"]) {
79+
for (NSDictionary *entryDictionary in [contents objectForKey:@"children"]) {
8080
HNEntry *entry = [HNEntry entryWithIdentifier:[entryDictionary objectForKey:@"identifier"]];
81-
[entry loadFromDictionary:entryDictionary];
81+
[entry loadContentsDictionary:entryDictionary];
8282
[children addObject:entry];
83-
84-
// XXX: should the entry be set to loaded here? probably not, since
85-
// it isn't fully loaded (as the loaded state represents).
83+
84+
[entry addLoadingState:kHNContainerLoadingStatePartial];
8685
}
8786

8887
if (outEntries != NULL) {
8988
*outEntries = children;
9089
}
9190
}
9291

92+
- (NSDictionary *)contentsDictionary {
93+
NSMutableDictionary *dictionary = [[[super contentsDictionary] mutableCopy] autorelease];
94+
95+
return dictionary;
96+
}
97+
9398
@end

Classes/HNKit/HNObject.h

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ typedef enum {
3030
id identifier;
3131
NSURL *url;
3232

33+
NSDate *lastUpdatedDate;
3334
HNObjectLoadingState loadingState;
3435

3536
HNAPIRequest *apiRequest;
@@ -38,14 +39,7 @@ typedef enum {
3839
@property (nonatomic, copy) id identifier;
3940
@property (nonatomic, copy) NSURL *URL;
4041
@property (nonatomic, readonly) HNObjectLoadingState loadingState;
41-
42-
+ (BOOL)isValidURL:(NSURL *)url_;
43-
+ (NSDictionary *)infoDictionaryForURL:(NSURL *)url_;
44-
+ (id)identifierForURL:(NSURL *)url_;
45-
46-
+ (NSString *)pathForURLWithIdentifier:(id)identifier_ infoDictionary:(NSDictionary *)info;
47-
+ (NSDictionary *)parametersForURLWithIdentifier:(id)identifier_ infoDictionary:(NSDictionary *)info;
48-
+ (NSURL *)generateURLWithIdentifier:(id)identifier_ infoDictionary:(NSDictionary *)info;
42+
@property (nonatomic, readonly) NSDate *lastUpdatedDate;
4943

5044
// These methods don't necessarily create a new instance if it's already in the
5145
// cache. The cache's keyed on (class, identifier, info) tuples inside HNObject.
@@ -68,16 +62,26 @@ typedef enum {
6862

6963
@end
7064

71-
@interface HNObject (Subclassing)
65+
@interface HNObject (Private)
7266

73-
@property (nonatomic, copy) NSDictionary *contentsDictionary;
67+
+ (NSString *)pathForURLWithIdentifier:(id)identifier_ infoDictionary:(NSDictionary *)info;
68+
+ (NSDictionary *)parametersForURLWithIdentifier:(id)identifier_ infoDictionary:(NSDictionary *)info;
69+
+ (NSURL *)generateURLWithIdentifier:(id)identifier_ infoDictionary:(NSDictionary *)info;
70+
71+
+ (BOOL)isValidURL:(NSURL *)url_;
72+
+ (id)identifierForURL:(NSURL *)url_;
73+
+ (NSDictionary *)infoDictionaryForURL:(NSURL *)url_;
7474

7575
- (void)beginLoadingWithState:(HNObjectLoadingState)state_;
7676
- (void)addLoadingState:(HNObjectLoadingState)state_;
7777
- (void)clearLoadingState:(HNObjectLoadingState)state_;
7878
- (BOOL)hasLoadingState:(HNObjectLoadingState)state_;
7979

80+
- (NSDictionary *)infoDictionary;
8081
- (void)loadInfoDictionary:(NSDictionary *)info;
8182

83+
- (NSDictionary *)contentsDictionary;
84+
- (void)loadContentsDictionary:(NSDictionary *)contents;
85+
8286
@end
8387

0 commit comments

Comments
 (0)