From 97fde6bce3c9e56b8bfe33a7298eb2f6d4e8bf4f Mon Sep 17 00:00:00 2001 From: Kirill Zhdanovich Date: Tue, 28 May 2013 23:44:09 +0300 Subject: [PATCH] [iOS] New pin color picker --- iphone/Maps/Bookmarks/PlacePageVC.h | 5 +- iphone/Maps/Bookmarks/PlacePageVC.mm | 253 ++++++++++++++++++++- iphone/Maps/Bookmarks/SelectColorVC.h | 13 -- iphone/Maps/Bookmarks/SelectColorVC.mm | 69 ------ iphone/Maps/Maps.xcodeproj/project.pbxproj | 8 - 5 files changed, 252 insertions(+), 96 deletions(-) delete mode 100644 iphone/Maps/Bookmarks/SelectColorVC.h delete mode 100644 iphone/Maps/Bookmarks/SelectColorVC.mm diff --git a/iphone/Maps/Bookmarks/PlacePageVC.h b/iphone/Maps/Bookmarks/PlacePageVC.h index cfa9027d88..b0e05912ba 100644 --- a/iphone/Maps/Bookmarks/PlacePageVC.h +++ b/iphone/Maps/Bookmarks/PlacePageVC.h @@ -4,15 +4,18 @@ @class BalloonView; -@interface PlacePageVC : UITableViewController +@interface PlacePageVC : UITableViewController { BOOL m_hideNavBar; // @TODO store as a property to retain reference BalloonView * m_balloon; // If YES, pin should be removed when closing this dialog BOOL m_removePinOnClose; + int selectedRow; } +@property (nonatomic, retain) NSArray * pinsArray; + - (id) initWithBalloonView:(BalloonView *)view; @end diff --git a/iphone/Maps/Bookmarks/PlacePageVC.mm b/iphone/Maps/Bookmarks/PlacePageVC.mm index 0a82c5658a..fc65bf6000 100644 --- a/iphone/Maps/Bookmarks/PlacePageVC.mm +++ b/iphone/Maps/Bookmarks/PlacePageVC.mm @@ -1,16 +1,93 @@ #import "PlacePageVC.h" #import "BalloonView.h" #import "SelectSetVC.h" -#import "SelectColorVC.h" #import "EditDescriptionVC.h" #import "Statistics.h" #import "MapsAppDelegate.h" #import "MapViewController.h" +@interface PinPickerView : UIView + +- (id)initWithImage:(UIImage *)image; + ++ (CGFloat)viewWidth; ++ (CGFloat)viewHeight; + +@end + +@interface PinPickerView () +@property (nonatomic, retain) UILabel * titleLabel; +@end + +@implementation PinPickerView + +CGFloat const pWidth = 200; +CGFloat const pHeight = 36; +CGFloat const pMargin = 10; + ++ (CGFloat)viewWidth +{ + return pWidth; +} + ++ (CGFloat)viewHeight +{ + return pHeight; +} + +- (id)initWithImage:(UIImage *)image +{ + self = [super initWithFrame:CGRectMake(0.0, 0.0, pWidth, pHeight)]; + if (self) + { + CGFloat y = (self.bounds.size.height - image.size.height) / 2; + UIImageView * imageView = [[UIImageView alloc] initWithFrame: + CGRectMake(pMargin, + y, + image.size.width, + image.size.height)]; + imageView.image = image; + [self addSubview:imageView]; + [imageView release]; + } + return self; +} + +// Enable accessibility for this view. +- (BOOL)isAccessibilityElement +{ + return YES; +} + +// Return a string that describes this view. +- (NSString *)accessibilityLabel +{ + return self.titleLabel.text; +} + +@end + #define TEXTFIELD_TAG 999 +static NSString * const g_colors [] = +{ + @"placemark-red", + @"placemark-blue", + @"placemark-brown", + @"placemark-green", + @"placemark-orange", + @"placemark-pink", + @"placemark-purple" +}; + +@interface PlacePageVC() +@property (nonatomic, retain) UIView * viewWithPicker; +@end + @implementation PlacePageVC +@synthesize pinsArray, viewWithPicker; + - (id) initWithBalloonView:(BalloonView *)view { self = [super initWithStyle:UITableViewStyleGrouped]; @@ -19,10 +96,29 @@ m_balloon = view; self.title = m_balloon.title; m_balloon.isCurrentPosition = NO; + + NSMutableArray * viewArray = [[NSMutableArray alloc] init]; + + for (size_t i = 0; i < ARRAY_SIZE(g_colors); ++i) + { + + PinPickerView * pinView = [[PinPickerView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@.png", g_colors[i]]]]; + [viewArray addObject:pinView]; + [pinView release]; + } + self.pinsArray = viewArray; + [viewArray release]; } return self; } +- (void) dealloc +{ + [pinsArray release]; + [viewWithPicker release]; + [super dealloc]; +} + - (void)viewWillAppear:(BOOL)animated { m_hideNavBar = YES; @@ -44,6 +140,8 @@ self.contentSizeForViewInPopover = size; } + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged) name:UIDeviceOrientationDidChangeNotification object:nil]; + [super viewWillAppear:animated]; } @@ -74,6 +172,7 @@ [m_balloon clear]; } } + [[NSNotificationCenter defaultCenter] removeObserver:self]; [super viewWillDisappear:animated]; } @@ -234,10 +333,7 @@ case 2: { - m_hideNavBar = NO; - SelectColorVC * vc = [[SelectColorVC alloc] initWithBalloonView:m_balloon]; - [self pushToNavigationControllerAndSetControllerToPopoverSize:vc]; - [vc release]; + [self createAndShowColorPicker]; } break; } @@ -384,4 +480,151 @@ [self.navigationController pushViewController:vc animated:YES]; } + +- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component +{ + return [PinPickerView viewWidth]; +} + +- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component +{ + return [PinPickerView viewHeight]; +} + +- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component +{ + return [pinsArray count]; +} + +- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView +{ + return 1; +} + + +#pragma mark - UIPickerViewDelegate + +// tell the picker which view to use for a given component and row, we have an array of views to show +- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row + forComponent:(NSInteger)component reusingView:(UIView *)view +{ + return [pinsArray objectAtIndex:row]; +} + +- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component +{ + selectedRow = row; +} + +-(void)pickerDoneClicked +{ + if (![m_balloon.color isEqualToString:g_colors[selectedRow]]) + { + m_balloon.pinImage.image = [UIImage imageNamed:g_colors[selectedRow]]; + [[Statistics instance] logEvent:@"Select Bookmark color"]; + m_balloon.color = g_colors[selectedRow]; + [self.tableView reloadData]; + } + [self pickerCancelClicked]; +} + +-(void)pickerCancelClicked +{ + self.tableView.scrollEnabled = YES; + [viewWithPicker removeFromSuperview]; + self.viewWithPicker = nil; +} + +-(void)createAndShowColorPicker +{ + double height, width; + CGRect rect; + [self getScreenHeight:height width:width rect:rect]; + + viewWithPicker = [[UIView alloc] initWithFrame:rect]; + UIPickerView * picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 0.0, 0.0, 0.0)]; + picker.delegate = self; + picker.showsSelectionIndicator = YES; + CGRect r = picker.frame; + [picker setCenter:CGPointMake(width / 2, height - r.size.height / 2)]; + [viewWithPicker addSubview:picker]; + + UIToolbar * pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, picker.frame.origin.y - 44, width, 44)]; + pickerToolbar.barStyle = UIBarStyleBlackOpaque; + //[pickerToolbar sizeToFit]; + + NSMutableArray *barItems = [[NSMutableArray alloc] init]; + UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(pickerDoneClicked)]; + UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(pickerCancelClicked)]; + [barItems addObject:doneBtn]; + [barItems addObject:cancelBtn]; + + [pickerToolbar setItems:barItems animated:YES]; + [barItems release]; + + + [picker release]; + [viewWithPicker addSubview:pickerToolbar]; + [pickerToolbar release]; + if (UI_USER_INTERFACE_IDIOM() != UIUserInterfaceIdiomPad) + { + UIWindow* window = [UIApplication sharedApplication].keyWindow; + if (!window) + window = [[UIApplication sharedApplication].windows objectAtIndex:0]; + [[[window subviews] objectAtIndex:0] addSubview:viewWithPicker]; + } + else + { + [self.view.superview addSubview:viewWithPicker]; + } + + self.tableView.scrollEnabled = NO; + + selectedRow = 0; +} + +-(void) orientationChanged +{ + if (viewWithPicker) + { + double height, width; + CGRect rect; + [self getScreenHeight:height width:width rect:rect]; + [viewWithPicker setFrame:rect]; + + NSArray *subviews = [viewWithPicker subviews]; + + UIPickerView * p = nil; + UIToolbar * t = nil; + for (UIView *subview in subviews) + { + if ([subview isKindOfClass:[UIPickerView class]]) + p = (UIPickerView *)subview; + else if ([subview isKindOfClass:[UIToolbar class]]) + t = (UIToolbar *)subview; + } + [p setFrame:CGRectMake(0, height - p.frame.size.height, width, p.frame.size.height)]; + [t setFrame:CGRectMake(0, p.frame.origin.y - 44, width, 44)]; + } +} + +-(void)getScreenHeight:(double &)height width:(double &)width rect:(CGRect &)rect +{ + if (UI_USER_INTERFACE_IDIOM() != UIUserInterfaceIdiomPad) + { + rect = [UIScreen mainScreen].bounds; + height = self.view.window.frame.size.height; + width = self.view.window.frame.size.width; + if(!UIInterfaceOrientationIsPortrait(self.interfaceOrientation)) + std::swap(height, width); + + } + else + { + height = self.view.superview.frame.size.height; + width = self.view.superview.frame.size.width; + rect = self.view.superview.frame; + } +} + @end diff --git a/iphone/Maps/Bookmarks/SelectColorVC.h b/iphone/Maps/Bookmarks/SelectColorVC.h deleted file mode 100644 index 25b73b1061..0000000000 --- a/iphone/Maps/Bookmarks/SelectColorVC.h +++ /dev/null @@ -1,13 +0,0 @@ -#import - -@class BalloonView; - -@interface SelectColorVC : UITableViewController -{ - // @TODO store as a property to retain reference - BalloonView * m_balloon; -} - -- (id) initWithBalloonView:(BalloonView *)view; - -@end diff --git a/iphone/Maps/Bookmarks/SelectColorVC.mm b/iphone/Maps/Bookmarks/SelectColorVC.mm deleted file mode 100644 index 74c37f6cd0..0000000000 --- a/iphone/Maps/Bookmarks/SelectColorVC.mm +++ /dev/null @@ -1,69 +0,0 @@ -#import "SelectColorVC.h" -#import "BalloonView.h" -#import "Statistics.h" - -static NSString * g_colors [] = { - @"placemark-red", - @"placemark-blue", - @"placemark-brown", - @"placemark-green", - @"placemark-orange", - @"placemark-pink", - @"placemark-purple" -}; - -@implementation SelectColorVC - -- (id) initWithBalloonView:(BalloonView *)view -{ - self = [super initWithStyle:UITableViewStyleGrouped]; - if (self) - { - m_balloon = view; - - self.title = NSLocalizedString(@"bookmark_color", @"Bookmark Color dialog title"); - } - return self; -} - -- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation -{ - return YES; -} - -- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView -{ - return 1; -} - -- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section -{ - // Number of supported bookmark colors - return sizeof(g_colors)/sizeof(g_colors[0]); -} - -- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath -{ - static NSString * kCellId = @"SelectColorCell"; - UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:kCellId]; - if (cell == nil) - cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellId] autorelease]; - - // Customize cell - cell.imageView.image = [UIImage imageNamed:g_colors[indexPath.row]]; - if (cell.imageView.image == m_balloon.pinImage.image) - cell.accessoryType = UITableViewCellAccessoryCheckmark; - return cell; -} - -- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath -{ - [self.tableView deselectRowAtIndexPath:indexPath animated:YES]; - UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath]; - m_balloon.pinImage.image = cell.imageView.image; - m_balloon.color = g_colors[indexPath.row]; - [self.navigationController popViewControllerAnimated:YES]; - [[Statistics instance] logEvent:@"Select Bookmark color"]; -} - -@end diff --git a/iphone/Maps/Maps.xcodeproj/project.pbxproj b/iphone/Maps/Maps.xcodeproj/project.pbxproj index dfb3cd54cd..968a7a5753 100644 --- a/iphone/Maps/Maps.xcodeproj/project.pbxproj +++ b/iphone/Maps/Maps.xcodeproj/project.pbxproj @@ -192,8 +192,6 @@ FA765AB41737BC6D00279CFF /* 44x58-lite.png in Resources */ = {isa = PBXBuildFile; fileRef = FA765AB01737BC6D00279CFF /* 44x58-lite.png */; }; FA765AB51737BC6D00279CFF /* 64-lite.png in Resources */ = {isa = PBXBuildFile; fileRef = FA765AB11737BC6D00279CFF /* 64-lite.png */; }; FA765AB61737BC6D00279CFF /* 320-lite.png in Resources */ = {isa = PBXBuildFile; fileRef = FA765AB21737BC6D00279CFF /* 320-lite.png */; }; - FA77353C15615E2300DB495F /* SelectColorVC.mm in Sources */ = {isa = PBXBuildFile; fileRef = FA77353B15615E2300DB495F /* SelectColorVC.mm */; }; - FA77353D15615E2300DB495F /* SelectColorVC.mm in Sources */ = {isa = PBXBuildFile; fileRef = FA77353B15615E2300DB495F /* SelectColorVC.mm */; }; FA81AE3314D061BF00A0D70D /* SearchCell.mm in Sources */ = {isa = PBXBuildFile; fileRef = FA81AE3214D061BF00A0D70D /* SearchCell.mm */; }; FA85F633145DDDC20090E1A0 /* packed_polygons.bin in Resources */ = {isa = PBXBuildFile; fileRef = FA85F632145DDDC20090E1A0 /* packed_polygons.bin */; }; FA87151B12B1518F00592DAF /* SystemConfiguration.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = FA87151A12B1518F00592DAF /* SystemConfiguration.framework */; }; @@ -1529,8 +1527,6 @@ FA765AB01737BC6D00279CFF /* 44x58-lite.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "44x58-lite.png"; sourceTree = ""; }; FA765AB11737BC6D00279CFF /* 64-lite.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "64-lite.png"; sourceTree = ""; }; FA765AB21737BC6D00279CFF /* 320-lite.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "320-lite.png"; sourceTree = ""; }; - FA77353A15615E2300DB495F /* SelectColorVC.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SelectColorVC.h; path = Bookmarks/SelectColorVC.h; sourceTree = SOURCE_ROOT; }; - FA77353B15615E2300DB495F /* SelectColorVC.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = SelectColorVC.mm; path = Bookmarks/SelectColorVC.mm; sourceTree = SOURCE_ROOT; }; FA81AE3114D061BF00A0D70D /* SearchCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SearchCell.h; sourceTree = ""; }; FA81AE3214D061BF00A0D70D /* SearchCell.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = SearchCell.mm; sourceTree = ""; }; FA85F632145DDDC20090E1A0 /* packed_polygons.bin */ = {isa = PBXFileReference; lastKnownFileType = archive.macbinary; name = packed_polygons.bin; path = ../../data/packed_polygons.bin; sourceTree = SOURCE_ROOT; }; @@ -2434,8 +2430,6 @@ F785EB3E16386FC4003A38A8 /* BookmarkCell.h */, F785EB3F16386FC4003A38A8 /* BookmarkCell.mm */, FA36B8171540466A004560CC /* Images */, - FA77353A15615E2300DB495F /* SelectColorVC.h */, - FA77353B15615E2300DB495F /* SelectColorVC.mm */, FAA614B6155F16950031C345 /* AddSetVC.h */, FAA614B7155F16950031C345 /* AddSetVC.mm */, FA054610155C465E001F4E37 /* SelectSetVC.h */, @@ -4485,7 +4479,6 @@ FAF457E715597D4600DCCC49 /* Framework.cpp in Sources */, FA054612155C465E001F4E37 /* SelectSetVC.mm in Sources */, FAA614B8155F16950031C345 /* AddSetVC.mm in Sources */, - FA77353C15615E2300DB495F /* SelectColorVC.mm in Sources */, FAAEA7D5161D8D3100CCD661 /* BookmarksRootVC.mm in Sources */, F785EB4016386FC4003A38A8 /* BookmarkCell.mm in Sources */, CB252D6F16FF82C9001E41E9 /* Statistics.m in Sources */, @@ -4521,7 +4514,6 @@ FAF457E815597D4600DCCC49 /* Framework.cpp in Sources */, FA054613155C465E001F4E37 /* SelectSetVC.mm in Sources */, FAA614B9155F16950031C345 /* AddSetVC.mm in Sources */, - FA77353D15615E2300DB495F /* SelectColorVC.mm in Sources */, FAAEA7D6161D8D3100CCD661 /* BookmarksRootVC.mm in Sources */, F785EB4116386FC4003A38A8 /* BookmarkCell.mm in Sources */, CB252D7016FF82C9001E41E9 /* Statistics.m in Sources */,