Share
Embed
Kotlin Multiplatform Projectで社内用APIクライアントを作る
potatochips #66 で発表
Kotlin Multiplatform Projectで社内用APIクライアントを作る
k-tomoyasu
November 11, 2019
Transcript
15 slides
-
࡞ΔʹࢸΔ·Ͱ • ࣾͷAPIͰAndroid, iOSΞϓϦΛ࡞ΔϓϩδΣΫ τΛ༗ࢤͰ࢝Ίͨ • ೝূΛ͡Ίͱͯ͠APIͷ༷͕݁ߏ͍͜͠ • ͜͜ΛΫϦΞ͢Δͱ։ൃʹͣΈΛ͚ͭΒΕͦ͏ •
APIΫϥΠΞϯτΛ༻ҙ͍͚ͨ͠ͲAndroid, iOS྆ ํ࡞Δͷ͠ΜͲ͍ -
,PUMJO.VMUJQMBUGPSN1SPKFDU • KotlinͰͷΫϩεϓϥοτϑΥʔϜ։ൃ • UIڞ௨ԽͤͣϩδοΫ෦ͷΈڞ௨Խ͢Δ • JVM, Native, javascript •
ݱ࣌ͰExperimental feature • ུশMPP? KMP? https://speakerdeck.com/kpgalligan/kotlin-multiplatform-libraries -
ϓϥοτϑΥʔϜݻ༗ͷ࣮ • expect / actual (≒ abstract / concrete) ͰϓϥοτϑΥʔϜݻ༗ͷ࣮͕Մೳ
• expectͰϓϥοτϑΥʔϜ͝ͱͷ࣮Λཁٻ internal expect val platform : String • actualͰϓϥοτϑΥʔϜ͝ͱʹ࣮ • Android internal actual val platform = “ANDROID” • iOS internal actual val platform = "IOS" • ࠓճͷAPIΫϥΠΞϯτͰiOS / AndroidͰϦΫΤετͷ༰ม͍͑ͨՕॴ ͳͲͰར༻ -
4XJGU͔Βݟͨ"1*ΫϥΠΞϯτ • Kotlin͔ΒframeworkΛग़ྗͰ͖Δ • suspend functionframeworkʹग़ྗ͞Εͳ͍ suspend fun InhouseClient.login(email: String,
password: String): Response<LoginSuccess, LoginFail> { return post<LoginSuccess, LoginFail>(email, password) } • Swift༻ʹDefferedͰฦ͢Α͏ʹ͢Δ fun InhouseClient.loginAsync(email: String, password: String): Deferred<Response<LoginSuccess, LoginFail>> { return GlobalScope.async(coroutineDispatcher) { login(email, password) } } -
4XJGU͔Β"1*ΫϥΠΞϯτΛ͏ import InhouseClient let deferred = client.loginAsync(email: "[email protected]",password:"password") deferred.invokeOnCompletion(cause in
let response = deferred.getCompleted() if let result = response as? Response.Success { if let success = result as! LoginSuccess { doSomething(user: success.user) } } }) • APIΫϥΠΞϯτΛimport • Deffered͔ΒgetCompleted()Ͱ݁ՌΛऔΓग़͢ • getCompleted()͕Any?Λฦ͢ͷͰΩϟετ -
4XJGU༻ͷϥούʔΛ࡞Δ • ͜ͷ··ͩͱݺͼग़͠ଆ͕ΫϥΠΞϯτͷ࣮ͷৄࡉΛ͍ͬͯΔඞཁ͕͋Δ • Swift༻ͷϥούʔΛ࡞Δ͜ͱʹͨ͠ • ܕΩϟετΛ୲͏ • Future<Output, Failure>Ͱฦ͢Α͏ʹ͢Δ
• ΞϓϦͷ࣮ͰSwiftUI͏ͱͷ͜ͱͩͬͨͷͰCombine͍ͬͯ͘ • droidkaigi2019ͷΞϓϦͰRxSwiftͷasSingleʹͯ͠ฦ͍ͯ͠Δͷ͕ࢀߟ ʹͳͬͨ https://qiita.com/takahirom/items/4df7bfca17928a40922e -
'VUVSFΛฦ͢FYUFOTJPOΛ༻ҙ extension Kotlinx_coroutines_coreDeferred { func asFuture<Success, Fail>(success: Success.Type, error: Fail.Type)
-> Future<Success, Fail> { return Future<Success, Fail> { promise in self.invokeOnCompletion( cause in if let cause = cause { promise(.failure(Fail(cause))) return } if let completion = self.getCompleted() as? Response.Success { let result = completion as! Success.Type promise(.success(result)) } } )} } -
'VUVSFΛฦ͢Α͏ʹ͍ͯ͘͠ public func login(email: String, password: String) -> Future<LoginSuccess, LoginFail>
{ return client .loginAsync(email: email, password: password) .asFuture(success: LoginSuccess.self, error: LoginiFail.self) } -
ݺͼग़͠ଆ#FGPSF"GUFS • Before import InhouseClient let deferred = client.loginAsync(email: "[email protected]",password:"password")
deferred.invokeOnCompletion(cause in let response = deferred.getCompleted() if let result = response as? Response.Success { if let success = result as! LoginSuccess { doSomething(user: success.user) } } }) • After import InhouseClientSwifty client.login(email: "[email protected]",password: "password") .sink(receiveValue: { value in doSomething(user: value.user) }, receiveCompletion { completion in handle(completion) })