Skip to content

Commit 3aef713

Browse files
Merge pull request #2 from studyplus/refactor_sdk_demo
Refactor SDK Demo
2 parents 6b84cd0 + 83aa6b4 commit 3aef713

18 files changed

+105
-67
lines changed

Demo/StudyplusSDKDemo/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ StudyplusSDK Demo
88

99
### Usage
1010

11-
1. Open this project in Xcode5.
12-
2. Overwrite Consumer Key and Secret in ViewController.m.
13-
3. Set custom URL Scheme.
14-
4. Run.
11+
1. Run `pod try StudyplusSDK` or open this project
12+
2. Overwrite Consumer Key and Secret in ViewController.m
13+
3. Set custom URL Scheme
14+
4. Run

Demo/StudyplusSDKDemo/StudyplusSDK/SPLStopwatch.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ @interface SPLStopwatch()
3535

3636
@implementation SPLStopwatch
3737

38-
-(id)init
38+
-(instancetype)init
3939
{
4040
if (self = [super init]) {
4141
self.elapsedSeconds = 0;

Demo/StudyplusSDKDemo/StudyplusSDK/SPLStudyplus.h

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
#import "SPLStudyplusRecordAmount.h"
2626
#import "SPLStopwatch.h"
2727

28+
NS_ASSUME_NONNULL_BEGIN
29+
2830
/**
2931
The class for using Studyplus.<br>
3032
For example, you can authenticate in Studyplus account, de-authentication, and post study record.
@@ -67,7 +69,7 @@
6769
/**
6870
@see SPLStudyplusDelegate protocol
6971
*/
70-
@property (nonatomic, weak) id<SPLStudyplusDelegate> delegate;
72+
@property (nonatomic, weak, nullable) id<SPLStudyplusDelegate> delegate;
7173

7274
/**
7375
When set to YES, if Studyplus is not installed, AppStore application starts when auth/login methods are called. Default value is YES.<br>
@@ -79,18 +81,18 @@
7981
Username of Studyplus account. It is set when the auth or login is successful. Default value is nil.<br>
8082
Studyplusアカウントのユーザ名です。login または authが成功したとき設定されます。設定されるまではnilです。
8183
*/
82-
@property (nonatomic, copy, readonly) NSString *username;
84+
@property (nonatomic, copy, readonly, nullable) NSString *username;
8385

8486
/**
8587
Access token of Studyplus API. It is set when the auth or login is successful. Default value is nil.<br>
8688
StudyplusAPIのアクセストークンです。login または authが成功したとき設定されます。設定されるまではnilです。
8789
*/
88-
@property (nonatomic, copy, readonly) NSString *accessToken;
90+
@property (nonatomic, copy, readonly, nullable) NSString *accessToken;
8991

9092
/**
9193
@see SPLStopwatch class
9294
*/
93-
@property (nonatomic, readonly) SPLStopwatch *stopwatch;
95+
@property (nonatomic, readonly, nullable) SPLStopwatch *stopwatch;
9496

9597
/**
9698
The convenience constructor of SPLStudyplus class.
@@ -106,8 +108,8 @@
106108
@return SPLStudyplus object. <br>
107109
SPLStudyplusオブジェクト。
108110
*/
109-
+ (SPLStudyplus*)studyplusWithConsumerKey:(NSString*)consumerKey
110-
andConsumerSecret:(NSString*)consumerSecret;
111+
+ (instancetype)studyplusWithConsumerKey:(NSString*)consumerKey
112+
andConsumerSecret:(NSString*)consumerSecret;
111113

112114
/**
113115
Opens the auth screen by invoking the Studyplus application.
@@ -182,4 +184,6 @@
182184
*/
183185
- (BOOL)openURL:(NSURL*)url;
184186

187+
NS_ASSUME_NONNULL_END
188+
185189
@end

Demo/StudyplusSDKDemo/StudyplusSDK/SPLStudyplus.m

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,16 @@ @interface SPLStudyplus()
4242

4343
@implementation SPLStudyplus
4444

45-
+ (SPLStudyplus*)studyplusWithConsumerKey:(NSString*)consumerKey
46-
andConsumerSecret:(NSString*)consumerSecret
45+
+ (instancetype)studyplusWithConsumerKey:(NSString*)consumerKey
46+
andConsumerSecret:(NSString*)consumerSecret
4747
{
4848
return [[SPLStudyplus alloc] __initWithConsumerKey:consumerKey
4949
andConsumerSecret:consumerSecret];
5050
}
5151

52-
- (id)__initWithConsumerKey:(NSString*)consumerKey
53-
andConsumerSecret:(NSString*)consumerSecret {
54-
52+
- (instancetype)__initWithConsumerKey:(NSString*)consumerKey
53+
andConsumerSecret:(NSString*)consumerSecret
54+
{
5555
if (self = [super init]) {
5656
self.consumerKey = consumerKey;
5757
self.consumerSecret = consumerSecret;
@@ -113,6 +113,14 @@ - (void)postStudyRecord:(SPLStudyplusRecord *)studyplusRecord
113113
return;
114114
}
115115

116+
if (![self isConnected]) {
117+
if ([self.delegate respondsToSelector:@selector(studyplusDidFailToPostStudyRecord:withError:)]) {
118+
[self.delegate studyplusDidFailToPostStudyRecord:self withError:[SPLStudyplusError errorFromStudyplusErrorCode:SPLErrorCodeNotConnected]];
119+
}
120+
121+
return;
122+
}
123+
116124
SPLStudyplusAPIRequest *request = [SPLStudyplusAPIRequest
117125
newRequestWithAccessToken:self.accessToken
118126
options:@{
@@ -141,8 +149,8 @@ - (BOOL)openURL:(NSURL*)url
141149
}
142150

143151
if ([url.pathComponents[1] isEqualToString:@"success"]) {
144-
NSString *accessToken = url.pathComponents[2];
145-
NSString *username = url.pathComponents[3];
152+
NSString *accessToken = [url.pathComponents[2] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
153+
NSString *username = [url.pathComponents[3] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
146154
[self saveAccessToken:accessToken andUsername:username];
147155
[self.delegate studyplusDidConnect:self];
148156
} else if ([url.pathComponents[1] isEqualToString:@"fail"]) {
@@ -164,7 +172,8 @@ - (BOOL)openURL:(NSURL*)url
164172

165173
#pragma mark - privates
166174

167-
- (id)init {
175+
- (id)init
176+
{
168177
@throw [NSException exceptionWithName:NSInternalInconsistencyException
169178
reason:@"-init method is not available."
170179
userInfo:nil];

Demo/StudyplusSDKDemo/StudyplusSDK/SPLStudyplusAPIRequest.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,18 @@
2222

2323
#import <Foundation/Foundation.h>
2424

25+
NS_ASSUME_NONNULL_BEGIN
26+
2527
@interface SPLStudyplusAPIRequest : NSObject
2628

27-
+ (SPLStudyplusAPIRequest*)newRequestWithAccessToken:(NSString*)accessToken
28-
options:(NSDictionary*)options;
29+
+ (instancetype)newRequestWithAccessToken:(NSString*)accessToken
30+
options:(NSDictionary*)options;
2931

3032
- (void)postRequestWithPath:(NSString *)path
3133
requestParameter:(NSDictionary *)requestParameter
32-
completed:(void(^)(NSDictionary *response))completed
34+
completed:(void(^)(NSDictionary * _Nullable response))completed
3335
failed:(void(^)(NSError *error))failed;
3436

37+
NS_ASSUME_NONNULL_END
38+
3539
@end

Demo/StudyplusSDKDemo/StudyplusSDK/SPLStudyplusAPIRequest.m

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,23 @@ @interface SPLStudyplusAPIRequest()
3535

3636
@implementation SPLStudyplusAPIRequest
3737

38-
- (id)init
38+
- (instancetype)init
3939
{
4040
if (self = [super init]) {
4141
_accessToken = nil;
4242
}
4343
return self;
4444
}
4545

46-
+ (SPLStudyplusAPIRequest*)newRequestWithAccessToken:(NSString*)accessToken
47-
options:(NSDictionary*)options
46+
+ (instancetype)newRequestWithAccessToken:(NSString*)accessToken
47+
options:(NSDictionary*)options
4848
{
4949
return [[SPLStudyplusAPIRequest alloc] initWithAccessToken:accessToken
5050
options:options];
5151
}
5252

53-
- (id)initWithAccessToken:(NSString*)accessToken
54-
options:(NSDictionary*)options
53+
- (instancetype)initWithAccessToken:(NSString*)accessToken
54+
options:(NSDictionary*)options
5555
{
5656
if (self = [super init]) {
5757
_accessToken = accessToken;
@@ -64,7 +64,7 @@ - (void)postRequestWithPath:(NSString *)path
6464
requestParameter:(NSDictionary *)requestParameter
6565
completed:(void(^)(NSDictionary *response))completed
6666
failed:(void(^)(NSError *error))failed
67-
{
67+
{
6868
[self sendRequestWithPath:path
6969
requestParams:requestParameter
7070
completed:completed
@@ -83,7 +83,7 @@ - (void)sendRequestWithPath:(NSString*)path
8383
failed:(void(^)(NSInteger httpStatusCode, NSError *error))failed
8484
{
8585
AFHTTPResponseSerializer *responseSerializer = [AFHTTPResponseSerializer serializer];
86-
AFJSONRequestSerializer * requestSerializer = [AFJSONRequestSerializer serializer];
86+
AFJSONRequestSerializer *requestSerializer = [AFJSONRequestSerializer serializer];
8787
[requestSerializer setValue:[NSString stringWithFormat:@"OAuth %@", self.accessToken]
8888
forHTTPHeaderField:@"HTTP_AUTHORIZATION"];
8989

@@ -119,14 +119,4 @@ - (NSString *)buildUrlFromPath:(NSString *)path
119119
return [NSString stringWithFormat:@"%@v%ld/%@", [self apiBaseURL], (long)self.apiVersion, path];
120120
}
121121

122-
+ (NSOperationQueue *)sharedQueue
123-
{
124-
static NSOperationQueue *queue;
125-
static dispatch_once_t onceToken;
126-
dispatch_once(&onceToken, ^{
127-
queue = [NSOperationQueue new];
128-
});
129-
return queue;
130-
}
131-
132122
@end

Demo/StudyplusSDKDemo/StudyplusSDK/SPLStudyplusError.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ typedef NS_ENUM(NSInteger, SPLErrorCode) {
3131
SPLErrorCodeNetworkUnavailable = 6000,
3232
SPLErrorCodeServerError = 7000,
3333
SPLErrorCodePostRecordFailed = 8000,
34+
SPLErrorCodeNotConnected = 9000,
3435
SPLErrorCodeUnknown = 90000
3536
};
3637

Demo/StudyplusSDKDemo/StudyplusSDK/SPLStudyplusError.m

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,12 @@ + (NSError*)errorFromStudyplusErrorCode:(SPLErrorCode)studyplusErrorCode
7070
error = [SPLStudyplusError errorWithCode:studyplusErrorCode
7171
localizedDescription:@"Failed to post study record. (400 bad request)"];
7272
break;
73-
73+
74+
case SPLErrorCodeNotConnected:
75+
error = [SPLStudyplusError errorWithCode:studyplusErrorCode
76+
localizedDescription:@"Not Connected"];
77+
break;
78+
7479
case SPLErrorCodeUnknown:
7580
error = [SPLStudyplusError errorWithCode:SPLErrorCodeUnknown
7681
localizedDescription:@"Unknown Error."];

Demo/StudyplusSDKDemo/StudyplusSDK/SPLStudyplusRecord.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
#import <Foundation/Foundation.h>
2424

25+
NS_ASSUME_NONNULL_BEGIN
26+
2527
@class SPLStudyplusRecordAmount;
2628

2729
/**
@@ -54,7 +56,7 @@
5456
The comment of learning.<br>
5557
勉強に関するコメントです。
5658
*/
57-
@property (nonatomic, readonly) NSString *comment;
59+
@property (nonatomic, readonly, nullable) NSString *comment;
5860

5961
/**
6062
Creates and returns StudyplusRecord object that has number of seconds, no amount, and empty comment.<br>
@@ -65,7 +67,7 @@
6567
6668
@result StudyplusRecord object.
6769
*/
68-
+ (SPLStudyplusRecord*)recordWithDuration:(NSTimeInterval)duration;
70+
+ (instancetype)recordWithDuration:(NSTimeInterval)duration;
6971

7072
/**
7173
Creates and returns StudyplusRecord object that has number of seconds and other attributes.
@@ -87,11 +89,13 @@
8789
8890
@result StudyplusRecord object.
8991
*/
90-
+ (SPLStudyplusRecord*)recordWithDuration:(NSTimeInterval)duration options:(NSDictionary*)options;
92+
+ (instancetype)recordWithDuration:(NSTimeInterval)duration options:(NSDictionary* _Nullable)options;
9193

9294
/**
9395
@result Returns the parameters of the study record for posting API
9496
*/
9597
- (NSDictionary*)toRequestParam;
9698

99+
NS_ASSUME_NONNULL_END
100+
97101
@end

Demo/StudyplusSDKDemo/StudyplusSDK/SPLStudyplusRecord.m

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,18 @@ - (id)getKey:(NSString*)key orElse:(id)elseValue;
3333

3434
@implementation SPLStudyplusRecord
3535

36-
+ (SPLStudyplusRecord*)recordWithDuration:(NSTimeInterval)duration
36+
+ (instancetype)recordWithDuration:(NSTimeInterval)duration
3737
{
3838
return [self recordWithDuration:duration options:@{}];
3939
}
4040

41-
+ (SPLStudyplusRecord*)recordWithDuration: (NSTimeInterval)duration options:(NSDictionary*)options
41+
+ (instancetype)recordWithDuration:(NSTimeInterval)duration options:(NSDictionary*)options
4242
{
4343
return [[[self class] alloc] initWithDuration:duration options:options];
4444
}
4545

46-
- (id)getKey:(NSString *)key from:(NSDictionary *)dict orElse:(id)elseValue {
46+
- (id)getKey:(NSString *)key from:(NSDictionary *)dict orElse:(id)elseValue
47+
{
4748
id value = dict[key];
4849
if (value == [NSNull null] || value == nil) {
4950
value = elseValue;

0 commit comments

Comments
 (0)