Upgrade to Pro — share decks privately, control downloads, hide ads and more …

KotlinでRetrofitを使いやすくするちょっとした工夫

 KotlinでRetrofitを使いやすくするちょっとした工夫

2016/02/26 Kotlin Birthday Party #jkug

307f675644572d96886ccc8c8303707c?s=128

Shinya Kumagai

February 26, 2016
Tweet

Transcript

  1. KotlinͰRetrofitΛ ࢖͍΍͘͢͢Δ ͪΐͬͱͨ͠޻෉ 2015/02/26 Shinya Kumagai(@droibit)

  2. About Me • Shinya Kumagai (۽୩ ৻໵) 25ࡀ • Twitter,

    GitHub - @droibit • λΦιϑτ΢ΣΞגࣜձࣾ
  3. Happy Birthday Kotlin!! (ɾ8ɾ)

  4. ֓ཁ • connpass API ༻ͷRetrofit ServiceΛର৅ - JavaͰఆٛ͞ΕͨService - KotlinͰServiceΛఆٛ

    KotlinͰ࢖͏ͱ͖ͷͪΐͬͱͨ͠޻෉ IUUQTRVBSFHJUIVCJPSFUSPpU
  5. Connpass API • ݕࡧΫΤϦ - keyword … Ωʔϫʔυ - order

    … ݁Ռͷදࣔॱ (1:ߋ৽ॱ, 2…) - ymd … Πϕϯτ։࠵೔ (yyyymmdd) • Ϩεϙϯε͸JSON ?keyword=kotlin&order=1&ymd=20150226 IUUQDPOOQBTTDPNBCPVUBQJ
  6. JavaͰఆٛ͞ΕͨService public interface ConnpassService { // ※ @Get, @Query ͸লུ

    Response searchEvent(@NonNull String keyword, @Nullable Integer order, @Nullable String ymd); } // શΫΤϦΛࢦఆ service.searchEvent(“kotlin”, 1, "20160226"); // ΩʔϫʔυͷΈͷ৔߹ service.searchEvent("kotlin", null, null);
  7. // ֦ுؔ਺Λఆٛ + σϑΥϧτҾ਺ fun ConnpassService.searchEventExt( keyword: String, order: Order?

    = null, ymd: String? = null ) = searchEvent(keyword, order?.index, ymd) // ݕࡧ݁Ռͷදࣔॱͷྻڍମ enum class Order(val index: Int) { UPDATE(1), EVENT(2), NEW(3); override fun toString() = "$index" } // ΩʔϫʔυҎ֎͸ඞཁͳΫΤϦͷΈ service.searchEventExt("kotlin", order = Order.UPDATE) KotlinͰվળ
  8. KotlinͰServiceΛఆٛ interface ConnpassService { // ※ @Get,@Query ͸লུ fun searchEvent(keyword:

    String, order: Order? = null, ymd: String? = null): Response } // ΩʔϫʔυҎ֎͸ඞཁͳΫΤϦͷΈ service.searchEvent(“kotlin”, order = Order.UPDATE)
  9. // ༻్ʹ߹Θ֦ͤͨுؔ਺Λఆٛ fun ConnpassService.searchEvent( keyword: String, order: Order? = null,

    ymd: Date? = null ) = searchEvent(keyword, order, format(ymd)) // DateΛ yyyymmdd ϑΥʔϚοτͷStringʹม׵ fun format(date: Date?): String? = … // ։࠵೔ʹ͸DateΦϒδΣΫτΛࢦఆ service.searchEvent(“kotlin", ymd = dateFromTimesSquare) վળྫ
  10. ·ͱΊ • ൚༻తͳΠϯλʔϑΣʔεʹͳΔ͹͍͋͸ɺ σϑΥϧτҾ਺ɺ֦ுؔ਺Ͱγϯϓϧͳ Service΁ fabricͷΑ͏ʹ͸ͳΒͳ͍Α͏ʹؾΛ͚ͭ·͠ΐ͏

  11. ͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠