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

MWMPlacePageData.mm « PlacePage « UI « Maps « iphone - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4e9ed0cd63a0b555c4426bbde14e115c55f4abb6 (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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
#import "MWMPlacePageData.h"
#import "AppInfo.h"
#import "MWMBannerHelpers.h"
#import "MWMNetworkPolicy.h"
#import "MWMSettings.h"
#import "Statistics.h"
#import "SwiftBridge.h"

#import <FBAudienceNetwork/FBAudienceNetwork.h>

#include "Framework.h"

#include "partners_api/banner.hpp"

#include "base/string_utils.hpp"

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

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

}  // namespace

using namespace place_page;

@interface MWMPlacePageData ()

@property(copy, nonatomic) NSString * cachedMinPrice;
@property(nonatomic) id<MWMBanner> nativeAd;
@property(copy, nonatomic) NSArray<MWMGalleryItemModel *> * photos;

@end

@implementation MWMPlacePageData
{
  Info m_info;

  vector<Sections> m_sections;
  vector<PreviewRows> m_previewRows;
  vector<MetainfoRows> m_metainfoRows;
  vector<ButtonsRows> m_buttonsRows;
  vector<HotelPhotosRow> m_hotelPhotosRows;
  vector<HotelDescriptionRow> m_hotelDescriptionRows;
  vector<HotelFacilitiesRow> m_hotelFacilitiesRows;
  vector<HotelReviewsRow> m_hotelReviewsRows;

  booking::HotelInfo m_hotelInfo;
}

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

  return self;
}

- (void)fillSections
{
  m_sections.clear();
  m_previewRows.clear();
  m_metainfoRows.clear();
  m_buttonsRows.clear();
  m_hotelPhotosRows.clear();
  m_hotelDescriptionRows.clear();
  m_hotelReviewsRows.clear();
  m_hotelFacilitiesRows.clear();

  m_sections.push_back(Sections::Preview);
  [self fillPreviewSection];

  // 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.IsSponsored())
  {
    m_sections.push_back(Sections::Buttons);
    [self fillButtonsSection];
  }
}

- (void)fillPreviewSection
{
  if (self.title.length) m_previewRows.push_back(PreviewRows::Title);
  if (self.externalTitle.length) m_previewRows.push_back(PreviewRows::ExternalTitle);
  if (self.subtitle.length || self.isMyPosition) m_previewRows.push_back(PreviewRows::Subtitle);
  if (self.schedule != OpeningHours::Unknown) m_previewRows.push_back(PreviewRows::Schedule);
  if (self.isBooking) m_previewRows.push_back(PreviewRows::Booking);
  if (self.address.length) m_previewRows.push_back(PreviewRows::Address);
  
  NSAssert(!m_previewRows.empty(), @"Preview row's can't be empty!");
  m_previewRows.push_back(PreviewRows::Space);
  if (network_policy::CanUseNetwork() && ![MWMSettings adForbidden] && m_info.HasBanner())
  {
    __weak auto wSelf = self;
    [[MWMBannersCache cache]
        getWithCoreBanners:banner_helpers::MatchPriorityBanners(m_info.GetBanners())
                completion:^(id<MWMBanner> ad, BOOL isAsync) {
                  __strong auto self = wSelf;
                  if (!self)
                    return;

                  self.nativeAd = ad;
                  self->m_previewRows.push_back(PreviewRows::Banner);
                  if (isAsync)
                    self.bannerIsReadyCallback();
                }
                 cacheOnly:NO];
  }
}

- (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:
      if (!self.isBooking)
        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);
  if (m_info.IsReachableByTaxi())
    m_metainfoRows.push_back(MetainfoRows::Taxi);
}

- (void)fillButtonsSection
{
  // We don't have to show edit, add place or business if it's booking object.
  if (self.isBooking)
  {
    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);

  switch (m_info.GetLocalAdsStatus())
  {
  case place_page::LocalAdsStatus::NotAvailable: break;
  case place_page::LocalAdsStatus::Candidate:
    m_buttonsRows.push_back(ButtonsRows::LocalAdsCandidate);
    break;
  case place_page::LocalAdsStatus::Customer:
    m_buttonsRows.push_back(ButtonsRows::LocalAdsCustomer);
    break;
  }
}

- (void)fillOnlineBookingSections
{
  if (!self.isBooking)
    return;

  if (Platform::ConnectionStatus() == Platform::EConnectionType::CONNECTION_NONE)
    return;

  network_policy::CallPartnersApi([self](platform::NetworkPolicy const & canUseNetwork)
  {
    auto api = GetFramework().GetBookingApi(canUseNetwork);
    if (!api)
      return;

    string const hotelId = self.sponsoredId.UTF8String;
    api->GetHotelInfo(hotelId, [[AppInfo sharedInfo] twoLetterLanguageId].UTF8String,
                      [hotelId, self](booking::HotelInfo const & hotelInfo)
    {

      if (hotelId != hotelInfo.m_hotelId)
        return;

      dispatch_async(dispatch_get_main_queue(), [hotelInfo, self]
      {
        m_hotelInfo = hotelInfo;

        auto & sections = self->m_sections;
        auto const begin = sections.begin();
        auto const end = sections.end();

        NSUInteger const position = find(begin, end, Sections::Bookmark) != end ? 2 : 1;
        NSUInteger length = 0;
        auto it = m_sections.begin() + position;

        if (!hotelInfo.m_photos.empty())
        {
          it = sections.insert(it, Sections::HotelPhotos) + 1;
          m_hotelPhotosRows.emplace_back(HotelPhotosRow::Regular);
          length++;
        }

        if (!hotelInfo.m_description.empty())
        {
          it = sections.insert(it, Sections::HotelDescription) + 1;
          m_hotelDescriptionRows.emplace_back(HotelDescriptionRow::Regular);
          m_hotelDescriptionRows.emplace_back(HotelDescriptionRow::ShowMore);
          length++;
        }

        auto const & facilities = hotelInfo.m_facilities;
        if (!facilities.empty())
        {
          it = sections.insert(it, Sections::HotelFacilities) + 1;
          auto & facilitiesRows = self->m_hotelFacilitiesRows;
          auto const size = facilities.size();
          auto constexpr maxNumberOfHotelCellsInPlacePage = 3UL;

          if (size > maxNumberOfHotelCellsInPlacePage)
          {
            facilitiesRows.insert(facilitiesRows.begin(), maxNumberOfHotelCellsInPlacePage, HotelFacilitiesRow::Regular);
            facilitiesRows.emplace_back(HotelFacilitiesRow::ShowMore);
          }
          else
          {
            facilitiesRows.insert(facilitiesRows.begin(), size, HotelFacilitiesRow::Regular);
          }

          length++;
        }

        auto const & reviews = hotelInfo.m_reviews;
        if (!reviews.empty())
        {
          sections.insert(it, Sections::HotelReviews);
          auto const size = reviews.size();
          auto & reviewsRows = self->m_hotelReviewsRows;

          reviewsRows.emplace_back(HotelReviewsRow::Header);
          reviewsRows.insert(reviewsRows.end(), size, HotelReviewsRow::Regular);
          reviewsRows.emplace_back(HotelReviewsRow::ShowMore);
          length++;
        }

        self.sectionsAreReadyCallback({position, length}, self);
      });
    });
  });
}

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

    auto category = f.GetBmCategory(categoryIndex);
    NSAssert(category, @"Category can't be nullptr!");
    {
      BookmarkCategory::Guard guard(*category);
      auto 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));
  }
}

- (void)dealloc
{
  if (self.nativeAd)
    [[MWMBannersCache cache] bannerIsOutOfScreenWithCoreBanner:self.nativeAd];
}

#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;
{
  using type = place_page::OpeningHours;
  auto const raw = m_info.GetOpeningHours();
  if (raw.empty())
    return type::Unknown;

  auto const t = time(nullptr);
  osmoh::OpeningHours oh(raw);
  if (!oh.IsValid())
    return type::Unknown;
  if (oh.IsTwentyFourHours())
    return type::AllDay;
  if (oh.IsOpen(t))
    return type::Open;
  if (oh.IsClosed(t))
    return type::Closed;

  return type::Unknown;
}

#pragma mark - Sponsored

- (NSString *)bookingRating { return self.isBooking ? @(m_info.GetRatingFormatted().c_str()) : nil; }
- (NSString *)bookingApproximatePricing { return self.isBooking ? @(m_info.GetApproximatePricing().c_str()) : nil; }
- (NSURL *)sponsoredURL
{
  return m_info.IsSponsored() ? [NSURL URLWithString:@(m_info.GetSponsoredUrl().c_str())]
                                   : nil;
}

- (NSURL *)sponsoredDescriptionURL
{
  return m_info.IsSponsored()
             ? [NSURL URLWithString:@(m_info.GetSponsoredDescriptionUrl().c_str())]
             : nil;
}

- (NSURL *)bookingSearchURL
{
  auto const & url = m_info.GetBookingSearchUrl();
  return url.empty() ? nil : [NSURL URLWithString:@(url.c_str())];
}

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

- (void)assignOnlinePriceToLabel:(UILabel *)label
{
  NSAssert(self.isBooking, @"Online price must be assigned to booking object!");
  if (self.cachedMinPrice.length)
  {
    label.text = self.cachedMinPrice;
    return;
  }

  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 const func = [self, label, currency, currencyFormatter](string const & hotelId,
                                                               string const & minPrice,
                                                               string const & priceCurrency) {
    if (currency != priceCurrency)
      return;

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

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

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

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

  network_policy::CallPartnersApi(
      [self, currency, func](platform::NetworkPolicy const & canUseNetwork) {
        auto const api = GetFramework().GetBookingApi(canUseNetwork);
        if (api)
          api->GetMinPrice(self.sponsoredId.UTF8String, currency, func);
  });
}

- (NSString *)hotelDescription { return @(m_hotelInfo.m_description.c_str()); }
- (vector<booking::HotelFacility> const &)facilities { return m_hotelInfo.m_facilities; }
- (vector<booking::HotelReview> const &)reviews { return m_hotelInfo.m_reviews; }
- (NSUInteger)numberOfReviews { return m_hotelInfo.m_scoreCount; }
- (NSURL *)URLToAllReviews { return [NSURL URLWithString:@(m_info.GetSponsoredReviewUrl().c_str())]; }
- (NSArray<MWMGalleryItemModel *> *)photos
{
  if (_photos)
    return _photos;

  NSMutableArray<MWMGalleryItemModel *> * res = [@[] mutableCopy];
  for (auto const & p : m_hotelInfo.m_photos)
  {
    auto big = [NSURL URLWithString:@(p.m_original.c_str())];
    auto preview = [NSURL URLWithString:@(p.m_small.c_str())];
    if (!big || !preview)
      continue;
    
    auto photo = [[MWMGalleryItemModel alloc] initWithImageURL:big previewURL:preview];
    [res addObject:photo];
  }

  self.photos = res;
  return _photos;
}

#pragma mark - Bookmark

- (NSString *)externalTitle
{
  if (m_info.IsBookmark() && m_info.m_bookmarkTitle != m_info.GetTitle())
    return @(m_info.m_bookmarkTitle.c_str());

  auto const secondaryTitle = m_info.GetSecondaryTitle();
  if (!secondaryTitle.empty())
    return @(secondaryTitle.c_str());

  return 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();
}

#pragma mark - Local Ads
- (NSString *)localAdsURL { return @(m_info.GetLocalAdsUrl().c_str()); }
#pragma mark - Getters

- (NSString *)address { return @(m_info.GetAddress().c_str()); }
- (NSString *)apiURL { return @(m_info.GetApiUrl().c_str()); }
- (vector<Sections> const &)sections { return m_sections; }
- (vector<PreviewRows> const &)previewRows { return m_previewRows; }
- (vector<MetainfoRows> const &)metainfoRows { return m_metainfoRows; }
- (vector<MetainfoRows> &)mutableMetainfoRows { return m_metainfoRows; }
- (vector<ButtonsRows> const &)buttonsRows { return m_buttonsRows; }
- (vector<HotelPhotosRow> const &)photosRows { return m_hotelPhotosRows; }
- (vector<HotelDescriptionRow> const &)descriptionRows { return m_hotelDescriptionRows; }
- (vector<HotelFacilitiesRow> const &)hotelFacilitiesRows { return m_hotelFacilitiesRows; }
- (vector<HotelReviewsRow> const &)hotelReviewsRows { return m_hotelReviewsRows; }
- (NSString *)stringForRow:(MetainfoRows)row
{
  switch (row)
  {
  case MetainfoRows::Taxi:
  case MetainfoRows::ExtendedOpeningHours: return nil;
  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.GetLocalizedCuisines(), 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.m_sponsoredType == SponsoredType::Booking; }
- (BOOL)isOpentable { return m_info.m_sponsoredType == SponsoredType::Opentable; }
- (BOOL)isBookingSearch { return !m_info.GetBookingSearchUrl().empty(); }
- (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
{
  // TODO: Move changing latlon's mode to the settings.
  NSUserDefaults * ud = [NSUserDefaults standardUserDefaults];
  [ud setBool:![ud boolForKey:kUserDefaultsLatLonAsDMSKey] forKey:kUserDefaultsLatLonAsDMSKey];
  [ud synchronize];
}

#pragma mark - Stats

- (NSString *)statisticsTags
{
  NSMutableArray<NSString *> * result = [@[] mutableCopy];
  for (auto const & s : m_info.GetRawTypes())
    [result addObject:@(s.c_str())];
  return [result componentsJoinedByString:@", "];
}

@end