save and restore per-post scroll offsets

This commit is contained in:
Sami Samhuri 2015-05-16 07:23:53 -07:00
parent 7ba1bce501
commit a34d5875ee

View file

@ -56,6 +56,7 @@
[notificationCenter addObserver:self selector:@selector(postDeleted:) name:DraftRemovedNotification object:nil];
[notificationCenter addObserver:self selector:@selector(postDeleted:) name:PublishedPostRemovedNotification object:nil];
[self configureView];
[self restoreScrollOffset];
}
- (void)viewWillDisappear:(BOOL)animated {
@ -148,14 +149,11 @@
- (void)configureBodyView {
NSString *body = nil;
CGPoint scrollOffset = CGPointZero;
Post *post = self.modifiedPost;
if (post) {
body = post.body;
// TODO: restore scroll offset for this post ... user defaults?
}
self.textView.text = body;
self.textView.contentOffset = scrollOffset;
}
- (void)configureToolbar {
@ -263,6 +261,8 @@ static NSString *const StateRestorationModifiedPostKey = @"modifiedPost";
return self.savePromise;
}
[self saveScrollOffset];
// TODO: persist on disk before going to the network
NSAssert(self.post, @"post is required");
[self updatePostBody];
@ -314,6 +314,25 @@ static NSString *const StateRestorationModifiedPostKey = @"modifiedPost";
});
}
- (NSString *)scrollOffsetKey {
NSString *key = [NSString stringWithFormat:@"ScrollOffset-%@", self.modifiedPost.path];
return key;
}
- (void)saveScrollOffset {
CGPoint scrollOffset = self.textView.contentOffset;
NSString *serializedOffset = NSStringFromCGPoint(scrollOffset);
[[NSUserDefaults standardUserDefaults] setObject:serializedOffset forKey:[self scrollOffsetKey]];
}
- (void)restoreScrollOffset {
NSString *serializedOffset = [[NSUserDefaults standardUserDefaults] objectForKey:[self scrollOffsetKey]];
CGPoint scrollOffset = serializedOffset ? CGPointFromString(serializedOffset) : CGPointZero;
dispatch_async(dispatch_get_main_queue(), ^{
[self.textView setContentOffset:scrollOffset animated:YES];
});
}
- (void)updatePostBody {
self.modifiedPost = [self.modifiedPost copyWithBody:self.textView.text];
[self configureSaveButton];