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

Mi Camino a Kotlin

Mi Camino a Kotlin

Kotlin es un lenguaje de programación que viene adoptando la comunidad de Android Developers, en esta presentación repasamos algunos aspectos básicos del lenguaje.

Fecha: Jueves 23 de noviembre del 2017

▶ Gracias al GDG Android Santiago por la oportunidad de brindar esta plática:
https://www.facebook.com/groups/androidchile/

----------------------------------------------------------------------------------------

▶ SLIDES
https://speakerdeck.com/devpicon/mi-camino-a-kotlin

▶ VIDEO
https://youtu.be/AdFFy0mG0AI

----------------------------------------------------------------------------------------

LOS LIBROS QUE MENCIONO:

▶ Kotlin in Action by Dmitry Jemerov et al.
Link: http://a.co/4fENbi2

▶ Kotlin for Android Developers: Learn Kotlin the easy way while developing an Android App by Antonio Leiva
Link: http://a.co/hYbk3oA

----------------------------------------------------------------------------------------

LAS PLÁTICAS QUE MENCIONO:

▶ Kotlin is Here: Life is great and everything will be OK
Link: https://www.youtube.com/watch?v=fPzxfeDJDzY

▶ Kickstarting Kotlin Culture: The Journey from Java to Kotlin
Link: https://www.youtube.com/watch?v=Zd2ELNO5M6A

----------------------------------------------------------------------------------------

▶ KOTLIN DEV REACTOR:
https://medium.com/kotlin-dev-reactor

▶ MI FACEBOOK:
http://www.facebook.com/DevPicon

▶ MI TWITTER:
http://twitter.com/DevPicon

▶ MI BLOG EN MEDIUM:
http://medium.com/@devpicon/

Armando Picón

November 23, 2017
Tweet

More Decks by Armando Picón

Other Decks in Programming

Transcript

  1. ¿Qué es Kotlin? • Lenguaje de programación • Desarrollo por

    JetBrains • Busca superar las limitaciones de Java • Su desarrollo llevó 6 años, antes de la versión 1.0
  2. Hitos 2010 - Kotlin Hello World Febrero 2016 - Kotlin

    1.0 Marzo 2017 - Kotlin 1.1 Mayo 2017 - Android soportará Kotlin
  3. ¿Por qué usar Kotlin? • Interoperabilidad con Java • Null

    safety • Functional programming features • Ligero • Poca curva de aprendizaje • Open Source
  4. ¿Qué necesito? • Android Studio 2.x y Plugin de Kotlin

    para IntelliJ IDEA • Android Studio 3.x
  5. MainActivity.java public class Person { private final String name; private

    int age; public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } Person.java
  6. List<Person> people = new ArrayList<>(); people.add(new Person("Armando", 36)); people.add(new Person("Ronald",

    24)); people.add(new Person("Alberto", 58)); for (Person person : people) { if (person.getName().equals("Ronald")) { return person; } }
  7. val people = listOf<Person>( Person("Armando", 36), Person("Ronald", 24), Person("Alberto", 58))

    val person = people.find { it.name == "Ronald" } return person
  8. val numberList = listOf(1, 2, 3, 4, 5, 6, 7)

    val squared = numberList.map { i -> i * i } squared.forEach { println(it) } // result: 1, 4, 9, 16, 25, 36, 49
  9. public class Utilities { public Locale getDefaultLocale(String deliveryArea){ String deliveryAreaLower

    = deliveryArea.toLowerCase(); if(deliveryAreaLower == "mexico" || deliveryAreaLower == "bolivia" || deliveryAreaLower == "peru"){ return MyLocale.SPANISH; } if (deliveryAreaLower == "brazil"){ return MyLocale.PORTUGUESE; } if (deliveryAreaLower == "usa"){ return MyLocale.ENGLISH; } return MyLocale.SPANISH; } } Utilities.jav a
  10. Utilities.k t class Utilities { fun getDefaultLocale(deliveryArea: String): Locale {

    val deliveryAreaLower = deliveryArea.toLowerCase() if (deliveryAreaLower === "mexico" || deliveryAreaLower === "bolivia" || deliveryAreaLower === "peru") { return MyLocale.SPANISH } if (deliveryAreaLower === "brazil") { return MyLocale.PORTUGUESE } if (deliveryAreaLower === "usa") { return MyLocale.ENGLISH } return MyLocale.SPANISH } }
  11. Utilities.k t class Utilities { fun getDefaultLocale(deliveryArea: String): Locale =

    when (deliveryArea.toLowerCase()) { "mexico", "bolivia", "peru" -> MyLocale.SPANISH "brazil" -> MyLocale.PORTUGUESE "usa" -> MyLocale.ENGLISH else -> MyLocale.SPANISH } }
  12. public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle

    savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } } MainActivity.jav a
  13. class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) {

    super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) } } MainActivity.k t
  14. class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) {

    super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val name = "GDG La Paz" val textView = findViewById(R.id.txt_hello_world) as TextView textView.text = "¡Hola $name!" } } MainActivity.k t
  15. class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) {

    super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val name = "GDG La Paz" txt_hello_world.text = "¡Hola $name!" } } MainActivity.k t
  16. class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) {

    super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val name = "GDG La Paz" txt_hello_world.text = "¡Hola $name!" } } MainActivity.k t import kotlinx.android.synthetic.main.activity_main.*
  17. class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) {

    super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val name = "GDG La Paz" txt_hello_world.text = "¡Hola $name!" } } MainActivity.k t import kotlinx.android.synthetic.main.activity_main.*
  18. class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) {

    super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val name = "GDG La Paz" txt_hello_world.text = "¡Hola $name!" Glide.with(this).load("http://cdm.devpicon.com/avatar.jpg").into(img_profile) } } MainActivity.k t
  19. class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) {

    super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val name = "GDG La Paz" txt_hello_world.text = "¡Hola $name!" Glide.with(this).load("http://cdm.devpicon.com/avatar.jpg").into(img_profile) } } fun ImageView.loadImage(imageUrl: String?) { if (imageUrl != null) { Glide.with(this.context).load(imageUrl).into(this) } } MainActivity.k t Extensions.k t
  20. class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) {

    super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val name = "GDG La Paz" txt_hello_world.text = "¡Hola $name!" Glide.with(this).load("http://cdm.devpicon.com/avatar.jpg").into(img_profile) } } fun ImageView.loadImage(imageUrl: String?) { if (imageUrl != null) { Glide.with(this.context).load(imageUrl).into(this) } } MainActivity.k t Extensions.k t
  21. fun ImageView.loadImage(imageUrl: String?) { if (imageUrl != null) { Glide.with(this.context).load(imageUrl).into(this)

    } } class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val name = "GDG La Paz" txt_hello_world.text = "¡Hola $name!" img_profile.loadImage("http://cdm.devpicon.com/avatar.jpg") } } MainActivity.k t Extensions.k t
  22. class MainActivity : AppCompatActivity(), MainView { var controller : MainController?

    = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) controller = MainController(this) } } MainActivity.k t
  23. class MainActivity : AppCompatActivity(), MainView { var controller : MainController?

    = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) controller = MainController(this) } } MainActivity.k t class MainController(view: MainView) { val view = view } MainController.k t
  24. class MainActivity : AppCompatActivity(), MainView { var controller : MainController?

    = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) controller = MainController(this) } } MainActivity.k t class MainController(view: MainView) { val view = view } MainController.k t
  25. class MainActivity : AppCompatActivity(), MainView { var controller : MainController?

    = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) controller = MainController(this) } } MainActivity.k t class MainController(view: MainView) { val view = view } MainController.k t interface MainView { } MainView.k t
  26. class MainActivity : AppCompatActivity(), MainView { var controller : MainController?

    = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) controller = MainController(this) } } MainActivity.k t class MainController(view: MainView) { val view = view } MainController.k t interface MainView { } MainView.k t
  27. class MainActivity : AppCompatActivity(), MainView { lateinit var controller :

    MainController override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) controller = MainController(this) } } MainActivity.k t class MainController(view: MainView) { val view = view } MainController.k t interface MainView { } MainView.k t
  28. MyFragment.k t class MyFragment : Fragment() { override fun onCreateView(inflater:

    LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? { val myView: View? = inflater?.inflate(R.layout.my_fragment_layout, container, false) return myView } }
  29. MyFragment.k t class MyFragment : Fragment() { override fun onCreateView(inflater:

    LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? { val myView: View? = inflater?.inflate(R.layout.my_fragment_layout, container, false) return myView } } Extensions.k t fun ViewGroup.inflate(layoutId: Int, attachToRoot: Boolean = false): View { val inflater = LayoutInflater.from(context) return inflater.inflate(layoutId, this, attachToRoot) }
  30. MyFragment.k t Extensions.k t fun ViewGroup.inflate(layoutId: Int, attachToRoot: Boolean =

    false): View { val inflater = LayoutInflater.from(context) return inflater.inflate(layoutId, this, attachToRoot) } class MyFragment : Fragment() { override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? { val myView: View? = container?.inflate(R.layout.my_fragment_layout) return myView } }
  31. MyFragment.k t class MyFragment : Fragment() { override fun onCreateView(inflater:

    LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? { val myView: View? = container?.inflate(R.layout.my_fragment_layout) return myView } override fun onViewCreated(view: View?, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) val txtMyName: TextView = view?.findViewById(R.id.txtMyName) as TextView txtMyName.text = "Carlos" } }
  32. MyFragment.k t class MyFragment : Fragment() { override fun onCreateView(inflater:

    LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? { val myView: View? = container?.inflate(R.layout.my_fragment_layout) return myView } override fun onViewCreated(view: View?, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) txtMyName.text = "Carlos" } }
  33. MyFragment.k t class MyFragment : Fragment() { override fun onCreateView(inflater:

    LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? { val myView: View? = container?.inflate(R.layout.my_fragment_layout) return myView } override fun onViewCreated(view: View?, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) txtMyName.text = "Carlos" } } import kotlinx.android.synthetic.main.my_fragment_layout.*
  34. sealed class Posts { class Header(val title: String, val imageUrl:

    String) : Posts() class Reshare(val title: String, val imageUrl: String, val sourceUrl: String): Posts() class News(val title: String, val imageUrl: String, val body: String): Posts() } Posts.k t
  35. class MyAdapter(val posts: MutableList<Posts>) : RecyclerView.Adapter<MyAdapter.PostViewHolder>() { override fun getItemCount():

    Int = posts.size override fun onBindViewHolder(holder: PostViewHolder?, position: Int) { val post = posts[position] holder?.bind(post) } override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): PostViewHolder { val itemView = parent?.inflate(R.layout.item_post) return PostViewHolder(itemView) } } MyAdapter.k t
  36. class PostViewHolder(itemView: View?): RecyclerView.ViewHolder(itemView) { fun bind(post: Posts) { when(post){

    is Header -> { itemView.txtHeader.text = post.title itemView.imgHeader.loadImage(post.imageUrl) } is Reshare -> { itemView.txtHeader.text = post.title itemView.imgHeader.loadImage(post.imageUrl) itemView.txtSource.text = post.sourceUrl } is News -> { itemView.txtHeader.text = post.title itemView.imgHeader.loadImage(post.imageUrl) itemView.txtBody.text = post.body } } }} PostViewHolder.k t sealed class Posts { class Header(val title: String, val imageUrl: String) : Posts() class Reshare(val title: String, val imageUrl: String, val sourceUrl: String): Posts() class News(val title: String, val imageUrl: String, val body: String): Posts() } Posts.k t