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

Google I/O 2018’s Extensions🦔

Hunachi
September 20, 2018

Google I/O 2018’s Extensions🦔

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

Hunachi

September 20, 2018
Tweet

More Decks by Hunachi

Other Decks in Technology

Transcript

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

    block() // ͙͢ʹbinding͍ͤͨ࣌͞ʹ࢖͏ɽ executePendingBindings() } // ࢖༻ྫ binding.executeAfter { //͜͜Ͱbind͢Δɽ event = item } ᶃ
  2. // ॳظԽɼࢀর͕ৗʹγϯάϧεϨουͰ͔͠ߦΘΕͳ͍࣌༻ɽ 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) } ᶅ
  3. 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) ᶆ
  4. 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) ) ᶇ
  5. inline fun FragmentManager.inTransaction( func: FragmentTransaction.() -> FragmentTransaction ) { beginTransaction().func().commit()

    } // ࢖༻ྫ supportFragmentManager.inTransaction { add(FRAGMENT_ID, fragment) } ᶈ
  6. // 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 ᶉ
  7. 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ଆͰ൑ఆͨ͠ํ͕҆શͳؾ͕͢Δ͚ͲͲ͏ͳΜͩΖ͏ɽɽ ᶊ
  8. 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) } ᶋ
  9. // ࢖༻ྫ liveData = parentLiveData.map { (it as? Result.Success)?.data?: 0

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

    OK when (sealed) { ONE -> {…} TWO -> {…} }.checkAllMatch // error when (sealed) { ONE -> {…} //TWO -> {…} }.checkAllMatch ᶌ