Share
Embed
Creating Great APIs With Kotlin
Slides from my talk at Kotlin/Everywhere Floripa:
https://www.meetup.com/GDGFloripa/events/262037183/
David Tiago Conceição
July 13, 2019
Transcript
60 slides
-
Creating Great APIs With Kotlin David Tiago Conceição Involves Android
Dev BR @davidtiagocon -
Android Development if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { return context.getResources() .getColor(colorId,
context.getTheme()); } else { return context.getResources() .getColor(colorId); } -
Android Development public final class ColorUtils { @ColorInt public static
int getColor( Context context, @ColorRes int colorId) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { return context.getResources() .getColor(colorId, context.getTheme()); } else { return context.getResources() .getColor(colorId); } } } -
Extension Functions @ColorInt fun Context.getThemedColor(@ColorRes colorId: Int) = if (Build.VERSION.SDK_INT
>= Build.VERSION_CODES.M) { this.resources.getColor(colorId, this.theme) } else { this.resources.getColor(colorId) } -
Extension Functions @ColorInt fun Context.getThemedColor(@ColorRes colorId: Int) = if (Build.VERSION.SDK_INT
>= Build.VERSION_CODES.M) { this.resources.getColor(colorId, this.theme) } else { this.resources.getColor(colorId) } -
Extension Functions public final class ContextExtensionsKt { @ColorInt public static
final int getThemedColor(@NotNull Context $this$getThemedColor, @ColorRes int colorId) { return Build.VERSION.SDK_INT >= 23 ? $this$getThemedColor.getResources().getColor(colorId, $this$getThemedColor.getTheme()) : $this$getThemedColor.getResources().getColor(colorId); } } -
Extension Functions public final class ContextExtensionsKt { @ColorInt public static
final int getThemedColor(@NotNull Context $this$getThemedColor, @ColorRes int colorId) { return Build.VERSION.SDK_INT >= 23 ? $this$getThemedColor.getResources().getColor(colorId, $this$getThemedColor.getTheme()) : $this$getThemedColor.getResources().getColor(colorId); } } -
Extension Functions @ColorInt inline fun Context.getThemedColor(@ColorRes colorId: Int) = if
(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { this.resources.getColor(colorId, this.theme) } else { this.resources.getColor(colorId) } -
Extension Functions var10000 = VERSION.SDK_INT >= 23 ? context.getResources().getColor(colorId$iv, context.getTheme())
: context.getResources().getColor(colorId$iv); -
Extension Functions useCase.execute(id, date) .lastElement() .test() .assertDefaultData(pointOfSaleNameFromItinerary) .assertExpectedState(state) private fun
TestObserver<State>.assertExpectedState( expectedState: SomeInternalState): TestObserver<State> { assertValue { it.someInternalState == expectedState } return this } -
animation.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationEnd(Animation animation) { onViewShowed();
} @Override public void onAnimationStart(Animation animation) {} @Override public void onAnimationRepeat(Animation animation) {} }); Arguments -
animation.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationEnd(Animation animation) { onViewShowed();
} @Override public void onAnimationStart(Animation animation) {} @Override public void onAnimationRepeat(Animation animation) {} }); Arguments -
fun Animation.listener( onAnimationStart: ((Animation) -> Unit)? = null, onAnimationEnd: ((Animation)
-> Unit)? = null, onAnimationRepeat: ((Animation) -> Unit)? = null) { ... Default Arguments -
fun Animation.listener( onAnimationStart: ((Animation) -> Unit)? = null, onAnimationEnd: ((Animation)
-> Unit)? = null, onAnimationRepeat: ((Animation) -> Unit)? = null) { this.setAnimationListener(object : Animation.AnimationListener { override fun onAnimationRepeat(animation: Animation?) { onAnimationStart?.invoke(animation!!) ... Default Arguments -
data class ClassWithManyIntegers( val integerProperty: Int = 0, val anotherIntegerProperty:
Int = 0, val someExtraIntegerProperty: Int = 0) Builders -
data class ClassWithManyIntegers( val integerProperty: Int = 0, val anotherIntegerProperty:
Int = 0, val someExtraIntegerProperty: Int = 0) val complexInstance = ClassWithManyIntegers() Builders -
data class ClassWithManyIntegers( val integerProperty: Int = 0, val anotherIntegerProperty:
Int = 0, val someExtraIntegerProperty: Int = 0) val complexInstance = ClassWithManyIntegers( someExtraIntegerProperty = 10) Builders -
val firstPoint = Point(10, 10) val secondPoint = Point(20, 20)
val firstX = firstPoint.x val firstY = firstPoint.y val secondX = secondPoint.x val secondY = secondPoint.y val plusPoint = Point(firstX + secondX, secondY + secondY) Operations -
val firstPoint = Point(10, 10) val secondPoint = Point(20, 20)
val plusPoint = firstPoint + secondPoint Operations -
val firstPoint = Point(10, 10) val secondPoint = Point(20, 20)
val plusPoint = firstPoint + secondPoint operator fun Point.plus(another: Point) = Point(x + another.x, y + another.y) Operators -
val (x,y) = firstPoint operator fun Point.component1() = x operator
fun Point.component2() = y Operators -
val viewGroup: ViewGroup = getViewGroup() viewGroup += view operator fun
ViewGroup.plusAssign(view: View) = addView(view) Operators -
val view = viewGroup[0] operator fun ViewGroup.get(index: Int): View? =
getChildAt(index) Operators -
for (index in 0 until viewGroup.childCount) { doSomething(viewGroup.getChildAt(index)) } viewGroup.forEach
{ view -> doSomething(view) } Functions -
viewGroup.forEach { view -> doSomething(view) } fun ViewGroup.forEach(onEach: (View) ->
Unit) { for (index in 0 until childCount) { onEach(getChildAt(index)) } } Functions -
viewGroup.forEach { view -> doSomething(view) } fun ViewGroup.forEach(onEach: (View) ->
Unit) { for (index in 0 until childCount) { onEach(getChildAt(index)) } } Functions -
fun runInDatabaseTransaction(databaseFunction: (SQLiteDatabase) -> Unit) { val dataBase = getDatabase()
try { dataBase.beginTransaction() databaseFunction(dataBase) dataBase.setTransactionSuccessful() } finally { dataBase.endTransaction() } Functions -
/** Returns a range from this value up to but
excluding the specified [to] value. * If the [to] value is less than or equal to `this` value, then the returned range is empty.*/ public infix fun Int.until(to: Int): IntRange { if (to <= Int.MIN_VALUE) return IntRange.EMPTY return this .. (to - 1).toInt() } Functions -
val untilRange = 0 until 10 val untilCharRange = 'a'
until 'z' val downProgression = 10 downTo 0 Functions -
//Mockk every { saveEvent(any(), any()) } returns Unit every {
mock invoke "openDoor" withArguments listOf("left", "rear") } returns "OK" verify { sendMessageHandler wasNot Called } DSLs -
class Loading class Error class Success class ViewModel { var
currentState: Any = Loading() } State -
open class PossibleState class Loading : PossibleState() class Error :
PossibleState() class Success : PossibleState() class ViewModel { var currentState: PossibleState = Loading() } State -
sealed class PossibleState { object Loading : PossibleState() object Error
: PossibleState() object Success : PossibleState()} class ViewModel { var currentState: PossibleState = PossibleState.Loading } Sealed Classes -
class Scheduler { fun schedule(delay: Long, action: () -> Unit)
{ //... } } Argument Types -
class Scheduler { fun schedule( delay: Long, timeUnit: TimeUnit, action:
() -> Unit) { //... } } Argument Types -
class Minutes(val value: Long) class Scheduler { fun schedule( delay:
Minutes, action: () -> Unit) { //... } } Argument Types -
inline class Minutes(val value: Long) class Scheduler { fun schedule(
delay: Minutes, action: () -> Unit) { //... } } Inline classes -
Extensions Named and Default arguments Operators Higher order functions Infix
DSLs Sealed classes Inline classes Wrapping up -
How to know more? • Extensions: kotlinlang.org/docs/reference/extensions.html • Kotlin Android
Extensions: kotlinlang.org/docs/tutorials/android-plugin.html • Android KTX: developer.android.com/kotlin/ktx • Keywords and Operators: kotlinlang.org/docs/reference/keyword-reference.html • Functions: kotlinlang.org/docs/reference/functions.html • MockK: mockk.io • Sealed Classes: kotlinlang.org/docs/reference/sealed-classes.html • KotlinConf 2018 - Representing State: the Kotlin Edition by Christina Lee: www.youtube.com/watch?v=-lVVfxsRjcY • Inline classes: kotlinlang.org/docs/reference/inline-classes.html -
Thank you! David Tiago Conceição [email protected] Android Dev BR @davidtiagoconceicao
twitter.com/@davidtiagocon speakerdeck.com/davidtcdeveloper