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

Inside Android KTX

Inside Android KTX

Talk given at Kotlin Night 2018 Milan, A Kotlin Perspective and Droidcon Turin 2018

Roberto Orgiu

March 12, 2018
Tweet

More Decks by Roberto Orgiu

Other Decks in Programming

Transcript

  1. Rob

  2. animator = ObjectAnimator.ofFloat(view, View.ALPHA, 0f, 1f) animator.doOnStartT{ ... }T animator.doOnEndT{

    ... }T animator.doOnCancelT{ ... }T animator.doOnRepeatT{ ... }T animator.doOnPauseT{ ... }T animator.doOnResumeT{ ... }T
  3. animatorT= ObjectAnimator.ofFloat(view, View.ALPHA, 0f, 1f) animator.doOnStartT{ ... }T animator.doOnEndT{ ...

    }T animator.doOnCancelT{ ... }T animator.doOnRepeatT{ ... }T animator.doOnPauseT{ ... }T animator.doOnResumeT{ ... }T Animator.doOnStart
  4. fun Animator.addListener(T onEnd:T((animator:TAnimator)T->TUnit)?T=Tnull,T onStart:T((animator:TAnimator)T-> Unit)?T=Tnull,T onCancel:T((animator:TAnimator)T-> Unit)?T=Tnull,T onRepeat:T((animator:TAnimator)T-> Unit)?T=TnullT ):TAnimator.AnimatorListenerT{T

    valTlistenerT=TobjectT:TAnimator.AnimatorListenerT{TT overrideTfunTonAnimationRepeat(animator:TAnimator)T{T onRepeat?.invoke(animator) }T overrideTfunTonAnimationEnd(animator:TAnimator)T{T onEnd?.invoke(animator) }T overrideTfunTonAnimationCancel(animator:TAnimator)T{T onCancel?.invoke(animator) }T overrideTfunTonAnimationStart(animator:TAnimator)T{T onStart?.invoke(animator)
  5. onRepeat:T((animator:TAnimator)T-> Unit)?T=TnullT ):TAnimator.AnimatorListenerT{T valTlistenerT=TobjectT:TAnimator.AnimatorListenerT{TT overrideTfunTonAnimationRepeat(animator:TAnimator)T{T onRepeat?.invoke(animator) }T overrideTfunTonAnimationEnd(animator:TAnimator)T{T onEnd?.invoke(animator) }T

    overrideTfunTonAnimationCancel(animator:TAnimator)T{T onCancel?.invoke(animator) }T overrideTfunTonAnimationStart(animator:TAnimator)T{T onStart?.invoke(animator) }T }T addListener(listener) returnTlistener }T
  6. fun Animator.addListener(T onEnd:T((animator:TAnimator)T->TUnit)?T=Tnull,T onStart:T((animator:TAnimator)T-> Unit)?T=Tnull,T onCancel:T((animator:TAnimator)T-> Unit)?T=Tnull,T onRepeat:T((animator:TAnimator)T-> Unit)?T=TnullT ):TAnimator.AnimatorListenerT{T

    valTlistenerT=TobjectT:TAnimator.AnimatorListenerT{TT overrideTfunTonAnimationRepeat(animator:TAnimator)T{T onRepeat?.invoke(animator) }T overrideTfunTonAnimationEnd(animator:TAnimator)T{T onEnd?.invoke(animator) }T overrideTfunTonAnimationCancel(animator:TAnimator)T{T onCancel?.invoke(animator) }T overrideTfunTonAnimationStart(animator:TAnimator)T{T onStart?.invoke(animator) }T }T addListener(listener) returnTlistener }T
  7. context.withStyledAttributes(…){…}T context.withStyledAttributes( inline fun Context.withStyledAttributes( ... attrs: IntArray, ... block:

    TypedArray.() -> Unit ) { val typedArray = obtainStyledAttributes(...) try { typedArray.block() } finally { typedArray.recycle() } }
  8. inline fun Context.withStyledAttributes( ... attrs: IntArray, ... block: TypedArray.() ->

    Unit ) { val typedArray = obtainStyledAttributes(...) try { typedArray.block() } finally { typedArray.recycle() } } context.withStyledAttributes(…){…}T context.withStyledAttributes(
  9. inline operator fun @receiver:ColorInt Int.component1() = (this shr 24) and

    0xff inline operator fun @receiver:ColorInt Int.component2() = (this shr 16) and 0xff inline operator fun @receiver:ColorInt Int.component3() = (this shr 8) and 0xff inline operator fun @receiver:ColorInt Int.component4() = this and 0xff valT(a,Tr,Tg,Tb)T=T0x337f3010
  10. inline operator fun @receiver:ColorInt Int.component1() = (this shr 24) and

    0xff inline operator fun @receiver:ColorInt Int.component2() = (this shr 16) and 0xff inline operator fun @receiver:ColorInt Int.component3() = (this shr 8) and 0xff inline operator fun @receiver:ColorInt Int.component4() = this and 0xff valT(a,Tr,Tg,Tb)T=T0x337f3010
  11. inline operator fun @receiver:ColorInt Int.component1() = (this shr 24) and

    0xff inline operator fun @receiver:ColorInt Int.component2() = (this shr 16) and 0xff inline operator fun @receiver:ColorInt Int.component3() = (this shr 8) and 0xff inline operator fun @receiver:ColorInt Int.component4() = this and 0xff valT(a,Tr,Tg,Tb)T=T0x337f3010
  12. inline operator fun @receiver:ColorInt Int.component1() = (this shr 24) and

    0xff inline operator fun @receiver:ColorInt Int.component2() = (this shr 16) and 0xff inline operator fun @receiver:ColorInt Int.component3() = (this shr 8) and 0xff inline operator fun @receiver:ColorInt Int.component4() = this and 0xff valT(a,Tr,Tg,Tb)T=T0x337f3010
  13. inline operator fun @receiver:ColorInt Int.component1() = (this shr 24) and

    0xff inline operator fun @receiver:ColorInt Int.component2() = (this shr 16) and 0xff inline operator fun @receiver:ColorInt Int.component3() = (this shr 8) and 0xff inline operator fun @receiver:ColorInt Int.component4() = this and 0xff valT(a,Tr,Tg,Tb)T=T0x337f3010
  14. valT(a,Tr,Tg,Tb)T=T0x337f3010 inline operator fun @receiver:ColorInt Int.component1()T= (thisTshrT24)TandT0xff inline operator fun

    @receiver:ColorInt Int.component2()T= (thisTshrT16)TandT0xff inline operator fun @receiver:ColorInt Int.component3()T= (thisTshrT8)TandT0xff inline operator fun @receiver:ColorInt Int.component4()T= thisTandT0xff
  15. valT(a,Tr,Tg,Tb)T=T0x337f3010 operatorTfun Int.component1() = (thisTshrT24) and 0xff operatorTfun Int.component2()T= (thisTshrT16)TandT0xff

    operatorTfun Int.component3()T= (thisTshrT8)TandT0xff operatorTfun Int.component4()T= thisTandT0xff
  16. val path = path1 + path2 inline operator fun Path.plus(p:

    Path): Path = Path(this).apply { op(p, Path.Op.UNION) }
  17. val path = path1 + path2 val path = path1

    and path2 inline infix fun Path.and(p: Path) = this + p
  18. varTcalledT=T0T handler.postDelayed(10)T{T called++ }T inline fun Handler.postDelayed( crossinline action: ()

    -> Unit) = Runnable { action() } fun foo() { ordinaryFunction { return } } ❌
  19. varTcalledT=T0T handler.postDelayed(10)T{T called++ }T inline fun Handler.postDelayed( crossinline action: ()

    -> Unit) = Runnable { action() } fun foo() { ordinaryFunction { return@ordinaryFunction } }
  20. varTcalledT=T0T handler.postDelayed(10)T{T called++ }T inline fun Handler.postDelayed( crossinline action: ()

    -> Unit) = Runnable { action() } fun foo() { ordinaryFunction f1@{ return@f1 } }
  21. varTcalledT=T0T handler.postDelayed(10)T{T called++ }T inline fun Handler.postDelayed( crossinline action: ()

    -> Unit) = Runnable { action() } fun foo() { inlineFunction { return } } ✅
  22. varTcalledT=T0T handler.postDelayed(10)T{T called++ }T inline fun Handler.postDelayed( crossinline action: ()

    -> Unit) = Runnable { action() } fun hasZeros(ints: List<Int>): Boolean { ints.forEach { if (it == 0) return true } return false }
  23. fun f1()T{T f2 {T return //ERROR return@f2 }T f3 {T

    return //f1 return@f3 }T f4 {T return //ERROR return@f4 }T }T
  24. fun f2(f: ()T-> Unit) inline fun f3(f: ()T-> Unit) inline

    fun f4(crossinline f: ()T-> Unit) fun f1()T{T f2 {T return //ERROR return@f2 }T f3 {T return //f1 return@f3 }T f4 {T return //ERROR return@f4 }T }T
  25. fun f2(f: ()T-> Unit) inline fun f3(f: ()T-> Unit) inline

    fun f4(crossinline f: ()T-> Unit) fun f1()T{T f2 {T return //ERROR return@f2 }T f3 {T return //f1 return@f3 }T f4 {T return //ERROR return@f4 }T }T
  26. fun f2(f: ()T-> Unit) inline fun f3(f: ()T-> Unit) inline

    fun f4(crossinline f: ()T-> Unit) fun f1()T{T f2 {T return //ERROR return@f2 }T f3 {T return //f1 return@f3 }T f4 {T return //ERROR return@f4 }T }T
  27. funTView.update(T callback: ViewPropertyAnimator.()T->TViewPropertyAnimator )T{T animate()T .setDuration(200)T .callback()T .start()T }T fun

    View.show() { update { alphaBy(0f) .alpha(1f) } } funTView.hide()T{T updateT{T scaleXBy(1f) .scaleYBy(1f) .scaleX(0f) .scaleY(0f) }T }T
  28. funTView.update(T callback: ViewPropertyAnimator.()T->TViewPropertyAnimator )T{T animate()T .setDuration(200)T .callback()T .start()T }T fun

    View.show() { update { alphaBy(0f) .alpha(1f) } } funTView.hide()T{T updateT{T scaleXBy(1f) .scaleYBy(1f) .scaleX(0f) .scaleY(0f) }T }T