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

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

#import "MWMPlacePageEntity.h"
#import "UIKitCategories.h"
#import "MWMPlacePageViewManager.h"
#import "MapViewController.h"
#import "UIKitCategories.h"
#include "../../../platform/measurement_utils.hpp"

extern NSArray * const kBookmarkColorsVariant = @[@"placemark-red", @"placemark-yellow", @"placemark-blue", @"placemark-green", @"placemark-purple", @"placemark-orange", @"placemark-brown", @"placemark-pink"];
extern NSString * const kUserDefaultsLatLonAsDMSKey = @"UserDefaultsLatLonAsDMS";
static NSArray * const kPatternTypesArray = @[@(MWMPlacePageMetadataTypePostcode), @(MWMPlacePageMetadataTypePhoneNumber), @(MWMPlacePageMetadataTypeWebsite), @(MWMPlacePageMetadataTypeURL), @(MWMPlacePageMetadataTypeEmail), @(MWMPlacePageMetadataTypeOpenHours), @(MWMPlacePageMetadataTypeWiFi), @(MWMPlacePageMetadataTypeCoordinate)];

static NSString * const kTypesKey = @"types";
static NSString * const kValuesKey = @"values";

using feature::Metadata;

@interface MWMPlacePageEntity ()

@property (nonatomic) NSDictionary * metadata;

@end

@implementation MWMPlacePageEntity

- (instancetype)initWithUserMark:(UserMark const *)mark
{
  self = [super init];
  if (self)
    [self configureWithUserMark:mark];

  return self;
}

- (void)configureWithUserMark:(UserMark const *)mark
{
  UserMark::Type const type = mark->GetMarkType();
  double x, y;
  mark->GetLatLon(x, y);
  self.point = m2::PointD(x, y);

  typedef UserMark::Type Type;
  switch (type)
  {
    case Type::API:
    {
      ApiMarkPoint const * apiMark = static_cast<ApiMarkPoint const *>(mark);
      [self configureForApi:apiMark];
      break;
    }
    case Type::DEBUG_MARK:
      break;
    case Type::MY_POSITION:
    {
      MyPositionMarkPoint const * myPositionMark = static_cast<MyPositionMarkPoint const *>(mark);
      [self configureForMyPosition:myPositionMark];
      break;
    }
    case Type::SEARCH:
      [self configureForSearch:static_cast<SearchMarkPoint const *>(mark)];
      break;
    case Type::POI:
      [self configureForPOI:static_cast<PoiMarkPoint const *>(mark)];
      break;
    case Type::BOOKMARK:
      [self configureForBookmark:mark];
      break;
  }
  GetFramework().ActivateUserMark(mark);
}

- (void)configureForBookmark:(UserMark const *)bookmark
{
  Framework & f = GetFramework();
  self.bac = f.FindBookmark(bookmark);
  self.type = MWMPlacePageEntityTypeBookmark;
  BookmarkCategory * category = f.GetBmCategory(self.bac.first);
  BookmarkData const & data = static_cast<Bookmark const *>(bookmark)->GetData();
  m2::PointD const & point = bookmark->GetOrg();
  Metadata metadata;
  search::AddressInfo info;
  f.FindClosestPOIMetadata(point, metadata);
  f.GetAddressInfoForGlobalPoint(point, info);

  self.bookmarkTitle = @(data.GetName().c_str());
  self.bookmarkCategory = @(category->GetName().c_str());
  string const description = data.GetDescription();
  self.bookmarkDescription = @(description.c_str());
  _isHTMLDescription = strings::IsHTML(description);
  self.bookmarkColor = @(data.GetType().c_str());

  [self configureEntityWithMetadata:metadata addressInfo:info];
  [self insertBookmarkInTypes];
}

- (void)configureForSearch:(SearchMarkPoint const *)searchMark
{
//Workaround for framework bug.
//TODO: Make correct way to get search metadata.
  Metadata metadata;
  GetFramework().FindClosestPOIMetadata(searchMark->GetOrg(), metadata);
  [self configureEntityWithMetadata:metadata addressInfo:searchMark->GetInfo()];
}

- (void)configureForPOI:(PoiMarkPoint const *)poiMark
{
  [self configureEntityWithMetadata:poiMark->GetMetadata() addressInfo:poiMark->GetInfo()];
}

- (void)configureForMyPosition:(MyPositionMarkPoint const *)myPositionMark
{
  self.title = L(@"my_position");
  self.type = MWMPlacePageEntityTypeMyPosition;
  NSMutableArray * types = [NSMutableArray array];
  NSMutableArray * values = [NSMutableArray array];
  [types addObject:kPatternTypesArray.lastObject];
  BOOL const isLatLonAsDMS = [[NSUserDefaults standardUserDefaults] boolForKey:kUserDefaultsLatLonAsDMSKey];
  NSString * latLonStr = isLatLonAsDMS ? @(MeasurementUtils::FormatLatLonAsDMS(self.point.x, self.point.y, 2).c_str()): @( MeasurementUtils::FormatLatLon(self.point.x, self.point.y).c_str());
  [values addObject:latLonStr];

  self.metadata = @{kTypesKey : types, kValuesKey : values};
}

- (void)configureForApi:(ApiMarkPoint const *)apiMark
{
  self.type = MWMPlacePageEntityTypeAPI;
  self.title = @(apiMark->GetName().c_str());
  self.category = @(GetFramework().GetApiDataHolder().GetAppTitle().c_str());
  NSMutableArray const * types = [NSMutableArray array];
  NSMutableArray const * values = [NSMutableArray array];
  [types addObject:kPatternTypesArray.lastObject];
  BOOL const isLatLonAsDMS = [[NSUserDefaults standardUserDefaults] boolForKey:kUserDefaultsLatLonAsDMSKey];
  NSString * latLonStr = isLatLonAsDMS ? @(MeasurementUtils::FormatLatLonAsDMS(self.point.x, self.point.y, 2).c_str()) : @(MeasurementUtils::FormatLatLon(self.point.x, self.point.y).c_str());
  latLonStr = isLatLonAsDMS ? @(MeasurementUtils::FormatLatLonAsDMS(self.point.x, self.point.y, 2).c_str()) : @(MeasurementUtils::FormatLatLon(self.point.x, self.point.y).c_str());
  [values addObject:latLonStr];
  self.metadata = @{kTypesKey : types, kValuesKey : values};
}

- (void)configureEntityWithMetadata:(Metadata const &)metadata addressInfo:(search::AddressInfo const &)info
{
  NSString * const name = @(info.GetPinName().c_str());
  self.title = name.length > 0 ? name : L(@"dropped_pin");
  self.category = @(info.GetPinType().c_str());

  vector<Metadata::EType> const presentTypes = metadata.GetPresentTypes();

  NSMutableArray const * types = [NSMutableArray array];
  NSMutableArray const * values = [NSMutableArray array];

  for (auto const & type : presentTypes)
  {
    switch (type)
    {
      case Metadata::FMD_CUISINE:
      {
        NSString * result = @(metadata.Get(type).c_str());
        NSString * cuisine = [NSString stringWithFormat:@"cuisine_%@", result];
        NSString * localizedResult = L(cuisine);
        NSString * currentCategory = self.category;
        if (![localizedResult isEqualToString:currentCategory])
        {
          if ([localizedResult isEqualToString:cuisine])
          {
            if (![result isEqualToString:currentCategory])
              self.category = [NSString stringWithFormat:@"%@, %@", self.category, result];
          }
          else
          {
            self.category = [NSString stringWithFormat:@"%@, %@", self.category, localizedResult];
          }
        }
        break;
      }
      case Metadata::FMD_ELE:
      {
        self.typeDescriptionValue = atoi(metadata.Get(type).c_str());
        if (self.type != MWMPlacePageEntityTypeBookmark)
          self.type = MWMPlacePageEntityTypeEle;
        break;
      }
      case Metadata::FMD_OPERATOR:
      {
        NSString const * bank = @(metadata.Get(type).c_str());
        if (self.category.length)
          self.category = [NSString stringWithFormat:@"%@, %@", self.category, bank];
        else
          self.category = [NSString stringWithFormat:@"%@", bank];
        break;
      }
      case Metadata::FMD_STARS:
      {
        self.typeDescriptionValue = atoi(metadata.Get(type).c_str());
        if (self.type != MWMPlacePageEntityTypeBookmark)
          self.type = MWMPlacePageEntityTypeHotel;
        break;
      }
      case Metadata::FMD_URL:
      case Metadata::FMD_WEBSITE:
      case Metadata::FMD_PHONE_NUMBER:
      case Metadata::FMD_OPEN_HOURS:
      case Metadata::FMD_EMAIL:
      case Metadata::FMD_POSTCODE:
      case Metadata::FMD_INTERNET:
      {
        NSString * v;
        if (type == Metadata::EType::FMD_OPEN_HOURS)
          v = [self formattedOpenHoursFromString:metadata.Get(type)];
        else if (type == Metadata::FMD_INTERNET)
          v = L(@"WiFi_available");
        else
          v = @(metadata.Get(type).c_str());

        NSNumber const * t = [self typeFromMetadata:type];
        [types addObject:t];
        [values addObject:v];
        break;
      }

      default:
        break;
    }
  }

  NSUInteger swappedIndex = 0;
  for (NSNumber * pattern in kPatternTypesArray)
  {
    NSUInteger const index = [types indexOfObject:pattern];
    if (index == NSNotFound)
      continue;
    [types exchangeObjectAtIndex:index withObjectAtIndex:swappedIndex];
    [values exchangeObjectAtIndex:index withObjectAtIndex:swappedIndex];
    swappedIndex++;
  }

  [types addObject:kPatternTypesArray.lastObject];
  BOOL const isLatLonAsDMS = [[NSUserDefaults standardUserDefaults] boolForKey:kUserDefaultsLatLonAsDMSKey];
  NSString * latLonStr = isLatLonAsDMS ? @(MeasurementUtils::FormatLatLonAsDMS(self.point.x, self.point.y, 2).c_str()) : @( MeasurementUtils::FormatLatLon(self.point.x, self.point.y).c_str());
  latLonStr = isLatLonAsDMS ? @(MeasurementUtils::FormatLatLonAsDMS(self.point.x, self.point.y, 2).c_str()) : @( MeasurementUtils::FormatLatLon(self.point.x, self.point.y).c_str());
  [values addObject:latLonStr];
  
  self.metadata = @{kTypesKey : types, kValuesKey : values};
}

- (NSArray *)metadataTypes
{
  return (NSArray *)self.metadata[kTypesKey];
}

- (NSArray *)metadataValues
{
  return (NSArray *)self.metadata[kValuesKey];
}

- (void)insertBookmarkInTypes
{
  if ([self.metadataTypes containsObject:@(MWMPlacePageMetadataTypeBookmark)])
    return;
  [self.metadata[kTypesKey] insertObject:@(MWMPlacePageMetadataTypeBookmark) atIndex:self.metadataTypes.count];
}

- (void)removeBookmarkFromTypes
{
  [self.metadata[kTypesKey] removeObject:@(MWMPlacePageMetadataTypeBookmark)];
}

- (NSNumber *)typeFromMetadata:(Metadata::EType)type
{
  switch (type)
  {
    case Metadata::FMD_URL:
      return @(MWMPlacePageMetadataTypeURL);
    case Metadata::FMD_WEBSITE:
      return @(MWMPlacePageMetadataTypeWebsite);
    case Metadata::FMD_PHONE_NUMBER:
      return @(MWMPlacePageMetadataTypePhoneNumber);
    case Metadata::FMD_OPEN_HOURS:
      return @(MWMPlacePageMetadataTypeOpenHours);
    case Metadata::FMD_EMAIL:
      return @(MWMPlacePageMetadataTypeEmail);
    case Metadata::FMD_POSTCODE:
      return @(MWMPlacePageMetadataTypePostcode);
    case Metadata::FMD_INTERNET:
      return @(MWMPlacePageMetadataTypeWiFi);
    default:
      return nil;
  }
}

#pragma mark - Bookmark editing

- (NSString *)bookmarkCategory
{
  if (!_bookmarkCategory)
  {
    Framework & f = GetFramework();
    BookmarkCategory * category = f.GetBmCategory(f.LastEditedBMCategory());
    _bookmarkCategory = @(category->GetName().c_str());
  }
  return _bookmarkCategory;
}

- (NSString *)bookmarkDescription
{
  if (!_bookmarkDescription)
    _bookmarkDescription = @"";
  return _bookmarkDescription;
}

- (NSString *)bookmarkColor
{
  if (!_bookmarkColor)
  {
    Framework & f = GetFramework();
    string type = f.LastEditedBMType();
    _bookmarkColor = @(type.c_str());
  }
  return _bookmarkColor;
}

- (NSString *)bookmarkTitle
{
  if (!_bookmarkTitle)
    _bookmarkTitle = self.title;
  return _bookmarkTitle;
}

- (void)synchronize
{
  Framework & f = GetFramework();
  BookmarkCategory * category = f.GetBmCategory(self.bac.first);
  if (!category)
    return;

  Bookmark * bookmark = category->GetBookmark(self.bac.second);
  if (!bookmark)
    return;
  
  if (self.bookmarkColor)
    bookmark->SetType(self.bookmarkColor.UTF8String);

  if (self.bookmarkDescription)
  {
    string const description(self.bookmarkDescription.UTF8String);
    _isHTMLDescription = strings::IsHTML(description);
    bookmark->SetDescription(description);
  }

  if (self.bookmarkTitle)
    bookmark->SetName(self.bookmarkTitle.UTF8String);

  category->SaveToKMLFile();
}

#pragma mark - Open hours string formatter

- (NSString *)formattedOpenHoursFromString:(string const &)s
{
//TODO (Vlad): Not the best solution, but this function is temporary and will be replaced in future.
  NSMutableString * r = [NSMutableString stringWithUTF8String:s.c_str()];
  [r replaceOccurrencesOfString:@"," withString:@", " options:NSCaseInsensitiveSearch range:NSMakeRange(0, r.length)];
  while (YES)
  {
    NSRange const range = [r rangeOfString:@"  "];
    if (range.location == NSNotFound)
      break;
    [r replaceCharactersInRange:range withString:@" "];
  }
  [r replaceOccurrencesOfString:@"; " withString:@"\n" options:NSCaseInsensitiveSearch range:NSMakeRange(0, r.length)];
  [r replaceOccurrencesOfString:@";" withString:@"\n" options:NSCaseInsensitiveSearch range:NSMakeRange(0, r.length)];
  return r.copy;
}

@end