Slide 1

Slide 1 text

Inside Android KTX

Slide 2

Slide 2 text

Rob

Slide 3

Slide 3 text

Such magic Much tech NOPE

Slide 4

Slide 4 text

animator = ObjectAnimator.ofFloat(view, View.ALPHA, 0f, 1f)

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

funTAnimator.doOnStart(action:T(anim:JAnimator)T->KUnit) T= addListener(onStartT=Taction) animator.doOnStartT{ ... }

Slide 8

Slide 8 text

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)

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

val actual = context.systemService()

Slide 12

Slide 12 text

val actual = context.systemService() @RequiresApi(23) inline fun Context.systemService() = getSystemService(T::class.java)

Slide 13

Slide 13 text

val actual = context.systemService() @RequiresApi(23) inline fun Context.systemService() = getSystemService(T::class.java)

Slide 14

Slide 14 text

val actual = context.systemService() @RequiresApi(23) inline fun Context.systemService() = getSystemService(T::class.java)

Slide 15

Slide 15 text

val actual = context.systemService() @RequiresApi(23) inline fun Context.systemService() = getSystemService(T::class.java) inline reified

Slide 16

Slide 16 text

inline reified Body is copied over at compile time Type passed is preserved

Slide 17

Slide 17 text

inline reified Body is copied over at compile time Type passed is preserved

Slide 18

Slide 18 text

inline reified Body is copied over at compile time Type passed is preserved

Slide 19

Slide 19 text

context.withStyledAttributes(T ... intArrayOf(android.R.attr.textColorPrimary) )T{ val resourceId = getResourceId(0, -1) ... }T context.withStyledAttributes(

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

valT(a,Tr,Tg,Tb)T=T0x337f3010

Slide 23

Slide 23 text

valT(a,Tr,Tg,Tb)T=T0x337f3010

Slide 24

Slide 24 text

valT(a,Tr,Tg,Tb)T=T0x337f3010

Slide 25

Slide 25 text

valT(a,Tr,Tg,Tb)T=T0x337f3010

Slide 26

Slide 26 text

valT(a,Tr,Tg,Tb)T=T0x337f3010

Slide 27

Slide 27 text

valT(a,Tr,Tg,Tb)T=T0x337f3010

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

valT(a,Tr,Tg,Tb)T=T0x337f3010 operatorTfun Int.component1()T= (thisTshrT24)TandT0xff operatorTfun Int.component2()T= (thisTshrT16)TandT0xff operatorTfun Int.component3()T= (thisTshrT8)TandT0xff operatorTfun Int.component4()T= thisTandT0xff

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

valT(a,Tr,Tg,Tb)T=T0x337f3010 operatorTfun Int.component1() = (this shr 24) and 0xff 0x337f3010.alpha val Int.alpha get() = (this shr 24) and 0xff

Slide 37

Slide 37 text

valT(a,Tr,Tg,Tb)T=T0x337f3010 operatorTfun Int.component1() = (this shr 24) and 0xff 0x337f3010.alpha val Int.alpha get() = (this shr 24) and 0xff

Slide 38

Slide 38 text

valT(a,Tr,Tg,Tb)T=T0x337f3010 operatorTfun Int.component1() = (this shr 24) and 0xff 0x337f3010.alpha val Int.alpha get() = (this shr 24) and 0xff

Slide 39

Slide 39 text

val path = path1 + path2

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

val path = path1 + path2 val path = path1 and path2

Slide 42

Slide 42 text

val path = path1 + path2 val path = path1 and path2 inline infix fun Path.and(p: Path) = this + p

Slide 43

Slide 43 text

varTcalledT=T0T handler.postDelayed(10)T{T called++ }T Outer scope

Slide 44

Slide 44 text

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

Slide 45

Slide 45 text

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

Slide 46

Slide 46 text

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

Slide 47

Slide 47 text

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

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

varTcalledT=T0T handler.postDelayed(10)T{T called++ }T inline fun Handler.postDelayed( crossinline action: () -> Unit) = Runnable { action() } fun hasZeros(ints: List): Boolean { ints.forEach { if (it == 0) return true } return false }

Slide 50

Slide 50 text

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

Slide 51

Slide 51 text

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

Slide 52

Slide 52 text

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

Slide 53

Slide 53 text

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

Slide 54

Slide 54 text

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

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

funTshow(v:TView)T{T v.animate()T .setDuration(200)T .alphaBy(0f)T .alpha(1f)T .start()T }T

Slide 57

Slide 57 text

funTView.show()T{T animate()T .setDuration(200)T .alphaBy(0f)T .alpha(1f)T .start()T }T

Slide 58

Slide 58 text

funTView.show()T{T animate()T .setDuration(200)T .alphaBy(0f)T .alpha(1f)T .start()T }T funTView.hide()T{T animate()T .setDuration(200)T .alphaBy(1f)T .alpha(0f)T .start()T }T

Slide 59

Slide 59 text

funTView.show()T{T animate()T .setDuration(200)T .alphaBy(0f)T .alpha(1f)T .start()T }T funTView.hide()T{T animate()T .setDuration(200)T .alphaBy(1f)T .alpha(0f)T .start()T }T

Slide 60

Slide 60 text

fun View.update(T callback: ViewPropertyAnimator.()T->TViewPropertyAnimator )T{T animate()T .setDuration(200)T .callback()T .start()T }T

Slide 61

Slide 61 text

fun View.update(T callback: ViewPropertyAnimator.()T->TViewPropertyAnimator )T{T animate()T .setDuration(200)T .callback()T .start()T }T

Slide 62

Slide 62 text

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

Slide 63

Slide 63 text

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

Slide 64

Slide 64 text

No content

Slide 65

Slide 65 text

open.nytimes.com @nytdev github.com/NYTimes developers.nytimes.com

Slide 66

Slide 66 text

QA & speakerdeck.com/tiwiz/inside-android-ktx