@@ -29,7 +29,7 @@ import UIKit
29
29
/**
30
30
The class for using Studyplus.
31
31
For example, you can authenticate in Studyplus account, de-authentication, and post study record.
32
-
32
+
33
33
Studyplusの各機能を使うためのクラスです。
34
34
Studyplusアカウントとの連携、連携解除、勉強記録の投稿ができます。
35
35
*/
@@ -49,13 +49,6 @@ final public class Studyplus {
49
49
*/
50
50
public static let shared : Studyplus = Studyplus ( )
51
51
52
- /**
53
- When set to true, then call back to the delegate without posting the actual processing.
54
-
55
- trueの場合、勉強記録の投稿時に実際の通信は行わずdelegateのコールバックメソッドを呼び出して処理を終了します。
56
- */
57
- public var debug : Bool = false
58
-
59
52
/**
60
53
Consumer Key for Studyplus API.
61
54
@@ -156,44 +149,23 @@ final public class Studyplus {
156
149
return String ( data: data, encoding: . utf8)
157
150
}
158
151
159
- /// Posts a new study record to Studyplus.
160
- ///
161
- /// Studyplusに勉強記録を新規投稿します。
152
+ /// Studyplusに学習記録を投稿
162
153
///
163
- /// - Parameter
164
- /// - studyRecord: see StudyplusRecord
165
- /// - success: callback when success to post the studyRecord
166
- /// - failure: callback when failure to post the studyRecord
167
- public func post( studyRecord: StudyplusRecord ,
168
- success: @escaping ( ) -> Void ,
169
- failure: @escaping ( _ error: StudyplusError ) -> Void ) {
170
-
171
- guard StudyplusRecord . durationRange ~= Int ( studyRecord. duration) else {
172
- failure ( . postRecordFailed)
173
- return
174
- }
175
-
176
- if !self . isConnected ( ) {
177
- failure ( . notConnected)
178
- return
179
- }
180
-
154
+ /// - Parameters:
155
+ /// - record: 学習記録
156
+ /// - completion: 投稿完了後のコールバック
157
+ public func post( _ record: StudyplusRecord , completion: @escaping ( Result < Void , StudyplusPostError > ) -> Void ) {
181
158
guard let accessToken = self . accessToken ( ) else {
182
- failure ( . notConnected )
159
+ completion ( . failure( . needLogin ) )
183
160
return
184
161
}
185
162
186
- if self . debug {
187
- success ( )
163
+ guard record . isValidDuration else {
164
+ completion ( . failure ( . invalidDuration ) )
188
165
return
189
166
}
190
167
191
- let request : StudyplusAPIRequest = StudyplusAPIRequest ( accessToken: accessToken)
192
- request. post ( path: " study_records " , params: studyRecord. requestParams ( ) , success: { ( _) in
193
- success ( )
194
- } , failure: { error in
195
- failure ( error)
196
- } )
168
+ StudyplusAPIRequest ( accessToken: accessToken) . post ( record, completion: completion)
197
169
}
198
170
199
171
/// It is responsible for processing custom URL scheme
@@ -213,20 +185,19 @@ final public class Studyplus {
213
185
/// The valid URL has a __[studyplus-{consumerKey}]__ scheme, and right pathComponents and host.
214
186
/// 渡されたurlがStudyplusSDKで対応すべきURLであれば true、それ以外は false を返します。
215
187
/// __[studyplus-{consumerKey}]__と正しいpathComponentsを持つことを確認してください。
216
- public func handle( appDelegateUrl: URL ) -> Bool {
217
-
218
- guard isAcceptableURL ( url: appDelegateUrl) else {
219
- delegate? . studyplusDidFailToLogin ( error: . unknownUrl( appDelegateUrl) )
188
+ public func handle( _ url: URL ) -> Bool {
189
+ guard isAcceptableURL ( url: url) else {
190
+ delegate? . studyplusDidFailToLogin ( error: . unknownUrl( url) )
220
191
return false
221
192
}
222
193
223
- switch appDelegateUrl . pathComponents [ 1 ] {
194
+ switch url . pathComponents [ 1 ] {
224
195
case " success " :
225
- let accessToken : Data = appDelegateUrl
196
+ let accessToken : Data = url
226
197
. pathComponents [ 2 ]
227
198
. trimmingCharacters ( in: . whitespacesAndNewlines)
228
199
. data ( using: . utf8, allowLossyConversion: false ) !
229
- let username : Data = appDelegateUrl
200
+ let username : Data = url
230
201
. pathComponents [ 3 ]
231
202
. trimmingCharacters ( in: . whitespacesAndNewlines)
232
203
. data ( using: . utf8, allowLossyConversion: false ) !
@@ -250,24 +221,13 @@ final public class Studyplus {
250
221
if statusAccessToken == noErr && statusUsername == noErr {
251
222
delegate? . studyplusDidSuccessToLogin ( )
252
223
} else {
253
- delegate? . studyplusDidFailToLogin ( error: . unknownReason ( " Could not access Keychain. " ) )
224
+ delegate? . studyplusDidFailToLogin ( error: . keychainError )
254
225
}
255
226
case " fail " :
256
-
257
- if let errorCode: Int = Int ( appDelegateUrl. pathComponents [ 2 ] ) {
258
- delegate? . studyplusDidFailToLogin ( error: StudyplusError ( errorCode) )
259
- } else {
260
- delegate? . studyplusDidFailToLogin ( error: . unknownReason( " ErrorCode is nil " ) )
261
- }
262
-
227
+ delegate? . studyplusDidFailToLogin ( error: . fail)
263
228
case " cancel " :
264
-
265
- delegate? . studyplusDidCancelToLogin ( )
266
-
229
+ delegate? . studyplusDidFailToLogin ( error: . cancel)
267
230
default :
268
- #if DEBUG
269
- print ( " StudyplusSDK: Unknown format: \( appDelegateUrl. absoluteString) " )
270
- #endif
271
231
return false
272
232
}
273
233
@@ -291,7 +251,6 @@ final public class Studyplus {
291
251
/// - consumerKey: consumer key
292
252
/// - consumerSecret: consumer secret
293
253
public func change( consumerKey: String , consumerSecret: String ) {
294
-
295
254
self . consumerKey = consumerKey
296
255
self . consumerSecret = consumerSecret
297
256
}
@@ -352,7 +311,6 @@ final public class Studyplus {
352
311
}
353
312
354
313
private func isAcceptableURL( url: URL ) -> Bool {
355
-
356
314
guard let host = url. host else { return false }
357
315
guard host == " auth-result " || host == " login-result " else { return false }
358
316
0 commit comments