Skip to content

Commit

Permalink
Merge pull request #5489 from pec1985/timob-15943-3_2_x
Browse files Browse the repository at this point in the history
[TIMOB-15943] TiErrorController Backport for 3.2.x
  • Loading branch information
vishalduggal committed Mar 18, 2014
2 parents e4bea4f + 55ea87a commit 6b1cbd9
Show file tree
Hide file tree
Showing 2 changed files with 251 additions and 53 deletions.
5 changes: 5 additions & 0 deletions iphone/Classes/TiErrorController.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
@interface TiErrorController : UIViewController {

NSString *error;
UILabel *disclosureLabel;
UILabel *messageLabel;
UIButton *dismissButton;
UIView *centerView;
UILabel *titleLabel;
}

-(id)initWithError:(NSString*)error_;
Expand Down
299 changes: 246 additions & 53 deletions iphone/Classes/TiErrorController.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,70 +25,263 @@ -(id)initWithError:(NSString*)error_

-(void)dealloc
{
RELEASE_TO_NIL(titleLabel);
RELEASE_TO_NIL(messageLabel);
RELEASE_TO_NIL(disclosureLabel);
RELEASE_TO_NIL(centerView);
RELEASE_TO_NIL(messageLabel);
RELEASE_TO_NIL(disclosureLabel);
RELEASE_TO_NIL(error);
[super dealloc];
}

-(UILabel*)dynamicLabel:(NSString*)text width:(CGFloat)width height:(CGFloat)height fontSize:(CGFloat)fontSize bottom:(CGFloat)bottom
-(void)dismiss:(id)sender
{
CGSize textSize = { width, 2000.0f };
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:fontSize] constrainedToSize:textSize lineBreakMode:UILineBreakModeWordWrap];

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, height-bottom-size.height, width-20, size.height+50)];
[label setText:text];
[label setFont:[UIFont boldSystemFontOfSize:fontSize]];
[label setBackgroundColor:[UIColor clearColor]];
[label setTextAlignment:UITextAlignmentCenter];
[label setLineBreakMode:UILineBreakModeWordWrap];
[label setNumberOfLines:0];
return [label autorelease];
[[TiApp app] hideModalController:self animated:YES];
}

-(void)dismiss:(id)sender
- (void)loadView
{
[[TiApp app] hideModalController:self animated:YES];
[super loadView];
self.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
UIView *view = [self view];
[view setBackgroundColor:[UIColor redColor]];

RELEASE_TO_NIL(titleLabel)
RELEASE_TO_NIL(messageLabel)
RELEASE_TO_NIL(disclosureLabel)
RELEASE_TO_NIL(centerView)
RELEASE_TO_NIL(messageLabel)
RELEASE_TO_NIL(disclosureLabel)

dismissButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
dismissButton.backgroundColor = [TiUtils isIOS7OrGreater] ? [UIColor whiteColor] : [UIColor clearColor];
dismissButton.translatesAutoresizingMaskIntoConstraints = NO;
[dismissButton setTitle:@"Dismiss" forState:UIControlStateNormal];

disclosureLabel = [[UILabel alloc] init];
disclosureLabel.backgroundColor = [UIColor clearColor];
disclosureLabel.font = [UIFont boldSystemFontOfSize:14];
disclosureLabel.numberOfLines = 0;
disclosureLabel.shadowOffset = CGSizeMake(0.0, -1.0);
disclosureLabel.text = @"Error messages will only be displayed during development. When your app is packaged for final distribution, no error screen will appear. Test your code!";
disclosureLabel.textColor = [UIColor blackColor];
disclosureLabel.translatesAutoresizingMaskIntoConstraints = NO;

centerView = [[UIView alloc] init];
centerView.translatesAutoresizingMaskIntoConstraints = NO;

titleLabel = [[UILabel alloc] init];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.font = [UIFont boldSystemFontOfSize:32];
titleLabel.shadowColor = [UIColor darkGrayColor];
titleLabel.shadowOffset = CGSizeMake(2.0, 1.0);
titleLabel.text = @"Application Error";
titleLabel.textAlignment = NSTextAlignmentCenter;
titleLabel.textColor = [UIColor greenColor];
titleLabel.translatesAutoresizingMaskIntoConstraints = NO;

messageLabel = [[UILabel alloc] init];
messageLabel.backgroundColor = [UIColor clearColor];
messageLabel.font = [UIFont boldSystemFontOfSize:20];
messageLabel.numberOfLines = 0;
messageLabel.shadowColor = [UIColor blackColor];
messageLabel.shadowOffset = CGSizeMake(0.0, -1.0);
messageLabel.text = error;
messageLabel.textAlignment = NSTextAlignmentCenter;
messageLabel.textColor = [UIColor whiteColor];
messageLabel.translatesAutoresizingMaskIntoConstraints = NO;

[centerView addSubview:messageLabel];
[centerView addSubview:dismissButton];
[[self view] addSubview:titleLabel];
[[self view] addSubview:centerView];
[[self view] addSubview:disclosureLabel];

NSLayoutConstraint *disclosureContraint1 =
[NSLayoutConstraint constraintWithItem:disclosureLabel
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:view
attribute:NSLayoutAttributeLeading
multiplier:1
constant:20];
NSLayoutConstraint *disclosureContraint2 =
[NSLayoutConstraint constraintWithItem:view
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:disclosureLabel
attribute:NSLayoutAttributeTrailing
multiplier:1
constant:20];
NSLayoutConstraint *disclosureContraint3 =
[NSLayoutConstraint constraintWithItem:view
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:disclosureLabel
attribute:NSLayoutAttributeBottom
multiplier:1
constant:20];
NSLayoutConstraint *titleConstraint1 =
[NSLayoutConstraint constraintWithItem:titleLabel
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:view
attribute:NSLayoutAttributeTop
multiplier:1
constant:20];
NSLayoutConstraint *titleConstraint2 =
[NSLayoutConstraint constraintWithItem:titleLabel
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:view
attribute:NSLayoutAttributeLeading
multiplier:1
constant:20];
NSLayoutConstraint *titleConstraint3 =
[NSLayoutConstraint constraintWithItem:view
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:titleLabel
attribute:NSLayoutAttributeTrailing
multiplier:1
constant:20];
NSLayoutConstraint *messageConstraint1 =
[NSLayoutConstraint constraintWithItem:centerView
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:messageLabel
attribute:NSLayoutAttributeTrailing
multiplier:1
constant:0];
NSLayoutConstraint *messageConstraint2 =
[NSLayoutConstraint constraintWithItem:messageLabel
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:centerView
attribute:NSLayoutAttributeLeading
multiplier:1
constant:0];
NSLayoutConstraint *messageConstraint3 =
[NSLayoutConstraint constraintWithItem:messageLabel
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:centerView
attribute:NSLayoutAttributeTop
multiplier:1
constant:0];
NSLayoutConstraint *buttonConstraint1 =
[NSLayoutConstraint constraintWithItem:centerView
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:dismissButton
attribute:NSLayoutAttributeTrailing
multiplier:1
constant:80];
NSLayoutConstraint *buttonConstraint2 =
[NSLayoutConstraint constraintWithItem:dismissButton
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:centerView
attribute:NSLayoutAttributeLeading
multiplier:1
constant:80];
NSLayoutConstraint *buttonConstraint3 =
[NSLayoutConstraint constraintWithItem:dismissButton
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:centerView
attribute:NSLayoutAttributeBottom
multiplier:1
constant:0];
NSLayoutConstraint *buttonConstraint4 =
[NSLayoutConstraint constraintWithItem:dismissButton
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:messageLabel
attribute:NSLayoutAttributeBottom
multiplier:1
constant:20];
NSLayoutConstraint *centerConstraint1 =
[NSLayoutConstraint constraintWithItem:view
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:centerView
attribute:NSLayoutAttributeCenterX
multiplier:1
constant:0];
NSLayoutConstraint *centerConstraint2 =
[NSLayoutConstraint constraintWithItem:view
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:centerView
attribute:NSLayoutAttributeCenterY
multiplier:1
constant:0];
NSLayoutConstraint *centerConstraint3 =
[NSLayoutConstraint constraintWithItem:centerView
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:view
attribute:NSLayoutAttributeLeading
multiplier:1
constant:20];
NSLayoutConstraint *centerConstraint4 =
[NSLayoutConstraint constraintWithItem:view
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:centerView
attribute:NSLayoutAttributeTrailing
multiplier:1
constant:20];
NSLayoutConstraint *centerConstraint5 =
[NSLayoutConstraint constraintWithItem:centerView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationGreaterThanOrEqual
toItem:titleLabel
attribute:NSLayoutAttributeBottom
multiplier:1
constant:10];
NSLayoutConstraint *centerConstraint6 =
[NSLayoutConstraint constraintWithItem:disclosureLabel
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationGreaterThanOrEqual
toItem:centerView
attribute:NSLayoutAttributeBottom
multiplier:1
constant:10];

[centerView addConstraint:messageConstraint1];
[centerView addConstraint:messageConstraint2];
[centerView addConstraint:messageConstraint3];
[centerView addConstraint:buttonConstraint1];
[centerView addConstraint:buttonConstraint2];
[centerView addConstraint:buttonConstraint3];
[centerView addConstraint:buttonConstraint4];

[[self view] addConstraint:titleConstraint1];
[[self view] addConstraint:titleConstraint2];
[[self view] addConstraint:titleConstraint3];
[[self view] addConstraint:centerConstraint1];
[[self view] addConstraint:centerConstraint2];
[[self view] addConstraint:centerConstraint3];
[[self view] addConstraint:centerConstraint4];
[[self view] addConstraint:centerConstraint5];
[[self view] addConstraint:centerConstraint6];
[[self view] addConstraint:disclosureContraint1];
[[self view] addConstraint:disclosureContraint2];
[[self view] addConstraint:disclosureContraint3];

[dismissButton addTarget:self action:@selector(dismiss:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)loadView
-(void)viewDidAppear:(BOOL)animated
{
self.modalTransitionStyle = UIModalTransitionStyleCoverVertical;

UIView *view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
[view setBackgroundColor:[UIColor redColor]];

UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, view.bounds.size.width, 35)];
[title setText:@"Application Error"];
[title setFont:[UIFont boldSystemFontOfSize:32]];
[title setBackgroundColor:[UIColor clearColor]];
[title setTextAlignment:UITextAlignmentCenter];
[title setTextColor:[UIColor greenColor]];
[title setShadowColor:[UIColor darkGrayColor]];
[title setShadowOffset:CGSizeMake(2.3, 1)];
[view addSubview:title];
[title release];

NSString *text = @"Error messages will only be displayed during development. When your app is packaged for final distribution, no error screen will appear. Test your code!";
UILabel *footer = [self dynamicLabel:text width:view.bounds.size.width height:view.bounds.size.height fontSize:14.0f bottom:55];
[footer setTextColor:[UIColor blackColor]];
[view addSubview:footer];

UILabel *message = [self dynamicLabel:error width:view.bounds.size.width height:view.bounds.size.height fontSize:20.0f bottom:285];
[message setTextColor:[UIColor whiteColor]];
[message setShadowColor:[UIColor blackColor]];
[view addSubview:message];

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:@"Dismiss" forState:UIControlStateNormal];
CGFloat left = ( view.bounds.size.width / 2 ) - 70;
[TiUtils setView:button positionRect:CGRectMake(left, view.bounds.size.height-170, 140, 40)];
[button setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
[view addSubview:button];

[button addTarget:self action:@selector(dismiss:) forControlEvents:UIControlEventTouchUpInside];

self.view = view;

[view release];
[super viewDidAppear:animated];
disclosureLabel.preferredMaxLayoutWidth = disclosureLabel.frame.size.width;
messageLabel.preferredMaxLayoutWidth = messageLabel.frame.size.width;
titleLabel.preferredMaxLayoutWidth = titleLabel.frame.size.width;
[self.view layoutIfNeeded];
}

@end

0 comments on commit 6b1cbd9

Please sign in to comment.