diff --git a/Blog/Base.lproj/Main.storyboard b/Blog/Base.lproj/Main.storyboard index 27cdf83..04efa97 100644 --- a/Blog/Base.lproj/Main.storyboard +++ b/Blog/Base.lproj/Main.storyboard @@ -58,7 +58,7 @@ - + Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda. @@ -68,36 +68,36 @@ + - + + - - - - + - + - - - - - - - - - - + + + + + + @@ -107,6 +107,7 @@ + diff --git a/Blog/EditorViewController.m b/Blog/EditorViewController.m index 0e11609..0ece4af 100644 --- a/Blog/EditorViewController.m +++ b/Blog/EditorViewController.m @@ -17,6 +17,8 @@ @property (nonatomic, weak) IBOutlet UILabel *titleView; @property (nonatomic, weak) IBOutlet UITextView *textView; +@property (nonatomic, weak) IBOutlet NSLayoutConstraint *textViewTopConstraint; +@property (nonatomic, weak) IBOutlet UILabel *linkLabel; @property (nonatomic, weak) IBOutlet UIToolbar *toolbar; @property (nonatomic, weak) IBOutlet UIBarButtonItem *publishBarButtonItem; @property (strong, nonatomic) Post *modifiedPost; @@ -37,7 +39,38 @@ } - (void)configureView { - NSString *title = nil; + [self configureTitleView]; + [self configureLinkView]; + [self configureBodyView]; + [self configureToolbar]; +} + +- (void)configureTitleView { + self.titleView.text = self.modifiedPost.title.length ? self.modifiedPost.title : @"Untitled"; + [self.titleView sizeToFit]; +} + +- (void)configureLinkView { + NSURL *url = self.modifiedPost.url; + if (url) { + self.linkLabel.text = url.absoluteString; + self.linkLabel.alpha = 0; + [UIView animateWithDuration:0.3 animations:^{ + self.linkLabel.alpha = 1; + self.textViewTopConstraint.constant = CGRectGetMaxY(self.linkLabel.frame) + 4; + }]; + } + else { + [UIView animateWithDuration:0.3 animations:^{ + self.linkLabel.alpha = 0; + self.textViewTopConstraint.constant = 0; + } completion:^(BOOL finished) { + self.linkLabel.text = nil; + }]; + } +} + +- (void)configureBodyView { NSString *body = nil; CGPoint scrollOffset = CGPointZero; Post *post = self.modifiedPost; @@ -46,23 +79,18 @@ body = post.body; // TODO: restore scroll offset for this post ... user defaults? } - [self configureTitleView]; self.textView.text = body; self.textView.contentOffset = scrollOffset; - // TODO: url +} - BOOL toolbarEnabled = post != nil; +- (void)configureToolbar { + BOOL toolbarEnabled = self.modifiedPost != nil; [self.toolbar.items enumerateObjectsUsingBlock:^(UIBarButtonItem *item, NSUInteger idx, BOOL *stop) { item.enabled = toolbarEnabled; }]; self.publishBarButtonItem.title = self.modifiedPost.draft ? @"Publish" : @"Unpublish"; } -- (void)configureTitleView { - self.titleView.text = self.modifiedPost.title.length ? self.modifiedPost.title : @"Untitled"; - [self.titleView sizeToFit]; -} - - (void)viewDidLoad { [super viewDidLoad]; [self configureView]; @@ -75,8 +103,8 @@ - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; - [self savePost]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillResignActiveNotification object:nil]; + [self savePost]; } - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { @@ -151,6 +179,7 @@ - (void)updatePostURL:(NSURL *)url { self.modifiedPost = [self.modifiedPost copyWithURL:url]; + [self configureLinkView]; } - (IBAction)publishOrUnpublish:(id)sender { @@ -189,7 +218,9 @@ presentationController.sourceView = self.view; presentationController.sourceRect = CGRectMake(CGRectGetWidth(self.view.bounds) / 2, 0, 1, 1); presentationController.permittedArrowDirections = UIPopoverArrowDirectionUp; + __weak __typeof__(changeTitleViewController) weakChangeTitleViewController = changeTitleViewController; changeTitleViewController.dismissBlock = ^{ + __typeof__(changeTitleViewController) changeTitleViewController = weakChangeTitleViewController; NSString *title = changeTitleViewController.articleTitle; [self updatePostTitle:title]; [self dismissViewControllerAnimated:YES completion:nil]; @@ -197,14 +228,94 @@ [self presentViewController:changeTitleViewController animated:YES completion:nil]; } +#pragma mark - UIPopoverPresentationControllerDelegate methods + - (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller { return UIModalPresentationNone; } - (void)popoverPresentationControllerDidDismissPopover:(UIPopoverPresentationController *)popoverPresentationController { - ChangeTitleViewController *changeTitleViewController = (ChangeTitleViewController *)popoverPresentationController.presentedViewController; - NSString *title = changeTitleViewController.articleTitle; - [self updatePostTitle:title]; + UIViewController *dismissedVC = popoverPresentationController.presentedViewController; + if ([dismissedVC isKindOfClass:[ChangeTitleViewController class]]) { + ChangeTitleViewController *changeTitleViewController = (ChangeTitleViewController *)dismissedVC; + NSString *title = changeTitleViewController.articleTitle; + [self updatePostTitle:title]; + } +} + +#pragma mark - Alerts + +- (void)showAlertWithTitle:(NSString *)title message:(NSString *)message { + UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert]; + __weak __typeof__(self) welf = self; + [alertController addAction:[UIAlertAction actionWithTitle:@"Dismiss" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { + __typeof__(self) self = welf; + [self dismissViewControllerAnimated:YES completion:nil]; + }]]; + [self presentViewController:alertController animated:YES completion:nil]; +} + +#pragma mark - Link management + +- (IBAction)presentLinkActionSheet:(id)sender { + if (self.presentedViewController) { + return; + } + + NSURL *currentURL = self.modifiedPost.url; + + // If nothing to add or remove, then don't present the action sheet. + if (!currentURL && ![self pasteboardHasLink]) { + [self showAlertWithTitle:@"No Link Found" message:@"Copy a link to the pasteboard to add it."]; + return; + } + + UIAlertController *menuController = [UIAlertController alertControllerWithTitle:nil message:currentURL.absoluteString preferredStyle:UIAlertControllerStyleActionSheet]; + + // Cancel + [menuController addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { + [self dismissViewControllerAnimated:YES completion:nil]; + }]]; + + // Add/Replace from Pasteboard + NSString *addVerb = self.modifiedPost.link ? @"Replace" : @"Add"; + if ([self pasteboardHasLink]) { + NSString *title = [NSString stringWithFormat:@"%@ from Pasteboard", addVerb]; + __weak __typeof__(self) welf = self; + [menuController addAction:[UIAlertAction actionWithTitle:title style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { + __typeof__(self) self = welf; + [self dismissViewControllerAnimated:YES completion:^{ + [self addLinkFromPasteboard]; + }]; + }]]; + } + + // Remove Link + if (currentURL) { + __weak __typeof__(self) welf = self; + [menuController addAction:[UIAlertAction actionWithTitle:@"Remove Link" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) { + __typeof__(self) self = welf; + [self dismissViewControllerAnimated:YES completion:^{ + [self updatePostURL:nil]; + }]; + }]]; + } + + [self presentViewController:menuController animated:YES completion:nil]; +} + +- (BOOL)pasteboardHasLink { + return [UIPasteboard generalPasteboard].URL != nil; +} + +- (void)addLinkFromPasteboard { + NSURL *pasteboardURL = [UIPasteboard generalPasteboard].URL; + if (pasteboardURL) { + [self updatePostURL:pasteboardURL]; + } + else { + [self showAlertWithTitle:@"Error" message:@"No link found on pasteboard"]; + } } @end