-
Notifications
You must be signed in to change notification settings - Fork 133
/
OSKTwitterActivity.m
188 lines (147 loc) · 5.27 KB
/
OSKTwitterActivity.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
//
// OSKTwitterActivity.m
// Overshare
//
//
// Copyright (c) 2013 Overshare Kit. All rights reserved.
//
@import Accounts;
#import "OSKTwitterActivity.h"
#import "OSKTwitterUtility.h"
#import "OSKMicrobloggingActivity.h"
#import "OSKShareableContentItem.h"
#import "OSKSystemAccountStore.h"
#import "OSKActivity_SystemAccounts.h"
static NSInteger OSKTwitterActivity_MaxCharacterCount = 140;
static NSInteger OSKTwitterActivity_MaxUsernameLength = 20;
static NSInteger OSKTwitterActivity_MaxImageCount = 1;
@interface OSKTwitterActivity ()
@property (copy, nonatomic) NSNumber *estimatedLengthOfAttachmentURL;
@end
@implementation OSKTwitterActivity
@synthesize activeSystemAccount = _activeSystemAccount;
- (instancetype)initWithContentItem:(OSKShareableContentItem *)item {
self = [super initWithContentItem:item];
if (self) {
//
}
return self;
}
#pragma mark - System Accounts
+ (NSString *)systemAccountTypeIdentifier {
return ACAccountTypeIdentifierTwitter;
}
#pragma mark - Methods for OSKActivity Subclasses
+ (NSString *)supportedContentItemType {
return OSKShareableContentItemType_MicroblogPost;
}
+ (BOOL)isAvailable {
return YES; // This is *in general*, not whether account access has been granted.
}
+ (NSString *)activityType {
return OSKActivityType_iOS_Twitter;
}
+ (NSString *)activityName {
return @"Twitter";
}
+ (UIImage *)iconForIdiom:(UIUserInterfaceIdiom)idiom {
UIImage *image = nil;
if (idiom == UIUserInterfaceIdiomPhone) {
image = [UIImage imageNamed:@"osk-twitterIcon-60.png"];
} else {
image = [UIImage imageNamed:@"osk-twitterIcon-76.png"];
}
return image;
}
+ (UIImage *)settingsIcon {
return [self iconForIdiom:UIUserInterfaceIdiomPhone];
}
+ (OSKAuthenticationMethod)authenticationMethod {
return OSKAuthenticationMethod_SystemAccounts;
}
+ (BOOL)requiresApplicationCredential {
return NO;
}
+ (OSKPublishingViewControllerType)publishingViewControllerType {
return OSKPublishingViewControllerType_Microblogging;
}
- (BOOL)isReadyToPerform {
BOOL accountPresent = (self.activeSystemAccount != nil);
NSInteger totalAvailableCharacters = [self maximumCharacterCount];
OSKMicroblogPostContentItem *contentItem = (OSKMicroblogPostContentItem *)self.contentItem;
if (contentItem.images.count) {
NSUInteger attachmentLength = (_estimatedLengthOfAttachmentURL.integerValue) ? _estimatedLengthOfAttachmentURL.integerValue : 24;
totalAvailableCharacters -= attachmentLength; // We only ever send the first image in the array, due to API limits.
}
BOOL textIsValid = (contentItem.text.length > 0 && contentItem.text.length <= totalAvailableCharacters);
return (accountPresent && textIsValid);
}
- (void)performActivity:(OSKActivityCompletionHandler)completion {
__weak OSKTwitterActivity *weakSelf = self;
UIBackgroundTaskIdentifier backgroundTaskIdentifier = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
if (completion) {
completion(weakSelf, NO, nil);
}
}];
[OSKTwitterUtility
postContentItem:(OSKMicroblogPostContentItem *)self.contentItem
toSystemAccount:self.activeSystemAccount
completion:^(BOOL success, NSError *error) {
if (completion) {
completion(weakSelf, (error == nil), error);
}
[[UIApplication sharedApplication] endBackgroundTask:backgroundTaskIdentifier];
}];
}
+ (BOOL)canPerformViaOperation {
return NO;
}
- (OSKActivityOperation *)operationForActivityWithCompletion:(OSKActivityCompletionHandler)completion {
return nil;
}
#pragma mark - Microblogging Activity Protocol
- (NSInteger)maximumCharacterCount {
return OSKTwitterActivity_MaxCharacterCount;
}
- (NSInteger)maximumImageCount {
return OSKTwitterActivity_MaxImageCount;
}
- (OSKMicroblogSyntaxHighlightingStyle)syntaxHighlightingStyle {
return OSKMicroblogSyntaxHighlightingStyle_Twitter;
}
- (NSInteger)maximumUsernameLength {
return OSKTwitterActivity_MaxUsernameLength;
}
- (BOOL)characterCountsAreAffectedByAttachments {
return YES;
}
- (void)getEstimatedAttachmentURLLength:(void(^)(NSUInteger length))completion {
if (_estimatedLengthOfAttachmentURL) {
NSUInteger roughEstimate = [_estimatedLengthOfAttachmentURL integerValue];
dispatch_async(dispatch_get_main_queue(), ^{
if (completion) {
completion(roughEstimate);
}
});
}
else if (self.activeSystemAccount) {
__weak OSKTwitterActivity *weakSelf = self;
[OSKTwitterUtility requestTwitterConfiguration:self.activeSystemAccount completion:^(NSError *error, NSDictionary *configurationParameters) {
NSNumber *estimateNumber = configurationParameters[OSKTwitterImageHttpsURLLengthKey];
CGFloat roughEstimate = (estimateNumber.integerValue) ? estimateNumber.integerValue : 24;
[weakSelf setEstimatedLengthOfAttachmentURL:estimateNumber];
if (completion) {
completion(roughEstimate);
}
}];
}
else {
NSUInteger roughEstimate = 24;
dispatch_async(dispatch_get_main_queue(), ^{
if (completion) {
completion(roughEstimate);
}
});
}
}
@end