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

MWMDownloadTransitMapAlert.mm « DownloadTransitMapsAlert « CustomAlert « Classes « Maps « iphone - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 62295bb548432ac7edd0976acc12d031a0252689 (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
//
//  MWMGetTransitionMapAlert.m
//  Maps
//
//  Created by v.mikhaylenko on 05.03.15.
//  Copyright (c) 2015 MapsWithMe. All rights reserved.
//

#import "ActiveMapsVC.h"
#import "Common.h"
#import "MWMAlertViewController.h"
#import "MWMDownloaderDialogCell.h"
#import "MWMDownloaderDialogHeader.h"
#import "MWMDownloadTransitMapAlert.h"
#import "UIColor+MapsMeColor.h"
#import "UIKitCategories.h"
#import "UILabel+RuntimeAttributes.h"

@interface MWMDownloaderEntity : NSObject 

@property (copy, nonatomic) NSArray * titles;
@property (copy, nonatomic) NSString * size;
@property (nonatomic) BOOL isMapsFiles;

- (instancetype)initWithIndexes:(vector<storage::TIndex> const &)indexes isMaps:(BOOL)isMaps;

@end

@implementation MWMDownloaderEntity

- (instancetype)initWithIndexes:(vector<storage::TIndex> const &)indexes isMaps:(BOOL)isMaps
{
  self = [super init];
  if (self)
  {
    auto & a = GetFramework().GetCountryTree().GetActiveMapLayout();
    NSMutableArray * titles = [@[] mutableCopy];
    uint64_t totalRoutingSize = 0;
    for (auto const & i : indexes)
    {
      [titles addObject:[NSString stringWithUTF8String:a.GetCountryName(i).c_str()]];
      totalRoutingSize += a.GetCountrySize(i, isMaps ? TMapOptions::MapWithCarRouting : TMapOptions::CarRouting).second;
    }
    self.isMapsFiles = isMaps;
    self.titles = titles;
    self.size = [NSString stringWithFormat:@"%@ %@", @(totalRoutingSize / MB), L(@"mb")];
  }
  return self;
}

@end

typedef void (^MWMDownloaderBlock)();

typedef NS_ENUM(NSUInteger, SelectionState)
{
  SelectionStateNone,
  SelectionStateFirst,
  SelectionStateSecond,
  SelectionStateBoth
};

static NSString * const kCellIdentifier = @"MWMDownloaderDialogCell";
static NSString * const kDownloadTransitMapAlertNibName = @"MWMDownloadTransitMapAlert";
static CGFloat const kCellHeight = 32.;
static CGFloat const kHeaderHeight = 43.;
static CGFloat const kHeaderAndFooterHeight = 44.;
static CGFloat const kMinimumOffset = 20.;

@interface MWMDownloadTransitMapAlert ()
{
  vector<storage::TIndex> maps;
  vector<storage::TIndex> routes;
}

@property (weak, nonatomic) IBOutlet UILabel * titleLabel;
@property (weak, nonatomic) IBOutlet UILabel * messageLabel;
@property (copy, nonatomic) MWMDownloaderBlock downloaderBlock;
@property (weak, nonatomic) IBOutlet UITableView * dialogsTableView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint * tableViewHeight;
@property (copy, nonatomic) NSArray * missedFiles;
@property (nonatomic) MWMDownloaderDialogHeader * firstHeader;
@property (nonatomic) MWMDownloaderDialogHeader * secondHeader;
@property (nonatomic) SelectionState state;

@end

@interface MWMDownloadTransitMapAlert (TableView) <UITableViewDataSource, UITableViewDelegate>

@end

@implementation MWMDownloadTransitMapAlert

+ (instancetype)crossCountryAlertWithMaps:(vector<storage::TIndex> const &)maps routes:(vector<storage::TIndex> const &)routes
{
  MWMDownloadTransitMapAlert * alert = [self alertWithMaps:maps routes:routes];
  alert.titleLabel.localizedText = @"dialog_routing_download_and_build_cross_route";
  alert.messageLabel.localizedText = @"dialog_routing_download_cross_route";
  return alert;
}

+ (instancetype)downloaderAlertWithMaps:(vector<storage::TIndex> const &)maps routes:(vector<storage::TIndex> const &)routes
{
  MWMDownloadTransitMapAlert * alert = [self alertWithMaps:maps routes:routes];
  alert.titleLabel.localizedText = @"dialog_routing_download_files";
  alert.messageLabel.localizedText = @"dialog_routing_download_and_update_all";
  return alert;
}

+ (instancetype)alertWithMaps:(vector<storage::TIndex> const &)maps routes:(vector<storage::TIndex> const &)routes
{
  MWMDownloadTransitMapAlert * alert = [[[NSBundle mainBundle] loadNibNamed:kDownloadTransitMapAlertNibName owner:nil options:nil] firstObject];
  NSMutableArray * missedFiles = [@[] mutableCopy];
  if (!maps.empty())
  {
    MWMDownloaderEntity * entity = [[MWMDownloaderEntity alloc] initWithIndexes:maps isMaps:YES];
    [missedFiles addObject:entity];
  }
  if (!routes.empty())
  {
    MWMDownloaderEntity * entity = [[MWMDownloaderEntity alloc] initWithIndexes:routes isMaps:NO];
    [missedFiles addObject:entity];
  }
  alert.missedFiles = missedFiles;
  alert->maps = maps;
  alert->routes = routes;
  __weak MWMDownloadTransitMapAlert * wAlert = alert;
  alert.downloaderBlock = ^()
  {
    __strong MWMDownloadTransitMapAlert * alert = wAlert;
    auto & a = GetFramework().GetCountryTree().GetActiveMapLayout();
    for (auto const & index : alert->maps)
      a.DownloadMap(index, TMapOptions::MapWithCarRouting);
    for (auto const & index : alert->routes)
      a.DownloadMap(index, TMapOptions::CarRouting);
  };
  [alert configure];
  return alert;
}

- (void)configure
{
  [self.dialogsTableView registerNib:[UINib nibWithNibName:kCellIdentifier bundle:nil] forCellReuseIdentifier:kCellIdentifier];
  self.state = SelectionStateNone;
  [self.dialogsTableView reloadData];
}

#pragma mark - Actions

- (IBAction)notNowButtonTap:(id)sender
{
  [self close];
}

- (IBAction)downloadButtonTap:(id)sender
{
  [self downloadMaps];
}

- (void)downloadMaps
{
  self.downloaderBlock();
  [self close];
  ActiveMapsVC * activeMapsViewController = [[ActiveMapsVC alloc] init];
  [self.alertController.ownerViewController.navigationController pushViewController:activeMapsViewController animated:YES];
}

- (void)showDownloadDetail:(UIButton *)sender
{
  if ([sender isEqual:self.firstHeader.headerButton])
  {
    if (!self.secondHeader.headerButton.selected)
      self.state = sender.selected ? SelectionStateFirst : SelectionStateNone;
    else
      self.state = sender.selected ? SelectionStateBoth : SelectionStateSecond;
  }
  else
  {
    if (!self.firstHeader.headerButton.selected)
      self.state = sender.selected ? SelectionStateSecond : SelectionStateNone;
    else
      self.state = sender.selected ? SelectionStateBoth : SelectionStateFirst;
  }
}

- (void)setState:(SelectionState)state
{
  _state = state;
  [self layoutIfNeeded];
  NSUInteger const numberOfSections = self.missedFiles.count;
  switch (state)
  {
    case SelectionStateNone:
    {
      CGFloat const height = kHeaderAndFooterHeight * numberOfSections;
      self.tableViewHeight.constant = height;
      self.dialogsTableView.scrollEnabled = NO;
      [self.dialogsTableView.visibleCells enumerateObjectsUsingBlock:^(MWMDownloaderDialogCell * obj, NSUInteger idx, BOOL *stop) {
        obj.titleLabel.alpha = 0.;
      }];
      [self.dialogsTableView beginUpdates];
      [self.dialogsTableView endUpdates];
      [UIView animateWithDuration:0.05 animations:^
      {
        [self layoutSubviews];
      }];
      break;
    }
    case SelectionStateFirst:
    case SelectionStateSecond:
    case SelectionStateBoth:
    {
      NSUInteger const cellCount = self.cellCountForCurrentState;
      CGFloat const actualHeight = kCellHeight * cellCount + kHeaderAndFooterHeight * numberOfSections;
      CGFloat const height = [self bounded:actualHeight withHeight:self.superview.height];
      self.tableViewHeight.constant = height;
      self.dialogsTableView.scrollEnabled = actualHeight > self.tableViewHeight.constant ? YES : NO;
      [UIView animateWithDuration:.05 animations:^
      {
        [self layoutSubviews];
      }
      completion:^(BOOL finished)
      {
        [UIView animateWithDuration:.3 animations:^{
          [self.dialogsTableView beginUpdates];
          [self.dialogsTableView.visibleCells enumerateObjectsUsingBlock:^(MWMDownloaderDialogCell * obj, NSUInteger idx, BOOL *stop)
            {
              obj.titleLabel.alpha = 1.;
          }];
          [self.dialogsTableView endUpdates];
        }];
      }];
      break;
    }
  }
}

- (CGFloat)bounded:(CGFloat)f withHeight:(CGFloat)h
{
  CGFloat const currentHeight = [self.subviews.firstObject height];
  CGFloat const maximumHeight = h - 2. * kMinimumOffset;
  CGFloat const availableHeight = maximumHeight - currentHeight;
  return MIN(f, availableHeight + self.tableViewHeight.constant);
}

- (void)invalidateTableConstraintWithHeight:(CGFloat)height
{
  NSUInteger const numberOfSections = self.missedFiles.count;
  switch (self.state)
  {
    case SelectionStateNone:
      self.tableViewHeight.constant = kHeaderAndFooterHeight * numberOfSections ;
      break;
    case SelectionStateFirst:
    case SelectionStateSecond:
    case SelectionStateBoth:
    {
      NSUInteger const cellCount = self.cellCountForCurrentState;
      CGFloat const actualHeight = kCellHeight * cellCount + kHeaderAndFooterHeight * numberOfSections;
      self.tableViewHeight.constant = [self bounded:actualHeight withHeight:height];;
      self.dialogsTableView.scrollEnabled = actualHeight > self.tableViewHeight.constant;
      break;
    }
  }
}

- (NSUInteger)cellCountForCurrentState
{
  if (self.state == SelectionStateBoth)
    return [[self.missedFiles[0] titles] count] + [[self.missedFiles[1] titles] count];
  else if (self.state == SelectionStateFirst)
    return [[self.missedFiles[0] titles] count];
  else
    return [[self.missedFiles[1] titles] count];
}

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)orientation
{
  CGFloat const height = UIInterfaceOrientationIsLandscape(orientation) ? MIN(self.superview.width, self.superview.height) : MAX(self.superview.width, self.superview.height);
  [self invalidateTableConstraintWithHeight:height];
}

@end

@implementation MWMDownloadTransitMapAlert (TableView)

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
  return self.missedFiles.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
  return [[self.missedFiles[section] titles] count];
}

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

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
  switch (self.state)
  {
    case SelectionStateNone:
      return 0.;
    case SelectionStateFirst:
      return indexPath.section == 0 ? kCellHeight : 0.;
    case SelectionStateSecond:
      return indexPath.section == 0 ? 0. : kCellHeight;
    case SelectionStateBoth:
      return kCellHeight;
  }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
  if (section == 0)
  {
    if (self.firstHeader)
      return self.firstHeader;
  }
  else
  {
    if (self.secondHeader)
      return self.secondHeader;
  }

  MWMDownloaderEntity * entity = self.missedFiles[section];
  NSUInteger const count = entity.titles.count;
  NSString * title;
  MWMDownloaderDialogHeader * header;
  if (count > 1)
  {
    title = entity.isMapsFiles ? [NSString stringWithFormat:@"%@ (%@)", L(@"maps"), @(count)] : [NSString stringWithFormat:@"%@(%@)", L(@"dialog_routing_routes_size"), @(count)];;
    header = [MWMDownloaderDialogHeader headerForOwnerAlert:self title:title size:entity.size];
  }
  else
  {
    title = entity.titles.firstObject;
    header = [MWMDownloaderDialogHeader headerForOwnerAlert:self title:title size:entity.size];
    header.headerButton.enabled = NO;
    header.expandImage.hidden = YES;
    [header layoutSizeLabel];
  }
  if (section == 0)
  {
    self.firstHeader = header;
    return self.firstHeader;
  }
  self.secondHeader = header;
  return self.secondHeader;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
  UIView * view = [[UIView alloc] init];
  view.backgroundColor = self.missedFiles.count == 2 && section == 0 ? UIColor.blackDividers : UIColor.clearColor;
  return view;
}

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

  NSString * title = [self.missedFiles[indexPath.section] titles][indexPath.row];
  cell.titleLabel.text = title;
  return cell;
}

@end