-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathOADefaultSpeedViewController.mm
More file actions
263 lines (229 loc) · 8.71 KB
/
Copy pathOADefaultSpeedViewController.mm
File metadata and controls
263 lines (229 loc) · 8.71 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
//
// OADefaultSpeedViewController.mm
// OsmAnd Maps
//
// Created by Anna Bibyk on 30.06.2020.
// Copyright © 2020 OsmAnd. All rights reserved.
//
#import "OADefaultSpeedViewController.h"
#import "OAValueTableViewCell.h"
#import "OASliderWithValuesCell.h"
#import "OAAppSettings.h"
#import "OAApplicationMode.h"
#import "Localization.h"
#import "OsmAnd_Maps-Swift.h"
#import "OsmAndApp.h"
#import "OsmAndAppImpl.h"
#import "OARoutingHelper.h"
#import "OAOsmAndFormatter.h"
#import "GeneratedAssetSymbols.h"
@implementation OADefaultSpeedViewController
{
NSArray<NSDictionary *> *_data;
NSDictionary *_speedParameters;
CGFloat _ratio;
NSInteger _maxValue;
NSInteger _minValue;
NSInteger _defaultValue;
NSInteger _selectedValue;
NSInteger _maxSpeedMaxValue;
NSInteger _maxSpeedMinValue;
NSInteger _maxSpeedValue;
NSString *_units;
}
#pragma mark - Initialization
- (instancetype)initWithApplicationMode:(OAApplicationMode *)am speedParameters:(NSDictionary *)speedParameters
{
self = [super initWithAppMode:am];
if (self)
{
_speedParameters = speedParameters;
[self postInit];
}
return self;
}
- (void)postInit
{
OAAppSettings *settings = [OAAppSettings sharedManager];
_units = [OASpeedConstant toShortString:[settings.speedSystem get:self.appMode]];
switch ([settings.speedSystem get:self.appMode])
{
case MILES_PER_HOUR:
_ratio = 3600. / METERS_IN_ONE_MILE;
break;
case KILOMETERS_PER_HOUR:
_ratio = 3600. / METERS_IN_KILOMETER;
break;
case MINUTES_PER_KILOMETER:
_ratio = 3600. / METERS_IN_KILOMETER;
_units = OALocalizedString(@"km_h");
break;
case NAUTICALMILES_PER_HOUR:
_ratio = 3600. / METERS_IN_ONE_NAUTICALMILE;
break;
case MINUTES_PER_MILE:
_ratio = 3600. / METERS_IN_ONE_MILE;
_units = OALocalizedString(@"mile_per_hour");
break;
case METERS_PER_SECOND:
_ratio = 1;
break;
}
CGFloat settingsDefaultSpeed = self.appMode.getDefaultSpeed;
CGFloat settingsMaxSpeed = self.appMode.getMaxSpeed;
auto router = [OsmAndApp.instance getRouter:self.appMode];
if (!router || self.appMode.getRouterService == STRAIGHT || self.appMode.getRouterService == DIRECT_TO)
{
_minValue = round(MIN(1, settingsDefaultSpeed) * _ratio);
_maxValue = round(MAX(300, settingsDefaultSpeed) * _ratio);
_maxSpeedMinValue = 0;
_maxSpeedMaxValue = round(MAX(300, settingsMaxSpeed) * _ratio);
}
else
{
_minValue = round(router->getMinSpeed() * _ratio / 2.);
_maxValue = round(router->getMaxSpeed() * _ratio * 1.5);
_maxSpeedMinValue = 0;
_maxSpeedMaxValue = round(router->getMaxSpeed() * _ratio * 1.5);
}
_defaultValue = round(self.appMode.getDefaultSpeed * _ratio);
_maxSpeedValue = round(self.appMode.getMaxSpeed * _ratio);
}
#pragma mark - Base UI
- (NSString *)getTitle
{
return OALocalizedString(@"default_speed_setting_title");
}
- (NSString *)getLeftNavbarButtonTitle
{
return OALocalizedString(@"shared_string_cancel");
}
- (NSArray<UIBarButtonItem *> *)getRightNavbarButtons
{
return @[[self createRightNavbarButton:OALocalizedString(@"shared_string_done")
iconName:nil
action:@selector(onRightNavbarButtonPressed)
menu:nil]];
}
#pragma mark - Table data
- (void)generateData
{
NSMutableArray *tableData = [NSMutableArray array];
if (_selectedValue == 0)
_selectedValue = _defaultValue;
[tableData addObject:@{
@"type" : [OAValueTableViewCell getCellIdentifier],
@"title" : OALocalizedString(@"default_speed_setting_title"),
@"value" : [NSString stringWithFormat:@"%ld %@", (long)_selectedValue, _units],
}];
[tableData addObject:@{
@"type" : [OASliderWithValuesCell getCellIdentifier],
@"minValue" : [NSString stringWithFormat:@"%ld %@", (long)_minValue, _units],
@"maxValue" : [NSString stringWithFormat:@"%ld %@", (long)_maxValue, _units],
@"tag": @(0)
}];
NSString *maxSpeedStr = (_maxSpeedValue == 0) ? OALocalizedString(@"shared_string_no") : [NSString stringWithFormat:@"%ld %@", (long)_maxSpeedValue, _units];
[tableData addObject:@{
@"type" : [OAValueTableViewCell getCellIdentifier],
@"title" : OALocalizedString(@"max_speed"),
@"value" : maxSpeedStr,
}];
if (_maxSpeedMaxValue < 120) _maxSpeedMaxValue = 120; // fallback
[tableData addObject:@{
@"type" : [OASliderWithValuesCell getCellIdentifier],
@"minValue" : OALocalizedString(@"shared_string_no"),
@"maxValue" : [NSString stringWithFormat:@"%ld %@", (long)_maxSpeedMaxValue, _units],
@"tag": @(1)
}];
_data = [NSArray arrayWithArray:tableData];
}
- (NSString *)getTitleForFooter:(NSInteger)section
{
return [NSString stringWithFormat:@"%@\n\n%@", OALocalizedString(@"default_speed_dialog_msg"), OALocalizedString(@"road_max_speed_descr")];
}
- (NSInteger)rowsCount:(NSInteger)section
{
return _data.count;
}
- (UITableViewCell *)getRow:(NSIndexPath *)indexPath
{
NSDictionary *item = _data[indexPath.row];
NSString *cellType = item[@"type"];
if ([cellType isEqualToString:[OAValueTableViewCell getCellIdentifier]])
{
OAValueTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:[OAValueTableViewCell getCellIdentifier]];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:[OAValueTableViewCell getCellIdentifier] owner:self options:nil];
cell = (OAValueTableViewCell *) nib[0];
[cell leftIconVisibility:NO];
[cell descriptionVisibility:NO];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.valueLabel.textColor = [UIColor colorNamed:ACColorNameTextColorPrimary];
}
if (cell)
{
cell.titleLabel.text = item[@"title"];
cell.valueLabel.text = item[@"value"];
}
return cell;
}
else if ([cellType isEqualToString:[OASliderWithValuesCell getCellIdentifier]])
{
OASliderWithValuesCell *cell = [self.tableView dequeueReusableCellWithIdentifier:[OASliderWithValuesCell getCellIdentifier]];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:[OASliderWithValuesCell getCellIdentifier] owner:self options:nil];
cell = (OASliderWithValuesCell *)[nib objectAtIndex:0];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.sliderView.continuous = YES;
}
if (cell)
{
cell.leftValueLabel.text = item[@"minValue"];
cell.rightValueLabel.text = item[@"maxValue"];
NSInteger tag = [item[@"tag"] integerValue];
cell.sliderView.tag = tag;
if (tag == 0) {
cell.sliderView.minimumValue = _minValue;
cell.sliderView.maximumValue = _maxValue;
cell.sliderView.value = _selectedValue;
} else {
cell.sliderView.minimumValue = _maxSpeedMinValue;
cell.sliderView.maximumValue = _maxSpeedMaxValue;
cell.sliderView.value = _maxSpeedValue;
}
[cell.sliderView removeTarget:self action:NULL forControlEvents:UIControlEventValueChanged];
[cell.sliderView addTarget:self action:@selector(speedValueChanged:) forControlEvents:UIControlEventValueChanged];
}
return cell;
}
return nil;
}
- (NSInteger)sectionsCount
{
return 1;
}
#pragma mark - Selectors
- (void)onRightNavbarButtonPressed
{
OARoutingHelper *routingHelper = [OARoutingHelper sharedInstance];
[self.appMode setDefaultSpeed:_selectedValue / _ratio];
[self.appMode setMaxSpeed:_maxSpeedValue / _ratio];
if (self.appMode == [routingHelper getAppMode] && ([routingHelper isRouteCalculated] || [routingHelper isRouteBeingCalculated]))
[routingHelper recalculateRouteDueToSettingsChange];
[self dismissViewController];
}
- (void)speedValueChanged:(UISlider *)sender
{
if (sender.tag == 0) {
_selectedValue = sender.value;
[self generateData];
[self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
} else if (sender.tag == 1) {
_maxSpeedValue = sender.value;
[self generateData];
[self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:2 inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
}
}
@end