mirror of
https://github.com/samsonjs/samhuri.net-ios.git
synced 2026-04-21 13:55:53 +00:00
add a save button and indicator
This commit is contained in:
parent
e24cbd9aac
commit
df1d329904
2 changed files with 48 additions and 2 deletions
|
|
@ -49,6 +49,12 @@
|
|||
<action selector="publishOrUnpublish:" destination="JEX-9P-axG" id="Vxe-Fp-IDn"/>
|
||||
</connections>
|
||||
</barButtonItem>
|
||||
<barButtonItem style="plain" systemItem="flexibleSpace" id="gv8-wH-bFT"/>
|
||||
<barButtonItem systemItem="save" id="7iR-uF-xqE">
|
||||
<connections>
|
||||
<action selector="save:" destination="JEX-9P-axG" id="gnN-zi-lxN"/>
|
||||
</connections>
|
||||
</barButtonItem>
|
||||
<barButtonItem style="plain" systemItem="flexibleSpace" id="dZ8-jR-S40"/>
|
||||
<barButtonItem title="Preview" id="uGx-Jy-rOz">
|
||||
<connections>
|
||||
|
|
@ -148,6 +154,7 @@
|
|||
<outlet property="linkView" destination="D5w-s5-7oj" id="iRD-md-Gea"/>
|
||||
<outlet property="publishBarButtonItem" destination="qEb-VA-ueB" id="biG-Yd-W5Z"/>
|
||||
<outlet property="removeLinkButton" destination="81X-Pe-PFV" id="Rx0-IH-fzp"/>
|
||||
<outlet property="saveBarButtonItem" destination="7iR-uF-xqE" id="8A5-PY-EuS"/>
|
||||
<outlet property="textView" destination="wrG-1y-ZY3" id="lvo-lm-t7Z"/>
|
||||
<outlet property="textViewTopConstraint" destination="tUU-ig-gJV" id="Wzj-Rc-kuM"/>
|
||||
<outlet property="toolbar" destination="YUD-Xe-6Cc" id="SIv-cT-WDD"/>
|
||||
|
|
|
|||
|
|
@ -25,6 +25,7 @@
|
|||
@property (nonatomic, weak) IBOutlet UIButton *removeLinkButton;
|
||||
@property (nonatomic, weak) IBOutlet UIToolbar *toolbar;
|
||||
@property (nonatomic, weak) IBOutlet UIBarButtonItem *publishBarButtonItem;
|
||||
@property (nonatomic, weak) IBOutlet UIBarButtonItem *saveBarButtonItem;
|
||||
@property (nonatomic, strong) Post *modifiedPost;
|
||||
@property (nonatomic, readonly, assign, getter=isDirty) BOOL dirty;
|
||||
@property (nonatomic, strong) PMKPromise *savePromise;
|
||||
|
|
@ -146,6 +147,13 @@
|
|||
item.enabled = toolbarEnabled;
|
||||
}];
|
||||
self.publishBarButtonItem.title = self.modifiedPost.draft ? @"Publish" : @"Unpublish";
|
||||
[self configureSaveButton];
|
||||
}
|
||||
|
||||
- (void)configureSaveButton {
|
||||
self.saveBarButtonItem.enabled = self.dirty;
|
||||
self.saveBarButtonItem.title = self.dirty ? @"Save" : nil;
|
||||
[self.toolbar setItems:self.toolbar.items animated:YES];
|
||||
}
|
||||
|
||||
- (void)viewDidLoad {
|
||||
|
|
@ -156,7 +164,8 @@
|
|||
|
||||
- (void)viewWillAppear:(BOOL)animated {
|
||||
[super viewWillAppear:animated];
|
||||
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(savePost) name:UIApplicationWillResignActiveNotification object:nil];
|
||||
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
|
||||
[notificationCenter addObserver:self selector:@selector(applicationWillResignActive:) name:UIApplicationWillResignActiveNotification object:nil];
|
||||
if ([self pasteboardHasLink]) {
|
||||
[self configureLinkView];
|
||||
}
|
||||
|
|
@ -164,7 +173,8 @@
|
|||
|
||||
- (void)viewWillDisappear:(BOOL)animated {
|
||||
[super viewWillDisappear:animated];
|
||||
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillResignActiveNotification object:nil];
|
||||
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
|
||||
[notificationCenter removeObserver:self name:UIApplicationWillResignActiveNotification object:nil];
|
||||
[self savePost];
|
||||
}
|
||||
|
||||
|
|
@ -178,6 +188,13 @@
|
|||
}
|
||||
}
|
||||
|
||||
#pragma mark - Notification handlers
|
||||
|
||||
- (void)applicationWillResignActive:(NSNotification *)note {
|
||||
[self savePost];
|
||||
}
|
||||
#pragma mark -
|
||||
|
||||
- (BOOL)isDirty;
|
||||
{
|
||||
return self.modifiedPost.new || ![self.modifiedPost isEqualToPost:self.post];
|
||||
|
|
@ -208,6 +225,15 @@
|
|||
savePromise = [self.blogController requestUpdatePost:newPost];
|
||||
}
|
||||
self.savePromise = savePromise;
|
||||
|
||||
UIActivityIndicatorView *indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
|
||||
[indicatorView startAnimating];
|
||||
UIBarButtonItem *indicatorItem = [[UIBarButtonItem alloc] initWithCustomView:indicatorView];
|
||||
NSMutableArray *items = [self.toolbar.items mutableCopy];
|
||||
UIBarButtonItem *saveItem = self.saveBarButtonItem; // get a strong reference since the property is weak and we're removing it
|
||||
[items replaceObjectAtIndex:[items indexOfObject:saveItem] withObject:indicatorItem];
|
||||
[self.toolbar setItems:items animated:NO];
|
||||
|
||||
return savePromise.then(^{
|
||||
NSLog(@"%@ post at path %@", verb, path);
|
||||
|
||||
|
|
@ -231,11 +257,14 @@
|
|||
return error;
|
||||
}).finally(^{
|
||||
self.savePromise = nil;
|
||||
[items replaceObjectAtIndex:[items indexOfObject:indicatorItem] withObject:saveItem];
|
||||
[self.toolbar setItems:items animated:NO];
|
||||
});
|
||||
}
|
||||
|
||||
- (void)updatePostBody {
|
||||
self.modifiedPost = [self.modifiedPost copyWithBody:self.textView.text];
|
||||
[self configureSaveButton];
|
||||
}
|
||||
|
||||
- (void)updatePostTitle:(NSString *)title {
|
||||
|
|
@ -270,6 +299,10 @@
|
|||
});
|
||||
}
|
||||
|
||||
- (IBAction)save:(id)sender {
|
||||
[self savePost];
|
||||
}
|
||||
|
||||
- (IBAction)presentChangeTitle:(id)sender {
|
||||
if (self.presentedViewController) {
|
||||
return;
|
||||
|
|
@ -355,4 +388,10 @@
|
|||
}
|
||||
}
|
||||
|
||||
#pragma mark - UITextViewDelegate methods
|
||||
|
||||
- (void)textViewDidChange:(UITextView *)textView {
|
||||
[self updatePostBody];
|
||||
}
|
||||
|
||||
@end
|
||||
|
|
|
|||
Loading…
Reference in a new issue