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

Kotlin

Rafael Toledo
February 16, 2017

 Kotlin

Apresentada na Droid Talks, do Android Meetup Belo Horizonte

Rafael Toledo

February 16, 2017
Tweet

More Decks by Rafael Toledo

Other Decks in Programming

Transcript

  1. Kotlin? Baseada na JVM (binding para JavaScript) Linguagem orientada a

    objetos, com algumas características funcionais Criada pela JetBrains (Android Studio, IntelliJ) Interoperável com Java nas duas vias
  2. Data Classes public class User { private String name; private

    String email; public void setName(String name) { this.name = name; } public String getName() { return name; } }
  3. Múltiplas Classes por Arquivo data class User(val name: String, val

    email: String) data class Order(val id: Int, val user: User) data class Address(val street: String)
  4. Properties var simple: Int? val inferredType = 1 val isEmpty:

    Boolean get() = this.size == 0 var stringValue: String get() = this.toString() set(value) { splitAndAssign(value) } var privateSetter: String = "a" private set var annotated: Any? = null @Inject set
  5. Extensions @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { return

    new ViewHolder(LayoutInflater.from( parent.getContext()).inflate(R.layout.item_list, parent, false)); }
  6. Extensions fun ViewGroup.inflate(layoutRes: Int, attachToRoot: Boolean = false): View {

    return LayoutInflater.from(context).inflate(layoutRes, this, attachToRoot) }
  7. Extensions @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { return

    new ViewHolder(LayoutInflater.from( parent.getContext()).inflate(R.layout.item_list, parent, false)); }
  8. Extensions override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) : ViewHolder {

    return ViewHolder(parent.inflate(R.layout.item_list)) } parent.inflate(R.layout.item_list) parent.inflate(R.layout.item_list, false) // Mesma coisa parent.inflate(R.layout.item_list, true)
  9. Singleton public class Singleton { private static final Singleton INSTANCE

    = new Singleton(); public static Singleton getInstance() { return INSTANCE; } private Singleton() { } }
  10. Lambdas public interface Callback { void onFinish(Result result); } public

    void asyncRequest(Callback callback) { ... callback.onFinish(result); }
  11. Lambdas asyncRequest(new Callback() { @Override public void onFinish(Result result) {

    Log.i("Callback", "Result was " + result.status()); } });
  12. Lambdas fun asyncRequest(callback: (Result) -> Unit) { ... callback(result) }

    asyncRequest { result -> Log.i("Callback", "Result was ${result.status()}") }
  13. Lambdas fun asyncRequest(callback: (Result) -> Unit) { ... callback(result) }

    asyncRequest { Log.i("Callback", "Result was ${it.status()}") }
  14. Assincronismo e UI Thread ... doAsync() { val result =

    api.fetchData() // Sync call uiThread { textView.text = result.data } }
  15. Kotlin Android Extension Adeus ao findViewById() import kotlinx.android.synthetic.main.activity_main.* ... override

    fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContextView(R.layout.activity_main) textView.setText("It's a kind of magic!") }
  16. Dex Limit Total methods in app-debug.apk: 35032 (53,46% used) Total

    fields in app-debug.apk: 15760 (24,05% used) Methods remaining in app-debug.apk: 30503 Fields remaining in app-debug.apk: 49775
  17. Dex Limit - Shrinker / Proguard Total methods in app-debug.apk:

    15315 (23,37% used) Total fields in app-debug.apk: 8238 (12,57% used) Methods remaining in app-debug.apk: 50220 Fields remaining in app-debug.apk: 57297
  18. Parcelable data class User(val name: String, val email: String) :

    Parcelable { constructor(parcel: Parcel) : this(parcel.readString(), parcel.readString()) override fun writeToParcel(dest: Parcel?, flags: Int) { dest?.writeString(name) dest?.writeString(email) } override fun describeContents() = 0 companion object { val CREATOR: Parcelable.Creator<User> = object: Parcelable.Creator<User> { override fun createFromParcel(parcel: Parcel) = User(parcel) override fun newArray(size: Int): Array<User?> = arrayOfNulls(size) } } }
  19. Coroutines fun loadUser(id: Int) = async { // getUser() retorna

    um CompletableFuture - API 24 :( val user = api.getUser(id).await() textView.text = user.name }
  20. Underscores para parâmetros fun loadProduct(id: Int) { api.fetchProduct(id) { (_,

    name, date) -> title.text = name createdOn.text = date.format() } }
  21. Type Alias typealias Lenght = Double typealias Action<T> = (T)

    -> Unit typealias Multimap<K, V> = Map<K, List<V>>
  22. Referências & Links Blog Oficial blog.jetbrains.com/kotlin anko github.com/kotlin/anko Blog do

    Antonio Leiva antonioleiva.com Kotlin Koans kotlinlang.org/docs/tutorials/koans.html
  23. www.concretesolutions.com.br Rio de Janeiro – Rua São José, 90 –

    cj. 2121 Centro – (21) 2240-2030 São Paulo - Av. Nações Unidas, 11.541 3º andar - Brooklin - (11) 4119-0449 Ajudamos empresas a criar produtos digitais de sucesso concretesolutions.com.br/vagas/