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

BookmarksRootVC.mm « Bookmarks « Maps « iphone - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 51f6320bd298c093614cb569e8096a9006c0caa3 (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
#import "BookmarksRootVC.h"
#import "BookmarksVC.h"
#import "Common.h"
#import "UIKitCategories.h"

#include "Framework.h"
#include "platform/platform.hpp"

#define TEXTFIELD_TAG 999

@implementation BookmarksRootVC

- (id)init
{
  self = [super initWithStyle:UITableViewStyleGrouped];
  if (self)
  {
    self.title = L(@"bookmarks");

    self.tableView.allowsSelectionDuringEditing = YES;
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(newCategoryAdded)
                                                 name:@"KML file added"
                                               object:nil];
  }
  return self;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
  return YES;
}

// Used to display add bookmarks hint
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
  CGFloat const offset = 10;

  CGRect const rect = tableView.bounds;
  // Use UILabel inside custom view to add padding on the left and right (there is no other way to do it)
  if (!m_hint)
  {
    m_hint = [[UIView alloc] initWithFrame:rect];
    m_hint.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    m_hint.backgroundColor = [UIColor clearColor];

    UILabel * label = [[UILabel alloc] initWithFrame:rect];
    label.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    label.backgroundColor = [UIColor clearColor];
    bool const showDetailedHint = !GetFramework().GetBmCategoriesCount();
    label.text = showDetailedHint ? L(@"bookmarks_usage_hint")
                                  : L(@"bookmarks_usage_hint_import_only");
    label.textAlignment = NSTextAlignmentCenter;
    label.lineBreakMode = NSLineBreakByWordWrapping;
    label.numberOfLines = 0;
    [m_hint addSubview:label];
  }
  UILabel * label = [m_hint.subviews objectAtIndex:0];
  label.bounds = CGRectInset(rect, offset, offset);
  [label sizeToIntegralFit];
  m_hint.bounds = CGRectMake(0, 0, rect.size.width, label.bounds.size.height + 2 * offset);
  label.center = CGPointMake(m_hint.bounds.size.width / 2, m_hint.bounds.size.height / 2);

  return m_hint.bounds.size.height;
}

// Used to display hint when no any categories with bookmarks are present
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
  return m_hint;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
  return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
  return GetFramework().GetBmCategoriesCount();
}

- (void)onEyeTapped:(id)sender
{
  NSInteger row = ((UITapGestureRecognizer *)sender).view.tag;
  UITableViewCell * cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:0]];
  BookmarkCategory * cat = GetFramework().GetBmCategory(row);
  if (cell && cat)
  {
    // Invert visibility
    bool visible = !cat->IsVisible();
    cell.imageView.image = [UIImage imageNamed:(visible ? @"eye" : @"empty")];
    cat->SetVisible(visible);
    cat->SaveToKMLFile();
  }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"BookmarksRootVCSetCell"];
  if (!cell)
  {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"BookmarksRootVCSetCell"];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    // Add "Eye" icon click handler to switch visibility
    cell.imageView.userInteractionEnabled = YES;
    UITapGestureRecognizer * tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onEyeTapped:)];
    tapped.numberOfTapsRequired = 1;
    [cell.imageView addGestureRecognizer:tapped];
  }
  // To detect which row was tapped when user clicked on image
  cell.imageView.tag = indexPath.row;

  BookmarkCategory const * cat = GetFramework().GetBmCategory(indexPath.row);
  if (cat)
  {
    NSString * title = @(cat->GetName().c_str());
    cell.textLabel.text = [self truncateString:title toWidth:(self.tableView.width - 122) withFont:cell.textLabel.font];
    cell.imageView.image = [UIImage imageNamed:(cat->IsVisible() ? @"eye" : @"empty")];
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%ld", cat->GetBookmarksCount() + cat->GetTracksCount()];
  }
  return cell;
}

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
  [self.tableView reloadRowsAtIndexPaths:self.tableView.indexPathsForVisibleRows withRowAnimation:UITableViewRowAnimationFade];
}

- (NSString *)truncateString:(NSString *)string toWidth:(CGFloat)width withFont:(UIFont *)font
{
  CGFloat tailLength = 3;
  CGFloat incrementStep = 1;
  if ([string length] < tailLength + incrementStep)
    return string;
  BOOL firstTime = YES;
  NSDictionary * attrs = @{NSFontAttributeName:font};
  while ([string sizeWithAttributes:attrs].width > width)
  {
    if (!firstTime)
      string = [[string substringToIndex:([string length] - tailLength - incrementStep)] stringByAppendingString:@"..."];
    firstTime = NO;
  }
  return string;
}

- (void)applyCategoryRenaming
{
  for (UITableViewCell * cell in self.tableView.visibleCells)
  {
    UITextField * f = (UITextField *)[cell viewWithTag:TEXTFIELD_TAG];
    if (f)
    {
      NSString * txt = f.text;
      // Update edited category name
      if (txt.length && ![txt isEqualToString:cell.textLabel.text])
      {
        cell.textLabel.text = txt;
        // Rename category
        BookmarkCategory * cat = GetFramework().GetBmCategory([self.tableView indexPathForCell:cell].row);
        if (cat)
        {
          cat->SetName([txt UTF8String]);
          cat->SaveToKMLFile();
        }
      }
      [f removeFromSuperview];
      cell.textLabel.hidden = NO;
      cell.detailTextLabel.hidden = NO;
      cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
      break;
    }
  }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  // Remove cell selection
  UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
  [self.tableView deselectRowAtIndexPath:indexPath animated:YES];

  if (tableView.editing)
  {
    [self applyCategoryRenaming];
    CGRect r = cell.textLabel.frame;
    r.size.width = cell.contentView.bounds.size.width - r.origin.x;
    UITextField * f = [[UITextField alloc] initWithFrame:r];
    f.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    f.returnKeyType = UIReturnKeyDone;
    f.enablesReturnKeyAutomatically = YES;
    f.clearButtonMode = UITextFieldViewModeWhileEditing;
    f.autocorrectionType = UITextAutocorrectionTypeNo;
    f.adjustsFontSizeToFitWidth = YES;
    f.text = cell.textLabel.text;
    f.textColor = cell.detailTextLabel.textColor;
    f.placeholder = L(@"bookmark_set_name");
    f.font = [cell.textLabel.font fontWithSize:[cell.textLabel.font pointSize]];
    f.tag = TEXTFIELD_TAG;
    f.delegate = self;
    f.autocapitalizationType = UITextAutocapitalizationTypeWords;
    cell.textLabel.hidden = YES;
    cell.detailTextLabel.hidden = YES;
    cell.accessoryType = UITableViewCellAccessoryNone;
    [cell.contentView addSubview:f];
    [f becomeFirstResponder];
  }
  else
  {
    BookmarksVC * bvc = [[BookmarksVC alloc] initWithCategory:indexPath.row];
    [self.navigationController pushViewController:bvc animated:YES];
  }
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
  return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
  if (editingStyle == UITableViewCellEditingStyleDelete)
  {
    [[NSNotificationCenter defaultCenter] postNotificationName:BOOKMARK_CATEGORY_DELETED_NOTIFICATION object:@(indexPath.row)];
    Framework & f = GetFramework();
    f.DeleteBmCategory(indexPath.row);
    [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    // Disable edit mode if no categories are left
    if (!f.GetBmCategoriesCount())
    {
      self.navigationItem.rightBarButtonItem = nil;
      [self setEditing:NO animated:YES];
    }
  }
}

- (void)viewDidLoad
{
  [super viewDidLoad];
  self.tableView.backgroundView = nil;
  self.tableView.backgroundColor = [UIColor applicationBackgroundColor];
}

- (void)viewWillAppear:(BOOL)animated
{
  [super viewWillAppear:animated];
  // Display Edit button only if table is not empty
  if (GetFramework().GetBmCategoriesCount())
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
  else
    self.navigationItem.rightBarButtonItem = nil;

  // Always reload table - we can open it after deleting bookmarks in any category
  [self.tableView reloadData];
}

// Used to remove active UITextField from the cell
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
  if (editing == NO)
    [self applyCategoryRenaming];

  [super setEditing:editing animated:animated];

  // Set or remove selection style for all cells
  NSInteger const rowsCount = [self.tableView numberOfRowsInSection:0];
  for (NSInteger i = 0; i < rowsCount; ++i)
  {
    UITableViewCell * cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
    if (self.editing)
    {
      cell.selectionStyle = UITableViewCellSelectionStyleNone;
      cell.textLabel.textColor = cell.detailTextLabel.textColor;
    }
    else
    {
      cell.selectionStyle = UITableViewCellSelectionStyleBlue;
      cell.textLabel.textColor = [UIColor blackColor];
    }
  }
}

// To hide keyboard and apply changes
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
  if (textField.text.length == 0)
    return YES;

  [textField resignFirstResponder];
  [self applyCategoryRenaming];
  // Exit from edit mode
  [self setEditing:NO animated:YES];
  return NO;
}

-(void)newCategoryAdded
{
  [self.tableView reloadData];
}

-(void)dealloc
{
  [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end