Skip to content
This repository has been archived by the owner on Dec 18, 2022. It is now read-only.

Commit

Permalink
More app-friendly stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
NSExceptional committed Oct 11, 2015
1 parent 61fb43b commit 533a127
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

/* Begin PBXFileReference section */
A3FE50062F493EF242FAFB51 /* Pods-SnapchatKit-OSX.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-SnapchatKit-OSX.release.xcconfig"; path = "../Pods/Target Support Files/Pods-SnapchatKit-OSX/Pods-SnapchatKit-OSX.release.xcconfig"; sourceTree = "<group>"; };
B4CF268F1BC48BF100AAF1D0 /* Login.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = Login.h; path = ../../../../../../Login.h; sourceTree = "<group>"; };
B4D11BA51B90E3850012116A /* SnapchatKit-OSX */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = "SnapchatKit-OSX"; sourceTree = BUILT_PRODUCTS_DIR; };
B4D11BA81B90E3850012116A /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
B4D11BAF1B90E3B60012116A /* TBTimer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TBTimer.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -84,6 +85,7 @@
B4D11BA71B90E3850012116A /* SnapchatKit-OSX */ = {
isa = PBXGroup;
children = (
B4CF268F1BC48BF100AAF1D0 /* Login.h */,
B4D11BA81B90E3850012116A /* main.m */,
B4D11BAF1B90E3B60012116A /* TBTimer.h */,
B4D11BB01B90E3B60012116A /* TBTimer.m */,
Expand Down
2 changes: 1 addition & 1 deletion Example/SnapchatKit-OSX/SnapchatKit-OSX/main.m
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ int main(int argc, const char * argv[]) {
// saveUnreadSnapsToDirectory(unread, directory);

// Mark snaps read
// markSnapsRead(unread);
markSnapsRead(unread);

// Mark chats read (not working)
// markChatsRead(session);
Expand Down
8 changes: 6 additions & 2 deletions Pod/Classes/Model/SKConversation.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ extern SKChatType SKChatTypeFromString(NSString *chatTypeString);
@property (nonatomic, readonly) NSString *lastChatReader;
/** \c nil if not applicable. */
@property (nonatomic, readonly) NSString *lastChatWriter;
/** A string representing the last chat interaction (i.e. the last message, or a message describing a snap or cash transaction. */
@property (nonatomic, readonly) NSString *suggestedChatPreview;

/** \c nil if not applicable. */
@property (nonatomic, readonly) SKCashTransaction *lastTransaction;
Expand All @@ -83,8 +85,10 @@ extern SKChatType SKChatTypeFromString(NSString *chatTypeString);
@end

@interface SKConversation (SKClient)
/** Returns the (first) participant that is not the current user. */
@property (nonatomic, readonly) NSString *recipient;
/** @return The (first) participant that is not the current (given) user. */
- (NSString *)recipientGivenUser:(NSString *)user;
/** Whether or not \c user has unread messages. */
- (BOOL)userHasUnreadChats:(NSString *)user;
/** All messages in a human-readable, newline separated format. */
@property (nonatomic, readonly) NSString *conversationString;
@end
35 changes: 32 additions & 3 deletions Pod/Classes/Model/SKConversation.m
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,42 @@ - (BOOL)isEqualToConversation:(SKConversation *)conversation {
return [self.identifier isEqualToString:conversation.identifier];
}

- (NSString *)suggestedChatPreview {
for (id message in self.messages.reverseObjectEnumerator) {
Class cls = [message class];
if (cls == [SKSnap class]) break;

if (cls == [SKMessage class]) {
SKMessage *mess = (id)message;
return mess.text ?: [mess.sender stringByAppendingString:@"sent a picture."];

} else if (cls == [SKCashTransaction class]) {
SKCashTransaction *cash = (id)message;
return [NSString stringWithFormat:@"%@ sent %@", cash.sender, cash.message];
}

[NSException raise:NSInternalInconsistencyException format:@"Unknown class in SKConversation.messages: %@", cls];
return nil;
}

return @"";
}

@end

@implementation SKConversation (SKClient)

- (NSString *)recipient {
if (![SKClient sharedClient].username.length) return nil;
return [self.participants[0] isEqualToString:[SKClient sharedClient].username] ? self.participants[1] : self.participants[0];
- (NSString *)recipientGivenUser:(NSString *)user {
if (user) return nil;
return [self.participants[0] isEqualToString:user] ? self.participants[1] : self.participants[0];
}

- (BOOL)userHasUnreadChats:(NSString *)user {
NSString *sender = [self recipientGivenUser:user];
NSUInteger yourCount = [self.state[@"user_chat_releases"][user][sender] integerValue];
NSUInteger senderCount = [self.state[@"user_chat_releases"][sender][user] integerValue];

return yourCount < senderCount;
}

- (NSString *)conversationString {
Expand Down
25 changes: 22 additions & 3 deletions Pod/Classes/Networking/SKClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@

extern NSString *SKMakeCapserSignature(NSDictionary *params, NSString *secret);

typedef NS_ENUM(NSUInteger, SKScreenIdiom) {
SKScreenIdiomiPhone4 = 1,
SKScreenIdiomiPhone5 = 0,
SKScreenIdiomiPhone6 = 2,
SKScreenIdiomiPhone6Plus = 3
};

/** Used to restructure JSON or modify an error returned from nearly any API call before being passed back to the application code. */
@protocol SKMiddleMan <NSObject>
/** @discussion This method is passed the JSON and any error generated by \c -[SKClient handleError:data:response:completion:].
Expand All @@ -30,14 +37,20 @@ extern NSString *SKMakeCapserSignature(NSDictionary *params, NSString *secret);
/** The default Snapchat session manager. To use more than one account, simply create and manage your own instances of \c SKClient instead of using the singleton. */
+ (instancetype)sharedClient;

/** Initializes an \c SKClient instance with the minimum data required to resume an existing session. \c currentSession needs to be updated afterwards. */
+ (instancetype)clientWithUsername:(NSString *)username authToken:(NSString *)authToken gauth:(NSString *)googleAuthToken;

/** See the \c SKMiddleMan protocol. */
@property (nonatomic) id<SKMiddleMan> middleMan;

/** The size of your device's screen. */
/** The size of your device's screen. On iOS, this defaults to the actual screen size. */
@property (nonatomic) CGSize screenSize;
/** The maxium sized to load videos in. */
/** The maxium sized to load videos in. On iOS, this defaults to the actual screen size. */
@property (nonatomic) CGSize maxVideoSize;

/** Use this to automatically adjust the \c screenSize and \c maxVideoSize properties. */
- (void)setScreenIdiom:(SKScreenIdiom)idiom;

/** The username of the currently signed in (or not yet singed in) user. @note Always lowercase. */
@property (nonatomic, readonly) NSString *username;
/** The \c SKSession object representing the current Snapchat session.
Expand Down Expand Up @@ -72,7 +85,13 @@ extern NSString *SKMakeCapserSignature(NSDictionary *params, NSString *secret);
@param gmailPassword The password associated with \c gmailEmail.
@param completion Takes an error, if any, and the JSON response from signing in as a dictionary. */
- (void)signInWithUsername:(NSString *)username password:(NSString *)password gmail:(NSString *)gmailEmail gpass:(NSString *)gmailPassword completion:(DictionaryBlock)completion;
//- (void)restoreSessionWithUsername:(NSString *)username snapchatAuthToken:(NSString *)authToken doGetUpdates:(ErrorBlock)comletion;
/** Signs into Snapchat without signing out an existing session by using that session's Snapchat auth token.
@param username The Snapchat username to sign in with.
@param authToken The auth token of an active Snapchat session.
@param gmailEmail A vaild GMail address.
@param gmailPassword The password associated with \c gmailEmail.
@param completion Takes an error, if any, and the JSON response from signing in as a dictionary. */
- (void)signInWithUsername:(NSString *)username authToken:(NSString *)authToken gmail:(NSString *)gmailEmail gpass:(NSString *)gmailPassword completion:(DictionaryBlock)completion;
/** Use this to restore a session that ended within the last hour. The google auth token must be re-generated every hour.
@discussion If you have a stale Google auth token, consider using \c -restoreSessionWithUsername:snapchatAuthToken:doGetUpdates:.
@param username Your Snapchat username.
Expand Down
65 changes: 64 additions & 1 deletion Pod/Classes/Networking/SKClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,33 @@ + (instancetype)sharedClient {
return sharedSKClient;
}

- (id)initWithCoder:(NSCoder *)aDecoder {
+ (instancetype)clientWithUsername:(NSString *)username authToken:(NSString *)authToken gauth:(NSString *)googleAuthToken {
SKClient *client = [self new];
client.username = username;
client->_authToken = authToken;
client->_googleAuthToken = googleAuthToken;

return client;
}

- (id)init {
self = [super init];
if (self) {
if (NSClassFromString(@"UIScreen")) {
id cls = NSClassFromString(@"UIScreen");
id screen = [cls performSelector:@selector(mainScreen)];
self.screenSize = [screen bounds].size;
self.maxVideoSize = self.screenSize;
} else {
[self setScreenIdiom:0];
}
}

return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder {
self = [self init];
if (self) {
_googleAuthToken = [aDecoder decodeObjectForKey:@"googleAuthToken"];
_googleAttestation = [aDecoder decodeObjectForKey:@"googleAttestation"];
Expand Down Expand Up @@ -103,6 +128,27 @@ - (void)setUsername:(NSString *)username {
_username = [username lowercaseString];
}

- (void)setScreenIdiom:(SKScreenIdiom)idiom {
switch (idiom) {
case SKScreenIdiomiPhone4: {
_screenSize = _maxVideoSize = CGSizeMake(640, 960);
break;
}
case SKScreenIdiomiPhone5: {
_screenSize = _maxVideoSize = CGSizeMake(640, 1136);
break;
}
case SKScreenIdiomiPhone6: {
_screenSize = _maxVideoSize = CGSizeMake(750, 1334);
break;
}
case SKScreenIdiomiPhone6Plus: {
_screenSize = _maxVideoSize = CGSizeMake(1080, 1920);
break;
}
}
}

#pragma mark Convenience

- (void)handleError:(NSError *)error data:(NSData *)data response:(NSURLResponse *)response completion:(ResponseBlock)completion {
Expand Down Expand Up @@ -534,6 +580,23 @@ - (void)signInWithData:(NSDictionary *)loginData username:(NSString *)username p
}] resume];
}

- (void)signInWithUsername:(NSString *)username authToken:(NSString *)authToken gmail:(NSString *)gmailEmail gpass:(NSString *)gmailPassword completion:(DictionaryBlock)completion {
NSParameterAssert(username); NSParameterAssert(authToken); NSParameterAssert(gmailEmail); NSParameterAssert(gmailPassword);

[self getAuthTokenForGmail:gmailEmail password:gmailPassword callback:^(NSString *gauth, NSError *error) {
if (!error) {
_googleAuthToken = gauth;
_authToken = authToken;
self.username = username;
[[SKClient sharedClient] updateSession:^(NSError *error2) {
completion([self.currentSession valueForKey:@"JSON"], error2);
}];
} else {
completion(nil, error);
}
}];
}

- (void)restoreSessionWithUsername:(NSString *)username snapchatAuthToken:(NSString *)authToken googleAuthToken:(NSString *)googleAuthToken doGetUpdates:(ErrorBlock)completion {
NSParameterAssert(username); NSParameterAssert(authToken); NSParameterAssert(googleAuthToken);
_username = username;
Expand Down

0 comments on commit 533a127

Please sign in to comment.