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

MWMRoutePreview.mm « RoutePreview « Views « NavigationDashboard « CustomViews « Classes « Maps « iphone - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8e07ef3f8a295f15b5588a90183112c9a5eaf91d (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
#import "MWMRoutePreview.h"
#import "Common.h"
#import "MWMCircularProgress.h"
#import "MWMNavigationDashboardManager.h"
#import "MWMRoutePointCell.h"
#import "MWMRoutePointLayout.h"
#import "MWMRouter.h"
#import "Statistics.h"
#import "UIButton+Orientation.h"
#import "UIImageView+Coloring.h"

static CGFloat constexpr kAdditionalHeight = 20.;

@interface MWMRoutePreview ()<MWMRoutePointCellDelegate, MWMCircularProgressProtocol>

@property(weak, nonatomic) IBOutlet UIButton * backButton;
@property(weak, nonatomic) IBOutlet UIView * pedestrian;
@property(weak, nonatomic) IBOutlet UIView * vehicle;
@property(weak, nonatomic) IBOutlet UIView * bicycle;
@property(weak, nonatomic) IBOutlet NSLayoutConstraint * planningRouteViewHeight;
@property(weak, nonatomic, readwrite) IBOutlet UIButton * extendButton;
@property(weak, nonatomic) IBOutlet UIButton * goButton;
@property(weak, nonatomic) IBOutlet UICollectionView * collectionView;
@property(weak, nonatomic) IBOutlet MWMRoutePointLayout * layout;
@property(weak, nonatomic) IBOutlet UIImageView * arrowImageView;
@property(weak, nonatomic) IBOutlet UIView * statusBox;
@property(weak, nonatomic) IBOutlet UIView * planningBox;
@property(weak, nonatomic) IBOutlet UIView * resultsBox;
@property(weak, nonatomic) IBOutlet UIView * heightBox;
@property(weak, nonatomic) IBOutlet UIView * errorBox;
@property(weak, nonatomic) IBOutlet UILabel * resultLabel;
@property(weak, nonatomic) IBOutlet UILabel * arriveLabel;
@property(weak, nonatomic) IBOutlet NSLayoutConstraint * statusBoxHeight;
@property(weak, nonatomic) IBOutlet NSLayoutConstraint * resultsBoxHeight;
@property(weak, nonatomic) IBOutlet NSLayoutConstraint * heightBoxHeight;
@property(weak, nonatomic) IBOutlet UIImageView * heightProfileImage;
@property(weak, nonatomic) IBOutlet UIView * heightProfileElevation;
@property(weak, nonatomic) IBOutlet UIImageView * elevationImage;
@property(weak, nonatomic) IBOutlet UILabel * elevationHeight;

@property(nonatomic) UIImageView * movingCellImage;

@property(nonatomic) BOOL isNeedToMove;
@property(nonatomic) NSIndexPath * indexPathOfMovingCell;

@end

@implementation MWMRoutePreview
{
  map<routing::RouterType, MWMCircularProgress *> m_progresses;
}

- (void)awakeFromNib
{
  [super awakeFromNib];
  self.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  self.layer.shouldRasterize = YES;
  self.layer.rasterizationScale = UIScreen.mainScreen.scale;
  [self.collectionView registerNib:[UINib nibWithNibName:[MWMRoutePointCell className] bundle:nil]
        forCellWithReuseIdentifier:[MWMRoutePointCell className]];
  [self setupProgresses];

  [self.backButton matchInterfaceOrientation];

  self.elevationImage.mwm_coloring = MWMImageColoringBlue;
}

- (void)setupProgresses
{
  [self addProgress:self.vehicle imageName:@"ic_drive" routerType:routing::RouterType::Vehicle];
  [self addProgress:self.pedestrian
          imageName:@"ic_walk"
         routerType:routing::RouterType::Pedestrian];
  [self addProgress:self.bicycle
          imageName:@"ic_bike_route"
         routerType:routing::RouterType::Bicycle];
}

- (void)addProgress:(UIView *)parentView
          imageName:(NSString *)imageName
         routerType:(routing::RouterType)routerType
{
  MWMCircularProgress * progress = [[MWMCircularProgress alloc] initWithParentView:parentView];
  MWMCircularProgressStateVec const imageStates = {
      MWMCircularProgressStateNormal,   MWMCircularProgressStateFailed,
      MWMCircularProgressStateSelected, MWMCircularProgressStateProgress,
      MWMCircularProgressStateSpinner,  MWMCircularProgressStateCompleted};
  [progress setImage:[UIImage imageNamed:imageName] forStates:imageStates];

  [progress setColoring:MWMButtonColoringGray forStates:{MWMCircularProgressStateNormal}];

  [progress setColoring:MWMButtonColoringWhiteText
              forStates:{MWMCircularProgressStateFailed, MWMCircularProgressStateSelected,
                         MWMCircularProgressStateProgress, MWMCircularProgressStateSpinner,
                         MWMCircularProgressStateCompleted}];

  [progress setSpinnerBackgroundColor:[UIColor clearColor]];
  [progress setColor:[UIColor whiteColor]
           forStates:{MWMCircularProgressStateProgress, MWMCircularProgressStateSpinner}];

  progress.delegate = self;
  m_progresses[routerType] = progress;
}

- (void)didMoveToSuperview { [self setupActualHeight]; }
- (void)statePrepare
{
  for (auto const & progress : m_progresses)
    progress.second.state = MWMCircularProgressStateNormal;
  self.arrowImageView.transform = CGAffineTransformMakeRotation(M_PI);
  self.goButton.hidden = NO;
  self.goButton.enabled = NO;
  self.extendButton.selected = YES;
  [self setupActualHeight];
  [self reloadData];
  [self.layout invalidateLayout];
  self.statusBox.hidden = YES;
  self.resultsBox.hidden = YES;
  self.heightBox.hidden = YES;
  self.heightProfileElevation.hidden = YES;
  self.planningBox.hidden = YES;
  self.errorBox.hidden = YES;
}

- (void)statePlanning
{
  self.goButton.hidden = NO;
  self.goButton.enabled = NO;
  self.goButton.enabled = NO;
  self.statusBox.hidden = NO;
  self.resultsBox.hidden = YES;
  self.heightBox.hidden = YES;
  self.heightProfileElevation.hidden = YES;
  self.errorBox.hidden = YES;
  self.planningBox.hidden = NO;
  [self reloadData];
  if (IPAD)
    [self iPadNotReady];
}

- (void)stateError
{
  self.goButton.hidden = NO;
  self.goButton.enabled = NO;
  self.statusBox.hidden = NO;
  self.planningBox.hidden = YES;
  self.resultsBox.hidden = YES;
  self.heightBox.hidden = YES;
  self.heightProfileElevation.hidden = YES;
  self.errorBox.hidden = NO;
  if (IPAD)
    [self iPadNotReady];
}

- (void)stateReady
{
  self.goButton.hidden = NO;
  self.goButton.enabled = YES;
  self.statusBox.hidden = NO;
  self.planningBox.hidden = YES;
  self.errorBox.hidden = YES;
  self.resultsBox.hidden = NO;
  BOOL const hasAltitude = [MWMRouter hasRouteAltitude];
  self.heightBox.hidden = !hasAltitude;
  self.heightProfileElevation.hidden = !hasAltitude;
  if (IPAD)
    [self iPadReady];
}

- (void)iPadReady
{
  [self layoutIfNeeded];
  self.statusBoxHeight.constant =
      self.resultsBoxHeight.constant +
      ([MWMRouter hasRouteAltitude] ? self.heightBoxHeight.constant : 0);
  [UIView animateWithDuration:kDefaultAnimationDuration
      animations:^{
        [self layoutIfNeeded];
      }
      completion:^(BOOL finished) {
        [UIView animateWithDuration:kDefaultAnimationDuration
            animations:^{
              self.arriveLabel.alpha = 1.;
            }
            completion:^(BOOL finished) {
              [self updateHeightProfile];
            }];
      }];
}

- (void)updateHeightProfile
{
  if (![MWMRouter hasRouteAltitude])
    return;
  dispatch_async(dispatch_get_main_queue(), ^{
    [[MWMRouter router] routeAltitudeImageForSize:self.heightProfileImage.frame.size
                                       completion:^(UIImage * image, NSString * altitudeElevation) {
                                         self.heightProfileImage.image = image;
                                         self.elevationHeight.text = altitudeElevation;
                                       }];
  });
}

- (void)iPadNotReady
{
  self.arriveLabel.alpha = 0.;
  [self layoutIfNeeded];
  self.statusBoxHeight.constant = self.resultsBoxHeight.constant;
  [UIView animateWithDuration:kDefaultAnimationDuration
                   animations:^{
                     [self layoutIfNeeded];
                   }];
}

- (void)reloadData { [self.collectionView reloadData]; }
- (void)selectRouter:(routing::RouterType)routerType
{
  for (auto const & progress : m_progresses)
    progress.second.state = MWMCircularProgressStateNormal;
  m_progresses[routerType].state = MWMCircularProgressStateSelected;
}

- (void)layoutSubviews
{
  [super layoutSubviews];
  [self setupActualHeight];
  [self.delegate routePreviewDidChangeFrame:self.frame];
  [super layoutSubviews];
}

- (void)router:(routing::RouterType)routerType setState:(MWMCircularProgressState)state
{
  m_progresses[routerType].state = state;
}

- (void)router:(routing::RouterType)routerType setProgress:(CGFloat)progress
{
  m_progresses[routerType].progress = progress;
}

#pragma mark - MWMCircularProgressProtocol

- (void)progressButtonPressed:(nonnull MWMCircularProgress *)progress
{
  [Statistics logEvent:kStatEventName(kStatNavigationDashboard, kStatButton)
        withParameters:@{kStatValue : kStatProgress}];
  MWMCircularProgressState const s = progress.state;
  if (s == MWMCircularProgressStateSelected || s == MWMCircularProgressStateCompleted)
    return;

  for (auto const & prg : m_progresses)
  {
    if (prg.second != progress)
      continue;
    routing::RouterType const routerType = prg.first;
    [self selectRouter:routerType];
    MWMRouter * router = [MWMRouter router];
    router.type = routerType;
    [router rebuildWithBestRouter:NO];
    switch (routerType)
    {
    case routing::RouterType::Vehicle:
      [Statistics logEvent:kStatPointToPoint
            withParameters:@{kStatAction : kStatChangeRoutingMode, kStatValue : kStatVehicle}];
      break;
    case routing::RouterType::Pedestrian:
      [Statistics logEvent:kStatPointToPoint
            withParameters:@{kStatAction : kStatChangeRoutingMode, kStatValue : kStatPedestrian}];
      break;
    case routing::RouterType::Bicycle:
      [Statistics logEvent:kStatPointToPoint
            withParameters:@{kStatAction : kStatChangeRoutingMode, kStatValue : kStatBicycle}];
      break;
    }
  }
}

#pragma mark - Properties

- (CGRect)defaultFrame
{
  if (!IPAD)
    return super.defaultFrame;
  CGFloat const width = 320.;
  CGFloat const origin = self.isVisible ? 0. : -width;
  return {{origin, self.topBound}, {width, self.superview.height - kAdditionalHeight}};
}

- (CGFloat)visibleHeight { return self.planningRouteViewHeight.constant + kAdditionalHeight; }
- (IBAction)extendTap
{
  BOOL const isExtended = !self.extendButton.selected;
  [Statistics logEvent:kStatEventName(kStatPointToPoint, kStatExpand)
        withParameters:@{kStatValue : (isExtended ? kStatYes : kStatNo)}];
  self.extendButton.selected = isExtended;
  [self layoutIfNeeded];
  [self setupActualHeight];
  [UIView animateWithDuration:kDefaultAnimationDuration
                   animations:^{
                     self.arrowImageView.transform = isExtended
                                                         ? CGAffineTransformMakeRotation(M_PI)
                                                         : CGAffineTransformIdentity;
                     [self layoutIfNeeded];
                   }];
}

- (void)setupActualHeight
{
  if (!self.superview)
    return;
  if (IPAD)
  {
    CGFloat const selfHeight = self.superview.height - kAdditionalHeight;
    self.defaultHeight = selfHeight;
    self.height = selfHeight;
    return;
  }
  BOOL const isPortrait = self.superview.height > self.superview.width;
  CGFloat const height = isPortrait ? 140. : 96.;
  CGFloat const selfHeight = self.extendButton.selected ? height : 44.;
  self.planningRouteViewHeight.constant = selfHeight;
  self.defaultHeight = selfHeight;
  self.height = selfHeight;
}

- (void)snapshotCell:(MWMRoutePointCell *)cell
{
  UIGraphicsBeginImageContextWithOptions(cell.bounds.size, NO, 0.);
  [cell drawViewHierarchyInRect:cell.bounds afterScreenUpdates:YES];
  UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();
  self.movingCellImage = [[UIImageView alloc] initWithImage:image];
  self.movingCellImage.alpha = 0.8;
  [self.collectionView addSubview:self.movingCellImage];
  CALayer * l = self.movingCellImage.layer;
  l.masksToBounds = NO;
  l.shadowColor = UIColor.blackColor.CGColor;
  l.shadowRadius = 4.;
  l.shadowOpacity = 0.4;
  l.shadowOffset = {0., 0.};
  l.shouldRasterize = YES;
  l.rasterizationScale = [[UIScreen mainScreen] scale];
}

#pragma mark - MWMNavigationDashboardInfoProtocol

- (void)updateNavigationInfo:(MWMNavigationDashboardEntity *)info
{
  self.resultLabel.attributedText = info.estimate;
  if (!IPAD)
    return;

  NSString * arriveStr = [NSDateFormatter
      localizedStringFromDate:[[NSDate date] dateByAddingTimeInterval:info.timeToTarget]
                    dateStyle:NSDateFormatterNoStyle
                    timeStyle:NSDateFormatterShortStyle];
  self.arriveLabel.text = [NSString stringWithFormat:L(@"routing_arrive"), arriveStr.UTF8String];
}

#pragma mark - MWMRoutePointCellDelegate

- (void)startEditingCell:(MWMRoutePointCell *)cell
{
  NSUInteger const index = [self.collectionView indexPathForCell:cell].row;
  [self.dashboardManager.delegate didStartEditingRoutePoint:index == 0];
}

#pragma mark - PanGestureRecognizer

- (void)didPan:(UIPanGestureRecognizer *)pan cell:(MWMRoutePointCell *)cell
{
  CGPoint const locationPoint = [pan locationInView:self.collectionView];
  if (pan.state == UIGestureRecognizerStateBegan)
  {
    self.layout.isNeedToInitialLayout = NO;
    self.isNeedToMove = NO;
    self.indexPathOfMovingCell = [self.collectionView indexPathForCell:cell];
    [self snapshotCell:cell];
    [UIView animateWithDuration:kDefaultAnimationDuration
                     animations:^{
                       cell.contentView.alpha = 0.;
                       CGFloat const scaleY = 1.05;
                       self.movingCellImage.transform = CGAffineTransformMakeScale(1., scaleY);
                     }];
  }

  if (pan.state == UIGestureRecognizerStateChanged)
  {
    self.movingCellImage.center = {locationPoint.x - cell.width / 2 + 30, locationPoint.y};
    NSIndexPath * finalIndexPath = [self.collectionView indexPathForItemAtPoint:locationPoint];
    if (finalIndexPath && ![finalIndexPath isEqual:self.indexPathOfMovingCell])
    {
      if (self.isNeedToMove)
        return;
      self.isNeedToMove = YES;
      [self.collectionView performBatchUpdates:^{
        [self.collectionView moveItemAtIndexPath:finalIndexPath
                                     toIndexPath:self.indexPathOfMovingCell];
        self.indexPathOfMovingCell = finalIndexPath;
      }
                                    completion:nil];
    }
    else
    {
      self.isNeedToMove = NO;
    }
  }

  if (pan.state == UIGestureRecognizerStateEnded)
  {
    self.layout.isNeedToInitialLayout = YES;
    NSIndexPath * finalIndexPath = [self.collectionView indexPathForItemAtPoint:locationPoint];
    self.isNeedToMove = finalIndexPath && ![finalIndexPath isEqual:self.indexPathOfMovingCell];
    if (self.isNeedToMove)
    {
      cell.contentView.alpha = 1.;
      [self.collectionView performBatchUpdates:^{
        [self.collectionView moveItemAtIndexPath:self.indexPathOfMovingCell
                                     toIndexPath:finalIndexPath];
      }
          completion:^(BOOL finished) {
            [self.movingCellImage removeFromSuperview];
            self.movingCellImage.transform = CGAffineTransformIdentity;
          }];
    }
    else
    {
      cell.contentView.alpha = 1.;
      [self.movingCellImage removeFromSuperview];
      [[MWMRouter router] swapPointsAndRebuild];
      [self reloadData];
    }
  }
}

@end

#pragma mark - UICollectionView

@interface MWMRoutePreview (UICollectionView)<UICollectionViewDataSource, UICollectionViewDelegate>

@end

@implementation MWMRoutePreview (UICollectionView)

- (NSInteger)collectionView:(UICollectionView *)collectionView
     numberOfItemsInSection:(NSInteger)section
{
  return 2;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
                  cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
  MWMRoutePointCell * cell =
      [collectionView dequeueReusableCellWithReuseIdentifier:[MWMRoutePointCell className]
                                                forIndexPath:indexPath];
  cell.number.text = @(indexPath.row + 1).stringValue;
  if (indexPath.row == 0)
  {
    cell.title.text = [MWMRouter router].startPoint.Name();
    cell.title.placeholder = L(@"p2p_from");
  }
  else
  {
    cell.title.text = [MWMRouter router].finishPoint.Name();
    cell.title.placeholder = L(@"p2p_to");
  }
  cell.delegate = self;
  return cell;
}

@end