Share
Embed
Inside Android KTX
Talk given at Kotlin Night 2018 Milan, A Kotlin Perspective and Droidcon Turin 2018
Roberto Orgiu
March 12, 2018
Transcript
66 slides
-
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 -
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 -
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) -
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 -
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 -
val actual = context.systemService<Type>() @RequiresApi(23) inline fun <reified T> Context.systemService()
= getSystemService(T::class.java) -
val actual = context.systemService<Type>() @RequiresApi(23) inline fun <reified T> Context.systemService()
= getSystemService(T::class.java) -
val actual = context.systemService<Type>() @RequiresApi(23) inline fun <reified T> Context.systemService()
= getSystemService(T::class.java) -
val actual = context.systemService<Type>() @RequiresApi(23) inline fun <reified T> Context.systemService()
= getSystemService(T::class.java) inline reified -
inline reified Body is copied over at compile time Type
passed is preserved -
inline reified Body is copied over at compile time Type
passed is preserved -
inline reified Body is copied over at compile time Type
passed is preserved -
context.withStyledAttributes(T ... intArrayOf(android.R.attr.textColorPrimary) )T{ val resourceId = getResourceId(0, -1) ...
}T context.withStyledAttributes( -
context.withStyledAttributes(…){…}T context.withStyledAttributes( inline fun Context.withStyledAttributes( ... attrs: IntArray, ... block:
TypedArray.() -> Unit ) { val typedArray = obtainStyledAttributes(...) try { typedArray.block() } finally { typedArray.recycle() } } -
inline fun Context.withStyledAttributes( ... attrs: IntArray, ... block: TypedArray.() ->
Unit ) { val typedArray = obtainStyledAttributes(...) try { typedArray.block() } finally { typedArray.recycle() } } context.withStyledAttributes(…){…}T context.withStyledAttributes( -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
val path = path1 + path2 inline operator fun Path.plus(p:
Path): Path = Path(this).apply { op(p, Path.Op.UNION) } -
val path = path1 + path2 val path = path1
and path2 inline infix fun Path.and(p: Path) = this + p -
varTcalledT=T0T handler.postDelayed(10)T{T called++ }T inline fun Handler.postDelayed( crossinline action: ()
-> Unit) = Runnable { action() } -
varTcalledT=T0T handler.postDelayed(10)T{T called++ }T inline fun Handler.postDelayed( crossinline action: ()
-> Unit) = Runnable { action() } fun foo() { ordinaryFunction { return } } ❌ -
varTcalledT=T0T handler.postDelayed(10)T{T called++ }T inline fun Handler.postDelayed( crossinline action: ()
-> Unit) = Runnable { action() } fun foo() { ordinaryFunction { return@ordinaryFunction } } -
varTcalledT=T0T handler.postDelayed(10)T{T called++ }T inline fun Handler.postDelayed( crossinline action: ()
-> Unit) = Runnable { action() } fun foo() { ordinaryFunction f1@{ return@f1 } } -
varTcalledT=T0T handler.postDelayed(10)T{T called++ }T inline fun Handler.postDelayed( crossinline action: ()
-> Unit) = Runnable { action() } fun foo() { inlineFunction { return } } ✅ -
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 } -
varTcalledT=T0T handler.postDelayed(10)T{T called++ }T inline fun Handler.postDelayed( crossinline action: ()
-> Unit) = Runnable { action() } -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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