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

MWMPlacePageActionBar.mm « Classes « Maps « iphone - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5a85e0a7c391266ad6caaa739209ebf309b767f4 (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
#import "MWMPlacePageActionBar.h"
#import "Common.h"
#import "MWMActionBarButton.h"
#import "MWMBasePlacePageView.h"
#import "MWMPlacePageEntity.h"
#import "MWMPlacePageViewManager.h"
#import "MapViewController.h"
#import "MapsAppDelegate.h"

#include "Framework.h"

#include "std/vector.hpp"

extern NSString * const kAlohalyticsTapEventKey;

namespace
{
NSString * const kPlacePageActionBarNibName = @"PlacePageActionBar";

}  // namespace

@interface MWMPlacePageActionBar ()<MWMActionBarButtonDelegate>
{
  vector<EButton> m_visibleButtons;
  vector<EButton> m_additionalButtons;
}

@property(weak, nonatomic) MWMPlacePageViewManager * placePageManager;
@property(copy, nonatomic) IBOutletCollection(UIView) NSArray<UIView *> * buttons;
@property(weak, nonatomic) IBOutlet UIImageView * separator;
@property(nonatomic) BOOL isPrepareRouteMode;

@end

@implementation MWMPlacePageActionBar

+ (MWMPlacePageActionBar *)actionBarForPlacePageManager:(MWMPlacePageViewManager *)placePageManager
{
  MWMPlacePageActionBar * bar =
      [[NSBundle.mainBundle loadNibNamed:kPlacePageActionBarNibName owner:nil options:nil]
          firstObject];
  [bar configureWithPlacePageManager:placePageManager];
  return bar;
}

- (void)configureWithPlacePageManager:(MWMPlacePageViewManager *)placePageManager
{
  self.placePageManager = placePageManager;
  self.isPrepareRouteMode = MapsAppDelegate.theApp.routingPlaneMode != MWMRoutingPlaneModeNone;
  self.isBookmark = placePageManager.entity.isBookmark;
  [self configureButtons];
  self.autoresizingMask = UIViewAutoresizingNone;
}

- (void)configureButtons
{
  m_visibleButtons.clear();
  m_additionalButtons.clear();
  MWMPlacePageEntity * entity = self.placePageManager.entity;
  NSString * phone = [entity getCellValue:MWMPlacePageCellTypePhoneNumber];

  BOOL const isIphone = [[UIDevice currentDevice].model isEqualToString:@"iPhone"];
  BOOL const isPhoneNotEmpty = phone.length > 0;
  BOOL const isBooking = entity.isBooking;
  BOOL const itHasPhoneNumber = isIphone && isPhoneNotEmpty;
  BOOL const isApi = entity.isApi;
  BOOL const isP2P = self.isPrepareRouteMode;
  BOOL const isMyPosition = entity.isMyPosition;

  if (isMyPosition)
  {
    m_visibleButtons.push_back(EButton::Spacer);
    m_visibleButtons.push_back(EButton::Bookmark);
    m_visibleButtons.push_back(EButton::Share);
    m_visibleButtons.push_back(EButton::Spacer);
  }
  else if (isApi && isBooking)
  {
    m_visibleButtons.push_back(EButton::Api);
    m_visibleButtons.push_back(EButton::Booking);
    m_additionalButtons.push_back(EButton::Bookmark);
    m_additionalButtons.push_back(EButton::RouteFrom);
    m_additionalButtons.push_back(EButton::Share);
  }
  else if (isApi && itHasPhoneNumber)
  {
    m_visibleButtons.push_back(EButton::Api);
    m_visibleButtons.push_back(EButton::Call);
    m_additionalButtons.push_back(EButton::Bookmark);
    m_additionalButtons.push_back(EButton::RouteFrom);
    m_additionalButtons.push_back(EButton::Share);
  }
  else if (isApi && isP2P)
  {
    m_visibleButtons.push_back(EButton::Api);
    m_visibleButtons.push_back(EButton::RouteFrom);
    m_additionalButtons.push_back(EButton::Bookmark);
    m_additionalButtons.push_back(EButton::Share);
  }
  else if (isApi)
  {
    m_visibleButtons.push_back(EButton::Api);
    m_visibleButtons.push_back(EButton::Bookmark);
    m_additionalButtons.push_back(EButton::RouteFrom);
    m_additionalButtons.push_back(EButton::Share);
  }
  else if (isBooking && isP2P)
  {
    m_visibleButtons.push_back(EButton::Bookmark);
    m_visibleButtons.push_back(EButton::RouteFrom);
    m_additionalButtons.push_back(EButton::Booking);
    m_additionalButtons.push_back(EButton::Share);
  }
  else if (itHasPhoneNumber && isP2P)
  {
    m_visibleButtons.push_back(EButton::Bookmark);
    m_visibleButtons.push_back(EButton::RouteFrom);
    m_additionalButtons.push_back(EButton::Call);
    m_additionalButtons.push_back(EButton::Share);
  }
  else if (isBooking)
  {
    m_visibleButtons.push_back(EButton::Booking);
    m_visibleButtons.push_back(EButton::Bookmark);
    m_additionalButtons.push_back(EButton::RouteFrom);
    m_additionalButtons.push_back(EButton::Share);
  }
  else if (itHasPhoneNumber)
  {
    m_visibleButtons.push_back(EButton::Call);
    m_visibleButtons.push_back(EButton::Bookmark);
    m_additionalButtons.push_back(EButton::RouteFrom);
    m_additionalButtons.push_back(EButton::Share);
  }
  else
  {
    m_visibleButtons.push_back(EButton::Bookmark);
    m_visibleButtons.push_back(EButton::RouteFrom);
  }

  if (!isMyPosition)
  {
    m_visibleButtons.push_back(EButton::RouteTo);
    m_visibleButtons.push_back(m_additionalButtons.empty() ? EButton::Share : EButton::More);
  }

  for (UIView * v in self.buttons)
  {
    [v.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    auto const type = m_visibleButtons[v.tag - 1];
    [MWMActionBarButton addButtonToSuperview:v
                                    delegate:self
                                  buttonType:type
                                  isSelected:type == EButton::Bookmark ? self.isBookmark : NO];
  }
}

- (UIView *)shareAnchor
{
  UIView * last = nil;
  auto const size = self.buttons.count;
  for (UIView * v in self.buttons)
  {
    if (v.tag == size)
      last = v;
  }
  return last;
}

#pragma mark - MWMActionBarButtonDelegate

- (void)tapOnButtonWithType:(EButton)type
{
  MWMPlacePageViewManager * placePageManager = self.placePageManager;
  switch (type)
  {
  case EButton::Api: [placePageManager apiBack]; break;
  case EButton::Booking: [placePageManager book:NO]; break;
  case EButton::Call: [placePageManager call]; break;
  case EButton::Bookmark:
    if (self.isBookmark)
      [placePageManager removeBookmark];
    else
      [placePageManager addBookmark];
    break;
  case EButton::RouteFrom: [placePageManager routeFrom]; break;
  case EButton::RouteTo: [placePageManager routeTo]; break;
  case EButton::Share: [placePageManager share]; break;
  case EButton::More: [self showActionSheet]; break;
  case EButton::Spacer: break;
  }
}

#pragma mark - ActionSheet

- (void)showActionSheet
{
  NSString * cancel = L(@"cancel");
  MWMPlacePageEntity * entity = self.placePageManager.entity;
  BOOL const isTitleNotEmpty = entity.title.length > 0;
  NSString * title = isTitleNotEmpty ? entity.title : entity.subtitle;
  NSString * subtitle = isTitleNotEmpty ? entity.subtitle : nil;

  UIViewController * vc = static_cast<UIViewController *>([MapViewController controller]);
  NSMutableArray<NSString *> * titles = [@[] mutableCopy];
  for (auto const buttonType : m_additionalButtons)
  {
    BOOL const isSelected = buttonType == EButton::Bookmark ? self.isBookmark : NO;
    if (NSString * title = titleForButton(buttonType, isSelected))
      [titles addObject:title];
    else
      NSAssert(false, @"Title can't be nil!");
  }

  UIAlertController * alertController =
      [UIAlertController alertControllerWithTitle:title
                                          message:subtitle
                                   preferredStyle:UIAlertControllerStyleActionSheet];
  UIAlertAction * cancelAction =
      [UIAlertAction actionWithTitle:cancel style:UIAlertActionStyleCancel handler:nil];

  for (auto i = 0; i < titles.count; i++)
  {
    UIAlertAction * commonAction =
        [UIAlertAction actionWithTitle:titles[i]
                                 style:UIAlertActionStyleDefault
                               handler:^(UIAlertAction * action) {
                                 [self tapOnButtonWithType:self->m_additionalButtons[i]];
                               }];
    [alertController addAction:commonAction];
  }
  [alertController addAction:cancelAction];

  if (IPAD)
  {
    UIPopoverPresentationController * popPresenter =
        [alertController popoverPresentationController];
    popPresenter.sourceView = self.shareAnchor;
    popPresenter.sourceRect = self.shareAnchor.bounds;
  }
  [vc presentViewController:alertController animated:YES completion:nil];
}

#pragma mark - Layout

- (void)layoutSubviews
{
  [super layoutSubviews];
  self.separator.width = self.width;
  CGFloat const buttonWidth = self.width / self.buttons.count;
  for (UIView * button in self.buttons)
  {
    button.minX = buttonWidth * (button.tag - 1);
    button.width = buttonWidth;
  }
}

#pragma mark - Properties

- (void)setIsBookmark:(BOOL)isBookmark
{
  _isBookmark = isBookmark;
  [self configureButtons];
}

@end