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

CountryTreeVC.mm « Maps « iphone - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1529dbd3ee7b084eb8a2f9ba4462d64b0d32a817 (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

#import "ActiveMapsVC.h"
#import "Common.h"
#import "CountryTreeVC.h"
#import "Statistics.h"

extern NSString * const MapsStatusChangedNotification;

@interface CountryTreeVC () <CountryTreeObserverProtocol>

@end

@implementation CountryTreeVC
{
  CountryTree m_tree;
  CountryTreeObserver * m_treeObserver;
}

- (id)initWithNodePosition:(int)position
{
  self = [super init];

  if (position == -1)
  {
    self.tree.SetDefaultRoot();
    self.title = L(@"download_maps");
  }
  else
  {
    ASSERT(position < self.tree.GetChildCount(), ());
    self.title = @(self.tree.GetChildName(position).c_str());
    self.tree.SetChildAsRoot(position);
  }

  __weak CountryTreeVC * weakSelf = self;
  m_treeObserver = new CountryTreeObserver(weakSelf);
  self.tree.SetListener(m_treeObserver);

  return self;
}

- (void)viewDidLoad
{
  [super viewDidLoad];
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(outOfDateCountriesCountChanged:) name:MapsStatusChangedNotification object:nil];
}

- (void)viewWillAppear:(BOOL)animated
{
  [super viewWillAppear:animated];
  m_tree = CountryTree();
  [self.tableView reloadData];
}

- (void)viewWillDisappear:(BOOL)animated
{
  [super viewWillDisappear:animated];
  if (self.isMovingFromParentViewController)
  {
    if (self.tree.HasParent())
      self.tree.SetParentAsRoot();
    else
      self.tree.ResetListener();
  }
}

#define TOP_ROWS_COUNT 1

- (NSIndexPath *)downloadedCountriesIndexPath
{
  return [NSIndexPath indexPathForRow:0 inSection:0];
}

- (void)outOfDateCountriesCountChanged:(NSNotification *)notification
{
  NSInteger const count = [[notification userInfo][@"OutOfDate"] integerValue];
  if ([self activeMapsRowIsVisible])
  {
    MapCell * cell = (MapCell *)[self.tableView cellForRowAtIndexPath:[self downloadedCountriesIndexPath]];
    cell.badgeView.value = count;
  }
}

#pragma mark - Helpers

- (CountryTree &)tree
{
  if (m_tree.IsValid())
    return m_tree;
  else
    return GetFramework().GetCountryTree();
}

- (MapCell *)cellAtPositionInNode:(int)position
{
  NSInteger const section = [self activeMapsRowIsVisible] ? [self downloadedCountriesIndexPath].section + 1 : 0;
  return (MapCell *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:position inSection:section]];
}

- (BOOL)activeMapsRowIsVisible
{
  return !self.tree.GetActiveMapLayout().IsEmpty() && !self.tree.HasParent();
}

- (BOOL)isActiveMapsIndexPath:(NSIndexPath *)indexPath
{
  return [self activeMapsRowIsVisible] && [indexPath isEqual:[self downloadedCountriesIndexPath]];
}

- (void)configureSizeLabelOfMapCell:(MapCell *)cell position:(int)position status:(TStatus const &)status options:(MapOptions const &)options
{
  if (self.tree.IsLeaf(position))
  {
    if (status == TStatus::ENotDownloaded)
    {
      LocalAndRemoteSizeT const size = self.tree.GetRemoteLeafSizes(position);
      cell.sizeLabel.text = [NSString stringWithFormat:@"%@ / %@", formattedSize(size.first), formattedSize(size.second)];
    }
    else if (status == TStatus::EOnDisk || status == TStatus::EOnDiskOutOfDate)
      cell.sizeLabel.text = formattedSize(self.tree.GetLeafSize(position, options).second);
    else if (status == TStatus::EOutOfMemFailed || status == TStatus::EDownloadFailed || status == TStatus::EDownloading || status == TStatus::EInQueue)
      cell.sizeLabel.text = formattedSize(self.tree.GetDownloadableLeafSize(position).second);
  }
}

#pragma mark - DownloaderParentVC virtual methods implementation

- (NSString *)parentTitle
{
  return self.tree.IsCountryRoot() ? @(self.tree.GetRootName().c_str()) : nil;
}

- (NSString *)selectedMapName
{
  return @(self.tree.GetChildName(self.selectedPosition).c_str());
}

- (uint64_t)selectedMapSizeWithOptions:(MapOptions)options
{
  return self.tree.GetLeafSize(self.selectedPosition, options).second;
}

- (TStatus)selectedMapStatus
{
  return self.tree.GetLeafStatus(self.selectedPosition);
}

- (MapOptions)selectedMapOptions
{
  return self.tree.GetLeafOptions(self.selectedPosition);
}

- (void)performAction:(DownloaderAction)action withSizeCheck:(BOOL)check
{
  switch (action)
  {
    case DownloaderActionDownloadAll:
    case DownloaderActionDownloadMap:
    case DownloaderActionDownloadCarRouting:
      if (check == NO || [self canDownloadSelectedMap])
        self.tree.DownloadCountry(self.selectedPosition, self.selectedInActionSheetOptions);
      break;

    case DownloaderActionDeleteAll:
    case DownloaderActionDeleteMap:
    case DownloaderActionDeleteCarRouting:
      self.tree.DeleteCountry(self.selectedPosition, self.selectedInActionSheetOptions);
      break;

    case DownloaderActionCancelDownloading:
      self.tree.CancelDownloading(self.selectedPosition);
    break;

    case DownloaderActionZoomToCountry:
      self.tree.ShowLeafOnMap(self.selectedPosition);
      [[Statistics instance] logEvent:@"Show Map From Download Countries Screen"];
      [self.navigationController popToRootViewControllerAnimated:YES];
      break;
  }
}

#pragma mark - TableView

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
  return 13;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
  return 0.001;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
  return [self activeMapsRowIsVisible] ? 1 + TOP_ROWS_COUNT : 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
  if ([self activeMapsRowIsVisible] && section == [self downloadedCountriesIndexPath].section)
    return TOP_ROWS_COUNT;
  else
    return self.tree.GetChildCount();
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  MapCell * cell = [tableView dequeueReusableCellWithIdentifier:[MapCell className]];
  if (!cell)
    cell = [[MapCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[MapCell className]];

  if ([self isActiveMapsIndexPath:indexPath])
  {
    cell.titleLabel.text = L(@"downloader_downloaded_maps");
    cell.subtitleLabel.text = nil;
    cell.parentMode = YES;
    cell.badgeView.value = self.tree.GetActiveMapLayout().GetOutOfDateCount();
    cell.separatorTop.hidden = NO;
    cell.separatorBottom.hidden = NO;
    cell.separator.hidden = YES;
  }
  else
  {
    int const position = static_cast<int>(indexPath.row);
    bool const isLeaf = self.tree.IsLeaf(position);
    NSInteger const numberOfRows = [self tableView:tableView numberOfRowsInSection:indexPath.section];
    BOOL const isLast = (indexPath.row == numberOfRows - 1);
    BOOL const isFirst = (indexPath.row == 0);

    cell.titleLabel.text = @(self.tree.GetChildName(position).c_str());
    cell.subtitleLabel.text = [self parentTitle];
    cell.delegate = self;
    cell.badgeView.value = 0;
    cell.parentMode = !isLeaf;
    cell.separatorTop.hidden = !isFirst;
    cell.separatorBottom.hidden = !isLast;
    cell.separator.hidden = isLast;
    if (isLeaf)
    {
      MapOptions const options = self.tree.GetLeafOptions(position);
      TStatus const status = self.tree.GetLeafStatus(position);
      if (status == TStatus::EOutOfMemFailed || status == TStatus::EDownloadFailed || status == TStatus::EDownloading || status == TStatus::EInQueue)
      {
        LocalAndRemoteSizeT const size = self.tree.GetDownloadableLeafSize(position);
        cell.downloadProgress = (double)size.first / size.second;
      }
      cell.status = status;
      cell.options = options;
      [self configureSizeLabelOfMapCell:cell position:position status:status options:options];
    }
  }

  return cell;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
  int const position = static_cast<int>(indexPath.row);
  if (self.tree.IsLeaf(position))
  {
    TStatus const status = self.tree.GetLeafStatus(position);
    return status == TStatus::EOnDisk || status == TStatus::EOnDiskOutOfDate;
  }
  return NO;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
  if (editingStyle == UITableViewCellEditingStyleDelete)
  {
    int const position = static_cast<int>(indexPath.row);
    self.tree.DeleteCountry(position, self.tree.GetLeafOptions(position));
    [tableView setEditing:NO animated:YES];
  }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  [tableView deselectRowAtIndexPath:indexPath animated:YES];
  if ([self isActiveMapsIndexPath:indexPath])
  {
    ActiveMapsVC * vc = [[ActiveMapsVC alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
  }
  else
  {
    self.selectedPosition = static_cast<int>(indexPath.row);
    if (self.tree.IsLeaf(self.selectedPosition))
    {
      MapCell * cell = [self cellAtPositionInNode:self.selectedPosition];
      UIActionSheet * actionSheet = [self actionSheetToPerformActionOnSelectedMap];
      [actionSheet showFromRect:cell.frame inView:cell.superview animated:YES];
    }
    else
    {
      m_tree = self.tree;
      CountryTreeVC * vc = [[CountryTreeVC alloc] initWithNodePosition:self.selectedPosition];
      [self.navigationController pushViewController:vc animated:YES];
    }
  }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
  return [MapCell cellHeight];
}

#pragma mark - MapCellDelegate

- (void)mapCellDidStartDownloading:(MapCell *)cell
{
  self.selectedPosition = static_cast<int>([self.tableView indexPathForCell:cell].row);
  TStatus const status = [self selectedMapStatus];
  if (status == TStatus::EDownloadFailed || status == TStatus::EOutOfMemFailed)
    if ([self canDownloadSelectedMap])
      self.tree.RetryDownloading(self.selectedPosition);
}

- (void)mapCellDidCancelDownloading:(MapCell *)cell
{
  self.selectedPosition = static_cast<int>([self.tableView indexPathForCell:cell].row);
  [[self actionSheetToCancelDownloadingSelectedMap] showFromRect:cell.frame inView:cell.superview animated:YES];
}

#pragma mark - CountryTree core callbacks

- (void)countryStatusChangedAtPositionInNode:(int)position
{
  if ([self activeMapsRowIsVisible])
  {
    [self.tableView reloadData];
  }
  else
  {
    MapCell * cell = [self cellAtPositionInNode:position];
    TStatus const status = self.tree.GetLeafStatus(position);
    MapOptions const options = self.tree.GetLeafOptions(position);
    [self configureSizeLabelOfMapCell:cell position:position status:status options:options];
    [cell setStatus:self.tree.GetLeafStatus(position) options:options animated:YES];
  }
}

- (void)countryDownloadingProgressChanged:(LocalAndRemoteSizeT const &)progress atPositionInNode:(int)position
{
  MapCell * cell = [self cellAtPositionInNode:position];
  [cell setDownloadProgress:((double)progress.first / progress.second) animated:YES];
}

@end