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