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