@@ -29,7 +29,7 @@ import UIKit
2929/**
3030 The class for using Studyplus.
3131 For example, you can authenticate in Studyplus account, de-authentication, and post study record.
32-
32+
3333 Studyplusの各機能を使うためのクラスです。
3434 Studyplusアカウントとの連携、連携解除、勉強記録の投稿ができます。
3535 */
@@ -49,13 +49,6 @@ final public class Studyplus {
4949 */
5050 public static let shared : Studyplus = Studyplus ( )
5151
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-
5952 /**
6053 Consumer Key for Studyplus API.
6154
@@ -156,44 +149,23 @@ final public class Studyplus {
156149 return String ( data: data, encoding: . utf8)
157150 }
158151
159- /// Posts a new study record to Studyplus.
160- ///
161- /// Studyplusに勉強記録を新規投稿します。
152+ /// Studyplusに学習記録を投稿
162153 ///
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 ) {
181158 guard let accessToken = self . accessToken ( ) else {
182- failure ( . notConnected )
159+ completion ( . failure( . needLogin ) )
183160 return
184161 }
185162
186- if self . debug {
187- success ( )
163+ guard record . isValidDuration else {
164+ completion ( . failure ( . invalidDuration ) )
188165 return
189166 }
190167
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)
197169 }
198170
199171 /// It is responsible for processing custom URL scheme
@@ -213,20 +185,19 @@ final public class Studyplus {
213185 /// The valid URL has a __[studyplus-{consumerKey}]__ scheme, and right pathComponents and host.
214186 /// 渡されたurlがStudyplusSDKで対応すべきURLであれば true、それ以外は false を返します。
215187 /// __[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) )
220191 return false
221192 }
222193
223- switch appDelegateUrl . pathComponents [ 1 ] {
194+ switch url . pathComponents [ 1 ] {
224195 case " success " :
225- let accessToken : Data = appDelegateUrl
196+ let accessToken : Data = url
226197 . pathComponents [ 2 ]
227198 . trimmingCharacters ( in: . whitespacesAndNewlines)
228199 . data ( using: . utf8, allowLossyConversion: false ) !
229- let username : Data = appDelegateUrl
200+ let username : Data = url
230201 . pathComponents [ 3 ]
231202 . trimmingCharacters ( in: . whitespacesAndNewlines)
232203 . data ( using: . utf8, allowLossyConversion: false ) !
@@ -250,24 +221,13 @@ final public class Studyplus {
250221 if statusAccessToken == noErr && statusUsername == noErr {
251222 delegate? . studyplusDidSuccessToLogin ( )
252223 } else {
253- delegate? . studyplusDidFailToLogin ( error: . unknownReason ( " Could not access Keychain. " ) )
224+ delegate? . studyplusDidFailToLogin ( error: . keychainError )
254225 }
255226 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)
263228 case " cancel " :
264-
265- delegate? . studyplusDidCancelToLogin ( )
266-
229+ delegate? . studyplusDidFailToLogin ( error: . cancel)
267230 default :
268- #if DEBUG
269- print ( " StudyplusSDK: Unknown format: \( appDelegateUrl. absoluteString) " )
270- #endif
271231 return false
272232 }
273233
@@ -291,7 +251,6 @@ final public class Studyplus {
291251 /// - consumerKey: consumer key
292252 /// - consumerSecret: consumer secret
293253 public func change( consumerKey: String , consumerSecret: String ) {
294-
295254 self . consumerKey = consumerKey
296255 self . consumerSecret = consumerSecret
297256 }
@@ -352,7 +311,6 @@ final public class Studyplus {
352311 }
353312
354313 private func isAcceptableURL( url: URL ) -> Bool {
355-
356314 guard let host = url. host else { return false }
357315 guard host == " auth-result " || host == " login-result " else { return false }
358316
0 commit comments