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

Kotlinを使って思った10のコト fukuoka.kt #1

Kotlinを使って思った10のコト fukuoka.kt #1

chocomelonchan

July 05, 2017
Tweet

More Decks by chocomelonchan

Other Decks in Programming

Transcript

  1. KotlinΛ࢖ͬͯࢥͬͨ10ͷίτ ࡾ௡ࢁ խن (chocomelon) 2017/0705 fukuoka.kt #1

  2. ࣗݾ঺հ ࡾ௡ࢁ խن (chocomelon) ωΠςΟϒΞϓϦΛओʹ୲౰ 2013೥ϐΫγϒೖࣾ Kotlinྺ 3िؒ

  3. ฐࣾͷKotlin࠾༻ঢ়گ

  4. pixivʢJavaʣ

  5. pixiv ίϛοΫʢJava & Kotlinʣ

  6. pixiv SketchʢJavaɺKotlinʹ͍ͨ͠ʣ

  7. PawooʢJavaɺKotlinʹ͍ͨ͠ʣ

  8. ෱ԬͰ৽ن։ൃதͷΞϓϦ => Kotlin

  9. ෱ԬͰ৽ن։ൃதͷΞϓϦ ɾKotlin ɾAndroid Architecture Components ɹɾViewModel, LiveData, Room ɾDataBinding ɾRxJava/Android

  10. KotlinΛ࢖ͬͯࢥ͏10ͷίτ

  11. 1. Extension͕͢͹Β͍͠ fun ImageView.setImageUrl(imageUrl: String?) { if (imageUrl == null

    || !URLUtil.isValidUrl(imageUrl)) { return } Glide.with(context).load(imageUrl).into(this) }
  12. 2. ؔ਺ܕ௒࠷ߴ͔Α arrayOf(1, 2, 3, 4, 5) // 1, 2,

    3, 4, 5 .filter { it % 2 == 0 } // 2, 4 .map {it * 2 } // 4, 8 .sum() // 12
  13. 3. σϑΥϧτ஋Ҿ/਺͕ศར fun hoge(fugaString: String, fooInt: Int = 0, barBoolean:

    = true) { // Do something } val fuga: Int = optionalVal1 ?: 5
  14. 4. when͕εϚʔτ viewModel?.getDataSourceState()?.observe(this, Observer { when (it) { DataSourceState.SUCCESS ->

    infoView.setType(InfoView.Type.HIDE) DataSourceState.LOADING -> infoView.setType(InfoView.Type.LOADING) DataSourceState.EMPTY_DATA -> infoView.setType(InfoView.Type.NOTFOUND) DataSourceState.ERROR -> infoView.setType( InfoView.Type.ERROR, View.OnClickListener { reload() }) } })
  15. 5. SmartCast͕ྑ͍ fun hoge(optionalVal1: String?, optionalVal2: String?) { if (optionalVal1

    != null && optionalVal2 != null) { val hoge = optionalVal1 + optionalVal2 // Do something } } if (object is Person) { print(object.name) }
  16. 6. είʔϓؔ਺͕ศར class HogeActivity: AppCompatActivity() { companion object { val

    BUNDLE_KEY_HOGE_INT = "HOGE_INT" fun createIntent(context: Context, hogeInt: Int): Intent = Intent(context, HogeActivity::class.java).apply { putExtra(BUNDLE_KEY_HOGE_INT, hogeInt) } } }
  17. 7. let let ͕Ϝζ͍ optionalVal1?.let { optionalVal1 -> optionalVal2?.let {

    optionalVal2 -> // Do something } }
  18. 7. let let ͕Ϝζ͍ if (optionalVal1 != null || optionalVal2

    != null) { return } // Do something
  19. 7. let let ͕Ϝζ͍ optionalVal1 ?: optionalVal2 ?: return //

    Do something
  20. 7. let let ͕ຊ౰ʹϜζ͍ inline fun <S, T, R> let(s:

    S?, t: T?, block: (s: S?, t: T?) -> R) { if (s != null && t != null) { block(s, t) } } let(optionalVal1, optionalVal2) { optionalVal1, optionalVal2 -> // Do something }
  21. 8. Gson࢖͍ͬͯΔ৔߹͸஫ҙ✋ ɾNonNullͷobjectʹNull͕ೖͬͯ͘Δ ɾύʔε࣌͸Θ͔Β࣮ͣߦ࣌ʹࢮ͵ ɾmoshi࢖͓͏ https://github.com/square/moshi

  22. 9. AndroidΤϯδχΞ͕΍Δؾ ɾؓࢄͱͯͨ͠Slack͕Kotlinͷॻ͖ํٞ࿦ ɹͰ੝Γ্͕Δ ɾࢼߦࡨޡָ͕͍͠ ɾ͙͢͸͡ΊΒΕΔ => Ұ෦Kotlinʹม͑ͯ΋ྑ͍͠ɺ ɹ ࣗಈม׵΋ͦͦ͜͜༗ೳ

  23. 10. iOSͷΤϯδχΞ͕΍Δؾ ɾʮKotlinͩͬͨΒྑ͍Ͱ͢Αʯ ɾʮSwiftͱࣅͯΔ͡ΌΜʯ ɾʮָͦ͠͏ʢখฒʣʯ

  24. ෱ԬΦϑΟεͰΤϯδχΞืूத ڵຯ͕͋Δํ͸·ͣϥϯνͰ΋͠·͠ΐ͏ʂ https://www.wantedly.com/projects/90433