sage-iphone-app / Classes / ShareButtonDelegate.m

//
//  ShareButtonDelegate.m
//  iSage
//
//  Created by Ivan Andrus on 10/27/12.
//  Copyright (c) 2012 Sagemath. All rights reserved.
//

#import "ShareButtonDelegate.h"

@implementation ShareButtonDelegate

@synthesize cell;

-(ShareButtonDelegate*)init
{
    self = [super init];
    if (self) {
        isPhone = (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone);
    }
    return self;
}

-(void)displayShareSheet:(UIViewController*)controller forCell:(AlephCell*)newCell fromWhence:(id)origin
{
    if (viewController) {
        NSLog(@"I shouldn't have to release this controller.");
        [viewController release];
    }
    if (cell) {
        NSLog(@"I shouldn't have to release this cell.");
        [cell release];
    }
    cell = [newCell retain];
    viewController = [controller retain];

    if (!actionSheet) {

        actionSheet = [[UIActionSheet alloc] initWithTitle:@"Share"
                                                  delegate:self
                                         cancelButtonTitle:(isPhone ? @"Cancel" : nil)
                                    destructiveButtonTitle:nil
                                         otherButtonTitles:@"Duplicate",
                       @"Copy Output",
                       @"Copy Input and Output",
                       // http://stackoverflow.com/questions/5349139/how-to-send-an-email-through-ios-simulator
                       ([MFMailComposeViewController canSendMail] ? @"Send Email" : nil),
                       ([MFMailComposeViewController canSendMail] ? @"Report Bug" : nil),
                       nil];
    }

    if ( isPhone ) {
        [actionSheet showInView:[UIApplication sharedApplication].keyWindow];
    } else if ( [origin isKindOfClass:[UIBarButtonItem class]] ) {
        [actionSheet showFromBarButtonItem:origin animated:YES];
    } else if ( [origin isKindOfClass:[UIView class]] ) {
        //        [actionSheet showFromBarButtonItem:origin animated:YES];
        UIView *superview = [origin superview];
        [actionSheet showFromRect:[superview convertRect:[origin bounds] fromView:origin]
                           inView:superview animated:YES];
    }
}

#pragma mark -
#pragma mark - UIActionSheetDelegate

- (void)actionSheet:(UIActionSheet *)as clickedButtonAtIndex:(NSInteger)buttonIndex
{
    BOOL releaseViewController = YES;
    if (cell != nil && buttonIndex != [as cancelButtonIndex]) {
        UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
        switch (buttonIndex) {

            case 0: // duplicate
                [viewController performSelector:@selector(newCellWithDefault:)
                                     withObject:cell.input
                                     afterDelay:0];
                break;


            case 1: // copy output
                pasteboard.string = cell.output;
                if (cell.image) {
                    [pasteboard addItems:[NSArray arrayWithObject:
                                          [NSDictionary dictionaryWithObject:cell.image
                                                                      forKey:@"public.png"]]];
                }
                break;

            case 2: // copy both
                pasteboard.string = [NSString stringWithFormat:@"%@\n\n%@",
                                     cell.input,
                                     cell.output];
                if (cell.image) {
                    [pasteboard addItems:[NSArray arrayWithObject:
                                          [NSDictionary dictionaryWithObject:cell.image
                                                                      forKey:@"public.png"]]];
                }
                break;

            case 3:  // email
            case 4:  // report bug
            {
                MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
                picker.mailComposeDelegate = self;

                if (buttonIndex == 4) { // Report Bug
                    [picker setSubject:[NSString stringWithFormat:@"Bug in Sage on my %@",
                                        [[UIDevice currentDevice] name]]];
                    [picker setToRecipients:[NSArray arrayWithObject:@"sage-support@googlegroups.com"]];
                } else {
                    [picker setSubject:[NSString stringWithFormat:@"Sage on my %@",
                                        [[UIDevice currentDevice] name]]];
                }

                NSString *emailBody =[NSString stringWithFormat:@"<html><style>.sagecell_messages {display:%@;}</style><pre>%@</pre>\n\n<div>%@</div></html>",
                                      ((buttonIndex == 4) ? @"block" : @"none"),
                                      cell.input,
                                      cell.html_output];
                [picker setMessageBody:emailBody isHTML:YES];
                [viewController presentModalViewController:picker animated:YES];
                [picker release];
                releaseViewController = NO;
            }
                break;

            default:
                break;
        }
    }
    [cell release];
    cell = nil;
    // We might still need the viewController for email composer
    if (releaseViewController) {
        [viewController release];
        viewController = nil;
    }
}

#pragma mark -
#pragma mark MFMailComposeViewControllerDelegate

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
    // Notifies users about errors associated with the interface
    switch (result) {
        case MFMailComposeResultCancelled:
            NSLog(@"Mail canceled");
            break;
        case MFMailComposeResultSaved:
            NSLog(@"Mail saved");
            break;
        case MFMailComposeResultSent:
            NSLog(@"Mail sent");
            break;
        case MFMailComposeResultFailed:
            NSLog(@"Mail failed: %@",[error localizedDescription]);
            break;
        default:
            NSLog(@"Mail not sent");
            break;
    }
    [viewController dismissModalViewControllerAnimated:YES];
    [viewController release];
    viewController = nil;
}

@end
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.