Created
March 27, 2012 22:22
-
-
Save rnystrom/2220960 to your computer and use it in GitHub Desktop.
Implementation file for SaveSpot.m that is having performance issues
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "SaveSpot.h" | |
@implementation SaveSpot | |
@synthesize spot = _spot; | |
@synthesize maxSpeedLabel = _maxSpeedLabel; | |
@synthesize avgSpeedLabel = _avgSpeedLabel; | |
@synthesize distanceLabel = _distanceLabel; | |
@synthesize nameTextField = _nameLabel; | |
@synthesize descriptionTextField = _descriptionLabel; | |
@synthesize mapView = _mapView; | |
@synthesize routeLine = _routeLine; | |
@synthesize routeLineView = _routeLineView; | |
@synthesize containingView = _containingView; | |
@synthesize user = _user; | |
@synthesize statsBG = _statsBG; | |
@synthesize lightBgView = _lightBgView; | |
- (void)viewDidLoad | |
{ | |
[super viewDidLoad]; | |
User *user = [[User alloc] init]; | |
[user setDelegate:self]; | |
[self setUser:user]; | |
keyboardIsShown = NO; | |
[[NSNotificationCenter defaultCenter] addObserver:self | |
selector:@selector(keyboardWillShow:) | |
name:UIKeyboardWillShowNotification | |
object:[[self view] window]]; | |
[[NSNotificationCenter defaultCenter] addObserver:self | |
selector:@selector(keyboardWillHide:) | |
name:UIKeyboardWillHideNotification | |
object:[[self view] window]]; | |
[[self maxSpeedLabel] setText:[NSString stringWithFormat:@"%.1f mph", [[[self spot] maxSpeed] floatValue]]]; | |
[[self avgSpeedLabel] setText:[NSString stringWithFormat:@"%.1f mph", [[[self spot] averageSpeed] floatValue]]]; | |
[[self distanceLabel] setText:[NSString stringWithFormat:@"%.1f mi", [[[self spot] distance] floatValue]]]; | |
[[self mapView] setDelegate:self]; | |
[[self nameTextField] setDelegate:self]; | |
[[self descriptionTextField] setDelegate:self]; | |
NSArray *points = [[self spot] points]; | |
MKMapPoint northEastPoint; | |
MKMapPoint southWestPoint; | |
MKMapPoint* pointArr = malloc(sizeof(CLLocationCoordinate2D) * [points count]); | |
for (int i = 0; i < [points count]; ++i) { | |
SpotPoint *sp = nil; | |
if ([[points objectAtIndex:i] isKindOfClass:[SpotPoint class]]) { | |
sp = (SpotPoint*)[points objectAtIndex:i]; | |
} | |
else { | |
sp = [SpotPoint unSerialize:[points objectAtIndex:i]]; | |
} | |
CLLocation *location = [[CLLocation alloc] initWithLatitude:[[sp lat] floatValue] longitude:[[sp lon] floatValue]]; | |
MKMapPoint point = MKMapPointForCoordinate([location coordinate]); | |
if (i == 0) { | |
northEastPoint = point; | |
southWestPoint = point; | |
} | |
else { | |
if (point.x > northEastPoint.x) | |
northEastPoint.x = point.x; | |
if(point.y > northEastPoint.y) | |
northEastPoint.y = point.y; | |
if (point.x < southWestPoint.x) | |
southWestPoint.x = point.x; | |
if (point.y < southWestPoint.y) | |
southWestPoint.y = point.y; | |
} | |
pointArr[i] = point; | |
} | |
[self setRouteLine:[MKPolyline polylineWithPoints:pointArr count:[points count]]]; | |
_routeRect = MKMapRectMake(southWestPoint.x, southWestPoint.y, northEastPoint.x - southWestPoint.x, northEastPoint.y - southWestPoint.y); | |
free(pointArr); | |
[[self mapView] addOverlay:[self routeLine]]; | |
[[self mapView] setVisibleMapRect:_routeRect]; | |
CALayer *statsLayer = [[self statsBG] layer]; | |
[statsLayer setCornerRadius:10.0f]; | |
[statsLayer setShadowColor:[[UIColor blackColor] CGColor]]; | |
[statsLayer setShadowRadius:3.0f]; | |
[statsLayer setShadowOffset:CGSizeMake(0, 0)]; | |
[statsLayer setShadowOpacity:0.5f]; | |
CALayer *imageLayer = [[self lightBgView] layer]; | |
[imageLayer setShadowColor:[[UIColor blackColor] CGColor]]; | |
[imageLayer setShadowRadius:5.0f]; | |
[imageLayer setShadowOpacity:0.5f]; | |
[[self lightBgView] setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"lightbg2.png"]]]; | |
} | |
- (void) viewDidUnload | |
{ | |
[self setContainingView:nil]; | |
[self setStatsBG:nil]; | |
[self setLightBgView:nil]; | |
[super viewDidUnload]; | |
[[NSNotificationCenter defaultCenter] removeObserver:self | |
name:UIKeyboardWillShowNotification | |
object:nil]; | |
[[NSNotificationCenter defaultCenter] removeObserver:self | |
name:UIKeyboardWillHideNotification | |
object:nil]; | |
} | |
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id )overlay | |
{ | |
MKOverlayView* overlayView = nil; | |
if(overlay == [self routeLine]) { | |
if(nil == [self routeLineView]) { | |
[self setRouteLineView: [[MKPolylineView alloc] initWithPolyline:[self routeLine]]]; | |
[[self routeLineView] setFillColor:[UIColor redColor]]; | |
[[self routeLineView] setStrokeColor:[UIColor redColor]]; | |
[[self routeLineView] setLineWidth:5.0f]; | |
} | |
overlayView = [self routeLineView]; | |
} | |
return overlayView; | |
} | |
- (void)viewWillAppear:(BOOL)animated | |
{ | |
[super viewWillAppear:animated]; | |
[[self navigationController] setNavigationBarHidden:NO animated:NO]; | |
[[self navigationController] setToolbarHidden:YES animated:NO]; | |
[[self mapView] setAlpha:0.0f]; | |
} | |
- (void)viewWillDisappear:(BOOL)animated | |
{ | |
[super viewWillDisappear:animated]; | |
[[self mapView] setAlpha:0.0f]; | |
} | |
- (void)viewDidAppear:(BOOL)animated | |
{ | |
[[self mapView] setAlpha:1.0f]; | |
} | |
- (void)keyboardWillHide:(NSNotification *)n | |
{ | |
NSDictionary* userInfo = [n userInfo]; | |
NSTimeInterval animationDuration; | |
UIViewAnimationCurve animationCurve; | |
CGRect keyboardEndFrame; | |
[[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve]; | |
[[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration]; | |
[[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardEndFrame]; | |
CGRect viewFrame = [[self view] frame]; | |
viewFrame.origin.y += (keyboardEndFrame.size.height); | |
[UIView beginAnimations:nil context:NULL]; | |
[UIView setAnimationBeginsFromCurrentState:YES]; | |
[UIView setAnimationDuration:animationDuration]; | |
[UIView setAnimationCurve:animationCurve]; | |
[[self view] setFrame:viewFrame]; | |
[UIView commitAnimations]; | |
keyboardIsShown = NO; | |
} | |
- (void)keyboardWillShow:(NSNotification *)n | |
{ | |
if (keyboardIsShown) return; | |
NSDictionary* userInfo = [n userInfo]; | |
NSTimeInterval animationDuration; | |
UIViewAnimationCurve animationCurve; | |
CGRect keyboardEndFrame; | |
[[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve]; | |
[[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration]; | |
[[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardEndFrame]; | |
CGRect viewFrame = [[self view] frame]; | |
viewFrame.origin.y -= (keyboardEndFrame.size.height); | |
[UIView beginAnimations:nil context:NULL]; | |
[UIView setAnimationBeginsFromCurrentState:YES]; | |
[UIView setAnimationDuration:animationDuration]; | |
[UIView setAnimationCurve:animationCurve]; | |
[[self view] setFrame:viewFrame]; | |
[UIView commitAnimations]; | |
keyboardIsShown = YES; | |
} | |
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event | |
{ | |
[[self nameTextField] resignFirstResponder]; | |
[[self descriptionTextField] resignFirstResponder]; | |
} | |
- (BOOL) textFieldShouldReturn:(UITextField *)textField | |
{ | |
if ([textField isEqual:[self nameTextField]]) { | |
[[self descriptionTextField] becomeFirstResponder]; | |
} | |
else { | |
[[self descriptionTextField] resignFirstResponder]; | |
[self saveSpot]; | |
} | |
return YES; | |
} | |
- (void) saveSpot | |
{ | |
NSString *name = [[self nameTextField] text]; | |
NSString *description = [[self descriptionTextField] text]; | |
if (!name || [name length] == 0) { | |
UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"Missing Name" message:@"You need to at least name your new spot." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil]; | |
[av show]; | |
return; | |
} | |
if (! [[self spot] points] || [[[self spot] points] count] == 0 || ! [[self spot] averageSpeed] || ! [[self spot] maxSpeed] || ! [[self spot] distance]) { | |
UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Spot data is corrupt. Try recording again." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil]; | |
[av show]; | |
return; | |
} | |
_hud = [[MBProgressHUD alloc] initWithView:[[self navigationController] view]]; | |
[[[self navigationController] view] addSubview:_hud]; | |
[_hud setDimBackground:YES]; | |
[_hud setLabelText:@"Saving..."]; | |
[_hud setLabelFont:kHudFont]; | |
[_hud setDelegate:self]; | |
[_hud show:YES]; | |
[[self spot] setName:name]; | |
[[self spot] setDescription:description]; | |
[[self user] saveSpot:[self spot]]; | |
// [[self user] shareSpot:[self spot]]; | |
} | |
- (void)spotDidSave | |
{ | |
[_hud hide:YES]; | |
[[self navigationController] popViewControllerAnimated:YES]; | |
} | |
- (void)requestDidFailWithError:(NSString *)error | |
{ | |
[_hud hide:YES]; | |
UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"Error" message:error delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil]; | |
[av show]; | |
} | |
- (IBAction)onSave:(id)sender { | |
[[self nameTextField] resignFirstResponder]; | |
[[self descriptionTextField] resignFirstResponder]; | |
[self saveSpot]; | |
} | |
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation | |
{ | |
// Return YES for supported orientations | |
return (interfaceOrientation == UIInterfaceOrientationPortrait); | |
} | |
- (void)hudWasHidden:(MBProgressHUD *)hud { | |
[hud removeFromSuperview]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment