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

MWMBookmarkCell.mm « Classes « Maps « iphone - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4698529b7da4a4ef413dca28d2232ac837609c68 (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
#import "MWMPlacePageCellUpdateProtocol.h"
#import "MWMBookmarkCell.h"

#import "MWMPlacePageProtocol.h"
#import "UIColor+MapsMeColor.h"
#import "UIFont+MapsMeFonts.h"

namespace
{
void * kContext = &kContext;
NSString * const kTextViewContentSizeKeyPath = @"contentSize";

}  // namespace

@interface MWMBookmarkCell ()

@property(weak, nonatomic) IBOutlet UITextView * textView;
@property(weak, nonatomic) IBOutlet UIImageView * spinner;
@property(weak, nonatomic) IBOutlet UIButton * editButton;

@property(weak, nonatomic) IBOutlet UIImageView * gradientView;

@property(nonatomic) IBOutlet NSLayoutConstraint * textViewHeight;
@property(weak, nonatomic) IBOutlet NSLayoutConstraint * moreButtonHeight;
@property(nonatomic) IBOutlet NSLayoutConstraint * textViewZeroHeight;

@property(weak, nonatomic) id<MWMPlacePageCellUpdateProtocol> updateCellDelegate;
@property(weak, nonatomic) id<MWMPlacePageButtonsProtocol> editBookmarkDelegate;

@property(copy, nonatomic) NSAttributedString * attributedHTML;

@property (nonatomic) BOOL isOpen;

@end

@implementation MWMBookmarkCell

- (void)awakeFromNib
{
  [super awakeFromNib];
  [self registerObserver];
}

- (void)dealloc
{
  [self unregisterObserver];
}

- (void)unregisterObserver
{
  [self.textView removeObserver:self forKeyPath:kTextViewContentSizeKeyPath context:kContext];
}

- (void)registerObserver
{
  [self.textView addObserver:self forKeyPath:kTextViewContentSizeKeyPath options:NSKeyValueObservingOptionNew context:kContext];
}

- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary *)change
                       context:(void *)context
{
  if (context == kContext)
  {
    NSValue * s = change[@"new"];
    CGFloat const height = s.CGSizeValue.height;
    auto const boundedHeight = self.textViewHeight.constant;

    if (height < boundedHeight || self.isOpen)
      [self stateOpen:YES];
    else if (height > boundedHeight && !self.isOpen)
      [self stateOpen:NO];

    [self setNeedsLayout];
    [self.updateCellDelegate updateCellWithForceReposition:NO];
    return;
  }

  [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}

- (void)configureWithText:(NSString *)text
       updateCellDelegate:(id<MWMPlacePageCellUpdateProtocol>)updateCellDelegate
     editBookmarkDelegate:(id<MWMPlacePageButtonsProtocol>)editBookmarkDelegate
                  isHTML:(BOOL)isHTML
{
  self.attributedHTML = nil;
  self.isOpen = NO;
  self.textViewHeight.active = NO;
  self.textViewZeroHeight.active = NO;
  self.updateCellDelegate = updateCellDelegate;
  self.editBookmarkDelegate = editBookmarkDelegate;

  if (!text.length)
  {
    [self configWithEmptyDescription];
  }
  else if (isHTML)
  {
    [self configHTML:text];
  }
  else
  {
    [self configPlain:text];
  }
}

- (void)configWithEmptyDescription
{
  [self stopSpinner];

  [self stateOpen:YES];
  self.textViewZeroHeight.active = YES;
}

- (void)configHTML:(NSString *)text
{
  if (self.attributedHTML)
  {
    [self stopSpinner];
    self.textViewZeroHeight.active = NO;
    self.textView.attributedText = self.attributedHTML;
    if (fabs(self.textView.contentSize.height - 0.5) < 1)
      [self.textView sizeToFit];
  }
  else
  {
    self.textViewZeroHeight.active = YES;
    [self startSpinner];
    [self stateOpen:YES];

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
      NSDictionary<NSString *, id> * attr = @{
                                              NSForegroundColorAttributeName : [UIColor blackPrimaryText],
                                              NSFontAttributeName : [UIFont regular16]
                                              };
      NSError * error = nil;
      NSMutableAttributedString * str = [[NSMutableAttributedString alloc]
                                         initWithData:[text dataUsingEncoding:NSUnicodeStringEncoding]
                                         options:@{
                                                   NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType
                                                   }
                                         documentAttributes:nil
                                         error:&error];
      if (error)
      {
        // If we failed while attempting to render html than just show plain text in bookmark.
        // Ussualy there is a problem only in iOS7.
        self.attributedHTML = [[NSAttributedString alloc] initWithString:text attributes:attr];
      }
      else
      {
        [str addAttributes:attr range:{0, str.length}];
        self.attributedHTML = str;
      }

      dispatch_async(dispatch_get_main_queue(), ^{

        [self configHTML:nil];
      });
    });
  }
}

- (void)configPlain:(NSString *)text
{
  [self stopSpinner];
  self.textView.text = text;
}

- (void)stateOpen:(BOOL)isOpen
{
  self.moreButtonHeight.constant = isOpen ? 0 : 33;
  self.textViewHeight.active = !isOpen;
  self.gradientView.hidden = isOpen;
}

- (void)startSpinner
{
//  self.textViewZeroHeight.active = YES;
  self.editButton.hidden = YES;
  NSUInteger const animationImagesCount = 12;
  NSMutableArray * animationImages = [NSMutableArray arrayWithCapacity:animationImagesCount];
  NSString * postfix = [UIColor isNightMode] ? @"dark" : @"light";
  for (NSUInteger i = 0; i < animationImagesCount; ++i)
    animationImages[i] =
    [UIImage imageNamed:[NSString stringWithFormat:@"Spinner_%@_%@", @(i + 1), postfix]];

  self.spinner.animationDuration = 0.8;
  self.spinner.animationImages = animationImages;
  self.spinner.hidden = NO;
  [self.spinner startAnimating];
}

- (void)stopSpinner
{
  [self.spinner stopAnimating];
  self.editButton.hidden = NO;
  self.spinner.hidden = YES;
}

- (IBAction)moreTap
{
  self.isOpen = YES;
  [self stateOpen:YES];
  [self setNeedsLayout];
//  [self.delegate updateCellWithForceReposition:NO];
}

- (IBAction)editTap
{
  [self.editBookmarkDelegate editBookmark];
}

@end