2014-01-06 17:55:14 -08:00
//
// OSKAppDotNetActivity . m
// Overshare
//
//
// Copyright ( c ) 2013 Overshare Kit . All rights reserved .
//
# import "OSKAppDotNetActivity.h"
# import "OSKMicrobloggingActivity.h"
# import "OSKActivitiesManager.h"
# import "OSKActivity_ManagedAccounts.h"
# import "OSKADNLoginManager.h"
# import "OSKAppDotNetUtility.h"
# import "OSKLogger.h"
# import "OSKManagedAccount.h"
# import "OSKShareableContentItem.h"
2014-02-27 16:54:25 -08:00
# import "NSString+OSKEmoji.h"
2014-01-06 17:55:14 -08:00
static NSInteger OSKAppDotNetActivity_MaxCharacterCount = 256 ;
static NSInteger OSKAppDotNetActivity_MaxUsernameLength = 20 ;
static NSInteger OSKAppDotNetActivity_MaxImageCount = 4 ;
@ interface OSKAppDotNetActivity ( )
@ property ( strong , nonatomic ) NSTimer * authenticationTimeoutTimer ;
@ property ( assign , nonatomic ) BOOL authenticationTimedOut ;
@ property ( copy , nonatomic ) OSKManagedAccountAuthenticationHandler completionHandler ;
@ end
@ implementation OSKAppDotNetActivity
@ synthesize activeManagedAccount = _activeManagedAccount ;
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 ) {
}
return self ;
}
- ( void ) dealloc {
[ self cancelAuthenticationTimeoutTimer ] ;
}
# pragma mark - System Account Methods
+ ( OSKManagedAccountAuthenticationViewControllerType ) authenticationViewControllerType {
OSKManagedAccountAuthenticationViewControllerType method ;
if ( [ [ OSKADNLoginManager sharedInstance ] loginAvailable ] ) {
method = OSKManagedAccountAuthenticationViewControllerType_None ;
} else {
method = OSKManagedAccountAuthenticationViewControllerType_OneOfAKindCustomBespokeViewController ;
}
return method ;
}
2014-02-27 16:54:25 -08:00
- ( OSKUsernameNomenclature ) usernameNomenclatureForSignInScreen {
return OSKUsernameNomenclature_Username ;
}
2014-01-06 17:55:14 -08:00
- ( void ) authenticateNewAccountWithoutViewController : ( OSKManagedAccountAuthenticationHandler ) completion {
[ self authenticateWithADNLogin : completion ] ;
}
# pragma mark - Methods for OSKActivity Subclasses
+ ( NSString * ) supportedContentItemType {
return OSKShareableContentItemType_MicroblogPost ;
}
+ ( BOOL ) isAvailable {
return YES ;
}
+ ( NSString * ) activityType {
return OSKActivityType_API _AppDotNet ;
}
+ ( NSString * ) activityName {
return @ "App.net" ;
}
+ ( UIImage * ) iconForIdiom : ( UIUserInterfaceIdiom ) idiom {
UIImage * image = nil ;
if ( idiom = = UIUserInterfaceIdiomPhone ) {
image = [ UIImage imageNamed : @ "osk-appDotNetIcon-60.png" ] ;
} else {
image = [ UIImage imageNamed : @ "osk-appDotNetIcon-76.png" ] ;
}
return image ;
}
+ ( UIImage * ) settingsIcon {
return [ UIImage imageNamed : @ "osk-appDotNetIcon-29.png" ] ;
}
+ ( OSKAuthenticationMethod ) authenticationMethod {
return OSKAuthenticationMethod_ManagedAccounts ;
}
+ ( BOOL ) requiresApplicationCredential {
return YES ;
}
2014-02-27 16:54:25 -08:00
+ ( OSKPublishingMethod ) publishingMethod {
return OSKPublishingMethod_ViewController _Microblogging ;
2014-01-06 17:55:14 -08:00
}
- ( BOOL ) isReadyToPerform {
BOOL appCredentialPreset = ( [ self . class applicationCredential ] ! = nil ) ;
BOOL credentialPresent = ( self . activeManagedAccount . credential ! = nil ) ;
BOOL accountPresent = ( self . activeManagedAccount ! = nil ) ;
NSInteger maxCharacterCount = [ self maximumCharacterCount ] ;
2014-02-27 16:54:25 -08:00
BOOL textIsValid = ( 0 <= self . remainingCharacterCount && self . remainingCharacterCount < maxCharacterCount ) ;
2014-01-06 17:55:14 -08:00
return ( appCredentialPreset && credentialPresent && accountPresent && textIsValid ) ;
}
- ( void ) performActivity : ( OSKActivityCompletionHandler ) completion {
__weak OSKAppDotNetActivity * weakSelf = self ;
dispatch_async ( dispatch_get _global _queue ( DISPATCH_QUEUE _PRIORITY _DEFAULT , 0 ) , ^ {
[ OSKAppDotNetUtility
postContentItem : ( OSKMicroblogPostContentItem * ) weakSelf . contentItem
withCredential : weakSelf . activeManagedAccount . credential
appCredential : [ weakSelf . class applicationCredential ]
completion : ^ ( BOOL success , NSError * error ) {
OSKLog ( @ "Success! Sent new post to App.net." ) ;
if ( completion ) {
completion ( weakSelf , success , error ) ;
}
} ] ;
} ) ;
}
+ ( BOOL ) canPerformViaOperation {
return NO ;
}
- ( OSKActivityOperation * ) operationForActivityWithCompletion : ( OSKActivityCompletionHandler ) completion {
2014-02-27 16:54:25 -08:00
return nil ;
2014-01-06 17:55:14 -08:00
}
# pragma mark - Microblogging Activity Protocol
- ( NSInteger ) maximumCharacterCount {
return OSKAppDotNetActivity_MaxCharacterCount ;
}
- ( NSInteger ) maximumImageCount {
return OSKAppDotNetActivity_MaxImageCount ;
}
2014-05-19 13:33:42 -07:00
- ( OSKSyntaxHighlighting ) syntaxHighlighting {
return OSKSyntaxHighlighting_Hashtags | OSKSyntaxHighlighting_Links | OSKSyntaxHighlighting_Usernames ;
2014-01-06 17:55:14 -08:00
}
- ( NSInteger ) maximumUsernameLength {
return OSKAppDotNetActivity_MaxUsernameLength ;
}
2014-02-27 16:54:25 -08:00
- ( NSInteger ) updateRemainingCharacterCount : ( OSKMicroblogPostContentItem * ) contentItem urlEntities : ( NSArray * ) urlEntities {
NSString * text = contentItem . text ;
NSInteger composedLength = [ text osk_lengthAdjustingForComposedCharacters ] ;
NSInteger remainingCharacterCount = [ self maximumCharacterCount ] - composedLength ;
[ self setRemainingCharacterCount : remainingCharacterCount ] ;
return remainingCharacterCount ;
}
2014-01-06 17:55:14 -08:00
# pragma mark - ADNLogin
- ( void ) authenticateWithADNLogin : ( OSKManagedAccountAuthenticationHandler ) completion {
__weak OSKAppDotNetActivity * weakSelf = self ;
[ [ OSKADNLoginManager sharedInstance ] loginWithScopes : @ [ @ "basic" , @ "write_post" ] withCompletion : ^ ( NSString * userID , NSString * token , NSError * error ) {
if ( weakSelf . authenticationTimedOut = = NO && completion ) {
OSKApplicationCredential * appCredential = [ [ OSKActivitiesManager sharedInstance ] applicationCredentialForActivityType : [ weakSelf . class activityType ] ] ;
[ OSKAppDotNetUtility createNewUserWithAccessToken : token appCredential : appCredential completion : ^ ( OSKManagedAccount * account , NSError * error ) {
completion ( account , error ) ;
} ] ;
}
} ] ;
}
# pragma mark - Authentication Timeout
- ( void ) startAuthenticationTimeoutTimer {
NSTimer * timer = [ [ NSTimer alloc ] initWithFireDate : [ NSDate dateWithTimeIntervalSinceNow : 60 * 2 ]
interval : 0
target : self
selector : @ selector ( authenticationTimedOut : )
userInfo : nil
repeats : NO ] ;
[ [ NSRunLoop mainRunLoop ] addTimer : timer forMode : NSRunLoopCommonModes ] ;
}
- ( void ) cancelAuthenticationTimeoutTimer {
[ _authenticationTimeoutTimer invalidate ] ;
_authenticationTimeoutTimer = nil ;
}
- ( void ) authenticationTimedOut : ( NSTimer * ) timer {
[ self setAuthenticationTimedOut : YES ] ;
if ( self . completionHandler ) {
__weak OSKAppDotNetActivity * weakSelf = self ;
dispatch_async ( dispatch_get _main _queue ( ) , ^ {
NSError * error = [ NSError errorWithDomain : @ "OSKAppDotNetActivity" code : 408 userInfo : @ { NSLocalizedFailureReasonErrorKey : @ "ADN authentication via the Passport app timed out." } ] ;
weakSelf . completionHandler ( nil , error ) ;
} ) ;
}
}
@ end