Skip to content

Commit 7a7f05c

Browse files
Merge pull request #4 from studyplus/refactor_sdk
Refactor SDK
2 parents 3aef713 + b39ca47 commit 7a7f05c

11 files changed

+73
-50
lines changed

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;

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

StudyplusSDK/SPLStudyplus.m

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +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 {
52+
- (instancetype)__initWithConsumerKey:(NSString*)consumerKey
53+
andConsumerSecret:(NSString*)consumerSecret
54+
{
5455

5556
if (self = [super init]) {
5657
self.consumerKey = consumerKey;
@@ -113,6 +114,14 @@ - (void)postStudyRecord:(SPLStudyplusRecord *)studyplusRecord
113114
return;
114115
}
115116

117+
if (![self isConnected]) {
118+
if ([self.delegate respondsToSelector:@selector(studyplusDidFailToPostStudyRecord:withError:)]) {
119+
[self.delegate studyplusDidFailToPostStudyRecord:self withError:[SPLStudyplusError errorFromStudyplusErrorCode:SPLErrorCodeNotConnected]];
120+
}
121+
122+
return;
123+
}
124+
116125
SPLStudyplusAPIRequest *request = [SPLStudyplusAPIRequest
117126
newRequestWithAccessToken:self.accessToken
118127
options:@{
@@ -141,8 +150,8 @@ - (BOOL)openURL:(NSURL*)url
141150
}
142151

143152
if ([url.pathComponents[1] isEqualToString:@"success"]) {
144-
NSString *accessToken = url.pathComponents[2];
145-
NSString *username = url.pathComponents[3];
153+
NSString *accessToken = [url.pathComponents[2] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
154+
NSString *username = [url.pathComponents[3] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
146155
[self saveAccessToken:accessToken andUsername:username];
147156
[self.delegate studyplusDidConnect:self];
148157
} else if ([url.pathComponents[1] isEqualToString:@"fail"]) {
@@ -164,7 +173,8 @@ - (BOOL)openURL:(NSURL*)url
164173

165174
#pragma mark - privates
166175

167-
- (id)init {
176+
- (id)init
177+
{
168178
@throw [NSException exceptionWithName:NSInternalInconsistencyException
169179
reason:@"-init method is not available."
170180
userInfo:nil];

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

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

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

StudyplusSDK/SPLStudyplusError.m

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ + (NSError*)errorFromStudyplusErrorCode:(SPLErrorCode)studyplusErrorCode
7171
localizedDescription:@"Failed to post study record. (400 bad request)"];
7272
break;
7373

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."];

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

StudyplusSDK/SPLStudyplusRecord.m

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,25 +33,26 @@ - (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;
5051
}
5152
return value;
5253
}
5354

54-
- (id)initWithDuration:(NSTimeInterval)duration options:(NSDictionary*)options
55+
- (instancetype)initWithDuration:(NSTimeInterval)duration options:(NSDictionary*)options
5556
{
5657
if (self = [super init]) {
5758
_duration = duration;

StudyplusSDK/SPLStudyplusRecordAmount.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
/**
2628
The class that represents the amount of learning. <br>
2729
学習量を表すクラスです。
@@ -35,7 +37,7 @@
3537
@param amount 学習量。
3638
@result SPLStudyplusRecordAmount* 生成したAmountオブジェクトです。
3739
*/
38-
+ (SPLStudyplusRecordAmount*)amount:(NSUInteger)amount;
40+
+ (instancetype)amount:(NSUInteger)amount;
3941

4042
/**
4143
Creates and returns the Amount object with a range of learning amount.<br>
@@ -45,18 +47,20 @@
4547
@param to 学習量の終点。
4648
@result SPLStudyplusRecordAmount* 生成したAmountオブジェクトです。
4749
*/
48-
+ (SPLStudyplusRecordAmount*)amountAsRangeWithFrom:(NSUInteger)from to:(NSUInteger)to;
50+
+ (instancetype)amountAsRangeWithFrom:(NSUInteger)from to:(NSUInteger)to;
4951

5052
/**
5153
Creates and returns the Amount object that has no learning amount.<br>
5254
学習量を持たないAmountオブジェクトを生成して返します。
5355
@result SPLStudyplusRecordAmount* 生成したAmountオブジェクトです。
5456
*/
55-
+ (SPLStudyplusRecordAmount*)amountAsNone;
57+
+ (instancetype)amountAsNone;
5658

5759
/**
5860
@result Returns the parameters of the study record for posting API
5961
*/
6062
- (NSDictionary*)toRequestParameter;
6163

64+
NS_ASSUME_NONNULL_END
65+
6266
@end

0 commit comments

Comments
 (0)