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

MWMMigrationViewController.mm « Migration « Classes « Maps « iphone - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5c3075ed02f7c58b7c6389a798f6f67a178bf8c3 (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
#import "LocationManager.h"
#import "MapsAppDelegate.h"
#import "MWMAlertViewController.h"
#import "MWMCircularProgress.h"
#import "MWMMapDownloaderViewController.h"
#import "MWMMigrationView.h"
#import "MWMMigrationViewController.h"
#import "MWMStorage.h"
#import "Statistics.h"

#include "Framework.h"

#include "platform/platform.hpp"
#include "storage/storage.hpp"

namespace
{
NSString * const kDownloaderSegue = @"Migration2MapDownloaderSegue";
} // namespace

using namespace storage;

@interface MWMStorage ()

+ (void)checkConnectionAndPerformAction:(TMWMVoidBlock)action alertController:(MWMAlertViewController *)alertController;

@end

@interface MWMMigrationViewController () <MWMCircularProgressProtocol>

@end

@implementation MWMMigrationViewController
{
  TCountryId m_countryId;
}

- (void)viewDidLoad
{
  [super viewDidLoad];
  m_countryId = kInvalidCountryId;
  [self configNavBar];
  [self checkState];
  static_cast<MWMMigrationView *>(self.view).delegate = self;
}

- (void)configNavBar
{
  self.title = L(@"download_maps");
}

- (void)checkState
{
  if (!GetFramework().IsEnoughSpaceForMigrate())
    [self setState:MWMMigrationViewState::ErrorNoSpace];
  else if (!Platform::IsConnected())
    [self setState:MWMMigrationViewState::ErrorNoConnection];
  else
    [self setState:MWMMigrationViewState::Default];
}

- (void)performLimitedMigration:(BOOL)limited
{
  [Statistics logEvent:kStatDownloaderMigrationStarted
        withParameters:@{kStatType : limited ? kStatCurrentMap : kStatAllMaps}];
  auto & f = GetFramework();
  LocationManager * lm = [MapsAppDelegate theApp].locationManager;
  ms::LatLon position{};
  if (![lm getLat:position.lat Lon:position.lon])
    position = MercatorBounds::ToLatLon(f.GetViewportCenter());

  auto migrate = ^
  {
    GetFramework().Migrate(!limited);
    [self performSegueWithIdentifier:kDownloaderSegue sender:self];
    [Statistics logEvent:kStatDownloaderMigrationCompleted];
  };

  auto onStatusChanged = [self, migrate](TCountryId const & countryId)
  {
    if (m_countryId == kInvalidCountryId || m_countryId != countryId)
      return;
    auto & f = GetFramework();
    auto s = f.Storage().GetPrefetchStorage();
    NodeStatuses nodeStatuses{};
    s->GetNodeStatuses(countryId, nodeStatuses);
    switch (nodeStatuses.m_status)
    {
      case NodeStatus::OnDisk:
        migrate();
        break;
      case NodeStatus::Undefined:
      case NodeStatus::Error:
        [self showError:nodeStatuses.m_error countryId:countryId]; break;
      default:
        break;
    }
  };

  auto onProgressChanged = [self](TCountryId const & countryId, TLocalAndRemoteSize const & progress)
  {
    MWMMigrationView * view = static_cast<MWMMigrationView *>(self.view);
    [view setProgress:static_cast<CGFloat>(progress.first) / progress.second];
  };

  [MWMStorage checkConnectionAndPerformAction:^
  {
    self->m_countryId = f.PreMigrate(position, onStatusChanged, onProgressChanged);
    if (self->m_countryId != kInvalidCountryId)
      [self setState:MWMMigrationViewState::Processing];
    else
      migrate();
  }
  alertController:self.alertController];
}

- (void)showError:(NodeErrorCode)errorCode countryId:(TCountryId const &)countryId
{
  [self setState:MWMMigrationViewState::Default];
  MWMAlertViewController * avc = self.alertController;
  auto const retryBlock = ^
  {
    GetFramework().Storage().GetPrefetchStorage()->RetryDownloadNode(self->m_countryId);
    [self setState:MWMMigrationViewState::Processing];
  };
  auto const cancelBlock = ^
  {
    GetFramework().Storage().GetPrefetchStorage()->CancelDownloadNode(self->m_countryId);
  };
  switch (errorCode)
  {
    case NodeErrorCode::NoError:
      break;
    case NodeErrorCode::UnknownError:
      [Statistics logEvent:kStatDownloaderMigrationError withParameters:@{kStatType : kStatUnknownError}];
      [avc presentDownloaderInternalErrorAlertWithOkBlock:retryBlock cancelBlock:cancelBlock];
      [avc presentInternalErrorAlert];
      break;
    case NodeErrorCode::OutOfMemFailed:
      [Statistics logEvent:kStatDownloaderMigrationError withParameters:@{kStatType : kStatNoSpace}];
      [avc presentDownloaderNotEnoughSpaceAlert];
      break;
    case NodeErrorCode::NoInetConnection:
      [Statistics logEvent:kStatDownloaderMigrationError withParameters:@{kStatType : kStatNoConnection}];
      [avc presentDownloaderNoConnectionAlertWithOkBlock:retryBlock cancelBlock:cancelBlock];
      break;
  }
}

- (void)setState:(MWMMigrationViewState)state
{
  MWMMigrationView * migrationView = static_cast<MWMMigrationView *>(self.view);
  if (state == MWMMigrationViewState::Processing)
  {
    NodeAttrs nodeAttrs;
    GetFramework().Storage().GetPrefetchStorage()->GetNodeAttrs(m_countryId, nodeAttrs);
    migrationView.nodeLocalName = @(nodeAttrs.m_nodeLocalName.c_str());
    self.navigationItem.leftBarButtonItem.enabled = NO;
  }
  else
  {
    self.navigationItem.leftBarButtonItem.enabled = YES;
  }
  migrationView.state = state;
}

#pragma mark - MWMCircularProgressProtocol

- (void)progressButtonPressed:(MWMCircularProgress *)progress
{
  GetFramework().Storage().GetPrefetchStorage()->CancelDownloadNode(m_countryId);
  [self setState:MWMMigrationViewState::Default];
}

#pragma mark - Segue

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
  if ([segue.identifier isEqualToString:kDownloaderSegue])
  {
    MWMMapDownloaderViewController * dvc = segue.destinationViewController;
    [dvc setParentCountryId:@(GetFramework().Storage().GetRootId().c_str()) mode:mwm::DownloaderMode::Downloaded];
  }
}

#pragma mark - Actions

- (IBAction)primaryAction
{
  [self performLimitedMigration:NO];
}

- (IBAction)secondaryAction
{
  [self performLimitedMigration:YES];
}

@end