This repository was archived by the owner on Dec 6, 2024. It is now read-only.
forked from lukkas/SlackTextViewController
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSLKTypingIndicatorView.m
More file actions
346 lines (260 loc) · 8.94 KB
/
SLKTypingIndicatorView.m
File metadata and controls
346 lines (260 loc) · 8.94 KB
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
//
// SlackTextViewController
// https://github.com/slackhq/SlackTextViewController
//
// Copyright 2014-2016 Slack Technologies, Inc.
// Licence: MIT-Licence
//
#import "SLKTypingIndicatorView.h"
#import "UIView+SLKAdditions.h"
#import "SLKUIConstants.h"
#define SLKTypingIndicatorViewIdentifier [NSString stringWithFormat:@"%@.%@", SLKTextViewControllerDomain, NSStringFromClass([self class])]
@interface SLKTypingIndicatorView ()
// The text label used to display the typing indicator content.
@property (nonatomic, strong) UILabel *textLabel;
@property (nonatomic, strong) NSMutableArray *usernames;
@property (nonatomic, strong) NSMutableArray *timers;
// Auto-Layout margin constraints used for updating their constants
@property (nonatomic, strong) NSLayoutConstraint *leftContraint;
@property (nonatomic, strong) NSLayoutConstraint *rightContraint;
@end
@implementation SLKTypingIndicatorView
@synthesize visible = _visible;
#pragma mark - Initializer
- (id)init
{
if (self = [super init]) {
[self slk_commonInit];
}
return self;
}
- (instancetype)initWithCoder:(NSCoder *)coder
{
if (self = [super initWithCoder:coder]) {
[self slk_commonInit];
}
return self;
}
- (void)slk_commonInit
{
self.backgroundColor = [UIColor whiteColor];
self.interval = 6.0;
self.canResignByTouch = NO;
self.usernames = [NSMutableArray new];
self.timers = [NSMutableArray new];
self.textColor = [UIColor grayColor];
self.highlightTextColor = [UIColor grayColor];
self.textFont = [UIFont systemFontOfSize:12.0];
self.highlightFont = [UIFont boldSystemFontOfSize:12.0];
self.contentInset = UIEdgeInsetsMake(10.0, 40.0, 10.0, 10.0);
[self addSubview:self.textLabel];
[self slk_setupConstraints];
}
#pragma mark - SLKTypingIndicatorProtocol
- (void)setVisible:(BOOL)visible
{
// Skip when updating the same value, specially to avoid inovking KVO unnecessary
if (self.isVisible == visible) {
return;
}
// Required implementation for key-value observer compliance
[self willChangeValueForKey:NSStringFromSelector(@selector(isVisible))];
_visible = visible;
if (!visible) {
[self slk_invalidateTimers];
}
// Required implementation for key-value observer compliance
[self didChangeValueForKey:NSStringFromSelector(@selector(isVisible))];
}
- (void)dismissIndicator
{
if (self.isVisible) {
self.visible = NO;
}
}
#pragma mark - Getters
- (UILabel *)textLabel
{
if (!_textLabel) {
_textLabel = [UILabel new];
_textLabel.translatesAutoresizingMaskIntoConstraints = NO;
_textLabel.backgroundColor = [UIColor clearColor];
_textLabel.contentMode = UIViewContentModeTopLeft;
_textLabel.userInteractionEnabled = NO;
}
return _textLabel;
}
- (NSAttributedString *)attributedString
{
if (self.usernames.count == 0) {
return nil;
}
NSString *text = @"";
NSString *firstObject = [self.usernames firstObject];
NSString *lastObject = [self.usernames lastObject];
if (self.usernames.count == 1) {
text = [NSString stringWithFormat:NSLocalizedString(@"%@ is typing", nil), firstObject];
}
else if (self.usernames.count == 2) {
text = [NSString stringWithFormat:NSLocalizedString(@"%@ & %@ are typing", nil), firstObject, lastObject];
}
else if (self.usernames.count > 2) {
text = NSLocalizedString(@"Several people are typing", nil);
}
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
style.alignment = NSTextAlignmentLeft;
style.lineBreakMode = NSLineBreakByTruncatingTail;
style.minimumLineHeight = 10.0;
NSDictionary *attributes = @{NSFontAttributeName: self.textFont,
NSForegroundColorAttributeName: self.textColor,
NSParagraphStyleAttributeName: style,
};
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text attributes:attributes];
if (self.usernames.count <= 2) {
[attributedString addAttribute:NSFontAttributeName value:self.highlightFont range:[text rangeOfString:firstObject]];
[attributedString addAttribute:NSFontAttributeName value:self.highlightFont range:[text rangeOfString:lastObject]];
[attributedString addAttribute:NSForegroundColorAttributeName value:self.highlightTextColor range:[text rangeOfString:firstObject]];
[attributedString addAttribute:NSForegroundColorAttributeName value:self.highlightTextColor range:[text rangeOfString:lastObject]];
}
return attributedString;
}
- (CGSize)intrinsicContentSize
{
return CGSizeMake(UIViewNoIntrinsicMetric, [self height]);
}
- (CGFloat)height
{
CGFloat height = self.textFont.lineHeight;
height += self.contentInset.top;
height += self.contentInset.bottom;
return height;
}
#pragma mark - Setters
- (void)setContentInset:(UIEdgeInsets)insets
{
if (UIEdgeInsetsEqualToEdgeInsets(self.contentInset, insets)) {
return;
}
if (UIEdgeInsetsEqualToEdgeInsets(self.contentInset, UIEdgeInsetsZero)) {
_contentInset = insets;
return;
}
_contentInset = insets;
[self slk_updateConstraintConstants];
}
- (void)setHidden:(BOOL)hidden
{
if (self.isHidden == hidden) {
return;
}
if (hidden) {
[self slk_prepareForReuse];
}
[super setHidden:hidden];
}
#pragma mark - Public Methods
- (void)insertUsername:(NSString *)username;
{
if (!username) {
return;
}
BOOL isShowing = [self.usernames containsObject:username];
if (_interval > 0.0) {
if (isShowing) {
NSTimer *timer = [self slk_timerWithIdentifier:username];
[self slk_invalidateTimer:timer];
}
NSTimer *timer = [NSTimer timerWithTimeInterval:_interval target:self selector:@selector(slk_shouldRemoveUsername:) userInfo:@{SLKTypingIndicatorViewIdentifier: username} repeats:NO];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
[self.timers addObject:timer];
}
if (isShowing) {
return;
}
[self.usernames addObject:username];
NSAttributedString *attributedString = [self attributedString];
self.textLabel.attributedText = attributedString;
self.visible = YES;
}
- (void)removeUsername:(NSString *)username
{
if (!username || ![self.usernames containsObject:username]) {
return;
}
[self.usernames removeObject:username];
if (self.usernames.count > 0) {
self.textLabel.attributedText = [self attributedString];
}
else {
self.visible = NO;
}
}
#pragma mark - Private Methods
- (void)slk_shouldRemoveUsername:(NSTimer *)timer
{
NSString *identifier = [timer.userInfo objectForKey:SLKTypingIndicatorViewIdentifier];
[self removeUsername:identifier];
[self slk_invalidateTimer:timer];
}
- (NSTimer *)slk_timerWithIdentifier:(NSString *)identifier
{
for (NSTimer *timer in self.timers) {
if ([identifier isEqualToString:[timer.userInfo objectForKey:SLKTypingIndicatorViewIdentifier]]) {
return timer;
}
}
return nil;
}
- (void)slk_invalidateTimer:(NSTimer *)timer
{
if (timer) {
[timer invalidate];
[self.timers removeObject:timer];
timer = nil;
}
}
- (void)slk_invalidateTimers
{
for (NSTimer *timer in self.timers) {
[timer invalidate];
}
[self.timers removeAllObjects];
}
- (void)slk_prepareForReuse
{
[self slk_invalidateTimers];
self.textLabel.text = nil;
[self.usernames removeAllObjects];
}
- (void)slk_setupConstraints
{
NSDictionary *views = @{@"textLabel": self.textLabel};
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[textLabel]|" options:0 metrics:nil views:views]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(0)-[textLabel]-(0@750)-|" options:0 metrics:nil views:views]];
self.leftContraint = [[self slk_constraintsForAttribute:NSLayoutAttributeLeading] firstObject];
self.rightContraint = [[self slk_constraintsForAttribute:NSLayoutAttributeTrailing] firstObject];
[self slk_updateConstraintConstants];
}
- (void)slk_updateConstraintConstants
{
self.leftContraint.constant = self.contentInset.left;
self.rightContraint.constant = self.contentInset.right;
}
#pragma mark - Hit Testing
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesEnded:touches withEvent:event];
if (self.canResignByTouch) {
[self dismissIndicator];
}
}
#pragma mark - Lifeterm
- (void)dealloc
{
[self slk_invalidateTimers];
}
@end