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