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

MWMEditorViewController.mm « Editor « Classes « Maps « iphone - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e7e39e53657d1a33681e20d4cfded2ac214dd00f (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
#import "MWMAuthorizationCommon.h"
#import "MWMCuisineEditorViewController.h"
#import "MWMEditorCommon.h"
#import "MWMEditorSelectTableViewCell.h"
#import "MWMEditorSwitchTableViewCell.h"
#import "MWMEditorTextTableViewCell.h"
#import "MWMEditorViewController.h"
#import "MWMOpeningHoursEditorViewController.h"
#import "MWMPlacePageOpeningHoursCell.h"
#import "MWMStreetEditorViewController.h"
#import "Statistics.h"

#include "std/algorithm.hpp"

namespace
{
NSString * const kOpeningHoursEditorSegue = @"Editor2OpeningHoursEditorSegue";
NSString * const kCuisineEditorSegue = @"Editor2CuisineEditorSegue";
NSString * const kStreetEditorSegue = @"Editor2StreetEditorSegue";

typedef NS_ENUM(NSUInteger, MWMEditorSection)
{
  MWMEditorSectionName,
  MWMEditorSectionAddress,
  MWMEditorSectionDetails
};

vector<MWMPlacePageCellType> const gSectionNameCellTypes{MWMPlacePageCellTypeName};

vector<MWMPlacePageCellType> const gSectionAddressCellTypes{
    {MWMPlacePageCellTypeStreet, MWMPlacePageCellTypeBuilding}};

vector<MWMPlacePageCellType> const gSectionDetailsCellTypes{
    {MWMPlacePageCellTypeOpenHours, MWMPlacePageCellTypePhoneNumber, MWMPlacePageCellTypeWebsite,
     MWMPlacePageCellTypeEmail, MWMPlacePageCellTypeCuisine, MWMPlacePageCellTypeWiFi}};

using CellTypesSectionMap = pair<vector<MWMPlacePageCellType>, MWMEditorSection>;

vector<CellTypesSectionMap> const gCellTypesSectionMap{
    {gSectionNameCellTypes, MWMEditorSectionName},
    {gSectionAddressCellTypes, MWMEditorSectionAddress},
    {gSectionDetailsCellTypes, MWMEditorSectionDetails}};

MWMPlacePageCellTypeValueMap const gCellType2ReuseIdentifier{
    {MWMPlacePageCellTypeName, "MWMEditorNameTableViewCell"},
    {MWMPlacePageCellTypeStreet, "MWMEditorSelectTableViewCell"},
    {MWMPlacePageCellTypeBuilding, "MWMEditorTextTableViewCell"},
    {MWMPlacePageCellTypeOpenHours, "MWMPlacePageOpeningHoursCell"},
    {MWMPlacePageCellTypePhoneNumber, "MWMEditorTextTableViewCell"},
    {MWMPlacePageCellTypeWebsite, "MWMEditorTextTableViewCell"},
    {MWMPlacePageCellTypeEmail, "MWMEditorTextTableViewCell"},
    {MWMPlacePageCellTypeCuisine, "MWMEditorSelectTableViewCell"},
    {MWMPlacePageCellTypeWiFi, "MWMEditorSwitchTableViewCell"}};

NSString * reuseIdentifier(MWMPlacePageCellType cellType)
{
  auto const it = gCellType2ReuseIdentifier.find(cellType);
  BOOL const haveCell = (it != gCellType2ReuseIdentifier.end());
  ASSERT(haveCell, ());
  return haveCell ? @(it->second.c_str()) : @"";
}
} // namespace

@interface MWMEditorViewController() <UITableViewDelegate, UITableViewDataSource,
                                      UITextFieldDelegate, MWMPlacePageOpeningHoursCellProtocol,
                                      MWMEditorCellProtocol, MWMCuisineEditorProtocol,
                                      MWMStreetEditorProtocol>

@property (nonatomic) NSMutableDictionary<NSString *, UITableViewCell *> * offscreenCells;
@property (weak, nonatomic) UITableViewCell * editCell;

@property (nonatomic) BOOL needsReload;

@end

@implementation MWMEditorViewController
{
  vector<MWMEditorSection> m_sections;
  map<MWMEditorSection, vector<MWMPlacePageCellType>> m_cells;
  MWMPlacePageCellTypeValueMap m_edited_cells;
}

- (void)viewDidLoad
{
  [[Statistics instance] logEvent:kStatEventName(kStatEdit, kStatOpen)];
  [super viewDidLoad];
  [self configTable];
  [self configNavBar];
}

- (void)viewWillAppear:(BOOL)animated
{
  [super viewWillAppear:animated];
  [self reloadData];
}

#pragma mark - Configuration

- (void)configNavBar
{
  self.title = L(@"edit_place").capitalizedString;
  self.navigationItem.leftBarButtonItem =
      [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
                                                    target:self
                                                    action:@selector(onCancel)];
  self.navigationItem.rightBarButtonItem =
      [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave
                                                    target:self
                                                    action:@selector(onSave)];
  self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
}

#pragma mark - Actions

- (void)onCancel
{
  UINavigationController * parentNavController = self.navigationController.navigationController;
  if (parentNavController)
    [parentNavController popViewControllerAnimated:YES];
  else
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)onSave
{
  if (!m_edited_cells.empty())
  {
    [[Statistics instance] logEvent:kStatEventName(kStatEdit, kStatSave)];
    osm_auth_ios::AuthorizationSetNeedCheck(YES);
    self.entity.cuisines = self.cuisines;
    [self.entity saveEditedCells:m_edited_cells];
  }
  [self onCancel];
}

#pragma mark - Offscreen cells

- (UITableViewCell *)offscreenCellForIdentifier:(NSString *)reuseIdentifier
{
  UITableViewCell * cell = self.offscreenCells[reuseIdentifier];
  if (!cell)
  {
    cell = [[[NSBundle mainBundle] loadNibNamed:reuseIdentifier owner:nil options:nil] firstObject];
    self.offscreenCells[reuseIdentifier] = cell;
  }
  return cell;
}

#pragma mark - Data source

- (NSString *)getCellValue:(MWMPlacePageCellType)cellType
{
  auto const it = m_edited_cells.find(cellType);
  if (it != m_edited_cells.end())
    return @(it->second.c_str());
  return [self.entity getCellValue:cellType];
}

- (void)setCell:(MWMPlacePageCellType)cellType value:(NSString *)value
{
  if ([value isEqualToString:[self getCellValue:cellType]])
    return;
  self.needsReload = YES;
  m_edited_cells[cellType] = value.UTF8String;
}

#pragma mark - Table

- (void)reloadData
{
  if (self.needsReload)
  {
    [self.tableView reloadData];
    self.needsReload = NO;
  }
}

- (void)configTable
{
  NSAssert(self.entity, @"Entity must be set");
  self.offscreenCells = [NSMutableDictionary dictionary];
  m_sections.clear();
  m_cells.clear();
  m_edited_cells.clear();
  for (auto cellsSection : gCellTypesSectionMap)
  {
    for (auto cellType : cellsSection.first)
    {
      if (![self.entity isCellEditable:cellType])
        continue;
      m_sections.emplace_back(cellsSection.second);
      m_cells[cellsSection.second].emplace_back(cellType);
      NSString * identifier = reuseIdentifier(cellType);
      [self.tableView registerNib:[UINib nibWithNibName:identifier bundle:nil]
           forCellReuseIdentifier:identifier];
    }
  }
  sort(m_sections.begin(), m_sections.end());
  m_sections.erase(unique(m_sections.begin(), m_sections.end()), m_sections.end());
}

- (MWMPlacePageCellType)cellTypeForIndexPath:(NSIndexPath *)indexPath
{
  MWMEditorSection const section = m_sections[indexPath.section];
  MWMPlacePageCellType const cellType = m_cells[section][indexPath.row];
  return cellType;
}

- (NSString *)cellIdentifierForIndexPath:(NSIndexPath *)indexPath
{
  MWMPlacePageCellType const cellType = [self cellTypeForIndexPath:indexPath];
  return reuseIdentifier(cellType);
}

#pragma mark - Fill cells with data

- (void)fillCell:(UITableViewCell * _Nonnull)cell atIndexPath:(NSIndexPath * _Nonnull)indexPath
{
  MWMPlacePageCellType const cellType = [self cellTypeForIndexPath:indexPath];
  NSString * entityValue = [self getCellValue:cellType];
  switch (cellType)
  {
    case MWMPlacePageCellTypePhoneNumber:
    {
      MWMEditorTextTableViewCell * tCell = (MWMEditorTextTableViewCell *)cell;
      [tCell configWithDelegate:self
                           icon:[UIImage imageNamed:@"ic_placepage_phone_number"]
                           text:entityValue
                    placeholder:L(@"phone")
                   keyboardType:UIKeyboardTypePhonePad];
      break;
    }
    case MWMPlacePageCellTypeWebsite:
    {
      MWMEditorTextTableViewCell * tCell = (MWMEditorTextTableViewCell *)cell;
      [tCell configWithDelegate:self
                           icon:[UIImage imageNamed:@"ic_placepage_website"]
                           text:entityValue
                    placeholder:L(@"website")
                   keyboardType:UIKeyboardTypeURL];
      break;
    }
    case MWMPlacePageCellTypeEmail:
    {
      MWMEditorTextTableViewCell * tCell = (MWMEditorTextTableViewCell *)cell;
      [tCell configWithDelegate:self
                           icon:[UIImage imageNamed:@"ic_placepage_email"]
                           text:entityValue
                    placeholder:L(@"email")
                   keyboardType:UIKeyboardTypeEmailAddress];
      break;
    }
    case MWMPlacePageCellTypeOpenHours:
    {
      MWMPlacePageOpeningHoursCell * tCell = (MWMPlacePageOpeningHoursCell *)cell;
      NSString * text = entityValue ? entityValue : L(@"add_opening_hours");
      [tCell configWithDelegate:self info:text];
      break;
    }
    case MWMPlacePageCellTypeWiFi:
    {
      MWMEditorSwitchTableViewCell * tCell = (MWMEditorSwitchTableViewCell *)cell;
      BOOL const on = (entityValue != nil);
      [tCell configWithDelegate:self
                           icon:[UIImage imageNamed:@"ic_placepage_wifi"]
                           text:L(@"wifi")
                             on:on];
      break;
    }
    case MWMPlacePageCellTypeName:
    {
      MWMEditorTextTableViewCell * tCell = (MWMEditorTextTableViewCell *)cell;
      [tCell configWithDelegate:self
                           icon:nil
                           text:entityValue
                    placeholder:L(@"name")
                   keyboardType:UIKeyboardTypeDefault];
      break;
    }
    case MWMPlacePageCellTypeStreet:
    {
      MWMEditorSelectTableViewCell * tCell = (MWMEditorSelectTableViewCell *)cell;
      [tCell configWithDelegate:self
                           icon:[UIImage imageNamed:@"ic_placepage_adress"]
                           text:entityValue
                    placeholder:L(@"add_street")];
      break;
    }
    case MWMPlacePageCellTypeBuilding:
    {
      MWMEditorTextTableViewCell * tCell = (MWMEditorTextTableViewCell *)cell;
      [tCell configWithDelegate:self
                           icon:nil
                           text:entityValue
                    placeholder:L(@"house")
                   keyboardType:UIKeyboardTypeDefault];
      break;
    }
    case MWMPlacePageCellTypeCuisine:
    {
      MWMEditorSelectTableViewCell * tCell = (MWMEditorSelectTableViewCell *)cell;
      NSString * text = [entityValue capitalizedStringWithLocale:[NSLocale currentLocale]];
      [tCell configWithDelegate:self
                           icon:[UIImage imageNamed:@"ic_placepage_cuisine"]
                           text:text
                    placeholder:L(@"select_cuisine")];
      break;
    }
    default:
      NSAssert(false, @"Invalid field for editor");
      break;
  }
}

#pragma mark - UITableViewDataSource

- (UITableViewCell * _Nonnull)tableView:(UITableView * _Nonnull)tableView cellForRowAtIndexPath:(NSIndexPath * _Nonnull)indexPath
{
  NSString * reuseIdentifier = [self cellIdentifierForIndexPath:indexPath];
  return [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView * _Nonnull)tableView
{
  return m_sections.size();
}

- (NSInteger)tableView:(UITableView * _Nonnull)tableView numberOfRowsInSection:(NSInteger)section
{
  MWMEditorSection const sec = m_sections[section];
  return m_cells[sec].size();
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView * _Nonnull)tableView heightForRowAtIndexPath:(NSIndexPath * _Nonnull)indexPath
{
  NSString * reuseIdentifier = [self cellIdentifierForIndexPath:indexPath];
  UITableViewCell * cell = [self offscreenCellForIdentifier:reuseIdentifier];
  [self fillCell:cell atIndexPath:indexPath];
  MWMPlacePageCellType const cellType = [self cellTypeForIndexPath:indexPath];
  switch (cellType)
  {
    case MWMPlacePageCellTypeOpenHours:
      return ((MWMPlacePageOpeningHoursCell *)cell).cellHeight;
    default:
    {
      [cell setNeedsUpdateConstraints];
      [cell updateConstraintsIfNeeded];
      cell.bounds = {{}, {CGRectGetWidth(tableView.bounds), CGRectGetHeight(cell.bounds)}};
      [cell setNeedsLayout];
      [cell layoutIfNeeded];
      CGSize const size = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
      return size.height;
    }
  }
}

- (void)tableView:(UITableView * _Nonnull)tableView willDisplayCell:(UITableViewCell * _Nonnull)cell forRowAtIndexPath:(NSIndexPath * _Nonnull)indexPath
{
  [self fillCell:cell atIndexPath:indexPath];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
  switch (m_sections[section])
  {
  case MWMEditorSectionName:
    return nil;
  case MWMEditorSectionAddress:
    return L(@"address");
  case MWMEditorSectionDetails:
    return L(@"details");
  }
}

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
  switch (m_sections[section])
  {
  case MWMEditorSectionName:
    return L(@"place_name_caption");
  case MWMEditorSectionAddress:
  case MWMEditorSectionDetails:
    return nil;
  }
}

#pragma mark - MWMPlacePageOpeningHoursCellProtocol

- (BOOL)forcedButton
{
  return YES;
}

- (BOOL)isPlaceholder
{
  return [self getCellValue:MWMPlacePageCellTypeOpenHours] == nil;
}

- (BOOL)isEditor
{
  return YES;
}

- (BOOL)openingHoursCellExpanded
{
  return YES;
}

- (void)setOpeningHoursCellExpanded:(BOOL)openingHoursCellExpanded forCell:(UITableViewCell *)cell
{
  [self performSegueWithIdentifier:kOpeningHoursEditorSegue sender:nil];
}

#pragma mark - MWMEditorCellProtocol

- (void)cellBeginEditing:(UITableViewCell *)cell
{
  self.editCell = cell;
}

- (void)cell:(UITableViewCell *)cell changeText:(NSString *)changeText
{
  NSIndexPath * indexPath = [self.tableView indexPathForCell:cell];
  MWMPlacePageCellType const cellType = [self cellTypeForIndexPath:indexPath];
  switch (cellType)
  {
    case MWMPlacePageCellTypeName:
    case MWMPlacePageCellTypePhoneNumber:
    case MWMPlacePageCellTypeWebsite:
    case MWMPlacePageCellTypeEmail:
    case MWMPlacePageCellTypeBuilding:
      [self setCell:cellType value:changeText];
      break;
    default:
      NSAssert(false, @"Invalid field for changeText");
      break;
  }
}

- (void)cell:(UITableViewCell *)cell changeSwitch:(BOOL)changeSwitch
{
  NSIndexPath * indexPath = [self.tableView indexPathForCell:cell];
  MWMPlacePageCellType const cellType = [self cellTypeForIndexPath:indexPath];
  switch (cellType)
  {
    case MWMPlacePageCellTypeWiFi:
    {
      BOOL const on = ([self getCellValue:cellType] != nil);
      if (changeSwitch != on)
        [self setCell:cellType value:changeSwitch ? @"wlan" : @""];
      break;
    }
    default:
      NSAssert(false, @"Invalid field for changeSwitch");
      break;
  }
}

- (void)cellSelect:(UITableViewCell *)cell
{
  NSIndexPath * indexPath = [self.tableView indexPathForCell:cell];
  MWMPlacePageCellType const cellType = [self cellTypeForIndexPath:indexPath];
  switch (cellType)
  {
    case MWMPlacePageCellTypeStreet:
      [self performSegueWithIdentifier:kStreetEditorSegue sender:nil];
      break;
    case MWMPlacePageCellTypeCuisine:
      [self performSegueWithIdentifier:kCuisineEditorSegue sender:nil];
      break;
    default:
      NSAssert(false, @"Invalid field for cellSelect");
      break;
  }
}

#pragma mark - MWMOpeningHoursEditorProtocol

- (void)setOpeningHours:(NSString *)openingHours
{
  [self setCell:MWMPlacePageCellTypeOpenHours value:openingHours];
}

#pragma mark - MWMCuisineEditorProtocol

@synthesize cuisines = _cuisines;
- (NSSet<NSString *> *)cuisines
{
  if (_cuisines)
    return _cuisines;
  return self.entity.cuisines;
}

- (void)setCuisines:(NSSet<NSString *> *)cuisines
{
  if ([self.cuisines isEqualToSet:cuisines])
    return;
  _cuisines = cuisines;
  self.needsReload = YES;
  [self setCell:MWMPlacePageCellTypeCuisine value:[MWMPlacePageEntity makeMWMCuisineString:cuisines]];
}

#pragma mark - MWMStreetEditorProtocol

- (NSString *)getStreet
{
  return [self getCellValue:MWMPlacePageCellTypeStreet];
}

- (void)setStreet:(NSString *)street
{
  [self setCell:MWMPlacePageCellTypeStreet value:street];
}

- (NSArray<NSString *> *)getNearbyStreets
{
  return self.entity.nearbyStreets;
}

#pragma mark - Segue

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
  if ([segue.identifier isEqualToString:kOpeningHoursEditorSegue])
  {
    MWMOpeningHoursEditorViewController * dvc = segue.destinationViewController;
    dvc.openingHours = [self getCellValue:MWMPlacePageCellTypeOpenHours];
    dvc.delegate = self;
  }
  else if ([segue.identifier isEqualToString:kCuisineEditorSegue])
  {
    MWMCuisineEditorViewController * dvc = segue.destinationViewController;
    dvc.delegate = self;
  }
  else if ([segue.identifier isEqualToString:kStreetEditorSegue])
  {
    MWMStreetEditorViewController * dvc = segue.destinationViewController;
    dvc.delegate = self;
  }
}

@end