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

100% Kotlin and Anko

100% Kotlin and Anko

Overview of alternatives to Java in Android, focusing on Kotlin and taking a sneak peek at Anko.

goncalossilva

October 07, 2016
Tweet

More Decks by goncalossilva

Other Decks in Technology

Transcript

  1. data class Customer(val name: String, val purchases: List<Purchase>) data class

    Purchase(val value: Int, val success: Boolean) object Dataset { val customers: MutableList<Customer> = ArrayList() init { customers.add(Customer( "Gonçalo”, arrayListOf(Purchase(42, true), Purchase(9000, false)))) } }
  2. fun successfulPurchases(name: String) : List<Purchase> { val successfulPurchases: MutableList<Purchase> =

    ArrayList() var customer: Customer? = null for (c in Dataset.customers) { if (c.name == name) { customer = c break } } if (customer != null) { for (purchase in customer.purchases) { if (purchase.success) successfulPurchases.add(purchase) } } return successfulPurchases }
  3. fun successfulPurchases(name: String) : List<Purchase> { val successfulPurchases: MutableList<Purchase> =

    ArrayList() var customer: Customer? = null for (c in Dataset.customers) { if (c.name == name) { customer = c break } } if (customer != null) { for (purchase in customer.purchases) { if (purchase.success) successfulPurchases.add(purchase) } } return successfulPurchases }
  4. fun successfulPurchases(name: String) : List<Purchase> { val successfulPurchases: MutableList<Purchase> =

    ArrayList() var customer: Customer? = null for (c in Dataset.customers) { if (c.name == name) { customer = c break } } if (customer != null) { for (purchase in customer.purchases) { if (purchase.success) successfulPurchases.add(purchase) } } return successfulPurchases }
  5. fun successfulPurchases(name: String) : List<Purchase> { val successfulPurchases: MutableList<Purchase> =

    ArrayList() var customer: Customer? = null for (c in Dataset.customers) { if (c.name == name) { customer = c break } } if (customer != null) { for (purchase in customer.purchases) { if (purchase.success) successfulPurchases.add(purchase) } } return successfulPurchases }
  6. fun successfulPurchases(name: String) : List<Purchase> { val successfulPurchases: MutableList<Purchase> =

    ArrayList() var customer: Customer? = null for (c in Dataset.customers) { if (c.name == name) { customer = c break } } if (customer != null) { for (purchase in customer.purchases) { if (purchase.success) successfulPurchases.add(purchase) } } return successfulPurchases }
  7. fun successfulPurchases(name: String) : List<Purchase> { val successfulPurchases: MutableList<Purchase> =

    ArrayList() var customer: Customer? = null for (c in Dataset.customers) { if (c.name == name) { customer = c break } } if (customer != null) { for (purchase in customer.purchases) { if (purchase.success) successfulPurchases.add(purchase) } } return successfulPurchases }
  8. fun successfulPurchases(name: String) : List<Purchase> { val successfulPurchases: MutableList<Purchase> =

    ArrayList() // Find the customer. val customer = Dataset.customers.find { it.name == name } // Gather successful purchases. if (customer != null) { successfulPurchases.addAll(customer.purchases.filter { it.success } } return successfulPurchases }
  9. fun successfulPurchases(name: String) : List<Purchase> { return Dataset.customers.find { it.name

    == name }?.purchases? .filter { it.success } ?: emptyList() }
  10. fun successfulPurchases(name: String) : List<Purchase> { return Dataset.customers.find { it.name

    == name }?.purchases? .filter { it.success } ?: emptyList() }
  11. fun successfulPurchases(name: String) : List<Purchase> { return Dataset.customers.find { it.name

    == name }?.purchases? .filter { it.success } ?: emptyList() }
  12. fun successfulPurchases(name: String) : List<Purchase> { return Dataset.customers.find { it.name

    == name }?.purchases? .filter { it.success } ?: emptyList() }
  13. fun successfulPurchases(name: String) : List<Purchase> { val successfulPurchases: MutableList<Purchase> =

    ArrayList() var customer: Customer? = null for (c in Dataset.customers) { if (c.name == name) { customer = c break } } if (customer != null) { for (purchase in customer.purchases) { if (purchase.success) successfulPurchases.add(purchase) } } return successfulPurchases }
  14. Higher-order functions, properties, mixins and delegation, extension functions, static nullability

    checking, automatic casts, reified generics, declaration-site variance, modules and build infrastructure, inline functions, operator overloading, String interpolation, pattern matching, first class IDE support, Java converter, default parameters, infix methods, destructuring declarations, and more...
  15. inline fun SQLiteDatabase.inTransaction(func: SQLiteDatabase.() -> Unit) { beginTransaction() try {

    func() setTransactionSuccessful() } finally { endTransaction() } } db.inTransaction { delete("hackathon", "name = ?", arrayOf("Pixels Camp")) }
  16. inline fun SQLiteDatabase.inTransaction(func: SQLiteDatabase.() -> Unit) { beginTransaction() try {

    func() setTransactionSuccessful() } finally { endTransaction() } } db.inTransaction { delete("hackathon", "name = ?", arrayOf("Pixels Camp")) }
  17. inline fun SQLiteDatabase.inTransaction(func: SQLiteDatabase.() -> Unit) { beginTransaction() try {

    func() setTransactionSuccessful() } finally { endTransaction() } } db.inTransaction { delete("hackathon", "name = ?", arrayOf("Pixels Camp")) }
  18. inline fun SQLiteDatabase.inTransaction(func: SQLiteDatabase.() -> Unit) { beginTransaction() try {

    func() setTransactionSuccessful() } finally { endTransaction() } } db.inTransaction { delete("hackathon", "name = ?", arrayOf("Pixels Camp")) }
  19. fun Cursor.getStringOrNull(columnName: String): String? { val index = getColumnIndexOrThrow(columnName) return

    if (isNull(index)) null else getString(index) } fun View.setPaddingTop(top: Int) { setPadding(paddingLeft, top, paddingRight, paddingBottom) } fun View.toBitmap() : Bitmap? { if (width > 0 && height > 0) { val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888) val canvas = Canvas(bitmap) draw(canvas) return bitmap } else { return null } }
  20. fun Cursor.getStringOrNull(columnName: String): String? { val index = getColumnIndexOrThrow(columnName) return

    if (isNull(index)) null else getString(index) } fun View.setPaddingTop(top: Int) { setPadding(paddingLeft, top, paddingRight, paddingBottom) } fun View.toBitmap() : Bitmap? { if (width > 0 && height > 0) { val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888) val canvas = Canvas(bitmap) draw(canvas) return bitmap } else { return null } }
  21. fun Cursor.getStringOrNull(columnName: String): String? { val index = getColumnIndexOrThrow(columnName) return

    if (isNull(index)) null else getString(index) } fun View.setPaddingTop(top: Int) { setPadding(paddingLeft, top, paddingRight, paddingBottom) } fun View.toBitmap() : Bitmap? { if (width > 0 && height > 0) { val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888) val canvas = Canvas(bitmap) draw(canvas) return bitmap } else { return null } }
  22. fun Cursor.getStringOrNull(columnName: String): String? { val index = getColumnIndexOrThrow(columnName) return

    if (isNull(index)) null else getString(index) } fun View.setPaddingTop(top: Int) { setPadding(paddingLeft, top, paddingRight, paddingBottom) } fun View.toBitmap() : Bitmap? { if (width > 0 && height > 0) { val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888) val canvas = Canvas(bitmap) draw(canvas) return bitmap } else { return null } }
  23. fun Cursor.getStringOrNull(columnName: String): String? { val index = getColumnIndexOrThrow(columnName) return

    if (isNull(index)) null else getString(index) } fun View.setPaddingTop(top: Int) { setPadding(paddingLeft, top, paddingRight, paddingBottom) } fun View.toBitmap() : Bitmap? { if (width > 0 && height > 0) { val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888) val canvas = Canvas(bitmap) draw(canvas) return bitmap } else { return null } }
  24. Anko XML is not typesafe nor null-safe XML is parsed

    at runtime XML makes code reuse hard Kotlin DSL
  25. override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) verticalLayout { val name

    = editText() button("Say Hello") { onClick { toast("Hello, ${name.text}!") } } } }
  26. fun _LinearLayout.inputField(name: String): TextView { textView("$name:") { textSize = 18f

    }.lparams { verticalMargin = dip(4) } return editText() } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) verticalLayout { padding = dip(16) val login = inputField("Username") val email = inputField("E-mail") button("Sign up") { textSize = 14f onClick { login(login.text) } }.lparams { topMargin = dip(8) } } }
  27. fun _LinearLayout.inputField(name: String): TextView { textView("$name:") { textSize = 18f

    }.lparams { verticalMargin = dip(4) } return editText() } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) verticalLayout { padding = dip(16) val login = inputField("Username") val email = inputField("E-mail") button("Sign up") { textSize = 14f onClick { login(login.text) } }.lparams { topMargin = dip(8) } } }
  28. fun _LinearLayout.inputField(name: String): TextView { textView("$name:") { textSize = 18f

    }.lparams { verticalMargin = dip(4) } return editText() } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) verticalLayout { padding = dip(16) val login = inputField("Username") val email = inputField("E-mail") button("Sign up") { textSize = 14f onClick { login(login.text) } }.lparams { topMargin = dip(8) } } }