Welcome to mirror list, hosted at ThFree Co, Russian Federation.

MWMSettingsViewController.mm « Settings « UI « Maps « iphone - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7b48af0d2f72cd178002c963bbc20820a22cae16 (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
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
#import "MWMSettingsViewController.h"
#import "MWMAuthorizationCommon.h"
#import "MWMNetworkPolicy.h"
#import "MWMTextToSpeech+CPP.h"
#import "SwiftBridge.h"

#include "LocaleTranslator.h"

#include "Framework.h"

#include "routing/speed_camera_manager.hpp"

#include "map/gps_tracker.hpp"
#include "map/routing_manager.hpp"

#include "base/assert.hpp"

extern NSString * const kAlohalyticsTapEventKey;

using namespace power_management;

@interface MWMSettingsViewController ()<SettingsTableViewSwitchCellDelegate, RemoveAdsViewControllerDelegate>

@property(weak, nonatomic) IBOutlet SettingsTableViewLinkCell * profileCell;

@property(weak, nonatomic) IBOutlet SettingsTableViewLinkCell * unitsCell;
@property(weak, nonatomic) IBOutlet SettingsTableViewSwitchCell * zoomButtonsCell;
@property(weak, nonatomic) IBOutlet SettingsTableViewSwitchCell * is3dCell;
@property(weak, nonatomic) IBOutlet SettingsTableViewSwitchCell * autoDownloadCell;
@property(weak, nonatomic) IBOutlet SettingsTableViewSwitchCell * backupBookmarksCell;
@property(weak, nonatomic) IBOutlet SettingsTableViewLinkCell * mobileInternetCell;
@property(weak, nonatomic) IBOutlet SettingsTableViewLinkCell * powerManagementCell;
@property(weak, nonatomic) IBOutlet SettingsTableViewLinkCell * recentTrackCell;
@property(weak, nonatomic) IBOutlet SettingsTableViewSwitchCell * fontScaleCell;
@property(weak, nonatomic) IBOutlet SettingsTableViewSwitchCell * transliterationCell;
@property(weak, nonatomic) IBOutlet SettingsTableViewSwitchCell * compassCalibrationCell;
@property(weak, nonatomic) IBOutlet SettingsTableViewSwitchCell * showOffersCell;
@property(weak, nonatomic) IBOutlet SettingsTableViewSwitchCell * statisticsCell;

@property(weak, nonatomic) IBOutlet SettingsTableViewSelectableProgressCell * restoreSubscriptionCell;
@property(weak, nonatomic) IBOutlet SettingsTableViewLinkCell * manageSubscriptionsCell;

@property(weak, nonatomic) IBOutlet SettingsTableViewLinkCell * nightModeCell;
@property(weak, nonatomic) IBOutlet SettingsTableViewSwitchCell * perspectiveViewCell;
@property(weak, nonatomic) IBOutlet SettingsTableViewSwitchCell * autoZoomCell;
@property(weak, nonatomic) IBOutlet SettingsTableViewLinkCell * voiceInstructionsCell;
@property(weak, nonatomic) IBOutlet SettingsTableViewLinkCell * drivingOptionsCell;

@property(weak, nonatomic) IBOutlet SettingsTableViewLinkCell * helpCell;
@property(weak, nonatomic) IBOutlet SettingsTableViewLinkCell * aboutCell;

@property(nonatomic) BOOL restoringSubscription;

@end

@implementation MWMSettingsViewController

- (void)viewDidLoad
{
  [super viewDidLoad];
  self.title = L(@"settings");
}

- (void)viewWillAppear:(BOOL)animated
{
  [super viewWillAppear:animated];
  [self configCells];
}

- (void)configCells
{
  [self configProfileSection];
  [self configCommonSection];
  [self configSubsriptionsSection];
  [self configNavigationSection];
  [self configInfoSection];
}

- (void)configProfileSection
{
  NSString * userName = osm_auth_ios::OSMUserName();
  [self.profileCell configWithTitle:L(@"profile") info:userName.length != 0 ? userName : @""];
}

- (void)configCommonSection
{
  NSString * units = nil;
  switch ([MWMSettings measurementUnits])
  {
  case MWMUnitsMetric: units = L(@"kilometres"); break;
  case MWMUnitsImperial: units = L(@"miles"); break;
  }
  [self.unitsCell configWithTitle:L(@"measurement_units") info:units];

  [self.zoomButtonsCell configWithDelegate:self
                                     title:L(@"pref_zoom_title")
                                      isOn:[MWMSettings zoomButtonsEnabled]];

  bool on = true, _ = true;
  GetFramework().Load3dMode(_, on);
  [self.is3dCell configWithDelegate:self title:L(@"pref_map_3d_buildings_title") isOn:on];

  [self.autoDownloadCell configWithDelegate:self
                                      title:L(@"disable_autodownload")
                                       isOn:[MWMSettings autoDownloadEnabled]];

  [self.backupBookmarksCell configWithDelegate:self
                                         title:L(@"settings_backup_bookmarks")
                                          isOn:[[MWMBookmarksManager sharedManager] isCloudEnabled]];

  NSString * mobileInternet = nil;
  switch (network_policy::GetStage())
  {
  case network_policy::Ask:
  case network_policy::Today:
  case network_policy::NotToday: mobileInternet = L(@"mobile_data_option_ask"); break;
  case network_policy::Always: mobileInternet = L(@"mobile_data_option_always"); break;
  case network_policy::Never: mobileInternet = L(@"mobile_data_option_never"); break;
  }
  [self.mobileInternetCell configWithTitle:L(@"mobile_data") info:mobileInternet];
  
  NSString * powerManagement = nil;
  switch (GetFramework().GetPowerManager().GetScheme())
  {
  case Scheme::None: break;
  case Scheme::Normal: powerManagement = L(@"power_managment_setting_never"); break;
  case Scheme::EconomyMedium: break;
  case Scheme::EconomyMaximum: powerManagement = L(@"power_managment_setting_manual_max"); break;
  case Scheme::Auto: powerManagement = L(@"power_managment_setting_auto"); break;
  }
  [self.powerManagementCell configWithTitle:L(@"power_managment_title") info:powerManagement];

  NSString * recentTrack = nil;
  if (!GpsTracker::Instance().IsEnabled())
  {
    recentTrack = L(@"duration_disabled");
  }
  else
  {
    switch (GpsTracker::Instance().GetDuration().count())
    {
    case 1: recentTrack = L(@"duration_1_hour"); break;
    case 2: recentTrack = L(@"duration_2_hours"); break;
    case 6: recentTrack = L(@"duration_6_hours"); break;
    case 12: recentTrack = L(@"duration_12_hours"); break;
    case 24: recentTrack = L(@"duration_1_day"); break;
    default: NSAssert(false, @"Incorrect hours value"); break;
    }
  }
  [self.recentTrackCell configWithTitle:L(@"pref_track_record_title") info:recentTrack];

  [self.fontScaleCell configWithDelegate:self
                                   title:L(@"big_font")
                                    isOn:[MWMSettings largeFontSize]];

  [self.transliterationCell configWithDelegate:self
                                         title:L(@"whatsnew_transliteration_title")
                                          isOn:[MWMSettings transliteration]];

  [self.compassCalibrationCell configWithDelegate:self
                                            title:L(@"pref_calibration_title")
                                             isOn:[MWMSettings compassCalibrationEnabled]];

  auto & purchase = GetFramework().GetPurchase();
  bool const hasSubscription = purchase && purchase->IsSubscriptionActive(SubscriptionType::RemoveAds);
  [self.showOffersCell configWithDelegate:self
                                    title:L(@"showcase_settings_title")
                                     isOn:!hasSubscription];
  self.showOffersCell.isEnabled = !hasSubscription;

  [self.statisticsCell configWithDelegate:self
                                    title:L(@"allow_statistics")
                                     isOn:[MWMSettings statisticsEnabled]];
}

- (void)configSubsriptionsSection {
  [self.restoreSubscriptionCell configWithTitle:L(@"restore_subscription")];
  [self.manageSubscriptionsCell configWithTitle:L(@"manage_subscription") info:nil];
}

- (void)configNavigationSection
{
  NSString * nightMode = nil;
  switch ([MWMSettings theme])
  {
  case MWMThemeVehicleDay: NSAssert(false, @"Invalid case");
  case MWMThemeDay: nightMode = L(@"pref_map_style_default"); break;
  case MWMThemeVehicleNight: NSAssert(false, @"Invalid case");
  case MWMThemeNight: nightMode = L(@"pref_map_style_night"); break;
  case MWMThemeAuto: nightMode = L(@"pref_map_style_auto"); break;
  }
  [self.nightModeCell configWithTitle:L(@"pref_map_style_title") info:nightMode];

  bool _ = true, on = true;
  auto & f = GetFramework();
  f.Load3dMode(on, _);
  [self.perspectiveViewCell configWithDelegate:self title:L(@"pref_map_3d_title") isOn:on];

  [self.autoZoomCell configWithDelegate:self
                                  title:L(@"pref_map_auto_zoom")
                                   isOn:GetFramework().LoadAutoZoom()];

  NSString * ttsEnabledString = [MWMTextToSpeech isTTSEnabled] ? L(@"on") : L(@"off");
  [self.voiceInstructionsCell configWithTitle:L(@"pref_tts_enable_title") info:ttsEnabledString];
  [self.drivingOptionsCell configWithTitle:L(@"driving_options_title") info:@""];
}

- (void)configInfoSection
{
  [self.helpCell configWithTitle:L(@"help") info:nil];
  [self.aboutCell configWithTitle:L(@"about_menu_title") info:nil];
}

- (void)showRemoveAds
{
  auto removeAds = [[RemoveAdsViewController alloc] init];
  removeAds.delegate = self;
  [self.navigationController presentViewController:removeAds animated:YES completion:nil];
}

#pragma mark - SettingsTableViewSwitchCellDelegate

- (void)switchCell:(SettingsTableViewSwitchCell *)cell didChangeValue:(BOOL)value
{
  if (cell == self.zoomButtonsCell)
  {
    [Statistics logEvent:kStatEventName(kStatSettings, kStatToggleZoomButtonsVisibility)
          withParameters:@{kStatValue : (value ? kStatVisible : kStatHidden)}];
    [MWMSettings setZoomButtonsEnabled:value];
  }
  else if (cell == self.is3dCell)
  {
    [Statistics logEvent:kStatEventName(kStatSettings, kStat3DBuildings)
          withParameters:@{kStatValue : (value ? kStatOn : kStatOff)}];
    auto & f = GetFramework();
    bool _ = true, is3dBuildings = true;
    f.Load3dMode(_, is3dBuildings);
    is3dBuildings = static_cast<bool>(value);
    f.Save3dMode(_, is3dBuildings);
    f.Allow3dMode(_, is3dBuildings);
  }
  else if (cell == self.autoDownloadCell)
  {
    [Statistics logEvent:kStatEventName(kStatSettings, kStatAutoDownload)
          withParameters:@{kStatValue : (value ? kStatOn : kStatOff)}];
    [MWMSettings setAutoDownloadEnabled:value];
  }
  else if (cell == self.backupBookmarksCell)
  {
    [Statistics logEvent:kStatSettingsBookmarksSyncToggle
          withParameters:@{
            kStatState: (value ? @1 : @0)
          }];
    [[MWMBookmarksManager sharedManager] setCloudEnabled:value];
  }
  else if (cell == self.fontScaleCell)
  {
    [Statistics logEvent:kStatEventName(kStatSettings, kStatToggleLargeFontSize)
          withParameters:@{kStatValue : (value ? kStatOn : kStatOff)}];
    [MWMSettings setLargeFontSize:value];
  }
  else if (cell == self.transliterationCell)
  {
    [Statistics logEvent:kStatEventName(kStatSettings, kStatToggleTransliteration)
          withParameters:@{kStatValue : (value ? kStatOn : kStatOff)}];
    [MWMSettings setTransliteration:value];
  }
  else if (cell == self.compassCalibrationCell)
  {
    [Statistics logEvent:kStatEventName(kStatSettings, kStatToggleCompassCalibration)
          withParameters:@{kStatValue : (value ? kStatOn : kStatOff)}];
    [MWMSettings setCompassCalibrationEnabled:value];
  }
  else if (cell == self.showOffersCell)
  {
    [self showRemoveAds];
    [Statistics logEvent:kStatEventName(kStatSettings, kStatAd)
          withParameters:@{kStatAction : kStatAd, kStatValue : (value ? kStatOn : kStatOff)}];
  }
  else if (cell == self.statisticsCell)
  {
    [Statistics logEvent:kStatEventName(kStatSettings, kStatToggleStatistics)
          withParameters:@{
            kStatAction : kStatToggleStatistics,
            kStatValue : (value ? kStatOn : kStatOff)
          }];
    [MWMSettings setStatisticsEnabled:value];
  }
  else if (cell == self.perspectiveViewCell)
  {
    [Statistics logEvent:kStatEventName(kStatSettings, kStat3D)
          withParameters:@{kStatValue : (value ? kStatOn : kStatOff)}];
    auto & f = GetFramework();
    bool _ = true, is3d = true;
    f.Load3dMode(is3d, _);
    is3d = static_cast<bool>(value);
    f.Save3dMode(is3d, _);
    f.Allow3dMode(is3d, _);
  }
  else if (cell == self.autoZoomCell)
  {
    [Statistics logEvent:kStatEventName(kStatSettings, kStatAutoZoom)
          withParameters:@{kStatValue : value ? kStatOn : kStatOff}];
    auto & f = GetFramework();
    f.AllowAutoZoom(value);
    f.SaveAutoZoom(value);
  }
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  auto cell = [tableView cellForRowAtIndexPath:indexPath];
  if (cell == self.profileCell)
  {
    [Statistics logEvent:kStatSettingsOpenSection withParameters:@{kStatName : kStatAuthorization}];
    [self performSegueWithIdentifier:@"SettingsToProfileSegue" sender:nil];
  }
  else if (cell == self.unitsCell)
  {
    [Statistics logEvent:kStatEventName(kStatSettings, kStatChangeMeasureUnits)
          withParameters:@{kStatAction : kStatChangeMeasureUnits}];
    [self performSegueWithIdentifier:@"SettingsToUnits" sender:nil];
  }
  else if (cell == self.mobileInternetCell)
  {
    [Statistics logEvent:kStatEventName(kStatSettings, kStatMobileInternet)
          withParameters:@{kStatAction : kStatChangeMobileInternet}];
    [self performSegueWithIdentifier:@"SettingsToMobileInternetSegue" sender:nil];
  }
  else if (cell == self.powerManagementCell)
  {
    [self performSegueWithIdentifier:@"SettingsToPowerManagementSegue" sender:nil];
  }
  else if (cell == self.recentTrackCell)
  {
    [Statistics logEvent:kStatEventName(kStatSettings, kStatRecentTrack)
          withParameters:@{kStatAction : kStatChangeRecentTrack}];
    [self performSegueWithIdentifier:@"SettingsToRecentTrackSegue" sender:nil];
  }
  else if (cell == self.nightModeCell)
  {
    [Statistics logEvent:kStatEventName(kStatSettings, kStatNightMode)
          withParameters:@{kStatAction : kStatChangeNightMode}];
    [self performSegueWithIdentifier:@"SettingsToNightMode" sender:nil];
  }
  else if (cell == self.voiceInstructionsCell)
  {
    [Statistics logEvent:kStatEventName(kStatSettings, kStatTTS)
          withParameters:@{kStatAction : kStatChangeLanguage}];
    [self performSegueWithIdentifier:@"SettingsToTTSSegue" sender:nil];
  }
  else if (cell == self.drivingOptionsCell)
  {
    [self performSegueWithIdentifier:@"settingsToDrivingOptionsSegue" sender:nil];
  }
  else if (cell == self.helpCell)
  {
    [Statistics logEvent:kStatSettingsOpenSection withParameters:@{kStatName : kStatHelp}];
    [Alohalytics logEvent:kAlohalyticsTapEventKey withValue:@"help"];
    [self performSegueWithIdentifier:@"SettingsToHelp" sender:nil];
  }
  else if (cell == self.aboutCell)
  {
    [Statistics logEvent:kStatSettingsOpenSection withParameters:@{kStatName : kStatAbout}];
    [self performSegueWithIdentifier:@"SettingsToAbout" sender:nil];
  }
  else if (cell == self.restoreSubscriptionCell)
  {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    __weak auto s = self;
    [self signupWithAnchor:self.restoreSubscriptionCell.progress onComplete:^(BOOL success) {
      if (success) {
        [s restoreSubscription];
      }
    }];
  }
  else if (cell == self.manageSubscriptionsCell)
  {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [UIApplication.sharedApplication
     openURL:[NSURL URLWithString:@"https://buy.itunes.apple.com/WebObjects/MZFinance.woa/wa/manageSubscriptions"]
     options:@{}
     completionHandler:nil];
  }
}

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  auto cell = [tableView cellForRowAtIndexPath:indexPath];
  if (cell == self.restoreSubscriptionCell)
    return self.restoringSubscription ? nil : indexPath;

  return indexPath;
}

#pragma mark - UITableViewDataSource

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
  switch (section)
  {
  case 1: return L(@"general_settings");
  case 2: return L(@"subscriptions_title");
  case 3: return L(@"prefs_group_route");
  case 4: return L(@"info");
  default: return nil;
  }
}

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
  switch (section)
  {
  case 1: return L(@"allow_statistics_hint");
  default: return nil;
  }
}

#pragma mark - RemoveAdsViewControllerDelegate

- (void)didCompleteSubscribtion:(RemoveAdsViewController *)viewController
{
  [self.navigationController dismissViewControllerAnimated:YES completion:nil];
  self.showOffersCell.isEnabled = NO;
}

- (void)didCancelSubscribtion:(RemoveAdsViewController *)viewController
{
  [self.navigationController dismissViewControllerAnimated:YES completion:^{
    self.showOffersCell.isOn = YES;
  }];
}

#pragma mark - RestoreSubscription

- (void)restoreSubscription {
  dispatch_group_t dispatchGroup = dispatch_group_create();

  [self.restoreSubscriptionCell.progress startAnimating];
  self.restoringSubscription = YES;
  __block MWMValidationResult adsResult;
  __block MWMValidationResult bookmarksResult;

  dispatch_group_enter(dispatchGroup);
  [[InAppPurchase adsRemovalSubscriptionManager] restore:^(MWMValidationResult result) {
    adsResult = result;
    dispatch_group_leave(dispatchGroup);
  }];

  dispatch_group_enter(dispatchGroup);
  [[InAppPurchase bookmarksSubscriptionManager] restore:^(MWMValidationResult result) {
    bookmarksResult = result;
    dispatch_group_leave(dispatchGroup);
  }];

  __weak auto s = self;
  dispatch_group_notify(dispatchGroup, dispatch_get_main_queue(), ^{
    __strong auto self = s;
    self.restoringSubscription = NO;
    [self.restoreSubscriptionCell.progress stopAnimating];
    NSString *alertText;
    if (adsResult == MWMValidationResultNotValid && bookmarksResult == MWMValidationResultNotValid) {
      alertText = L(@"restore_no_subscription_alert");
    } else if (adsResult == MWMValidationResultValid || bookmarksResult == MWMValidationResultValid) {
      alertText = L(@"restore_success_alert");
    } else {
      alertText = L(@"restore_error_alert");
    }
    [MWMAlertViewController.activeAlertController presentInfoAlert:L(@"restore_subscription")
                                                              text:alertText];
  });
}

@end