1*fbd9c41bSBen Roth#import "EXAppLoadingCancelView.h"
2*fbd9c41bSBen Roth
3*fbd9c41bSBen Rothconst NSTimeInterval kEXTimeUntilCancelAppears = 5.0f;
4*fbd9c41bSBen Roth
5*fbd9c41bSBen Roth@interface EXAppLoadingCancelView ()
6*fbd9c41bSBen Roth
7*fbd9c41bSBen Roth@property (nonatomic, strong) UILabel *lblStatus;
8*fbd9c41bSBen Roth@property (nonatomic, strong) UILabel *lblAdvice;
9*fbd9c41bSBen Roth@property (nonatomic, strong) UIButton *btnCancel;
10*fbd9c41bSBen Roth@property (nonatomic, strong) NSTimer *tmrShowCancel;
11*fbd9c41bSBen Roth
12*fbd9c41bSBen Roth@end
13*fbd9c41bSBen Roth
14*fbd9c41bSBen Roth@implementation EXAppLoadingCancelView
15*fbd9c41bSBen Roth
16*fbd9c41bSBen Roth- (instancetype)init
17*fbd9c41bSBen Roth{
18*fbd9c41bSBen Roth  if (self = [super init]) {
19*fbd9c41bSBen Roth    [self _setUpViews];
20*fbd9c41bSBen Roth  }
21*fbd9c41bSBen Roth  return self;
22*fbd9c41bSBen Roth}
23*fbd9c41bSBen Roth
24*fbd9c41bSBen Roth- (void)dealloc
25*fbd9c41bSBen Roth{
26*fbd9c41bSBen Roth  [self _invalidateTimer];
27*fbd9c41bSBen Roth}
28*fbd9c41bSBen Roth
29*fbd9c41bSBen Roth- (void)setFrame:(CGRect)frame
30*fbd9c41bSBen Roth{
31*fbd9c41bSBen Roth  [super setFrame:frame];
32*fbd9c41bSBen Roth  _lblStatus.frame = CGRectMake(16.0f, 0, self.bounds.size.width - 32.0f, 24.0f);
33*fbd9c41bSBen Roth  _lblAdvice.frame = CGRectMake(_lblStatus.frame.origin.x, 0, _lblStatus.frame.size.width, CGFLOAT_MAX);
34*fbd9c41bSBen Roth  [_lblAdvice sizeToFit];
35*fbd9c41bSBen Roth  _lblAdvice.center = CGPointMake(CGRectGetMidX(_lblAdvice.frame), CGRectGetMaxY(_lblStatus.frame) + CGRectGetMidY(_lblAdvice.frame) + 16.0f);
36*fbd9c41bSBen Roth
37*fbd9c41bSBen Roth  _btnCancel.frame = CGRectMake(0, 0, self.bounds.size.width - 48.0f, 36.0f);
38*fbd9c41bSBen Roth  _btnCancel.center = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMaxY(_lblAdvice.frame) + 42.0f);
39*fbd9c41bSBen Roth}
40*fbd9c41bSBen Roth
41*fbd9c41bSBen Roth- (void)_setUpViews
42*fbd9c41bSBen Roth{
43*fbd9c41bSBen Roth  self.backgroundColor = [UIColor clearColor];
44*fbd9c41bSBen Roth  _lblStatus = [[UILabel alloc] init];
45*fbd9c41bSBen Roth  _lblStatus.text = @"Opening project...";
46*fbd9c41bSBen Roth  _lblStatus.font = [UIFont boldSystemFontOfSize:14.0f];
47*fbd9c41bSBen Roth  _lblStatus.textColor = [UIColor blackColor];
48*fbd9c41bSBen Roth  _lblStatus.textAlignment = NSTextAlignmentCenter;
49*fbd9c41bSBen Roth  [self addSubview:_lblStatus];
50*fbd9c41bSBen Roth
51*fbd9c41bSBen Roth  _lblAdvice = [[UILabel alloc] init];
52*fbd9c41bSBen Roth  _lblAdvice.text = @"This is taking much longer than it should. You might want to check your internet connectivity.";
53*fbd9c41bSBen Roth  _lblAdvice.numberOfLines = 0;
54*fbd9c41bSBen Roth  _lblAdvice.font = [UIFont systemFontOfSize:14.0f];
55*fbd9c41bSBen Roth  _lblAdvice.textColor = [UIColor darkGrayColor];
56*fbd9c41bSBen Roth  _lblAdvice.textAlignment = NSTextAlignmentCenter;
57*fbd9c41bSBen Roth  [self addSubview:_lblAdvice];
58*fbd9c41bSBen Roth
59*fbd9c41bSBen Roth  _btnCancel = [UIButton buttonWithType:UIButtonTypeRoundedRect];
60*fbd9c41bSBen Roth  [_btnCancel setTitle:@"Go Back to Home" forState:UIControlStateNormal];
61*fbd9c41bSBen Roth  _btnCancel.titleLabel.font = [UIFont boldSystemFontOfSize:14.0f];
62*fbd9c41bSBen Roth  [_btnCancel setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
63*fbd9c41bSBen Roth  _btnCancel.layer.borderWidth = 1.0f;
64*fbd9c41bSBen Roth  _btnCancel.layer.borderColor = [UIColor darkGrayColor].CGColor;
65*fbd9c41bSBen Roth  _btnCancel.layer.cornerRadius = 3.0f;
66*fbd9c41bSBen Roth  [_btnCancel addTarget:self action:@selector(_onTapCancel) forControlEvents:UIControlEventTouchUpInside];
67*fbd9c41bSBen Roth  [self addSubview:_btnCancel];
68*fbd9c41bSBen Roth
69*fbd9c41bSBen Roth  _lblAdvice.hidden = YES;
70*fbd9c41bSBen Roth  _btnCancel.hidden = YES;
71*fbd9c41bSBen Roth  _tmrShowCancel = [NSTimer scheduledTimerWithTimeInterval:kEXTimeUntilCancelAppears
72*fbd9c41bSBen Roth                                                    target:self
73*fbd9c41bSBen Roth                                                  selector:@selector(_onCancelTimerFinished)
74*fbd9c41bSBen Roth                                                  userInfo:nil repeats:NO];
75*fbd9c41bSBen Roth
76*fbd9c41bSBen Roth  [self setNeedsLayout];
77*fbd9c41bSBen Roth}
78*fbd9c41bSBen Roth
79*fbd9c41bSBen Roth- (void)_onTapCancel
80*fbd9c41bSBen Roth{
81*fbd9c41bSBen Roth  if (_delegate) {
82*fbd9c41bSBen Roth    [_delegate appLoadingCancelViewDidCancel:self];
83*fbd9c41bSBen Roth  }
84*fbd9c41bSBen Roth}
85*fbd9c41bSBen Roth
86*fbd9c41bSBen Roth#pragma mark - cancel timer
87*fbd9c41bSBen Roth
88*fbd9c41bSBen Roth- (void)_invalidateTimer
89*fbd9c41bSBen Roth{
90*fbd9c41bSBen Roth  if (_tmrShowCancel) {
91*fbd9c41bSBen Roth    [_tmrShowCancel invalidate];
92*fbd9c41bSBen Roth    _tmrShowCancel = nil;
93*fbd9c41bSBen Roth  }
94*fbd9c41bSBen Roth}
95*fbd9c41bSBen Roth
96*fbd9c41bSBen Roth- (void)_onCancelTimerFinished
97*fbd9c41bSBen Roth{
98*fbd9c41bSBen Roth  [self _invalidateTimer];
99*fbd9c41bSBen Roth  _lblAdvice.hidden = NO;
100*fbd9c41bSBen Roth  _btnCancel.hidden = NO;
101*fbd9c41bSBen Roth}
102*fbd9c41bSBen Roth
103*fbd9c41bSBen Roth@end
104