NewsBlur/clients/ios/Other Sources/Overshare Kit/OSKFacebookActivity.m

194 lines
6.3 KiB
Mathematica
Raw Normal View History

2014-01-06 17:55:14 -08:00
//
// OSKFacebookActivity.m
// Overshare
//
// Created by Jared Sinclair on 10/15/13.
// Copyright (c) 2013 Overshare Kit. All rights reserved.
//
@import Accounts;
#import "OSKFacebookActivity.h"
#import "OSKShareableContentItem.h"
#import "OSKFacebookUtility.h"
#import "OSKApplicationCredential.h"
2014-02-27 16:54:25 -08:00
#import "NSString+OSKEmoji.h"
2014-01-06 17:55:14 -08:00
static NSInteger OSKFacebookActivity_MaxCharacterCount = 6000;
static NSInteger OSKFacebookActivity_MaxUsernameLength = 20;
static NSInteger OSKFacebookActivity_MaxImageCount = 3;
static NSString * OSKFacebookActivity_PreviousAudienceKey = @"OSKFacebookActivity_PreviousAudienceKey";
2014-01-06 17:55:14 -08:00
@implementation OSKFacebookActivity
@synthesize activeSystemAccount = _activeSystemAccount;
2014-02-27 16:54:25 -08:00
@synthesize remainingCharacterCount = _remainingCharacterCount;
2014-01-06 17:55:14 -08:00
- (instancetype)initWithContentItem:(OSKShareableContentItem *)item {
self = [super initWithContentItem:item];
if (self) {
NSString *previousAudience = [[NSUserDefaults standardUserDefaults] objectForKey:OSKFacebookActivity_PreviousAudienceKey];
if (previousAudience) {
if( ([previousAudience isEqualToString:ACFacebookAudienceEveryone]) ||
([previousAudience isEqualToString:ACFacebookAudienceFriends]) ||
([previousAudience isEqualToString:ACFacebookAudienceOnlyMe]) ) {
_currentAudience = previousAudience;
} else {
_currentAudience = ACFacebookAudienceEveryone;
}
} else {
_currentAudience = ACFacebookAudienceEveryone;
}
2014-01-06 17:55:14 -08:00
}
return self;
}
- (void)setCurrentAudience:(NSString *)currentAudience {
if ([_currentAudience isEqualToString:currentAudience] == NO) {
_currentAudience = currentAudience;
if (_currentAudience) {
[[NSUserDefaults standardUserDefaults] setObject:_currentAudience forKey:OSKFacebookActivity_PreviousAudienceKey];
}
}
}
2014-01-06 17:55:14 -08:00
#pragma mark - System Accounts
+ (NSString *)systemAccountTypeIdentifier {
return ACAccountTypeIdentifierFacebook;
}
+ (NSDictionary *)readAccessRequestOptions {
OSKApplicationCredential *appCredential = [self applicationCredential];
return @{ACFacebookPermissionsKey:@[@"email"],
ACFacebookAudienceKey:ACFacebookAudienceEveryone,
ACFacebookAppIdKey:appCredential.applicationKey};
}
+ (NSDictionary *)writeAccessRequestOptions {
OSKApplicationCredential *appCredential = [self applicationCredential];
return @{ACFacebookPermissionsKey:@[@"publish_actions"],
ACFacebookAudienceKey:ACFacebookAudienceEveryone,
ACFacebookAppIdKey:appCredential.applicationKey};
}
#pragma mark - Methods for OSKActivity Subclasses
+ (NSString *)supportedContentItemType {
return OSKShareableContentItemType_Facebook;
2014-01-06 17:55:14 -08:00
}
+ (BOOL)isAvailable {
return YES; // This is *in general*, not whether account access has been granted.
}
+ (NSString *)activityType {
return OSKActivityType_iOS_Facebook;
}
+ (NSString *)activityName {
return @"Facebook";
}
+ (UIImage *)iconForIdiom:(UIUserInterfaceIdiom)idiom {
UIImage *image = nil;
if (idiom == UIUserInterfaceIdiomPhone) {
image = [UIImage imageNamed:@"osk-facebookIcon-60.png"];
} else {
image = [UIImage imageNamed:@"osk-facebookIcon-76.png"];
}
return image;
}
+ (UIImage *)settingsIcon {
return [self iconForIdiom:UIUserInterfaceIdiomPhone];
}
+ (OSKAuthenticationMethod)authenticationMethod {
return OSKAuthenticationMethod_SystemAccounts;
}
+ (BOOL)requiresApplicationCredential {
return YES;
}
2014-02-27 16:54:25 -08:00
+ (OSKPublishingMethod)publishingMethod {
return OSKPublishingMethod_ViewController_Facebook;
2014-01-06 17:55:14 -08:00
}
- (BOOL)isReadyToPerform {
BOOL accountPresent = (self.activeSystemAccount != nil);
OSKFacebookContentItem *contentItem = (OSKFacebookContentItem *)[self contentItem];
2014-02-27 16:54:25 -08:00
2014-01-06 17:55:14 -08:00
NSInteger maxCharacterCount = [self maximumCharacterCount];
BOOL textIsLongerThanZero = (contentItem.text.length > 0);
BOOL textIsWithinTheMaxCount = (0 <= self.remainingCharacterCount && self.remainingCharacterCount < maxCharacterCount);
BOOL textIsValid = (textIsLongerThanZero && textIsWithinTheMaxCount);
BOOL isALinkPost = (contentItem.link != nil);
BOOL hasImageAttachment = [contentItem.images count] > 0;
2014-02-27 16:54:25 -08:00
return (accountPresent && (textIsValid || isALinkPost || hasImageAttachment));
2014-02-27 16:54:25 -08:00
}
2014-01-06 17:55:14 -08:00
- (void)performActivity:(OSKActivityCompletionHandler)completion {
__weak OSKFacebookActivity *weakSelf = self;
UIBackgroundTaskIdentifier backgroundTaskIdentifier = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
if (completion) {
completion(weakSelf, NO, nil);
}
}];
[OSKFacebookUtility postContentItem:(OSKFacebookContentItem *)self.contentItem
2014-01-06 17:55:14 -08:00
toSystemAccount:self.activeSystemAccount
options:@{ACFacebookAudienceKey:[self currentAudience]}
completion:^(BOOL success, NSError *error) {
if (completion) {
completion(weakSelf, success, error);
}
[[UIApplication sharedApplication] endBackgroundTask:backgroundTaskIdentifier];
}];
}
+ (BOOL)canPerformViaOperation {
return NO;
}
- (OSKActivityOperation *)operationForActivityWithCompletion:(OSKActivityCompletionHandler)completion {
return nil;
}
#pragma mark - Facebook Sharing Protocol
2014-01-06 17:55:14 -08:00
- (NSInteger)maximumCharacterCount {
return OSKFacebookActivity_MaxCharacterCount;
}
- (NSInteger)maximumImageCount {
return OSKFacebookActivity_MaxImageCount;
}
- (OSKSyntaxHighlighting)syntaxHighlighting {
return OSKSyntaxHighlighting_Links | OSKSyntaxHighlighting_Hashtags;
2014-01-06 17:55:14 -08:00
}
- (NSInteger)maximumUsernameLength {
return OSKFacebookActivity_MaxUsernameLength;
}
- (NSInteger)updateRemainingCharacterCount:(OSKFacebookContentItem *)contentItem urlEntities:(NSArray *)urlEntities {
NSString *text = contentItem.text;
NSInteger composedLength = [text osk_lengthAdjustingForComposedCharacters];
NSInteger remainingCharacterCount = [self maximumCharacterCount] - composedLength;
[self setRemainingCharacterCount:remainingCharacterCount];
return remainingCharacterCount;
}
- (BOOL)allowLinkShortening {
return YES;
}
2014-01-06 17:55:14 -08:00
@end