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

Kotlin para Java Haters...

Kotlin para Java Haters...

Mis slides de la presentación para el #io17extended del #gdglapaz

Armando Picón

June 17, 2017
Tweet

More Decks by Armando Picón

Other Decks in Technology

Transcript

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

    JetBrains • Busca superar las limitaciones de Java
  2. ¿Qué es Kotlin? • Lenguaje de programación • Desarrollado por

    JetBrains • Busca superar las limitaciones de Java • Su desarrollo llevó 6 años antes de la versión 1.0
  3. ¿Qué es Kotlin? • Lenguaje de programación • Desarrollado por

    JetBrains • Busca superar las limitaciones de Java • Su desarrollo llevó 6 años antes de la versión 1.0 • Google le brinda soporte como segundo lenguaje para Android
  4. Características • Interoperabilidad con Java • NPE safety • Características

    de programación funcional • Ligero • Poca curva de aprendizaje
  5. Características • Interoperabilidad con Java • NPE safety • Características

    de programación funcional • Ligero • Poca curva de aprendizaje • Open Source
  6. ¿Qué necesito para empezar? • Plugin de Kotlin para IntelliJ

    IDEA y Android Studio 2.x • Android Studio 3.x
  7. 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
  8. 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; } }
  9. val people = listOf<Person>( Person("Armando", 36), Person("Ronald", 24), Person("Alberto", 58))

    val person = people.find { it.name == "Ronald" } return person
  10. 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
  11. 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.java
  12. Utilities.kt 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 } }
  13. Utilities.kt class Utilities { fun getDefaultLocale(deliveryArea: String): Locale = when

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

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

    super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) } } MainActivity.kt
  16. 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.kt
  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.kt
  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!" } } MainActivity.kt import kotlinx.android.synthetic.main.activity_main.*
  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!" } } MainActivity.kt import kotlinx.android.synthetic.main.activity_main.*
  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) } } MainActivity.kt
  21. 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.kt Extensions.kt
  22. 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.kt Extensions.kt
  23. 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.kt Extensions.kt
  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.kt
  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.kt class MainController(view: MainView) { val view = view } MainController.kt
  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.kt class MainController(view: MainView) { val view = view } MainController.kt
  27. 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.kt class MainController(view: MainView) { val view = view } MainController.kt interface MainView { } MainView.kt
  28. 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.kt class MainController(view: MainView) { val view = view } MainController.kt interface MainView { } MainView.kt
  29. 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.kt class MainController(view: MainView) { val view = view } MainController.kt interface MainView { } MainView.kt
  30. MyFragment.kt 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 } }
  31. MyFragment.kt 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.kt fun ViewGroup.inflate(layoutId: Int, attachToRoot: Boolean = false): View { val inflater = LayoutInflater.from(context) return inflater.inflate(layoutId, this, attachToRoot) }
  32. MyFragment.kt Extensions.kt 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 } }
  33. MyFragment.kt 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" } }
  34. MyFragment.kt 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" } }
  35. MyFragment.kt 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.*
  36. 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.kt
  37. 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.kt
  38. 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.kt 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.kt
  39. Recursos • DevCode.la - Curso de Fundamentos de Kotlin http://devcode.la/cursos/kotlin

    • Kotlin Dev Reactor https://medium.com/kotlin-dev-reactor
  40. Artículos • How we made Basecamp 3’s Android app 100%

    Kotlin Dan Kim (Android Programmer at BaseCamp) https://goo.gl/JnXp4C • Kotlin in Production: Should you stay or should you go? Danny Preussler (GDE Android) https://goo.gl/SqxzzF