1// Copyright 2015-present 650 Industries. All rights reserved. 2 3#import "EXAppLoader.h" 4#import "EXErrorView.h" 5#import "EXEnvironment.h" 6#import "EXKernel.h" 7#import "EXKernelAppRecord.h" 8#import "EXUtil.h" 9 10@interface EXErrorView () 11 12@property (nonatomic, strong) UILabel *lblError; 13@property (nonatomic, strong) UIButton *btnRetry; 14@property (nonatomic, strong) UIButton *btnBack; 15@property (nonatomic, strong) UILabel *lblUrl; 16@property (nonatomic, strong) UILabel *lblErrorDetail; 17@property (nonatomic, strong) UIScrollView *vContainer; 18 19- (void)_onTapRetry; 20 21@end 22 23@implementation EXErrorView 24 25- (instancetype)initWithFrame:(CGRect)frame 26{ 27 if (self = [super initWithFrame:frame]) { 28 self.backgroundColor = [UIColor whiteColor]; 29 30 self.vContainer = [[UIScrollView alloc] init]; 31 [self addSubview:_vContainer]; 32 33 // error description label 34 self.lblError = [[UILabel alloc] init]; 35 _lblError.numberOfLines = 0; 36 _lblError.textAlignment = NSTextAlignmentCenter; 37 _lblError.font = [UIFont systemFontOfSize:14.0f]; 38 [_vContainer addSubview:_lblError]; 39 40 // retry button 41 self.btnRetry = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 42 [_btnRetry setTitle:@"Try again" forState:UIControlStateNormal]; 43 [_btnRetry addTarget:self action:@selector(_onTapRetry) forControlEvents:UIControlEventTouchUpInside]; 44 [_vContainer addSubview:_btnRetry]; 45 46 // back button 47 self.btnBack = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 48 [_btnBack setTitle:@"Go back to Expo Home" forState:UIControlStateNormal]; 49 [_btnBack addTarget:self action:@selector(_onTapBack) forControlEvents:UIControlEventTouchUpInside]; 50 [_vContainer addSubview:_btnBack]; 51 52 for (UIButton *btnToStyle in @[ _btnRetry, _btnBack ]) { 53 [btnToStyle setTintColor:[EXUtil colorWithRGB:0x49a7e8]]; 54 [btnToStyle.titleLabel setFont:[UIFont boldSystemFontOfSize:14.0f]]; 55 } 56 57 // url label 58 self.lblUrl = [[UILabel alloc] init]; 59 _lblUrl.numberOfLines = 0; 60 _lblUrl.textAlignment = NSTextAlignmentCenter; 61 [_vContainer addSubview:_lblUrl]; 62 63 // error detail label 64 self.lblErrorDetail = [[UILabel alloc] init]; 65 _lblErrorDetail.numberOfLines = 0; 66 _lblErrorDetail.textAlignment = NSTextAlignmentLeft; 67 [_vContainer addSubview:_lblErrorDetail]; 68 69 for (UILabel *lblToStyle in @[ _lblUrl, _lblErrorDetail ]) { 70 lblToStyle.font = [UIFont systemFontOfSize:14.0f]; 71 lblToStyle.textColor = [UIColor lightGrayColor]; 72 } 73 } 74 return self; 75} 76 77- (void)setType:(EXFatalErrorType)type 78{ 79 _type = type; 80 NSString *appOwnerName = @"the requested app"; 81 if (_appRecord) { 82 if (_appRecord == [EXKernel sharedInstance].appRegistry.homeAppRecord) { 83 appOwnerName = @"Expo"; 84 } else if (_appRecord.appLoader.manifest && _appRecord.appLoader.manifest[@"name"]) { 85 appOwnerName = [NSString stringWithFormat:@"\"%@\"", _appRecord.appLoader.manifest[@"name"]]; 86 } 87 } 88 89 switch (type) { 90 case kEXFatalErrorTypeLoading: { 91 _lblError.text = [NSString stringWithFormat:@"There was a problem loading %@.", appOwnerName]; 92 if (_error.code == kCFURLErrorNotConnectedToInternet) { 93 _lblError.text = [NSString stringWithFormat:@"%@ Make sure you're connected to the internet.", _lblError.text]; 94 } else if (_appRecord.appLoader.manifestUrl) { 95 NSString *url = _appRecord.appLoader.manifestUrl.absoluteString; 96 if ([self _urlLooksLikeLAN:url]) { 97 _lblError.text = [NSString stringWithFormat: 98 @"%@ It looks like you may be using a LAN URL. " 99 "Make sure your device is on the same network as the server or try using a tunnel.", _lblError.text]; 100 } 101 } 102 break; 103 } 104 case kEXFatalErrorTypeException: { 105 _lblError.text = [NSString stringWithFormat:@"There was a problem running %@.", appOwnerName]; 106 break; 107 } 108 } 109 [self _resetUIState]; 110} 111 112- (void)setError:(NSError *)error 113{ 114 _error = error; 115 _lblErrorDetail.text = [error localizedDescription]; 116 [self _resetUIState]; 117} 118 119- (void)setAppRecord:(EXKernelAppRecord *)appRecord 120{ 121 _appRecord = appRecord; 122 [self _resetUIState]; 123} 124 125- (void)layoutSubviews 126{ 127 [super layoutSubviews]; 128 129 _vContainer.frame = self.bounds; 130 CGFloat maxLabelWidth = self.bounds.size.width - 32.0f; 131 132 _lblError.frame = CGRectMake(0, 0, maxLabelWidth, CGFLOAT_MAX); 133 [_lblError sizeToFit]; 134 _lblError.center = CGPointMake(self.bounds.size.width * 0.5f, self.bounds.size.height * 0.25f); 135 136 _btnRetry.frame = CGRectMake(0, 0, self.bounds.size.width, 24.0f); 137 _btnRetry.center = CGPointMake(_lblError.center.x, CGRectGetMaxY(_lblError.frame) + 32.0f); 138 139 _btnBack.frame = CGRectMake(0, 0, self.bounds.size.width, 24.0f); 140 _btnBack.center = CGPointMake(_lblError.center.x, CGRectGetMaxY(_btnRetry.frame) + 24); 141 142 _lblUrl.frame = CGRectMake(0, 0, self.bounds.size.width - 48.0f, CGFLOAT_MAX); 143 [_lblUrl sizeToFit]; 144 _lblUrl.center = CGPointMake(_lblError.center.x, CGRectGetMaxY(_btnBack.frame) + 12.0f + CGRectGetMidY(_lblUrl.bounds)); 145 146 _lblErrorDetail.frame = CGRectMake(0, 0, maxLabelWidth, CGFLOAT_MAX); 147 [_lblErrorDetail sizeToFit]; 148 149 _lblErrorDetail.center = CGPointMake(_lblError.center.x, CGRectGetMaxY(_lblUrl.frame) + 24.0f + CGRectGetMidY(_lblErrorDetail.bounds)); 150 151 _vContainer.contentSize = CGSizeMake(_vContainer.bounds.size.width, CGRectGetMaxY(_lblErrorDetail.frame) + 12.0f); 152} 153 154#pragma mark - Internal 155 156- (void)_resetUIState 157{ 158 EXKernelAppRecord *homeRecord = [EXKernel sharedInstance].appRegistry.homeAppRecord; 159 _btnBack.hidden = (!homeRecord || _appRecord == homeRecord); 160 _lblUrl.hidden = (!homeRecord && ![self _isDevDetached]); 161 _lblUrl.text = _appRecord.appLoader.manifestUrl.absoluteString; 162 // TODO: maybe hide retry (see BrowserErrorView) 163 [self setNeedsLayout]; 164} 165 166- (void)_onTapRetry 167{ 168 if (_delegate) { 169 [_delegate errorViewDidSelectRetry:self]; 170 } 171} 172 173- (void)_onTapBack 174{ 175 if ([EXKernel sharedInstance].browserController) { 176 [[EXKernel sharedInstance].browserController moveHomeToVisible]; 177 } 178} 179 180- (BOOL)_urlLooksLikeLAN:(NSString *)url 181{ 182 return ( 183 url && ( 184 [url rangeOfString:@".local"].length > 0 || 185 [url rangeOfString:@"192."].length > 0 || 186 [url rangeOfString:@"10."].length > 0 || 187 [url rangeOfString:@"172."].length > 0 188 ) 189 ); 190} 191 192- (BOOL)_isDevDetached 193{ 194 return [EXEnvironment sharedEnvironment].isDetached && [EXEnvironment sharedEnvironment].isDebugXCodeScheme; 195} 196 197@end 198