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

MWMPlacePageData.mm « Classes « Maps « iphone - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 15c934909bddbdb6ff9b5c16d1a458d3eb26c1d1 (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
#import "MWMPlacePageData.h"

#include "Framework.h"

#include "base/string_utils.hpp"

#include "3party/opening_hours/opening_hours.hpp"

namespace
{
NSString * const kUserDefaultsLatLonAsDMSKey = @"UserDefaultsLatLonAsDMS";

}  // namespace

using namespace place_page;

@implementation MWMPlacePageData
{
  Info m_info;

  vector<Sections> m_sections;
  vector<MetainfoRows> m_metainfoRows;
  vector<ButtonsRows> m_buttonsRows;
}

- (instancetype)initWithPlacePageInfo:(Info const &)info
{
  self = [super init];
  if (self)
  {
    m_info = info;
    [self fillSections];
  }

  return self;
}

- (void)fillSections
{
  m_sections.clear();
  m_metainfoRows.clear();
  m_buttonsRows.clear();

  m_sections.push_back(Sections::Preview);

  // It's bookmark.
  if (m_info.IsBookmark())
    m_sections.push_back(Sections::Bookmark);

  // There is always at least coordinate meta field.
  m_sections.push_back(Sections::Metainfo);
  [self fillMetaInfoSection];

  // There is at least one of these buttons.
  if (m_info.ShouldShowAddPlace() || m_info.ShouldShowEditPlace() || m_info.ShouldShowAddBusiness() || m_info.IsSponsoredHotel())
  {
    m_sections.push_back(Sections::Buttons);
    [self fillButtonsSection];
  }
}

- (void)fillMetaInfoSection
{
  using namespace osm;
  auto const availableProperties = m_info.AvailableProperties();
  // We can't match each metadata property to its UI field and thats why we need to use our own enum.
  for (auto const p : availableProperties)
  {
    switch (p)
    {
    case Props::OpeningHours:
      m_metainfoRows.push_back(MetainfoRows::OpeningHours);
      break;
    case Props::Phone:
      m_metainfoRows.push_back(MetainfoRows::Phone);
      break;
    case Props::Website:
      m_metainfoRows.push_back(MetainfoRows::Website);
      break;
    case Props::Email:
      m_metainfoRows.push_back(MetainfoRows::Email);
      break;
    case Props::Cuisine:
      m_metainfoRows.push_back(MetainfoRows::Cuisine);
      break;
    case Props::Operator:
      m_metainfoRows.push_back(MetainfoRows::Operator);
      break;
    case Props::Internet:
      m_metainfoRows.push_back(MetainfoRows::Internet);
      break;

    case Props::Wikipedia:
    case Props::Elevation:
    case Props::Stars:
    case Props::Flats:
    case Props::BuildingLevels:
    case Props::Fax:
      break;
    }
  }

  auto const address = m_info.GetAddress();
  if (!address.empty())
    m_metainfoRows.push_back(MetainfoRows::Address);

  m_metainfoRows.push_back(MetainfoRows::Coordinate);
}

- (void)fillButtonsSection
{
  // We don't have to show edit, add place or business if it's booking object.
  if (m_info.IsSponsoredHotel())
  {
    m_buttonsRows.push_back(ButtonsRows::HotelDescription);
    return;
  }

  if (m_info.ShouldShowAddPlace())
    m_buttonsRows.push_back(ButtonsRows::AddPlace);

  if (m_info.ShouldShowEditPlace())
    m_buttonsRows.push_back(ButtonsRows::EditPlace);

  if (m_info.ShouldShowAddBusiness())
    m_buttonsRows.push_back(ButtonsRows::AddBusiness);
}

- (void)updateBookmarkStatus:(BOOL)isBookmark
{
  auto & f = GetFramework();
  auto & bmManager = f.GetBookmarkManager();
  if (isBookmark)
  {
    auto const categoryIndex = static_cast<int>(f.LastEditedBMCategory());
    BookmarkData bmData{m_info.FormatNewBookmarkName(), f.LastEditedBMType()};
    auto const bookmarkIndex =
    static_cast<int>(bmManager.AddBookmark(categoryIndex,self.mercator, bmData));

    auto category = f.GetBmCategory(categoryIndex);
    NSAssert(category, @"Category can't be nullptr!");
    {
      BookmarkCategory::Guard guard(*category);
      Bookmark * bookmark =
      static_cast<Bookmark *>(guard.m_controller.GetUserMarkForEdit(bookmarkIndex));
      f.FillBookmarkInfo(*bookmark, {bookmarkIndex, categoryIndex}, m_info);
    }
    m_sections.insert(m_sections.begin() + 1, Sections::Bookmark);
  }
  else
  {
    auto const bac = m_info.GetBookmarkAndCategory();
    auto category = bmManager.GetBmCategory(bac.m_categoryIndex);
    NSAssert(category, @"Category can't be nullptr!");
    {
      BookmarkCategory::Guard guard(*category);
      guard.m_controller.DeleteUserMark(bac.m_bookmarkIndex);
    }
    category->SaveToKMLFile();

    m_info.m_bac = {};
    m_sections.erase(remove(m_sections.begin(), m_sections.end(), Sections::Bookmark));
  }
}

#pragma mark - Getters

- (storage::TCountryId const &)countryId
{
  return m_info.m_countryId;
}

- (FeatureID const &)featureId
{
  return m_info.GetID();
}

- (NSString *)title
{
  return @(m_info.GetTitle().c_str());
}

- (NSString *)subtitle
{
  return @(m_info.GetSubtitle().c_str());
}

- (place_page::OpeningHours)schedule;
{
  auto const raw = m_info.GetOpeningHours();
  if (raw.empty())
    return place_page::OpeningHours::Unknown;

  auto const t = time(nullptr);
  osmoh::OpeningHours oh(raw);
  if (oh.IsValid())
  {

    if (oh.IsTwentyFourHours())
      return place_page::OpeningHours::AllDay;
    else if (oh.IsOpen(t))
      return place_page::OpeningHours::Open;
    else if (oh.IsClosed(t))
      return place_page::OpeningHours::Closed;
    else
      return place_page::OpeningHours::Unknown;
  }
  else
  {
    return place_page::OpeningHours::Unknown;
  }
}

- (NSString *)bookingRating
{
  return m_info.IsHotel() ? @(m_info.GetRatingFormatted().c_str()) : nil;
}

- (NSString *)bookingApproximatePricing
{
  return m_info.IsHotel() ? @(m_info.GetApproximatePricing().c_str()) : nil;
}

- (NSURL *)bookingURL
{
  return m_info.IsSponsoredHotel() ? [NSURL URLWithString:@(m_info.m_sponsoredBookingUrl.c_str())] : nil;
}

- (NSURL *)bookingDescriptionURL
{
  return m_info.IsSponsoredHotel() ? [NSURL URLWithString:@(m_info.m_sponsoredDescriptionUrl.c_str())] : nil;
}

- (NSString *)hotelId
{
  return m_info.IsSponsoredHotel() ? @(m_info.GetMetadata().Get(feature::Metadata::FMD_SPONSORED_ID).c_str())
                                     : nil;
}

- (void)assignOnlinePriceToLabel:(UILabel *)label
{
  NSAssert(m_info.IsSponsoredHotel(), @"Online price must be assigned to booking object!");
  if (Platform::ConnectionStatus() == Platform::EConnectionType::CONNECTION_NONE)
    return;

  NSNumberFormatter * currencyFormatter = [[NSNumberFormatter alloc] init];
  if (currencyFormatter.currencyCode.length != 3)
    currencyFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];

  currencyFormatter.numberStyle = NSNumberFormatterCurrencyStyle;
  currencyFormatter.maximumFractionDigits = 0;

  string const currency = currencyFormatter.currencyCode.UTF8String;
  auto & api = GetFramework().GetBookingApi();

  auto const func = [self, label, currency, currencyFormatter](string const & minPrice, string const & priceCurrency)
  {
    if (currency != priceCurrency)
      return;

    NSNumberFormatter * decimalFormatter = [[NSNumberFormatter alloc] init];
    decimalFormatter.numberStyle = NSNumberFormatterDecimalStyle;

    NSString * currencyString = [currencyFormatter stringFromNumber:[decimalFormatter numberFromString:
                                [@(minPrice.c_str()) stringByReplacingOccurrencesOfString:@"."
                                                                       withString:decimalFormatter.decimalSeparator]]];

    NSString * pattern = [L(@"place_page_starting_from") stringByReplacingOccurrencesOfString:@"%s" withString:@"%@"];

    dispatch_async(dispatch_get_main_queue(), ^{
      label.text = [NSString stringWithFormat:pattern, currencyString];
    });
  };

  api.GetMinPrice(self.hotelId.UTF8String, currency, func);
}

- (NSString *)address
{
  return @(m_info.GetAddress().c_str());
}

- (NSString *)apiURL
{
  return @(m_info.GetApiUrl().c_str());
}

- (NSString *)externalTitle
{
  return m_info.IsBookmark() && m_info.m_bookmarkTitle != m_info.GetTitle() ?
                                                          @(m_info.m_bookmarkTitle.c_str())
                                                          : nil;
}

- (NSString *)bookmarkColor
{
  return m_info.IsBookmark() ? @(m_info.m_bookmarkColorName.c_str()) : nil;;
}

- (NSString *)bookmarkDescription
{
  return m_info.IsBookmark() ? @(m_info.m_bookmarkDescription.c_str()) : nil;
}

- (NSString *)bookmarkCategory
{
  return m_info.IsBookmark() ? @(m_info.m_bookmarkCategoryName.c_str()) : nil;;
}

- (BookmarkAndCategory)bac;
{
  return m_info.IsBookmark() ? m_info.m_bac : BookmarkAndCategory();
}

- (vector<Sections> const &)sections
{
  return m_sections;
}

- (vector<MetainfoRows> const &)metainfoRows
{
  return m_metainfoRows;
}

- (vector<ButtonsRows> const &)buttonsRows
{
  return m_buttonsRows;
}

- (NSString *)stringForRow:(MetainfoRows)row
{
  switch (row)
  {
  case MetainfoRows::OpeningHours:
    return @(m_info.GetOpeningHours().c_str());
  case MetainfoRows::Phone:
    return @(m_info.GetPhone().c_str());
  case MetainfoRows::Address:
    return @(m_info.GetAddress().c_str());
  case MetainfoRows::Website:
    return @(m_info.GetWebsite().c_str());
  case MetainfoRows::Email:
    return @(m_info.GetEmail().c_str());
  case MetainfoRows::Cuisine:
    return @(strings::JoinStrings(m_info.GetCuisines(), Info::kSubtitleSeparator).c_str());
  case MetainfoRows::Operator:
    return @(m_info.GetOperator().c_str());
  case MetainfoRows::Internet:
    return L(@"WiFi_available");
  case MetainfoRows::Coordinate:
    return @(m_info.GetFormattedCoordinate([[NSUserDefaults standardUserDefaults] boolForKey:kUserDefaultsLatLonAsDMSKey]).c_str());
  }
}

#pragma mark - Helpres

- (NSString *)phoneNumber
{
  return @(m_info.GetPhone().c_str());
}

- (BOOL)isBookmark
{
  return m_info.IsBookmark();
}

- (BOOL)isApi
{
  return m_info.HasApiUrl();
}

- (BOOL)isBooking
{
  return m_info.IsHotel();
}

- (BOOL)isMyPosition
{
  return m_info.IsMyPosition();
}

- (BOOL)isHTMLDescription
{
  return strings::IsHTML(m_info.m_bookmarkDescription);
}

#pragma mark - Coordinates

- (m2::PointD const &)mercator
{
  return m_info.GetMercator();
}

- (ms::LatLon)latLon
{
  return m_info.GetLatLon();
}

+ (void)toggleCoordinateSystem
{
  NSUserDefaults * ud = [NSUserDefaults standardUserDefaults];
  [ud setBool:![ud boolForKey:kUserDefaultsLatLonAsDMSKey] forKey:kUserDefaultsLatLonAsDMSKey];
  [ud synchronize];
}

@end