blob: 98d5f87bfb7e7a1294eb95e3852dca1576a15fd2 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
#import "MWMTrafficButtonViewController.h"
#import "MWMCommon.h"
#import "MWMAlertViewController.h"
#import "MWMButton.h"
#import "MWMMapViewControlsCommon.h"
#import "MWMMapViewControlsManager.h"
#import "MWMToast.h"
#import "MWMTrafficManager.h"
#import "MapViewController.h"
namespace
{
CGFloat const kTopOffset = 6;
NSArray<UIImage *> * imagesWithName(NSString * name)
{
NSUInteger const imagesCount = 3;
NSMutableArray<UIImage *> * images = [NSMutableArray arrayWithCapacity:imagesCount];
NSString * mode = [UIColor isNightMode] ? @"dark" : @"light";
for (NSUInteger i = 1; i <= imagesCount; i += 1)
{
NSString * imageName = [NSString stringWithFormat:@"%@_%@_%@", name, mode, @(i).stringValue];
[images addObject:static_cast<UIImage * _Nonnull>([UIImage imageNamed:imageName])];
}
return [images copy];
}
} // namespace
@interface MWMMapViewControlsManager ()
@property(nonatomic) MWMTrafficButtonViewController * trafficButton;
@end
@interface MWMTrafficButtonViewController ()<MWMTrafficManagerObserver>
@property(nonatomic) NSLayoutConstraint * topOffset;
@property(nonatomic) NSLayoutConstraint * leftOffset;
@property(nonatomic) CGRect availableArea;
@end
@implementation MWMTrafficButtonViewController
+ (MWMTrafficButtonViewController *)controller
{
return [MWMMapViewControlsManager manager].trafficButton;
}
- (instancetype)init
{
self = [super init];
if (self)
{
MapViewController * ovc = [MapViewController controller];
[ovc addChildViewController:self];
[ovc.view addSubview:self.view];
[self configLayout];
[self refreshAppearance];
[MWMTrafficManager addObserver:self];
}
return self;
}
- (void)configLayout
{
UIView * sv = self.view;
UIView * ov = sv.superview;
self.topOffset = [NSLayoutConstraint constraintWithItem:sv
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:ov
attribute:NSLayoutAttributeTop
multiplier:1
constant:kTopOffset];
self.leftOffset = [NSLayoutConstraint constraintWithItem:sv
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:ov
attribute:NSLayoutAttributeLeading
multiplier:1
constant:kViewControlsOffsetToBounds];
[ov addConstraints:@[ self.topOffset, self.leftOffset ]];
}
- (void)mwm_refreshUI
{
[self.view mwm_refreshUI];
[self refreshAppearance];
}
- (void)setHidden:(BOOL)hidden
{
_hidden = hidden;
[self refreshLayout];
}
- (void)refreshLayout
{
runAsyncOnMainQueue(^{
auto const availableArea = self.availableArea;
auto const leftOffset =
self.hidden ? -self.view.width : availableArea.origin.x + kViewControlsOffsetToBounds;
UIView * ov = self.view.superview;
[ov setNeedsLayout];
self.topOffset.constant = availableArea.origin.y + kTopOffset;
self.leftOffset.constant = leftOffset;
[UIView animateWithDuration:kDefaultAnimationDuration
animations:^{
[ov layoutIfNeeded];
}];
});
}
- (void)refreshAppearance
{
MWMButton * btn = static_cast<MWMButton *>(self.view);
UIImageView * iv = btn.imageView;
// Traffic state machine: https://confluence.mail.ru/pages/viewpage.action?pageId=103680959
[iv stopAnimating];
switch ([MWMTrafficManager state])
{
case MWMTrafficManagerStateDisabled: btn.imageName = @"btn_traffic_off"; break;
case MWMTrafficManagerStateEnabled: btn.imageName = @"btn_traffic_on"; break;
case MWMTrafficManagerStateWaitingData:
iv.animationImages = imagesWithName(@"btn_traffic_update");
iv.animationDuration = 0.8;
[iv startAnimating];
break;
case MWMTrafficManagerStateOutdated: btn.imageName = @"btn_traffic_outdated"; break;
case MWMTrafficManagerStateNoData:
btn.imageName = @"btn_traffic_on";
[MWMToast showWithText:L(@"traffic_data_unavailable")];
break;
case MWMTrafficManagerStateNetworkError:
btn.imageName = @"btn_traffic_off";
[MWMTrafficManager enableTraffic:NO];
[[MWMAlertViewController activeAlertController] presentNoConnectionAlert];
break;
case MWMTrafficManagerStateExpiredData:
btn.imageName = @"btn_traffic_on";
[MWMToast showWithText:L(@"traffic_update_maps_text")];
break;
case MWMTrafficManagerStateExpiredApp:
btn.imageName = @"btn_traffic_on";
[MWMToast showWithText:L(@"traffic_update_app_message")];
break;
}
}
- (IBAction)buttonTouchUpInside
{
[MWMTrafficManager enableTraffic:[MWMTrafficManager state] == MWMTrafficManagerStateDisabled];
}
+ (void)updateAvailableArea:(CGRect)frame
{
auto controller = [self controller];
if (CGRectEqualToRect(controller.availableArea, frame))
return;
controller.availableArea = frame;
[controller refreshLayout];
}
#pragma mark - MWMTrafficManagerObserver
- (void)onTrafficStateUpdated { [self refreshAppearance]; }
@end
|