Slide 1

Slide 1 text

Kotlin para Androides Jorge Juan Barroso Carmona [email protected] @flipper83 Developer Android GDE

Slide 2

Slide 2 text

Sergio Gutierrez Pedro Gomez Davide Mendolia Fran Toro Antonio Lopez

Slide 3

Slide 3 text

Testing for android & iOS. Trainings Architecture, Patterns and principles for Android & iOS. Mastering Git. Advanced mobile development. Testing for android & iOS. Architecture, Patterns and principles for Android & iOS. Companies For Everybody Next open: Testing Training 23 Jun. Kotlin Training 20 Feb. Swift Training 26 Mar. Arch Training 3 Apr.

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

Adam Tornhill “Computer languages differ not so much in what they make possible, but in what they make easy.” Larry Wall

Slide 6

Slide 6 text

https://github.com/Karumi/SuperHeroesKotlin https://github.com/Karumi/KataLogInLogOutKotlin https://github.com/Karumi/KotlinTrainingTeacher

Slide 7

Slide 7 text

Nullability

Slide 8

Slide 8 text

if (user != null && user.email != null) { String email = user.email } Java

Slide 9

Slide 9 text

val email: String? = user?.email Kotlin Is user == null? email is null and the property is never accessed Then email is the user email (that… can be null!) YES NO

Slide 10

Slide 10 text

val email: String? = user?.email ?: "[email protected]" Is user?.email == null? Then email is “[email protected]" Then email is the user email YES NO Kotlin Elvis operator

Slide 11

Slide 11 text

val email: String = user!!.email Kotlin Force cast Is user == null? Then NullPointerException is thrown Then email is the user email YES NO

Slide 12

Slide 12 text

Android obtain Views

Slide 13

Slide 13 text

TextView nameView = (TextView) findViewById(R.id.nameView); nameView.setText(“Flipper”); Java

Slide 14

Slide 14 text

import kotlinx.android.synthetic.main.activity_main.* nameView.text = “Flipper” Kotlin

Slide 15

Slide 15 text

Callbacks and High order function

Slide 16

Slide 16 text

TextView nameView = (TextView) findViewById(R.id.nameView); nameView .setOnClickListener((view) -> {doSomething(view)}); Java

Slide 17

Slide 17 text

nameView.setOnClickListener{ doSomething(it) } Kotlin

Slide 18

Slide 18 text

listOf( User(name = “Jane”, age = 45), User(name = “Joe”, age = 23, email = “[email protected]”), User(name = “Jan”, age = 19, email = “[email protected]"), User(name = “J.D.”, age = 66) ).filter { it.email != null } .map {it.email} // [“[email protected]”,"[email protected]"] Kotlin

Slide 19

Slide 19 text

manage async

Slide 20

Slide 20 text

Java AsyncTasks Threads JDeferred RxJava

Slide 21

Slide 21 text

fun doSomethingUsefulOne(): Int { delay(1000L) return 13 } fun doSomethingUsefulTwo(): Int { delay(1000L) return 29 } val time = measureTimeMillis { val one = doSomethingUsefulOne() val two = doSomethingUsefulTwo() println("${one + two}") } println("$time ms”) [“42”, “time 2017”] Kotlin Corrutines suspend fun doSomethingUsefulOne(): Int { delay(1000L) return 13 } suspend fun doSomethingUsefulTwo(): Int { delay(1000L) return 29 } val time = measureTimeMillis { val one = async { doSomethingUsefulOne() } val two = async { doSomethingUsefulTwo() } println("${one.await() + two.await()}") } println("$time ms") [“42”, “time 1017”]

Slide 22

Slide 22 text

Error handling

Slide 23

Slide 23 text

try { User user = repository.getUserById(id); showUser(user); } catch(Exception exception) { showError(exception); } Java

Slide 24

Slide 24 text

val user: Either = repository.getUserById(id); when(user) { is Right -> showUser(it) is Left -> showError(getMessage(it)) } Kotlin val user: Either = repository.getUserById(id); user.fold( isLeft = {showUser(it)}, isRight= {showError(getMessage(it))});

Slide 25

Slide 25 text

fun getMessage(domainError: Error): Int = when (domainError) { is NotInternetDomainError -> R.string.error_not_internet_message is NotIndexFoundDomainError -> R.string.error_superhero_not_found_message is UnknownDomainError -> R.string.error_unknown_message } Kotlin sealed class DomainError object NotInternetDomainError : DomainError() data class UnknownDomainError( val errorMessage: String = "Unknown Error") : DomainError() data class NotIndexFoundDomainError(val key: String) : DomainError() Compile error!!

Slide 26

Slide 26 text

Find me I am very social! [email protected] @flipper83 What problems do you have with Android?

Slide 27

Slide 27 text

No content