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

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

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

2016/02/26 Kotlin Birthday Party #jkug

Shinya Kumagai

February 26, 2016
Tweet

Other Decks in Programming

Transcript

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

    GitHub - @droibit • λΦιϑτ΢ΣΞגࣜձࣾ
  2. ֓ཁ • connpass API ༻ͷRetrofit ServiceΛର৅ - JavaͰఆٛ͞ΕͨService - KotlinͰServiceΛఆٛ

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

    … ݁Ռͷදࣔॱ (1:ߋ৽ॱ, 2…) - ymd … Πϕϯτ։࠵೔ (yyyymmdd) • Ϩεϙϯε͸JSON ?keyword=kotlin&order=1&ymd=20150226 IUUQDPOOQBTTDPNBCPVUBQJ
  4. 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);
  5. // ֦ுؔ਺Λఆٛ + σϑΥϧτҾ਺ 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Ͱվળ
  6. KotlinͰServiceΛఆٛ interface ConnpassService { // ※ @Get,@Query ͸লུ fun searchEvent(keyword:

    String, order: Order? = null, ymd: String? = null): Response } // ΩʔϫʔυҎ֎͸ඞཁͳΫΤϦͷΈ service.searchEvent(“kotlin”, order = Order.UPDATE)
  7. // ༻్ʹ߹Θ֦ͤͨுؔ਺Λఆٛ 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) վળྫ