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

MWMPlacePageBookmarkCell.mm « Classes « Maps « iphone - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6e7024c82f5ac2bfbe0151c184923082de0281b4 (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
#import "MWMBasePlacePageView.h"
#import "MWMPlacePage.h"
#import "MWMPlacePageBookmarkCell.h"
#import "MWMPlacePageEntity.h"
#import "MWMPlacePageViewManager.h"
#import "MWMTextView.h"

extern CGFloat const kBookmarkCellHeight = 136.0;

static CGFloat const kSeparatorAndTitleHeight = 52.0;

static NSUInteger sWebViewHeight = 0;

@interface MWMPlacePageBookmarkCell () <UITextFieldDelegate, UIWebViewDelegate>

@property (weak, nonatomic) IBOutlet UITextField * title;
@property (weak, nonatomic) IBOutlet UIButton * categoryButton;
@property (weak, nonatomic) IBOutlet UIButton * markButton;

@property (weak, nonatomic) IBOutlet UIView * note;
@property (weak, nonatomic) IBOutlet UILabel * noteLabel;
@property (weak, nonatomic) IBOutlet UIWebView * noteWebView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint * noteViewHeight;

@property (weak, nonatomic) MWMPlacePage * placePage;

@property (nonatomic) BOOL forHeight;

@property (nonatomic) NSString * webViewContent;

@end

@implementation MWMPlacePageBookmarkCell

- (void)config:(MWMPlacePage *)placePage forHeight:(BOOL)forHeight
{
  self.placePage = placePage;
  self.forHeight = forHeight;

  [self configNote];

  if (forHeight && self.entity.isHTMLDescription)
    return;

  self.title.text = self.entity.bookmarkTitle;
  [self.categoryButton setTitle:[NSString stringWithFormat:@"%@ >", self.entity.bookmarkCategory]
                       forState:UIControlStateNormal];
  [self.markButton
   setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@-on", self.entity.bookmarkColor]]
   forState:UIControlStateNormal];
  [[NSNotificationCenter defaultCenter] addObserver:self
                                           selector:@selector(keyboardWillShown:)
                                               name:UIKeyboardWillShowNotification
                                             object:nil];

  [[NSNotificationCenter defaultCenter] addObserver:self
                                           selector:@selector(keyboardWillBeHidden)
                                               name:UIKeyboardWillHideNotification
                                             object:nil];
}

- (void)configNote
{
  if (self.entity.bookmarkDescription.length == 0)
  {
    self.note.hidden = YES;
    self.noteViewHeight.constant = 0.0;
  }
  else
  {
    self.note.hidden = NO;
    if (self.entity.isHTMLDescription)
    {
      self.noteWebView.hidden = NO;
      self.noteLabel.hidden = YES;
      if (!self.forHeight && ![self.webViewContent isEqualToString:self.entity.bookmarkDescription])
      {
        sWebViewHeight = 0.0;
        self.webViewContent = self.entity.bookmarkDescription;
        [self.noteWebView loadHTMLString:self.entity.bookmarkDescription baseURL:nil];
        self.noteWebView.scrollView.scrollEnabled = NO;
      }
    }
    else
    {
      self.noteWebView.hidden = YES;
      self.noteLabel.hidden = NO;
      self.noteLabel.text = self.entity.bookmarkDescription;
      [self.noteLabel sizeToFit];
      self.noteViewHeight.constant = kSeparatorAndTitleHeight + self.noteLabel.height;
    }
  }
}

- (void)keyboardWillShown:(NSNotification *)aNotification
{
  NSDictionary const * info = [aNotification userInfo];
  CGSize const kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
  if ([self.title isEditing])
    [self.placePage willStartEditingBookmarkTitle:kbSize.height];
}

- (void)keyboardWillBeHidden
{
  if ([self.title isEditing])
    [self.placePage willFinishEditingBookmarkTitle:self.title.text.length > 0 ? self.title.text : self.entity.title];
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
  self.entity.bookmarkTitle = textField.text.length > 0 ? textField.text : self.entity.title;
  [self.entity synchronize];
  [textField resignFirstResponder];
}

- (BOOL)textFieldShouldClear:(UITextField *)textField
{
  return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
  [textField resignFirstResponder];
  return YES;
}

- (void)dealloc
{
  if (!self.forHeight)
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (IBAction)colorPickerButtonTap
{
  [self.placePage changeBookmarkColor];
  [self.title resignFirstResponder];
}

- (IBAction)categoryButtonTap
{
  [self.placePage changeBookmarkCategory];
  [self.title resignFirstResponder];
}

- (IBAction)editTap
{
  [self.placePage changeBookmarkDescription];
  [self.title resignFirstResponder];
}

- (MWMPlacePageEntity *)entity
{
  return self.placePage.manager.entity;
}

- (CGFloat)cellHeight
{
  CGFloat const noteViewHeight = self.entity.isHTMLDescription ? sWebViewHeight : self.noteViewHeight.constant;
  return kBookmarkCellHeight + ceil(self.note.hidden ? 0.0 : noteViewHeight);
}

#pragma mark - UIWebViewDelegate

- (void)webViewDidFinishLoad:(UIWebView * _Nonnull)webView
{
  webView.height = webView.scrollView.contentSize.height;
  NSUInteger webViewHeight = ceil(kSeparatorAndTitleHeight + webView.height);
  self.noteViewHeight.constant = webViewHeight;
  if (sWebViewHeight != webViewHeight)
  {
    sWebViewHeight = webViewHeight;
    [self.placePage reloadBookmark];
  }
}

- (BOOL)webView:(UIWebView *)inWeb
    shouldStartLoadWithRequest:(NSURLRequest *)inRequest
                navigationType:(UIWebViewNavigationType)inType
{
  if (inType == UIWebViewNavigationTypeLinkClicked)
  {
    [[UIApplication sharedApplication] openURL:[inRequest URL]];
    return NO;
  }
  return YES;
}

@end