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

Google I/O 2018’s Extensions🦔

30aa1a1587afd7e895108d0295f62434?s=47 Hunachi
September 20, 2018

Google I/O 2018’s Extensions🦔

Kotlin大好き!Kotlin愛好会 vol4で発表しました.
是非Googleさんを参考にしましょう!

30aa1a1587afd7e895108d0295f62434?s=128

Hunachi

September 20, 2018
Tweet

Transcript

  1. ˏ)VOBDIJ ,PUMJOѪ޷ձWPM (PPHMF*0`T &YUFOTJPOT

  2. ࣗݾ঺հ ;ͳͪ ߴઐ೥ੜ (JU)VC!)VOBDIJ 5XJUUFS!@IVOBDIJ ,PUMJOΛॻ͖࢝Ί͔ͯΒϓϩάϥϛϯά͕޷͖ʹͳͬͨɽ ࠓ͸8BOUFEMZ*OD͞ΜͷݩͰΠϯλʔϯதͰ͢ʗʢ,PUMJOಋೖ͠·ͨ͠ʣ

  3. $PEFɿIUUQTHJUIVCDPNHPPHMFJPTDIFE 0⒏DJBM#MPHɿIUUQTNFEJVNDPNBOESPJEEFWFMPQFSTHPPHMFJPBQQ BSDIJUFDUVSFBOEUFTUJOHGFGDFC (PPHMF*0BQQ

  4. w ,PUMJO w ࠓ೥͸ྫ೥ΑΓσεϚͯ͠ͳͦ͞͏ ʢ$POUSJCVUJPO਺͚ͩͰ͸ݴ͑ͳ͍͔΋ ͚ͩͲʣ

  5. 6TFE5PPMBOE-JCSBSZ w 'JSF4UPSFɼ'JSF#BTF"VUIFOUJDBUJPO w "OESPJEY w %BHHFSɼ5JNCFSɼ(TPOɼ5ISFF5FOɼ$SBTIMZUJDTɼ(MJEFɼ&TQSFTTPɼ .PDLJUPɼ+6OJUɼMFBLDBOBSZɽ

  6. "SDIJUFDUVSF w $MFBO"SDIJUFDUVSF w 1SFTFOUBUJPOɿ7JFXˍ7JFX.PEFM w %PNBJOɿ6TF$BTF w %BUBɿ3FQPTJUPSZ 6TFS.BOBHFS

    w .77.
  7. None
  8. &YUFOTJPOT /PUBMM FYUFOTJPOTPG NZGBWPSJUF

  9. // Listͱ͔ͰView͕ΨλϯͬͯͳͬͨΓ͢ΔͷΛ๷͙༻ɽ inline fun <T : ViewDataBinding> T.executeAfter(block: T.() ->Unit){

    block() // ͙͢ʹbinding͍ͤͨ࣌͞ʹ࢖͏ɽ executePendingBindings() } // ࢖༻ྫ binding.executeAfter { //͜͜Ͱbind͢Δɽ event = item } ᶃ
  10. // TransitionΛ஗Ԇͤ͞Δ fun Activity.postponeEnterTransition(timeout: Long) { postponeEnterTransition() window.decorView.postDelayed(timeout) { startPostponedEnterTransition()

    } } // ࢖༻ྫ activity?.postponeEnterTransition(500L) ᶄ
  11. // ॳظԽɼࢀর͕ৗʹγϯάϧεϨουͰ͔͠ߦΘΕͳ͍࣌༻ɽ fun <T> lazyFast(operation: () -> T): Lazy<T> =

    lazy(LazyThreadSafetyMode.NONE) { operation() } // SYNCRONIZED: σϑΥϧτɽ୯ҰεϨου͔ΒͷॳظԽͰɼෳ਺εϨου͔Βͷࢀর͕Մೳɽ // PUBLICATION: ෳ਺εϨου͔ΒॳظԽ͠Α͏ͱ͢Δࣄ͕Մೳɽ // ࢖༻ྫ private val day: Int by lazyFast { val args = arguments ?: throw IllegalStateException("Missing arguments!") args.getInt(ARG_INT) } ᶅ
  12. inline fun <reified VM : ViewModel> FragmentActivity.viewModelProvider( provider: ViewModelProvider.Factory )

    = ViewModelProviders.of(this, provider).get(VM::class.java) // ࢖༻ྫ // Before val viewModel: HogeViewModel = ViewModelProviders.of(this, viewModelFactory) .get(HogeViewModel::class.java) //After val viewModel: HogeViewModel = viewModelProvider(viewModelFactory) ᶆ
  13. fun ViewGroup.inflate( @LayoutRes layout: Int, attachToRoot: Boolean = false ):

    View { return LayoutInflater.from(context) .inflate(layout, this, attachToRoot) } // ࢖༻ྫ … Holder( parent.inflate(VIEW_TYPE_HEADING, false) ) ᶇ
  14. inline fun FragmentManager.inTransaction( func: FragmentTransaction.() -> FragmentTransaction ) { beginTransaction().func().commit()

    } // ࢖༻ྫ supportFragmentManager.inTransaction { add(FRAGMENT_ID, fragment) } ᶈ
  15. // enumΫϥεΛbundleʹೖΕΕΔܗʹ͢Δ fun <T : Enum<T>> Bundle.putEnum(key: String, value: T)

    = putString(key, value.name) // ↑ͰೖΕͨenumΫϥεΛऔΓग़͢ inline fun <reified T : Enum<T>> Bundle.getEnum(key: String): T = enumValueOf(getString(key)) // ParcelʹbooleanΛॻ͖ࠐΉ fun Parcel.writeBoolean(value: Boolean) = writeInt(if (value) 1 else 0) // ↑Ͱม׵ͨ͠booleanΛಡΈࠐΉ fun Parcel.readBoolean() = readInt() != 0 ᶉ
  16. // ࢖༻ྫ // ಡΈ΍͍͢ʂ pacel.apply { writeInt(state) writeInt(peekHeight) writeBoolean(isFitToContents) writeBoolean(isHideable)

    writeBoolean(skipCollapsed) writeBoolean(isDraggable) }
  17. fun <T> MutableLiveData<T>.setValueIfNew(newValue: T) { if (this.value != newValue) value

    = newValue } fun <T> MutableLiveData<T>.postValueIfNew(newValue: T) { if (this.value != newValue) postValue(newValue) } // ObserverଆͰ൑ఆͨ͠ํ͕҆શͳؾ͕͢Δ͚ͲͲ͏ͳΜͩΖ͏ɽɽ ᶊ
  18. fun <X, Y> LiveData<X>.map(body: (X) -> Y): LiveData<Y> { return

    Transformations.map(this, body) } fun <X, Y> LiveData<X>.switchMap(body: (X) -> LiveData<Y>): LiveData<Y> { return Transformations.switchMap(this, body) } ᶋ
  19. // ࢖༻ྫ liveData = parentLiveData.map { (it as? Result.Success)?.data?: 0

    } liveData = parentLiveData.switchMap { if(value == null) dataLiveData else parentLiveData } // ࠷ॳ֦ுؔ਺ͬͯࣄʹؾ͔ͮͣɼڻ͍ͨɽ
  20. val <T> T.checkAllMatched: T get() = this // ࢖༻ྫ //

    OK when (sealed) { ONE -> {…} TWO -> {…} }.checkAllMatch // error when (sealed) { ONE -> {…} //TWO -> {…} }.checkAllMatch ᶌ
  21. w ௒ஸೡʂ ͜ͷ࣮૷͸७ਮʹ಄ ͍͍ͱࢥ͏ɽ

  22. 5IBOLZPV ,PUMJOՄѪ͍ʂ