use constants for keys

This commit is contained in:
Sami Samhuri 2015-05-03 19:07:41 -07:00
parent 41f939ef97
commit 33979d0369
4 changed files with 41 additions and 20 deletions

View file

@ -212,15 +212,23 @@
#pragma mark - State restoration #pragma mark - State restoration
static NSString *const StateRestorationPostKey = @"post";
static NSString *const StateRestorationModifiedPostKey = @"modifiedPost";
static NSString *const StateRestorationTextViewContentOffsetKey = @"textView.contentOffset";
- (void)encodeRestorableStateWithCoder:(NSCoder *)coder { - (void)encodeRestorableStateWithCoder:(NSCoder *)coder {
[coder encodeObject:self.post forKey:@"post"]; NSLog(@"%@ encode restorable state with coder %@", self, coder);
[coder encodeObject:self.modifiedPost forKey:@"modifiedPost"]; [coder encodeObject:self.post forKey:StateRestorationPostKey];
[coder encodeObject:self.modifiedPost forKey:StateRestorationModifiedPostKey];
[coder encodeCGPoint:self.textView.contentOffset forKey:StateRestorationTextViewContentOffsetKey];
[super encodeRestorableStateWithCoder:coder]; [super encodeRestorableStateWithCoder:coder];
} }
- (void)decodeRestorableStateWithCoder:(NSCoder *)coder { - (void)decodeRestorableStateWithCoder:(NSCoder *)coder {
self.post = [coder decodeObjectForKey:@"post"]; NSLog(@"%@ decode restorable state with coder %@", self, coder);
self.modifiedPost = [coder decodeObjectForKey:@"modifiedPost"]; self.post = [coder decodeObjectForKey:StateRestorationPostKey];
self.modifiedPost = [coder decodeObjectForKey:StateRestorationModifiedPostKey];
self.textView.contentOffset = [coder decodeCGPointForKey:StateRestorationTextViewContentOffsetKey];
[super decodeRestorableStateWithCoder:coder]; [super decodeRestorableStateWithCoder:coder];
} }

View file

@ -19,18 +19,21 @@
return self; return self;
} }
static NSString *const PostsKey = @"posts";
static NSString *const TitleKey = @"title";
- (id)initWithCoder:(NSCoder *)coder { - (id)initWithCoder:(NSCoder *)coder {
self = [super init]; self = [super init];
if (self) { if (self) {
_title = [coder decodeObjectForKey:@"title"]; _title = [coder decodeObjectForKey:TitleKey];
_posts = [coder decodeObjectForKey:@"posts"]; _posts = [coder decodeObjectForKey:PostsKey];
} }
return self; return self;
} }
- (void)encodeWithCoder:(NSCoder *)coder { - (void)encodeWithCoder:(NSCoder *)coder {
[coder encodeObject:self.title forKey:@"title"]; [coder encodeObject:self.title forKey:TitleKey];
[coder encodeObject:self.posts forKey:@"posts"]; [coder encodeObject:self.posts forKey:PostsKey];
} }
@end @end

View file

@ -246,21 +246,26 @@ static const NSUInteger SectionPublished = 1;
#pragma mark - State restoration #pragma mark - State restoration
static NSString *const StateRestorationPostCollectionsKey = @"postCollections";
static NSString *const StateRestorationBlogStatusDateKey = @"blogStatusDate";
static NSString *const StateRestorationBlogStatusTextKey = @"blogStatusText";
static NSString *const StateRestorationTabelViewContentOffsetKey = @"tableView.contentOffset";
- (void)encodeRestorableStateWithCoder:(NSCoder *)coder { - (void)encodeRestorableStateWithCoder:(NSCoder *)coder {
NSLog(@"%@ encode restorable state with coder %@", self, coder); NSLog(@"%@ encode restorable state with coder %@", self, coder);
[coder encodeObject:self.postCollections forKey:@"postCollections"]; [coder encodeObject:self.postCollections forKey:StateRestorationPostCollectionsKey];
[coder encodeObject:self.blogStatusDate forKey:@"blogStatusDate"]; [coder encodeObject:self.blogStatusDate forKey:StateRestorationBlogStatusDateKey];
[coder encodeObject:self.blogStatusText forKey:@"blogStatusText"]; [coder encodeObject:self.blogStatusText forKey:StateRestorationBlogStatusTextKey];
[coder encodeCGPoint:self.tableView.contentOffset forKey:@"tableView.contentOffset"]; [coder encodeCGPoint:self.tableView.contentOffset forKey:StateRestorationTabelViewContentOffsetKey];
[super encodeRestorableStateWithCoder:coder]; [super encodeRestorableStateWithCoder:coder];
} }
- (void)decodeRestorableStateWithCoder:(NSCoder *)coder { - (void)decodeRestorableStateWithCoder:(NSCoder *)coder {
NSLog(@"%@ decode restorable state with coder %@", self, coder); NSLog(@"%@ decode restorable state with coder %@", self, coder);
self.postCollections = [coder decodeObjectForKey:@"postCollections"]; self.postCollections = [coder decodeObjectForKey:StateRestorationPostCollectionsKey];
self.blogStatusDate = [coder decodeObjectForKey:@"blogStatusDate"]; self.blogStatusDate = [coder decodeObjectForKey:StateRestorationBlogStatusDateKey];
self.blogStatusText = [coder decodeObjectForKey:@"blogStatusText"]; self.blogStatusText = [coder decodeObjectForKey:StateRestorationBlogStatusTextKey];
self.tableView.contentOffset = [coder decodeCGPointForKey:@"tableView.contentOffset"]; self.tableView.contentOffset = [coder decodeCGPointForKey:StateRestorationTabelViewContentOffsetKey];
[super decodeRestorableStateWithCoder:coder]; [super decodeRestorableStateWithCoder:coder];
} }

View file

@ -41,19 +41,24 @@
[self.webView loadHTMLString:@"<!doctype html><html><head><title></title></head><body></body></html>" baseURL:nil]; [self.webView loadHTMLString:@"<!doctype html><html><head><title></title></head><body></body></html>" baseURL:nil];
} }
#pragma mark - State restoration
static NSString *const StateRestorationInitialRequestKey = @"initialRequest";
static NSString *const StateRestorationWebViewRequestURLKey = @"webView.request.URL";
- (void)encodeRestorableStateWithCoder:(NSCoder *)coder { - (void)encodeRestorableStateWithCoder:(NSCoder *)coder {
[coder encodeObject:self.initialRequest forKey:@"initialRequest"]; [coder encodeObject:self.initialRequest forKey:StateRestorationInitialRequestKey];
[coder encodeObject:self.webView.request.URL forKey:@"webView.request.URL"]; [coder encodeObject:self.webView.request.URL forKey:StateRestorationWebViewRequestURLKey];
[super encodeRestorableStateWithCoder:coder]; [super encodeRestorableStateWithCoder:coder];
} }
- (void)decodeRestorableStateWithCoder:(NSCoder *)coder { - (void)decodeRestorableStateWithCoder:(NSCoder *)coder {
NSURL *url = [coder decodeObjectForKey:@"webView.request.URL"]; NSURL *url = [coder decodeObjectForKey:StateRestorationWebViewRequestURLKey];
if (url) { if (url) {
[self.webView loadRequest:[NSURLRequest requestWithURL:url]]; [self.webView loadRequest:[NSURLRequest requestWithURL:url]];
} }
else { else {
self.initialRequest = [coder decodeObjectForKey:@"initialRequest"]; self.initialRequest = [coder decodeObjectForKey:StateRestorationInitialRequestKey];
} }
[super decodeRestorableStateWithCoder:coder]; [super decodeRestorableStateWithCoder:coder];
} }