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

Potenciando el desarrollo Android con Kotlin

Potenciando el desarrollo Android con Kotlin

Esta charla fue dada en el marco. del Kotlin Everywhere Neuquén, un evento para difundir el uso de Kotlin en diferentes ambientes.

La charla trata sobre Android. KTX y de cómo las características únicas de Kotlin pueden potenciar el desarrollo de las aplicaciones Android.

Ricardo Markiewicz

November 23, 2019
Tweet

More Decks by Ricardo Markiewicz

Other Decks in Programming

Transcript

  1. HABÍA UNA VEZ UN BUG db.beginTransaction(); try { // Save

    data } catch { // Error in between database transaction } finally { db.endTransaction(); }
  2. HABÍA UNA VEZ UN BUG db.beginTransaction(); try { // Save

    data db.setTransactionSuccessful(); } catch { // Error in between database transaction } finally { db.endTransaction(); }
  3. KOTLIN EXTENSIONS fun SQLiteDatabase.transaction(code: SQLiteDatabase.() -> Unit) { try {

    beginTransaction() code() setTransactionSuccessful() } catch (e: TransactionAbortException) { // Do nothing, just stop the transaction } finally { endTransaction() } }
  4. KOTLIN EXTENSIONS fun SQLiteDatabase.transaction(code: SQLiteDatabase.() -> Unit) { try {

    beginTransaction() code() setTransactionSuccessful() } catch (e: TransactionAbortException) { // Do nothing, just stop the transaction } finally { endTransaction() } }
  5. KOTLIN EXTENSIONS fun SQLiteDatabase.transaction(code: SQLiteDatabase.() -> Unit) { try {

    beginTransaction() code() setTransactionSuccessful() } catch (e: TransactionAbortException) { // Do nothing, just stop the transaction } finally { endTransaction() } }
  6. KOTLIN EXTENSIONS fun SQLiteDatabase.transaction(code: SQLiteDatabase.() -> Unit) { try {

    beginTransaction() code() setTransactionSuccessful() } catch (e: TransactionAbortException) { // Do nothing, just stop the transaction } finally { endTransaction() } }
  7. KOTLIN EXTENSIONS fun SQLiteDatabase.transaction(code: SQLiteDatabase.() -> Unit) { try {

    beginTransaction() code() setTransactionSuccessful() } catch (e: TransactionAbortException) { // Do nothing, just stop the transaction } finally { endTransaction() } }
  8. KOTLIN EXTENSIONS fun SQLiteDatabase.transaction(code: SQLiteDatabase.() -> Unit) { try {

    beginTransaction() code() setTransactionSuccessful() } catch (e: TransactionAbortException) { // Do nothing, just stop the transaction } finally { endTransaction() } }
  9. KOTLIN EXTENSIONS fun SQLiteDatabase.transaction(code: SQLiteDatabase.() -> Unit) { try {

    beginTransaction() code() setTransactionSuccessful() } catch (e: TransactionAbortException) { // Do nothing, just stop the transaction } finally { endTransaction() } }
  10. KOTLIN EXTENSIONS fun SQLiteDatabase.transaction(code: SQLiteDatabase.() -> Unit) { try {

    beginTransaction() code() setTransactionSuccessful() } catch (e: TransactionAbortException) { // Do nothing, just stop the transaction } finally { endTransaction() } }
  11. HABÍA UNA VEZ UN BUG db.beginTransaction(); try { // Save

    data db.setTransactionSuccessful(); } catch { // Error in between database transaction } finally { db.endTransaction(); }
  12. EVITAR TAREAS REPETITIVAS ViewTreeObserver vto = view.getViewTreeObserver(); vto.addOnGlobalLayoutListener (new OnGlobalLayoutListener()

    { @Override public void onGlobalLayout() { view.getViewTreeObserver() .removeOnGlobalLayoutListener(this); int width = layout.getMeasuredWidth(); int height = layout.getMeasuredHeight(); } });
  13. EVITAR TAREAS REPETITIVAS ViewTreeObserver vto = view.getViewTreeObserver(); vto.addOnGlobalLayoutListener (new OnGlobalLayoutListener()

    { @Override public void onGlobalLayout() { view.getViewTreeObserver() .removeOnGlobalLayoutListener(this); int width = layout.getMeasuredWidth(); int height = layout.getMeasuredHeight(); } });
  14. EVITAR TAREAS REPETITIVAS inline fun View.waitForLayout(crossinline f: () -> Unit)

    { with(viewTreeObserver) { addOnGlobalLayoutListener( object : ViewTreeObserver.OnGlobalLayoutListener { override fun onGlobalLayout() { removeOnGlobalLayoutListener(this) f() } } ) } }
  15. EVITAR TAREAS REPETITIVAS inline fun View.waitForLayout(crossinline f: () -> Unit)

    { with(viewTreeObserver) { addOnGlobalLayoutListener( object : ViewTreeObserver.OnGlobalLayoutListener { override fun onGlobalLayout() { removeOnGlobalLayoutListener(this) f() } } ) } }
  16. EVITAR TAREAS REPETITIVAS inline fun View.waitForLayout(crossinline f: () -> Unit)

    { with(viewTreeObserver) { addOnGlobalLayoutListener( object : ViewTreeObserver.OnGlobalLayoutListener { override fun onGlobalLayout() { removeOnGlobalLayoutListener(this) f() } } ) } }
  17. EVITAR TAREAS REPETITIVAS inline fun View.waitForLayout(crossinline f: () -> Unit)

    { with(viewTreeObserver) { addOnGlobalLayoutListener( object : ViewTreeObserver.OnGlobalLayoutListener { override fun onGlobalLayout() { removeOnGlobalLayoutListener(this) f() } } ) } }
  18. EVITAR TAREAS REPETITIVAS inline fun View.waitForLayout(crossinline f: () -> Unit)

    { with(viewTreeObserver) { addOnGlobalLayoutListener( object : ViewTreeObserver.OnGlobalLayoutListener { override fun onGlobalLayout() { removeOnGlobalLayoutListener(this) f() } } ) } }
  19. EVITAR TAREAS REPETITIVAS ViewTreeObserver vto = view.getViewTreeObserver(); vto.addOnGlobalLayoutListener (new OnGlobalLayoutListener()

    { @Override public void onGlobalLayout() { view.getViewTreeObserver() .removeOnGlobalLayoutListener(this); int width = view.getMeasuredWidth(); int height = view.getMeasuredHeight(); } });
  20. ANDROID KTX Android KTX is a set of Kotlin extensions

    that are included with Android Jetpack and other Android libraries. KTX extensions provide concise, idiomatic Kotlin to Jetpack, Android platform, and other APIs. https://developer.android.com/kotlin/ktx
  21. USANDO KTX • Fragment KTX - androidx.fragment:fragment-ktx:$version • Palette KTX

    - androidx.palette:palette-ktx:$version • SQLite KTX - androidx.sqlite:sqlite-ktx:$version • Collection KTX - androidx.collection:collection-ktx:$version • ViewModel KTX - androidx.lifecycle:lifecycle-viewmodel-ktx:$version • Reactive Streams KTX - androidx.lifecycle:lifecycle- reactivestreams-ktx:$version • Navigation KTX - androidx.navigation:navigation-runtime-ktx: $version • WorkManager KTX - androidx.work:work-runtime-ktx:$version • Play Core KTX - com.google.android.play:core-ktx:$version https://developer.android.com/kotlin/ktx
  22. ¿ENTONCES TIRO TODO? https://developer.android.com/kotlin/ktx class TextUtils { static boolean isEmailValid(String

    str) { // } } inline fun String.isEmailValid = TextUtils.isEmailValid(this)
  23. ¿ENTONCES TIRO TODO? https://developer.android.com/kotlin/ktx class TextUtils { static boolean isEmailValid(@NonNull

    String str) { // } } inline fun String.isEmailValid = TextUtils.isEmailValid(this)
  24. ¿ENTONCES TIRO TODO? https://developer.android.com/kotlin/ktx class TextUtils { @ExtensionFunction static boolean

    isEmailValid(@NonNull String str) { // } } inline fun String.isEmailValid = TextUtils.isEmailValid(this)
  25. KEEP-110 https://github.com/Kotlin/KEEP/issues/110 • @ExtensionFunction / @ExtensionProperty
 Turn a static method

    with at least one argument into an extension
 function or an extension property. • @ParameterName - An explicit name for parameters. • @DefaultValue - Default parameter values.
  26. ¿POR QUÉ DEBERÍA IMPORTAR? Estas técnicas hacen el desarrollo en

    Android mas conciso y agradable Código más legible = mayor facilidad = menos bugs = más divertido :)
  27. DECONSTRUCCÍON val finalRect = rect1 + rect2 val left =

    finalRect.left val right = finalRect.right val top = finalRect.top val bottom = finalRect.bottom
  28. DECONSTRUCCÍON inline operator fun Rect.component1() = this.left inline operator fun

    Rect.component2() = this.right inline operator fun Rect.component3() = this.top inline operator fun Rect.component4() = this.bottom
  29. DECONSTRUCCÍON inline operator fun Rect.component1() = this.left inline operator fun

    Rect.component2() = this.right inline operator fun Rect.component3() = this.top inline operator fun Rect.component4() = this.bottom
  30. RESUMEN • Extension Functions nos ayudan a • Código más

    legible • Menor posibilidad de errores • Mayor facilidad para descubrir funcionalidad • Mayor expresividad