-
Notifications
You must be signed in to change notification settings - Fork 133
/
OSKMicroblogPublishingViewController.m
583 lines (498 loc) · 26.7 KB
/
OSKMicroblogPublishingViewController.m
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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
//
// OSKMicroblogPublishingViewController.m
// Overshare
//
// Created by Jared Sinclair on 10/16/13.
// Copyright (c) 2013 Overshare Kit. All rights reserved.
//
#import "OSKMicroblogPublishingViewController.h"
#import "OSKActivity.h"
#import "OSKActivity_SystemAccounts.h"
#import "OSKActivity_ManagedAccounts.h"
#import "OSKActionSheet.h"
#import "OSKBorderedButton.h"
#import "OSKManagedAccountStore.h"
#import "OSKSystemAccountStore.h"
#import "OSKMicrobloggingActivity.h"
#import "OSKShareableContentItem.h"
#import "OSKPresentationManager.h"
#import "OSKTextView.h"
#import "OSKManagedAccount.h"
#import "OSKAccountChooserViewController.h"
#import "UIImage+OSKUtilities.h"
#import "OSKLinkShorteningUtility.h"
#import "OSKTwitterText.h"
@interface OSKMicroblogPublishingViewController () <OSKTextViewDelegate, OSKAccountChooserViewControllerDelegate>
@property (weak, nonatomic) IBOutlet OSKTextView *textView;
@property (strong, nonatomic) OSKActivity <OSKMicrobloggingActivity> *activity;
@property (strong, nonatomic) OSKMicroblogPostContentItem *contentItem;
@property (strong, nonatomic) UIView *keyboardToolbar;
@property (strong, nonatomic) UILabel *characterCountLabel;
@property (strong, nonatomic) UIColor *characterCount_redColor;
@property (strong, nonatomic) UIColor *characterCount_normalColor;
@property (strong, nonatomic) UIButton *accountButton; // Used on iPhone
@property (assign, nonatomic) BOOL shouldShowLinkShorteningButton;
@property (strong, nonatomic) NSMutableSet *shortenedLinks;
@property (strong, nonatomic) UIBarButtonItem *cancelItem;
@property (strong, nonatomic) UIBarButtonItem *doneItem;
@property (strong, nonatomic) UIBarButtonItem *characterCountItem; // iPad only
@property (strong, nonatomic) UIBarButtonItem *accountItem; // iPad only
@property (strong, nonatomic) UIBarButtonItem *leftSpaceItemA;
@property (strong, nonatomic) UIBarButtonItem *leftSpaceItemB; // iPad only
@property (strong, nonatomic) UIBarButtonItem *rightSpaceItemA;
@property (strong, nonatomic) UIBarButtonItem *rightSpaceItemB;
@property (strong, nonatomic) UIBarButtonItem *rightSpaceItemC;
@property (strong, nonatomic) UIBarButtonItem *linkShorteningItem;
@property (strong, nonatomic) UIButton *linkShorteningButton;
@property (strong, nonatomic) UIActivityIndicatorView *linkShorteningActivityIndicator;
@property (assign, nonatomic) NSInteger activeLinkShorteningCount;
@property (assign, nonatomic) NSUInteger reservedLengthForAttachmentURL;
@end
#define NUM_ROWS 1
#define ROW_TEXT_VIEW 0
#define ROW_ACTIVE_ACCOUNT 1
#define TOOLBAR_FONT_SIZE 15
#define CANCEL_ITEM_INDEX_IPAD 0
#define ACCOUNT_ITEM_INDEX_IPAD 2
#define DONE_ITEM_INDEX_IPAD 0
#define CHARACTER_COUNT_ITEM_INDEX_IPAD 2
@implementation OSKMicroblogPublishingViewController
@synthesize oskPublishingDelegate = _oskPublishingDelegate;
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.edgesForExtendedLayout = UIRectEdgeAll;
self.automaticallyAdjustsScrollViewInsets = NO;
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
OSKPresentationManager *presManager = [OSKPresentationManager sharedInstance];
[self.view setBackgroundColor:presManager.color_opaqueBackground];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
[self setupKeyboardToolbar];
[self setupNavigationItems_Phone];
} else {
[self setupNavigationItems_Pad];
}
[self setupTextView];
[self.textView becomeFirstResponder];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self.textView becomeFirstResponder];
}
- (void)setupTextView {
[self.textView setTextViewDelegate:self];
[self.textView setSyntaxHighlighting:[self.activity syntaxHighlightingStyle]];
[self.textView setText:self.contentItem.text];
[self updateRemainingCharacterCountLabel];
[self updateDoneButton];
[self updateLinkShorteningButton];
NSUInteger numberOfImages = self.contentItem.images.count;
if (numberOfImages > 0) {
NSUInteger numberOfImagesToShow = MIN(MIN([self.activity maximumImageCount], 3), numberOfImages);
NSArray *imagesToShow = [self.contentItem.images subarrayWithRange:NSMakeRange(0, numberOfImagesToShow)];
OSKTextViewAttachment *attachment = [[OSKTextViewAttachment alloc] initWithImages:imagesToShow];
[self.textView setOskAttachment:attachment];
}
}
- (void)setupKeyboardToolbar {
OSKPresentationManager *presManager = [OSKPresentationManager sharedInstance];
// TOOLBAR
self.keyboardToolbar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44.0f)];
self.keyboardToolbar.backgroundColor = presManager.color_toolbarBackground;
self.keyboardToolbar.clipsToBounds = YES;
[self.textView setOSK_inputAccessoryView:self.keyboardToolbar];
// BORDER VIEW
CGRect borderedViewFrame = CGRectInset(self.keyboardToolbar.bounds,-1,0);
borderedViewFrame.origin.y = 0;
UIView *borderedView = [[UIView alloc] initWithFrame:borderedViewFrame];
borderedView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
borderedView.backgroundColor = [UIColor clearColor];
borderedView.layer.borderColor = presManager.color_toolbarBorders.CGColor;
borderedView.layer.borderWidth = ([[UIScreen mainScreen] scale] > 1) ? 0.5f : 1.0f;
[self.keyboardToolbar addSubview:borderedView];
// CHARACTER COUNT LABEL
CGRect countLabelFrame = CGRectMake(0, 0, 64.0f, 44.0f);
countLabelFrame.origin.x = self.keyboardToolbar.bounds.size.width - 74.0f;
UILabel *countLabel = [[UILabel alloc] initWithFrame:countLabelFrame];
countLabel.backgroundColor = [UIColor clearColor];
countLabel.clipsToBounds = NO;
countLabel.textAlignment = NSTextAlignmentRight;
countLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
countLabel.textColor = presManager.color_characterCounter_normal;
[self.keyboardToolbar addSubview:countLabel];
[self setCharacterCountLabel:countLabel];
[self updateRemainingCharacterCountLabel];
// ACCOUNT BUTTON
UIButton *accountButton = nil;
if ([presManager toolbarsUseUnjustifiablyBorderlessButtons] == NO) {
accountButton = [OSKBorderedButton buttonWithType:UIButtonTypeCustom];
accountButton.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleHeight;
accountButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
accountButton.contentEdgeInsets = UIEdgeInsetsMake(0, 12, 0, 13);
} else {
accountButton = [UIButton buttonWithType:UIButtonTypeSystem];
[accountButton setTitleColor:presManager.color_action forState:UIControlStateNormal];
accountButton.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 10);
}
accountButton.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleHeight;
accountButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
accountButton.frame = CGRectMake(-1, -1, 161, 46);
[accountButton addTarget:self action:@selector(accountButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.keyboardToolbar addSubview:accountButton];
[self setAccountButton:accountButton];
UIFontDescriptor *descriptor = [presManager normalFontDescriptor];
if (descriptor) {
countLabel.font = [UIFont fontWithDescriptor:descriptor size:TOOLBAR_FONT_SIZE];
[accountButton.titleLabel setFont:[UIFont fontWithDescriptor:descriptor size:TOOLBAR_FONT_SIZE]];
} else {
countLabel.font = [UIFont systemFontOfSize:TOOLBAR_FONT_SIZE];
[accountButton.titleLabel setFont:[UIFont systemFontOfSize:TOOLBAR_FONT_SIZE]];
}
[self updateAccountButton];
}
- (void)setupNavigationItems_Phone {
NSString *cancelTitle = [[OSKPresentationManager sharedInstance] localizedText_Cancel];
_cancelItem = [[UIBarButtonItem alloc] initWithTitle:cancelTitle style:UIBarButtonItemStylePlain target:self action:@selector(cancelButtonPressed:)];
self.navigationItem.leftBarButtonItems = @[_cancelItem];
NSString *doneTitle = [[OSKPresentationManager sharedInstance] localizedText_ActionButtonTitleForPublishingActivity:[self.activity.class activityType]];
_doneItem = [[UIBarButtonItem alloc] initWithTitle:doneTitle style:UIBarButtonItemStyleDone target:self action:@selector(doneButtonPressed:)];
self.navigationItem.rightBarButtonItems = @[_doneItem];
}
- (void)setupNavigationItems_Pad {
NSString *cancelTitle = [[OSKPresentationManager sharedInstance] localizedText_Cancel];
_cancelItem = [[UIBarButtonItem alloc] initWithTitle:cancelTitle style:UIBarButtonItemStylePlain target:self action:@selector(cancelButtonPressed:)];
NSString *doneTitle = [[OSKPresentationManager sharedInstance] localizedText_ActionButtonTitleForPublishingActivity:[self.activity.class activityType]];
_doneItem = [[UIBarButtonItem alloc] initWithTitle:doneTitle style:UIBarButtonItemStyleDone target:self action:@selector(doneButtonPressed:)];
// CHARACTER COUNT LABEL
CGRect countLabelFrame = CGRectMake(0, 0, 72.0f, 44.0f);
UILabel *countLabel = [[UILabel alloc] initWithFrame:countLabelFrame];
countLabel.backgroundColor = [UIColor clearColor];
countLabel.clipsToBounds = NO;
countLabel.textAlignment = NSTextAlignmentCenter;
countLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
UIFontDescriptor *descriptor = [[OSKPresentationManager sharedInstance] normalFontDescriptor];
if (descriptor) {
countLabel.font = [UIFont fontWithDescriptor:descriptor size:17];
} else {
countLabel.font = [UIFont systemFontOfSize:17];
}
countLabel.textColor = [OSKPresentationManager sharedInstance].color_characterCounter_normal;
[self setCharacterCountLabel:countLabel];
[self updateRemainingCharacterCountLabel];
_characterCountItem = [[UIBarButtonItem alloc] initWithCustomView:countLabel];
_accountItem = [[UIBarButtonItem alloc] initWithTitle:@"account" style:UIBarButtonItemStylePlain target:self action:@selector(accountButtonPressed:)];
_leftSpaceItemA = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
_leftSpaceItemB = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
_rightSpaceItemA = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
_rightSpaceItemB = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[self.navigationItem setLeftBarButtonItems:@[_cancelItem, _leftSpaceItemA, _accountItem, _leftSpaceItemB]];
[self.navigationItem setRightBarButtonItems:@[_doneItem, _rightSpaceItemA, _characterCountItem, _rightSpaceItemB]];
[self updateAccountButton];
}
#pragma mark - OSKTextView Delegate
- (void)textViewDidChange:(OSKTextView *)textView {
[self.contentItem setText:textView.attributedText.string];
[self updateRemainingCharacterCountLabel];
[self updateDoneButton];
[self updateLinkShorteningButton];
}
- (void)textViewDidTapRemoveAttachment:(OSKTextView *)textView {
[textView removeAttachment];
[self.contentItem setImages:nil];
[self updateRemainingCharacterCountLabel];
[self updateDoneButton];
}
#pragma mark - Autorotation
- (void)viewDidLayoutSubviews {
CGRect toolbarBounds = self.keyboardToolbar.bounds;
CGFloat targetHeight;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
targetHeight = UIInterfaceOrientationIsLandscape(self.interfaceOrientation) ? 32.0f : 44.0f;
} else {
targetHeight = 44.0f;
}
toolbarBounds.size.height = targetHeight;
[self.keyboardToolbar setBounds:toolbarBounds];
[self updateAccountButton];
[self adjustTextViewTopInset];
}
- (void)adjustTextViewTopInset {
UIEdgeInsets insets = self.textView.contentInset;
insets.top = [self.topLayoutGuide length];
self.textView.contentInset = insets;
UIEdgeInsets indicatorInsets = self.textView.scrollIndicatorInsets;
indicatorInsets.top = [self.topLayoutGuide length];
[self.textView setScrollIndicatorInsets:indicatorInsets];
}
#pragma mark - Done Button
- (void)updateDoneButton {
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
[self.navigationItem.rightBarButtonItems[DONE_ITEM_INDEX_IPAD] setEnabled:[self.activity isReadyToPerform]];
} else {
[self.navigationItem.rightBarButtonItem setEnabled:[self.activity isReadyToPerform]];
}
}
- (void)doneButtonPressed:(id)sender {
if ([self.activity isReadyToPerform]) {
[self.oskPublishingDelegate publishingViewController:self didTapPublishActivity:self.activity];
}
}
#pragma mark - Link Shortening Button
- (void)updateLinkShorteningButton {
if ([OSKPresentationManager sharedInstance].allowLinkShorteningButton) {
NSArray *links = _textView.detectedLinks;
BOOL shouldShow = NO;
for (OSKTwitterTextEntity *link in links) {
NSString *urlString = [self.contentItem.text substringWithRange:link.range];
if ([OSKLinkShorteningUtility shorteningRecommended:urlString]) {
shouldShow = YES;
break;
}
}
[self setShouldShowLinkShorteningButton:shouldShow];
} else {
[self setShouldShowLinkShorteningButton:NO];
}
}
- (void)setShouldShowLinkShorteningButton:(BOOL)shouldShowLinkShorteningButton {
if (_shouldShowLinkShorteningButton != shouldShowLinkShorteningButton) {
_shouldShowLinkShorteningButton = shouldShowLinkShorteningButton;
[self setupLinkShorteningBarButtonItemSpaces];
if (_shouldShowLinkShorteningButton) {
[self setupLinkShorteningBarButtonItem];
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
[self.navigationItem setRightBarButtonItems:@[_doneItem, _rightSpaceItemA, _characterCountItem,
_rightSpaceItemB, _linkShorteningItem, _rightSpaceItemC]];
} else {
[self.navigationItem setRightBarButtonItems:@[_doneItem, _rightSpaceItemB, _linkShorteningItem, _rightSpaceItemC]];
[self.navigationItem setLeftBarButtonItems:@[_cancelItem, _leftSpaceItemA]];
}
}
else {
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
[self.navigationItem setRightBarButtonItems:@[_doneItem, _rightSpaceItemA, _characterCountItem, _rightSpaceItemB]];
} else {
[self.navigationItem setRightBarButtonItems:@[_doneItem]];
[self.navigationItem setLeftBarButtonItems:@[_cancelItem]];
}
}
}
}
- (void)setupLinkShorteningBarButtonItem {
if (_linkShorteningItem == nil) {
UIImage *linkButtonImage = [[UIImage imageNamed:@"link-button.png"]
imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
_linkShorteningButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_linkShorteningButton setAccessibilityLabel:[OSKPresentationManager sharedInstance].localizedText_ShortenLinks];
[_linkShorteningButton setFrame:CGRectMake(0, 0, 44.0f, 30.0f)];
[_linkShorteningButton setImage:linkButtonImage forState:UIControlStateNormal];
[_linkShorteningButton addTarget:self
action:@selector(shortenLinkButtonTapped:)
forControlEvents:UIControlEventTouchUpInside];
UIView *buttonContainerView = [[UIView alloc] initWithFrame:_linkShorteningButton.bounds];
[buttonContainerView addSubview:_linkShorteningButton];
UIActivityIndicatorViewStyle style = (self.navigationController.navigationBar.barStyle == UIBarStyleBlack)
? UIActivityIndicatorViewStyleWhite
: UIActivityIndicatorViewStyleGray;
_linkShorteningActivityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:style];
_linkShorteningActivityIndicator.hidesWhenStopped = YES;
_linkShorteningActivityIndicator.center = _linkShorteningButton.center;
[buttonContainerView addSubview:_linkShorteningActivityIndicator];
_linkShorteningItem = [[UIBarButtonItem alloc]
initWithCustomView:buttonContainerView];
}
}
- (void)setupLinkShorteningBarButtonItemSpaces {
if (_rightSpaceItemC == nil) {
_rightSpaceItemC = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
}
if (_rightSpaceItemB == nil) {
_rightSpaceItemB = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
}
if (_leftSpaceItemA == nil) {
_leftSpaceItemA = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
}
}
- (void)shortenLinkButtonTapped:(id)sender {
if (_textView.detectedLinks.count) {
for (OSKTwitterTextEntity *URLEntity in _textView.detectedLinks) {
NSString *longURL = [self.contentItem.text substringWithRange:URLEntity.range];
if ([OSKLinkShorteningUtility shorteningRecommended:longURL] && [self.shortenedLinks containsObject:longURL] == NO) {
__weak OSKMicroblogPostContentItem *item = self.contentItem;
__weak OSKMicroblogPublishingViewController *weakSelf = self;
[self pushLinkShorteningActivity];
[OSKLinkShorteningUtility shortenURL:longURL completion:^(NSString *shortURL) {
[weakSelf popLinkShorteningActivity];
if (item == weakSelf.contentItem && shortURL.length) {
NSMutableString *textViewText = weakSelf.contentItem.text.mutableCopy;
NSRange rangeOfLongURL = [textViewText rangeOfString:longURL];
if (rangeOfLongURL.length) {
[textViewText replaceCharactersInRange:rangeOfLongURL withString:shortURL];
if (weakSelf.shortenedLinks == nil) {
weakSelf.shortenedLinks = [NSMutableSet set];
}
[weakSelf.shortenedLinks addObject:shortURL];
[weakSelf.textView setText:textViewText];
[weakSelf.contentItem setText:textViewText];
[weakSelf updateRemainingCharacterCountLabel];
[weakSelf updateDoneButton];
[weakSelf updateLinkShorteningButton];
}
}
}];
}
}
}
}
- (void)pushLinkShorteningActivity {
_activeLinkShorteningCount++;
if (_activeLinkShorteningCount > 0) {
[_linkShorteningActivityIndicator startAnimating];
__weak OSKMicroblogPublishingViewController *weakSelf = self;
[UIView animateWithDuration:0.12f delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
[weakSelf.linkShorteningButton setAlpha:0];
} completion:nil];
}
}
- (void)popLinkShorteningActivity {
if (_activeLinkShorteningCount > 0) {
_activeLinkShorteningCount--;
if (_activeLinkShorteningCount <= 0) {
[_linkShorteningActivityIndicator stopAnimating];
__weak OSKMicroblogPublishingViewController *weakSelf = self;
[UIView animateWithDuration:0.12f delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
[weakSelf.linkShorteningButton setAlpha:1];
UIAccessibilityPostNotification(UIAccessibilityAnnouncementNotification, [OSKPresentationManager sharedInstance].localizedText_LinksShortened);
} completion:nil];
}
}
}
#pragma mark - Account Button Changes
- (void)updateAccountButton {
NSString *accountName = nil;
if ([self.activity respondsToSelector:@selector(activeSystemAccount)]) {
ACAccount *systemAccount = [(OSKActivity <OSKActivity_SystemAccounts> *)self.activity activeSystemAccount];
if (systemAccount.username.length) {
accountName = systemAccount.username;
} else if (systemAccount.userFullName.length) {
accountName = systemAccount.userFullName;
} else {
accountName = self.title;
}
}
else if ([self.activity respondsToSelector:@selector(activeManagedAccount)]) {
OSKManagedAccount *managedAccount = [(OSKActivity <OSKActivity_ManagedAccounts> *)self.activity activeManagedAccount];
accountName = [managedAccount nonNilDisplayName];
}
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
[self updateAccountButton_Phone:accountName];
} else {
[self updateAccountButton_Pad:accountName];
}
}
- (void)updateAccountButton_Phone:(NSString *)accountName {
[self.accountButton setTitle:[NSString stringWithFormat:@"%@", accountName] forState:UIControlStateNormal];
CGSize newSize = [self.accountButton sizeThatFits:self.keyboardToolbar.bounds.size];
CGRect buttonFrame = self.accountButton.frame;
buttonFrame.size.width = newSize.width;
[self.accountButton setFrame:buttonFrame];
}
- (void)updateAccountButton_Pad:(NSString *)accountName {
UIBarButtonItem *item = self.navigationItem.leftBarButtonItems[ACCOUNT_ITEM_INDEX_IPAD];
[item setTitle:accountName];
}
- (void)accountButtonPressed:(id)sender {
if ([self.activity conformsToProtocol:@protocol(OSKActivity_ManagedAccounts)]) {
[self showManagedAccountChooser];
}
else if ([self.activity conformsToProtocol:@protocol(OSKActivity_SystemAccounts)]) {
[self showSystemAccountChooser];
}
}
- (void)showManagedAccountChooser {
OSKActivity <OSKActivity_ManagedAccounts> *activity = (OSKActivity <OSKActivity_ManagedAccounts> *)self.activity;
OSKManagedAccount *activeAccount = activity.activeManagedAccount;
OSKAccountChooserViewController *chooser = [[OSKAccountChooserViewController alloc] initWithManagedAccountActivity:activity activeAccount:activeAccount delegate:self];
[self.navigationController pushViewController:chooser animated:YES];
}
- (void)showSystemAccountChooser {
OSKSystemAccountStore *store = [OSKSystemAccountStore sharedInstance];
OSKActivity <OSKActivity_SystemAccounts> *activity = (OSKActivity <OSKActivity_SystemAccounts> *)self.activity;
NSArray *accounts = [store accountsForAccountTypeIdentifier:[activity.class systemAccountTypeIdentifier]];
ACAccount *activeAccount = activity.activeSystemAccount;
OSKAccountChooserViewController *chooser = [[OSKAccountChooserViewController alloc] initWithSystemAccounts:accounts activeAccount:activeAccount delegate:self];
[self.navigationController pushViewController:chooser animated:YES];
}
#pragma mark - Character Count
- (void)updateRemainingCharacterCountLabel {
NSInteger countAdjustingForEmoji = [self.textView.attributedText.string lengthOfBytesUsingEncoding:NSUTF32StringEncoding]/4;
NSInteger remaining = [self.activity maximumCharacterCount] - countAdjustingForEmoji;
if (self.contentItem.images.count) {
NSUInteger numberToBeAttached = MIN([self.activity maximumImageCount], self.contentItem.images.count);
remaining -= numberToBeAttached * _reservedLengthForAttachmentURL;
}
self.characterCountLabel.text = @(remaining).stringValue;
if (_characterCount_normalColor == nil) {
OSKPresentationManager *presManager = [OSKPresentationManager sharedInstance];
_characterCount_normalColor = presManager.color_characterCounter_normal;
_characterCount_redColor = presManager.color_characterCounter_warning;
}
if (remaining < 0) {
self.characterCountLabel.textColor = _characterCount_redColor;
} else {
self.characterCountLabel.textColor = _characterCount_normalColor;
}
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
CGSize size = [self.characterCountLabel sizeThatFits:CGSizeMake(160.0f, 44.0)];
CGRect characterCountRect = self.characterCountLabel.frame;
characterCountRect.size.width = size.width;
self.characterCountLabel.frame = characterCountRect;
}
}
#pragma mark - Button Actions
- (void)cancelButtonPressed:(id)sender {
[self.oskPublishingDelegate publishingViewControllerDidCancel:self withActivity:self.activity];
}
#pragma mark - Publishing View Controller
- (void)preparePublishingViewForActivity:(OSKActivity *)activity delegate:(id <OSKPublishingViewControllerDelegate>)oskPublishingDelegate {
[self setActivity:(OSKActivity <OSKMicrobloggingActivity> *)activity];
[self setContentItem:(OSKMicroblogPostContentItem *)self.activity.contentItem];
[self setOskPublishingDelegate:oskPublishingDelegate];
self.title = [self.activity.class activityName];
if ([self.activity respondsToSelector:@selector(characterCountsAreAffectedByAttachments)]) {
BOOL adjustForAttachments = [self.activity characterCountsAreAffectedByAttachments];
if (adjustForAttachments) {
__weak OSKMicroblogPublishingViewController *weakSelf = self;
[self.activity getEstimatedAttachmentURLLength:^(NSUInteger length) {
[weakSelf setReservedLengthForAttachmentURL:length];
[weakSelf updateRemainingCharacterCountLabel];
[weakSelf updateDoneButton];
}];
}
}
}
#pragma mark - Account Chooser Delegate
- (void)accountChooserDidSelectManagedAccount:(OSKManagedAccount *)managedAccount {
OSKActivity <OSKActivity_ManagedAccounts> *activity = (OSKActivity <OSKActivity_ManagedAccounts> *)self.activity;
[activity setActiveManagedAccount:managedAccount];
[self updateAccountButton];
}
- (void)accountChooserDidSelectSystemAccount:(ACAccount *)systemAccount {
OSKActivity <OSKActivity_SystemAccounts> *activity = (OSKActivity <OSKActivity_SystemAccounts> *)self.activity;
[activity setActiveSystemAccount:systemAccount];
[self updateAccountButton];
}
@end