fix saving posts, and update save button whenever the dirty status changes

This commit is contained in:
Sami Samhuri 2015-05-09 16:18:58 -07:00
parent 5d8dc3210a
commit 6220d04a91

View file

@ -253,6 +253,11 @@ static NSString *const StateRestorationModifiedPostKey = @"modifiedPost";
return self.post && (self.modifiedPost.new || ![self.modifiedPost isEqualToPost:self.post]); return self.post && (self.modifiedPost.new || ![self.modifiedPost isEqualToPost:self.post]);
} }
- (void)setModifiedPost:(Post *)modifiedPost {
_modifiedPost = modifiedPost;
[self configureSaveButton];
}
- (PMKPromise *)savePost { - (PMKPromise *)savePost {
if (self.savePromise) { if (self.savePromise) {
return self.savePromise; return self.savePromise;
@ -267,17 +272,17 @@ static NSString *const StateRestorationModifiedPostKey = @"modifiedPost";
self.textView.editable = NO; self.textView.editable = NO;
Post *newPost = self.modifiedPost; Post *modifiedPost = self.modifiedPost;
NSString *path = newPost.path; NSString *path = modifiedPost.path;
PMKPromise *savePromise; PMKPromise *savePromise;
NSString *verb; NSString *verb;
if (newPost.new) { if (modifiedPost.new) {
verb = @"create"; verb = @"create";
savePromise = [self.blogController requestCreateDraft:newPost publishImmediately:NO]; savePromise = [self.blogController requestCreateDraft:modifiedPost publishImmediately:NO];
} }
else { else {
verb = @"update"; verb = @"update";
savePromise = [self.blogController requestUpdatePost:newPost]; savePromise = [self.blogController requestUpdatePost:modifiedPost];
} }
self.savePromise = savePromise; self.savePromise = savePromise;
@ -289,14 +294,14 @@ static NSString *const StateRestorationModifiedPostKey = @"modifiedPost";
[items replaceObjectAtIndex:[items indexOfObject:saveItem] withObject:indicatorItem]; [items replaceObjectAtIndex:[items indexOfObject:saveItem] withObject:indicatorItem];
[self.toolbar setItems:items animated:NO]; [self.toolbar setItems:items animated:NO];
return savePromise.then(^{
__weak typeof(self) welf = self; __weak typeof(self) welf = self;
return savePromise.then(^(Post *savedPost) {
typeof(self) self = welf; typeof(self) self = welf;
NSLog(@"%@ post at path %@", verb, path); NSLog(@"%@ post at path %@", verb, path);
// update our post because "new" may have changed, which is essential to correct operation // update our post because "new" may have changed, which is essential to correct operation
[self configureWithPost:newPost]; [self configureWithPost:savedPost];
return newPost; return savedPost;
}).catch(^(NSError *error) { }).catch(^(NSError *error) {
NSLog(@"Failed to %@ post at path %@: %@ %@", verb, path, error.localizedDescription, error.userInfo); NSLog(@"Failed to %@ post at path %@: %@ %@", verb, path, error.localizedDescription, error.userInfo);
return error; return error;