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