[ios] ShareActionSheet refucktored

This commit is contained in:
Igor Khmurets 2013-12-12 15:37:14 +03:00 committed by Alex Zolotarev
parent 03ed34b00d
commit fabf4df131
8 changed files with 130 additions and 116 deletions

View file

@ -8,7 +8,7 @@
namespace search { struct AddressInfo; }
namespace url_scheme { struct ApiPoint; }
@interface PlacePageVC : UITableViewController <UITextFieldDelegate, UIActionSheetDelegate, MFMailComposeViewControllerDelegate, MFMessageComposeViewControllerDelegate, UITextViewDelegate, UIGestureRecognizerDelegate, ColorPickerDelegate>
@interface PlacePageVC : UITableViewController <UITextFieldDelegate, UITextViewDelegate, UIGestureRecognizerDelegate, ColorPickerDelegate>
- (id) initWithInfo:(search::AddressInfo const &)info point:(CGPoint)point;
- (id) initWithApiPoint:(url_scheme::ApiPoint const &)apiPoint;

View file

@ -50,6 +50,8 @@ typedef enum {Editing, Saved} Mode;
@property (nonatomic, retain) PlaceAndCompasView * placeAndCompass;
@property (nonatomic, retain) UIView * pickerView;
@property (retain) ShareActionSheet * shareActionSheet;
@end
@implementation PlacePageVC
@ -299,23 +301,6 @@ typedef enum {Editing, Saved} Mode;
return NO;
}
- (void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex
{
[ShareActionSheet resolveActionSheetChoice:actionSheet buttonIndex:buttonIndex text:self.pinTitle view:self delegate:self gX:_pinGlobalPosition.x gY:_pinGlobalPosition.y andMyPosition:NO];
}
- (void)mailComposeController:(MailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
[[Statistics instance] logEvent:@"ge0(zero) MAIL Export"];
[self dismissModalViewControllerAnimated:YES];
}
-(void)messageComposeViewController:(MessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
[[Statistics instance] logEvent:@"ge0(zero) MESSAGE Export"];
[self dismissModalViewControllerAnimated:YES];
}
-(void)pushToNavigationControllerAndSetControllerToPopoverSize:(UIViewController *)vc
{
if (isIPad)
@ -386,9 +371,11 @@ typedef enum {Editing, Saved} Mode;
[self goToTheMap];
}
-(void)share
- (void)share
{
[ShareActionSheet showShareActionSheetInView:self.view withObject:self];
ShareInfo * info = [[ShareInfo alloc] initWithText:self.pinTitle gX:_pinGlobalPosition.x gY:_pinGlobalPosition.y myPosition:NO];
self.shareActionSheet = [[ShareActionSheet alloc] initWithInfo:info viewController:self];
[self.shareActionSheet show];
}
-(void)savePin

View file

@ -35,11 +35,12 @@
const long long PRO_IDL = 510623322L;
const long long LITE_IDL = 431183278L;
@interface MapViewController () <SideToolbarDelegate, UIActionSheetDelegate>
@interface MapViewController () <SideToolbarDelegate>
@property (nonatomic) UIView * fadeView;
@property (nonatomic) LocationButton * locationButton;
@property (nonatomic, strong) UINavigationBar * apiNavigationBar;
@property ShareActionSheet * shareActionSheet;
@end
@ -772,10 +773,20 @@ const long long LITE_IDL = 431183278L;
}
else if (buttonIndex == 4)
{
if ([MapsAppDelegate theApp].m_locationManager.lastLocation)
[ShareActionSheet showShareActionSheetInView:self.view withObject:self];
CLLocation * location = [MapsAppDelegate theApp].m_locationManager.lastLocation;
if (location)
{
double gX = MercatorBounds::LonToX(location.coordinate.longitude);
double gY = MercatorBounds::LatToY(location.coordinate.latitude);
ShareInfo * info = [[ShareInfo alloc] initWithText:nil gX:gX gY:gY myPosition:YES];
self.shareActionSheet = [[ShareActionSheet alloc] initWithInfo:info viewController:self];
[self.shareActionSheet show];
[self.sideToolbar setMenuHidden:YES animated:YES];
}
else
{
[[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"unknown_current_position", nil) message:nil delegate:nil cancelButtonTitle:NSLocalizedString(@"ok", nil) otherButtonTitles:nil] show];
}
}
}
@ -818,31 +829,6 @@ const long long LITE_IDL = 431183278L;
}
}
- (void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex
{
CLLocation * location = [MapsAppDelegate theApp].m_locationManager.lastLocation;
double gX = MercatorBounds::LonToX(location.coordinate.longitude);
double gY = MercatorBounds::LatToY(location.coordinate.latitude);
[ShareActionSheet resolveActionSheetChoice:actionSheet buttonIndex:buttonIndex text:@"" view:self delegate:self gX:gX gY:gY andMyPosition:YES];
}
- (void)mailComposeController:(MailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
[self proccessShareActionCallbackWithEventName:@"ge0(zero) MAIL Export"];
}
- (void)messageComposeViewController:(MessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
[self proccessShareActionCallbackWithEventName:@"ge0(zero) MESSAGE Export"];
}
- (void)proccessShareActionCallbackWithEventName:(NSString *)eventName
{
[[Statistics instance] logEvent:eventName];
[self dismissModalViewControllerAnimated:YES];
[self.sideToolbar setMenuHidden:YES animated:YES];
}
- (void)toolbarButtonPressed:(id)sender
{
[self.sideToolbar setMenuHidden:!self.sideToolbar.isMenuHidden animated:YES];

View file

@ -4,7 +4,7 @@
namespace search { struct AddressInfo; }
namespace url_scheme { struct ApiPoint; }
@interface PlacePreviewViewController : UITableViewController <UIActionSheetDelegate, UIGestureRecognizerDelegate>
@interface PlacePreviewViewController : UITableViewController <UIGestureRecognizerDelegate>
-(id)initWith:(search::AddressInfo const &)info point:(CGPoint)point;
-(id)initWithApiPoint:(url_scheme::ApiPoint const &)apiPoint;
-(id)initWithPoint:(CGPoint)point;

View file

@ -29,6 +29,8 @@ typedef enum {APIPOINT, POI, MYPOSITION} Type;
CGPoint m_point;
}
@property (nonatomic, retain) PlaceAndCompasView * placeAndCompass;
@property (retain) ShareActionSheet * shareActionSheet;
@end
@implementation PlacePreviewViewController
@ -186,12 +188,7 @@ typedef enum {APIPOINT, POI, MYPOSITION} Type;
return [self.tableView sectionHeaderHeight];
}
-(void)share
{
[ShareActionSheet showShareActionSheetInView:self.view withObject:self];
}
- (void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex
- (void)share
{
string name = "";
if (m_previewType == POI)
@ -199,7 +196,11 @@ typedef enum {APIPOINT, POI, MYPOSITION} Type;
else if (m_previewType == APIPOINT)
name = m_apiPoint.m_name;
BOOL const myPos = (m_previewType == MYPOSITION) ? YES : NO;
[ShareActionSheet resolveActionSheetChoice:actionSheet buttonIndex:buttonIndex text:[NSString stringWithUTF8String:name.c_str()] view:self delegate:self gX:m_point.x gY:m_point.y andMyPosition:myPos];
NSString * text = [NSString stringWithUTF8String:name.c_str()];
ShareInfo * info = [[ShareInfo alloc] initWithText:text gX:m_point.x gY:m_point.y myPosition:myPos];
self.shareActionSheet = [[ShareActionSheet alloc] initWithInfo:info viewController:self];
[self.shareActionSheet show];
}
-(void)addToBookmark
@ -231,18 +232,6 @@ typedef enum {APIPOINT, POI, MYPOSITION} Type;
}
}
- (void)mailComposeController:(MailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
[[Statistics instance] logEvent:@"ge0(zero) MAIL Export"];
[self dismissModalViewControllerAnimated:YES];
}
-(void)messageComposeViewController:(MessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
[[Statistics instance] logEvent:@"ge0(zero) MESSAGE Export"];
[self dismissModalViewControllerAnimated:YES];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView.tag == BALLOON_PROPOSAL_ALERT_VIEW)

View file

@ -1,9 +1,22 @@
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface ShareActionSheet : NSObject
+(void)showShareActionSheetInView:(id)view withObject:(id)del;
+(void)resolveActionSheetChoice:(UIActionSheet *)as buttonIndex:(NSInteger)buttonIndex text:(NSString *)text
view:(id)view delegate:(id)del gX:(double)gX gY:(double)gY
andMyPosition:(BOOL)myPos;
@interface ShareInfo : NSObject
- (instancetype)initWithText:(NSString *)text gX:(double)gX gY:(double)gY myPosition:(BOOL)myPosition;
@property NSString * text;
@property double gX;
@property double gY;
@property BOOL myPosition;
@end
@interface ShareActionSheet : NSObject
- (instancetype)initWithInfo:(ShareInfo *)info viewController:(UIViewController *)viewController;
- (void)show;
@end

View file

@ -2,15 +2,48 @@
#import "Framework.h"
#import "MessageComposeViewController.h"
#import "MailComposeViewController.h"
#import "Statistics.h"
#include "../../search/result.hpp"
#define GE0LENGTH 16
@implementation ShareInfo
- (instancetype)initWithText:(NSString *)text gX:(double)gX gY:(double)gY myPosition:(BOOL)myPosition
{
self = [super init];
self.text = text ? text : @"";
self.gX = gX;
self.gY = gY;
self.myPosition = myPosition;
return self;
}
@end
@interface ShareActionSheet () <UIActionSheetDelegate, MFMailComposeViewControllerDelegate, MFMessageComposeViewControllerDelegate>
@property ShareInfo * info;
@property (weak) UIViewController * viewController;
@end
@implementation ShareActionSheet
+(void)showShareActionSheetInView:(id)view withObject:(id)del
- (instancetype)initWithInfo:(ShareInfo *)info viewController:(UIViewController *)viewController
{
UIActionSheet * as = [[UIActionSheet alloc] initWithTitle:NSLocalizedString(@"share", nil) delegate:del cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
self = [super init];
self.info = info;
self.viewController = viewController;
return self;
}
- (void)show
{
UIActionSheet * as = [[UIActionSheet alloc] initWithTitle:NSLocalizedString(@"share", nil) delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
if ([MessageComposeViewController canSendText])
[as addButtonWithTitle:NSLocalizedString(@"message", nil)];
@ -19,64 +52,57 @@
[as addButtonWithTitle:NSLocalizedString(@"copy_link", nil)];
[as addButtonWithTitle:NSLocalizedString(@"cancel", nil)];
[as setCancelButtonIndex:as.numberOfButtons - 1];
[as showInView:view];
[as release];
[as showInView:self.viewController.view];
}
+ (BOOL)canUseGmailApp
#define GE0_URL_LENGTH 16
- (void)actionSheet:(UIActionSheet *)as willDismissWithButtonIndex:(NSInteger)buttonIndex
{
Framework & f = GetFramework();
string const s = f.CodeGe0url(MercatorBounds::YToLat(self.info.gY), MercatorBounds::XToLon(self.info.gX), f.GetDrawScale(), [self.info.text UTF8String]);
NSString * shortUrl = [NSString stringWithUTF8String:s.c_str()];
if ([[as buttonTitleAtIndex:buttonIndex] isEqualToString:NSLocalizedString(@"email", nil)])
[self sendEmailWithUrl:shortUrl];
else if ([[as buttonTitleAtIndex:buttonIndex] isEqualToString:NSLocalizedString(@"message", nil)])
[self sendMessageWithUrl:[shortUrl substringWithRange:NSMakeRange(0, GE0_URL_LENGTH)]];
else if ([[as buttonTitleAtIndex:buttonIndex] isEqualToString:NSLocalizedString(@"copy_link", nil)])
[UIPasteboard generalPasteboard].string = shortUrl;
}
- (BOOL)canUseGmailApp
{
// This is not official google api
return [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"googlegmail://"]];
}
+(void)resolveActionSheetChoice:(UIActionSheet *)as buttonIndex:(NSInteger)buttonIndex text:(NSString *)text
view:(id)view delegate:(id)del gX:(double)gX gY:(double)gY
andMyPosition:(BOOL)myPos
- (void)sendEmailWithUrl:(NSString *)shortUrl
{
NSString * shortUrl = [self generateShortUrlWithName:text gX:gX gY:gY];
if ([[as buttonTitleAtIndex:buttonIndex] isEqualToString:NSLocalizedString(@"email", nil)])
[self sendEmailWith:text andUrl:shortUrl view:view delegate:del gX:gX gY:gY myPosition:myPos];
else if ([[as buttonTitleAtIndex:buttonIndex] isEqualToString:NSLocalizedString(@"message", nil)])
[self sendMessageWithUrl:[shortUrl substringWithRange:NSMakeRange(0, GE0LENGTH)] view:view delegate:del myPosition:myPos];
else if ([[as buttonTitleAtIndex:buttonIndex] isEqualToString:NSLocalizedString(@"copy_link", nil)])
[UIPasteboard generalPasteboard].string = [self generateShortUrlWithName:text gX:gX gY:gY];
}
+(NSString *)generateShortUrlWithName:(NSString *)name gX:(double)gX gY:(double)gY
{
Framework & f = GetFramework();
string const s = f.CodeGe0url(MercatorBounds::YToLat(gY), MercatorBounds::XToLon(gX),
f.GetDrawScale(), [(name ? name : @"") UTF8String]);
return [NSString stringWithUTF8String:s.c_str()];
}
+(void)sendEmailWith:(NSString *)textFieldText andUrl:(NSString *)shortUrl view:(id)view delegate:(id)del gX:(double)gX gY:(double)gY myPosition:(BOOL)myPos
{
NSString * subject;
NSString * body;
NSString * httpGe0Url = [shortUrl stringByReplacingCharactersInRange:NSMakeRange(0, 6) withString:@"http://ge0.me/"];
if (myPos)
if (self.info.myPosition)
{
search::AddressInfo info;
GetFramework().GetAddressInfoForGlobalPoint(m2::PointD(gX, gY), info);
GetFramework().GetAddressInfoForGlobalPoint(m2::PointD(self.info.gX, self.info.gY), info);
NSString * nameAndAddress = [NSString stringWithUTF8String:info.FormatNameAndAddress().c_str()];
body = [NSString stringWithFormat:NSLocalizedString(@"my_position_share_email", nil), nameAndAddress, shortUrl, httpGe0Url];
subject = NSLocalizedString(@"my_position_share_email_subject", nil);
}
else
{
body = [NSString stringWithFormat:NSLocalizedString(@"bookmark_share_email", nil), textFieldText, shortUrl, httpGe0Url];
body = [NSString stringWithFormat:NSLocalizedString(@"bookmark_share_email", nil), self.info.text, shortUrl, httpGe0Url];
subject = NSLocalizedString(@"bookmark_share_email_subject", nil);
}
if ([MailComposeViewController canSendMail])
{
MailComposeViewController * mailVC = [[[MailComposeViewController alloc] init] autorelease];
MailComposeViewController * mailVC = [[MailComposeViewController alloc] init];
[mailVC setMessageBody:body isHTML:NO];
[mailVC setSubject:subject];
mailVC.mailComposeDelegate = del;
[view presentModalViewController:mailVC animated:YES];
mailVC.mailComposeDelegate = self;
[self.viewController presentModalViewController:mailVC animated:YES];
}
else if ([self canUseGmailApp])
{
@ -87,17 +113,30 @@
}
}
+(void)sendMessageWithUrl:(NSString *)shortUrl view:(id)view delegate:(id)del myPosition:(BOOL)myPos
- (void)sendMessageWithUrl:(NSString *)shortUrl
{
NSString * httpGe0Url = [shortUrl stringByReplacingCharactersInRange:NSMakeRange(0, 6) withString:@"http://ge0.me/"];
MessageComposeViewController * messageVC = [[[MessageComposeViewController alloc] init] autorelease];
if (myPos)
MessageComposeViewController * messageVC = [[MessageComposeViewController alloc] init];
if (self.info.myPosition)
[messageVC setBody:[NSString stringWithFormat:NSLocalizedString(@"my_position_share_sms", nil), shortUrl, httpGe0Url]];
else
[messageVC setBody:[NSString stringWithFormat:NSLocalizedString(@"bookmark_share_sms", nil), shortUrl, httpGe0Url]];
messageVC.messageComposeDelegate = del;
[view presentModalViewController:messageVC animated:YES];
messageVC.messageComposeDelegate = self;
[self.viewController presentModalViewController:messageVC animated:YES];
}
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
[self.viewController dismissModalViewControllerAnimated:YES];
if (result == MessageComposeResultSent)
[[Statistics instance] logEvent:@"ge0(zero) MESSAGE Export"];
}
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
[self.viewController dismissModalViewControllerAnimated:YES];
if (result == MFMailComposeResultSent || result == MFMailComposeResultSaved)
[[Statistics instance] logEvent:@"ge0(zero) MAIL Export"];
}
@end

View file

@ -105,8 +105,8 @@
EDB811A4175E1A9C00E36BF2 /* TwoButtonsView.m in Sources */ = {isa = PBXBuildFile; fileRef = EDB811A2175E1A9C00E36BF2 /* TwoButtonsView.m */; };
EDBB18B816972B0600AF0742 /* libzlib.a in Frameworks */ = {isa = PBXBuildFile; fileRef = EDBB18B716972B0600AF0742 /* libzlib.a */; };
EDBB18B916972B3000AF0742 /* libzlib.a in Frameworks */ = {isa = PBXBuildFile; fileRef = EDBB18B716972B0600AF0742 /* libzlib.a */; };
EDC5C543175F2CA600420E92 /* ShareActionSheet.mm in Sources */ = {isa = PBXBuildFile; fileRef = EDC5C542175F2CA600420E92 /* ShareActionSheet.mm */; };
EDC5C544175F2CA600420E92 /* ShareActionSheet.mm in Sources */ = {isa = PBXBuildFile; fileRef = EDC5C542175F2CA600420E92 /* ShareActionSheet.mm */; };
EDC5C543175F2CA600420E92 /* ShareActionSheet.mm in Sources */ = {isa = PBXBuildFile; fileRef = EDC5C542175F2CA600420E92 /* ShareActionSheet.mm */; settings = {COMPILER_FLAGS = "-fobjc-arc"; }; };
EDC5C544175F2CA600420E92 /* ShareActionSheet.mm in Sources */ = {isa = PBXBuildFile; fileRef = EDC5C542175F2CA600420E92 /* ShareActionSheet.mm */; settings = {COMPILER_FLAGS = "-fobjc-arc"; }; };
EDCB4E8E175E67120005AA35 /* PlacePreviewViewController.mm in Sources */ = {isa = PBXBuildFile; fileRef = EDCB4E8D175E67110005AA35 /* PlacePreviewViewController.mm */; };
EDCB4E8F175E67120005AA35 /* PlacePreviewViewController.mm in Sources */ = {isa = PBXBuildFile; fileRef = EDCB4E8D175E67110005AA35 /* PlacePreviewViewController.mm */; };
EDF924DB183380CE00539672 /* FacebookSDK.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = EDF924DA183380CE00539672 /* FacebookSDK.framework */; };