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

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

#import "ActiveMapsVC.h"
#import "BadgeView.h"
#import "Common.h"
#import "MapCell.h"
#import "Statistics.h"

extern NSString * const MapsStatusChangedNotification;

@interface ActiveMapsVC () <ActiveMapsObserverProtocol>

@property (nonatomic) BadgeView * outOfDateBadge;
@property (nonatomic) ActiveMapsLayout::TGroup selectedGroup;

@end

@implementation ActiveMapsVC
{
  ActiveMapsObserver * m_mapsObserver;
  int m_mapsObserverSlotId;
}

- (id)init
{
  self = [super init];

  self.title = L(@"downloader_downloaded_maps");

  __weak ActiveMapsVC * weakSelf = self;
  m_mapsObserver = new ActiveMapsObserver(weakSelf);
  m_mapsObserverSlotId = self.mapsLayout.AddListener(m_mapsObserver);

  return self;
}

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

- (void)viewWillDisappear:(BOOL)animated
{
  [super viewWillDisappear:animated];

  if (self.isMovingFromParentViewController)
    self.mapsLayout.RemoveListener(m_mapsObserverSlotId);
}

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

- (void)outOfDateCountriesCountChanged:(NSNotification *)notification
{
  self.outOfDateBadge.value = [[notification userInfo][@"OutOfDate"] integerValue];
}

- (void)refreshTopRightButton
{
  UIBarButtonItem * item;
  if (self.mapsLayout.IsDownloadingActive())
    item = [[UIBarButtonItem alloc] initWithTitle:L(@"downloader_cancel_all") style:UIBarButtonItemStylePlain target:self action:@selector(cancelAllMaps:)];
  else if (self.mapsLayout.GetOutOfDateCount() > 0)
    item = [[UIBarButtonItem alloc] initWithTitle:L(@"downloader_update_all") style:UIBarButtonItemStylePlain target:self action:@selector(updateAllMaps:)];

  [self.navigationItem setRightBarButtonItem:item animated:YES];
}

- (void)updateAllMaps:(id)sender
{
  self.mapsLayout.UpdateAll();
}

- (void)cancelAllMaps:(id)sender
{
  self.mapsLayout.CancelAll();
}

#pragma mark - Helpers

- (ActiveMapsLayout &)mapsLayout
{
  return GetFramework().GetCountryTree().GetActiveMapLayout();
}

- (ActiveMapsLayout::TGroup)groupWithSection:(NSInteger)section
{
  ASSERT(section < (NSInteger)ActiveMapsLayout::TGroup::EGroupCount, ());
  return static_cast<ActiveMapsLayout::TGroup>(section);
}

- (MapCell *)cellAtPosition:(int)position inGroup:(ActiveMapsLayout::TGroup const &)group
{
  return (MapCell *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:position inSection:(NSInteger)group]];
}

- (void)markSelectedMapIndexPath:(NSIndexPath *)indexPath
{
  self.selectedPosition = static_cast<int>(indexPath.row);
  self.selectedGroup = [self groupWithSection:indexPath.section];
}

- (void)configureSizeLabelOfMapCell:(MapCell *)cell position:(int)position group:(ActiveMapsLayout::TGroup const &)group status:(TStatus const &)status options:(MapOptions const &)options
{
  if (status == TStatus::ENotDownloaded)
  {
    LocalAndRemoteSizeT const size = self.mapsLayout.GetRemoteCountrySizes(group, 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.mapsLayout.GetCountrySize(group, position, options).second);
  else if (status == TStatus::EOutOfMemFailed || status == TStatus::EDownloadFailed || status == TStatus::EDownloading || status == TStatus::EInQueue)
    cell.sizeLabel.text = formattedSize(self.mapsLayout.GetDownloadableCountrySize(group, position).second);
}

#pragma mark - DownloaderParentVC virtual methods implementation

- (NSString *)parentTitle
{
  return nil;
}

- (NSString *)selectedMapName
{
  return @(self.mapsLayout.GetCountryName(self.selectedGroup, self.selectedPosition).c_str());
}

- (uint64_t)selectedMapSizeWithOptions:(MapOptions)options
{
  return self.mapsLayout.GetCountrySize(self.selectedGroup, self.selectedPosition, options).second;
}

- (TStatus)selectedMapStatus
{
  return self.mapsLayout.GetCountryStatus(self.selectedGroup, self.selectedPosition);
}

- (MapOptions)selectedMapOptions
{
  return self.mapsLayout.GetCountryOptions(self.selectedGroup, self.selectedPosition);
}

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

    case DownloaderActionDeleteAll:
    case DownloaderActionDeleteMap:
    case DownloaderActionDeleteCarRouting:
      self.mapsLayout.DeleteMap(self.selectedGroup, self.selectedPosition, self.selectedInActionSheetOptions);
      break;

    case DownloaderActionCancelDownloading:
      self.mapsLayout.CancelDownloading(self.selectedGroup, self.selectedPosition);
      break;

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

#pragma mark - TableView

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
  ActiveMapsLayout::TGroup const group = [self groupWithSection:section];

  if (group == ActiveMapsLayout::TGroup::ENewMap)
    return nil;

  UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 40)];
  view.clipsToBounds = YES;
  UILabel * label = [[UILabel alloc] initWithFrame:view.bounds];
  label.font = [UIFont fontWithName:@"HelveticaNeue" size:13];
  label.backgroundColor = [UIColor clearColor];

  if (group == ActiveMapsLayout::TGroup::EOutOfDate)
    label.text = L(@"downloader_outdated_maps").uppercaseString;
  else if (group == ActiveMapsLayout::TGroup::EUpToDate)
    label.text = L(@"downloader_uptodate_maps").uppercaseString;

  [label sizeToIntegralFit];
  [view addSubview:label];
  label.minX = 13;
  label.maxY = view.height - 5;
  if (group == ActiveMapsLayout::TGroup::EOutOfDate)
  {
    BadgeView * badge = [[BadgeView alloc] init];
    badge.value = self.mapsLayout.GetOutOfDateCount();
    badge.center = CGPointMake(label.maxX + badge.width - 3, label.midY - 1.0 / [UIScreen mainScreen].scale);
    [view addSubview:badge];
    self.outOfDateBadge = badge;
  }
  return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
  ActiveMapsLayout::TGroup const group = [self groupWithSection:section];
  if (group == ActiveMapsLayout::TGroup::EOutOfDate || group == ActiveMapsLayout::TGroup::EUpToDate)
    return self.mapsLayout.GetCountInGroup(group) == 0 ? 0.001 : 42;
  else
    return 0.001;
}

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

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
  return (NSInteger)ActiveMapsLayout::TGroup::EGroupCount;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
  return self.mapsLayout.GetCountInGroup([self groupWithSection:section]);
}

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

  int const position = static_cast<int const>(indexPath.row);
  ActiveMapsLayout::TGroup const group = [self groupWithSection:indexPath.section];
  TStatus const status = self.mapsLayout.GetCountryStatus(group, position);
  MapOptions const options = self.mapsLayout.GetCountryOptions(group, 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.mapsLayout.GetCountryName(group, position).c_str());
  cell.parentMode = NO;
  cell.status = status;
  cell.options = options;
  cell.delegate = self;
  cell.badgeView.value = 0;
  cell.separatorTop.hidden = !isFirst;
  cell.separatorBottom.hidden = !isLast;
  cell.separator.hidden = isLast;

  if (status == TStatus::EOutOfMemFailed || status == TStatus::EDownloadFailed || status == TStatus::EDownloading || status == TStatus::EInQueue)
  {
    LocalAndRemoteSizeT const size = self.mapsLayout.GetDownloadableCountrySize(group, position);
    cell.downloadProgress = (double)size.first / size.second;
  }
  [self configureSizeLabelOfMapCell:cell position:position group:group status:status options:options];

  return cell;
}

- (void)updateAllCellsSeprators
{
  NSInteger numberOfSection = [self numberOfSectionsInTableView:self.tableView];
  for (NSInteger section = 0; section < numberOfSection; section++)
  {
    NSInteger numberOfRows = [self tableView:self.tableView numberOfRowsInSection:section];
    for (NSInteger row = 0; row < numberOfRows; row++)
    {
      NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];
      [self updateSeparatorsForCellAtIndexPath:indexPath];
    }
  }
}

- (void)updateSeparatorsForCellAtIndexPath:(NSIndexPath *)indexPath
{
  MapCell * cell = (MapCell *)[self.tableView cellForRowAtIndexPath:indexPath];
  NSInteger const numberOfRows = [self tableView:self.tableView numberOfRowsInSection:indexPath.section];
  BOOL const isLast = (indexPath.row == numberOfRows - 1);
  BOOL const isFirst = (indexPath.row == 0);
  cell.separatorTop.hidden = !isFirst;
  cell.separatorBottom.hidden = !isLast;
  cell.separator.hidden = isLast;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
  TStatus const status = self.mapsLayout.GetCountryStatus([self groupWithSection:indexPath.section], static_cast<int>(indexPath.row));
  return status == TStatus::EOnDisk || status == TStatus::EOnDiskOutOfDate;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
  if (editingStyle == UITableViewCellEditingStyleDelete)
  {
    int const position = static_cast<int const>(indexPath.row);
    ActiveMapsLayout::TGroup const group = [self groupWithSection:indexPath.section];
    MapOptions const options = self.mapsLayout.GetCountryOptions(group, position);
    self.mapsLayout.DeleteMap(group, position, options);
    [tableView setEditing:NO animated:YES];
    [self updateAllCellsSeprators];
  }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  [self markSelectedMapIndexPath:indexPath];

  MapCell * cell = [self cellAtPosition:self.selectedPosition inGroup:self.selectedGroup];
  UIActionSheet * actionSheet = [self actionSheetToPerformActionOnSelectedMap];
  [actionSheet showFromRect:cell.frame inView:cell.superview animated:YES];

  [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

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

#pragma mark - MapCellDelegate

- (void)mapCellDidStartDownloading:(MapCell *)cell
{
  [self markSelectedMapIndexPath:[self.tableView indexPathForCell:cell]];
  TStatus const status = [self selectedMapStatus];
  if (status == TStatus::EDownloadFailed || status == TStatus::EOutOfMemFailed)
    if ([self canDownloadSelectedMap])
      self.mapsLayout.RetryDownloading(self.selectedGroup, self.selectedPosition);
}

- (void)mapCellDidCancelDownloading:(MapCell *)cell
{
  NSIndexPath * indexPath = [self.tableView indexPathForCell:cell];
  self.selectedPosition = static_cast<int>(indexPath.row);
  self.selectedGroup = [self groupWithSection:indexPath.section];

  [[self actionSheetToCancelDownloadingSelectedMap] showFromRect:cell.frame inView:cell.superview animated:YES];
}

#pragma mark - ActiveMaps core callbacks

- (void)countryStatusChangedAtPosition:(int)position inGroup:(ActiveMapsLayout::TGroup const &)group
{
  [self refreshTopRightButton];

  MapCell * cell = [self cellAtPosition:position inGroup:group];

  TStatus const status = self.mapsLayout.GetCountryStatus(group, position);
  MapOptions const options = self.mapsLayout.GetCountryOptions(group, position);
  [self configureSizeLabelOfMapCell:cell position:position group:group status:status options:options];
  [cell setStatus:self.mapsLayout.GetCountryStatus(group, position) options:self.mapsLayout.GetCountryOptions(group, position) animated:YES];

  self.outOfDateBadge.value = self.mapsLayout.GetOutOfDateCount();
}

- (void)countryOptionsChangedAtPosition:(int)position inGroup:(ActiveMapsLayout::TGroup const &)group
{
  MapCell * cell = [self cellAtPosition:position inGroup:group];

  TStatus const status = self.mapsLayout.GetCountryStatus(group, position);
  MapOptions const options = self.mapsLayout.GetCountryOptions(group, position);
  [self configureSizeLabelOfMapCell:cell position:position group:group status:status options:options];
  [cell setStatus:self.mapsLayout.GetCountryStatus(group, position) options:self.mapsLayout.GetCountryOptions(group, position) animated:YES];
}

- (void)countryGroupChangedFromPosition:(int)oldPosition inGroup:(ActiveMapsLayout::TGroup const &)oldGroup toPosition:(int)newPosition inGroup:(ActiveMapsLayout::TGroup const &)newGroup
{
  if (oldGroup == newGroup && oldPosition == -1)
  {
    NSIndexPath * indexPath = [NSIndexPath indexPathForRow:newPosition inSection:(NSInteger)newGroup];
    [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
  }
  else if (oldGroup == newGroup && newPosition == -1)
  {
    NSIndexPath * indexPath = [NSIndexPath indexPathForRow:oldPosition inSection:(NSInteger)oldGroup];
    [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
  }
  else if (oldGroup != newGroup && oldPosition >= 0 && newPosition >= 0)
  {
    NSIndexPath * oldIndexPath = [NSIndexPath indexPathForRow:oldPosition inSection:(NSInteger)oldGroup];
    NSIndexPath * newIndexPath = [NSIndexPath indexPathForRow:newPosition inSection:(NSInteger)newGroup];
    [self.tableView moveRowAtIndexPath:oldIndexPath toIndexPath:newIndexPath];
  }
  [self updateAllCellsSeprators];
}

- (void)countryDownloadingProgressChanged:(LocalAndRemoteSizeT const &)progress atPosition:(int)position inGroup:(ActiveMapsLayout::TGroup const &)group
{
  MapCell * cell = [self cellAtPosition:position inGroup:group];
  [cell setDownloadProgress:((double)progress.first / progress.second) animated:YES];
}

@end