$30 off During Our Annual Pro Sale. View Details »

Develop a weather app with Kotlin (DevoxxFR 17)

Develop a weather app with Kotlin (DevoxxFR 17)

Hands-on DevoxxFR 2017

Laurent BARESSE

April 05, 2017
Tweet

More Decks by Laurent BARESSE

Other Decks in Programming

Transcript

  1. #DevoxxFR 2
    Develop a
    weather app
    with K

    View Slide

  2. #DevoxxFR 1
    Grab your stuff !
    https://github.com/Ekito/2017-handson-kotlinAndroid
    Develop a weather app with
    WIFI : devoxxfr-hol / hola#4321

    View Slide

  3. #DevoxxFR
    Roadmap
    3
    9:00 - Introduction

    9:05 - Intro to Kotlin part 1

    9:25 - Project presentation & setup

    9:35 - Hands-on part 1 (55 minutes)

    10:30 - Coffee break

    10:45 - Intro to Kotlin part 2

    11:00 - Hands-on part 2 (55 minutes)

    11:55 - Final Part

    View Slide

  4. #DevoxxFR
    4
    @Baresse
    Work @ ekito
    Developer & Geek
    Fullstack & Mobile
    Laurent BARESSE
    @arnogiu
    Work @ ekito
    Core & Gear Maker
    Mobile & Cloud
    Arnaud GIULIANI

    View Slide

  5. #DevoxxFR
    Join the team !
    5
    https://www.ekito.fr/careers

    View Slide

  6. #DevoxxFR 6
    Mind your vote

    View Slide

  7. #DevoxxFR 7
    Intro to
    part 1

    View Slide

  8. #DevoxxFR 8
    « production ready »
    15.02.2016

    View Slide

  9. #DevoxxFR 9
    Kotlin 1.1.1 @ 15.03.2017

    View Slide

  10. #DevoxxFR 10
    Yet another
    JVM
    Language ?

    View Slide

  11. #DevoxxFR 11
    The new « Swift »
    for Android ?

    View Slide

  12. #DevoxxFR 12

    View Slide

  13. #DevoxxFR 13
    Kotlin clearly considered by
    the Java community !
    https://spring.io/blog/2017/01/04/introducing-kotlin-support-in-spring-framework-5-0
    http://www.javamagazine.mozaicreader.com/
    #&pageSet=5&page=0&contentItem=0 (March/April 2017)
    https://www.thoughtworks.com/radar/languages-and-frameworks/kotlin

    View Slide

  14. #DevoxxFR 14
    - Conciseness & Smarter API
    - Null Safety & Immutability protection
    - Type Inference, Static Typing & Smart Casts
    - Open programming styles
    - Java Interop
    Why
    Kotlin ?

    View Slide

  15. #DevoxxFR 15
    - Fully open source (built by Jetbrains)
    - Run on Java 6+
    - Static typing & type inference
    - Modern language features
    - 1st grade tooling
    What’s
    Kotlin ?

    View Slide

  16. #DevoxxFR 16
    - String templates
    - Properties
    - Lambdas
    - Data class
    - Smart cast
    - Null safety
    - Lazy property
    - Default values for function
    parameters
    - Extension Functions
    - No more ;
    - Single-expression functions
    - When expression
    - let, apply, use, with
    - Collections
    - Android Extensions Plugin
    What’s inside
    Kotlin ?

    View Slide

  17. #DevoxxFR 17
    Compare with
    Same conciseness and expressive code, but
    Kotlin static typing and null-safety make a big
    difference.
    "Some people say Kotlin has 80% the power of Scala,
    with 20% of the features" * 

    "Kotlin is a software engineering language in contrast
    to Scala which is a computing science language." *
    Swift and Kotlin are VERY similar. Swift is LLVM based
    and has C interop while Kotlin is JVM based and has
    Java interop.
    * Quotes from Kotlin: The Ying and Yang of Programming Languages
    @
    sdeleuze

    View Slide

  18. #DevoxxFR 18
    is Kotlin Android friendly ?
    https://github.com/SidneyXu/AndroidDemoIn4Languages
    Method counts by Language

    View Slide

  19. #DevoxxFR 19
    KOTLIN is not just about writing
    your app with lesser lines.
    It’s all about writing
    SAFER & BETTER APPS !

    View Slide

  20. #DevoxxFR 20
    Let’s Go !

    View Slide

  21. #DevoxxFR 21
    Typing & Inference
    val a: Int = 1

    val b = 1 // `Int` type is inferred
    var x = 5 // `Int` type is inferred

    x += 1
    Inferred values
    Mutable values
    val : constant value - IMMUTABLE
    var : variable value - MUTABLE
    USE VAL AS MUCH AS POSSIBLE !

    View Slide

  22. #DevoxxFR 22
    Safety with Optionals
    var stringA: String = "foo"

    stringA = null //Compilation error - stringA is a String (non optional)


    var stringB: String? = "bar" // stringB is an Optional String

    stringB = null //ok
    Optional value

    View Slide

  23. #DevoxxFR
    23
    Late initialization, Lazy, Delegates …
    // set length default value manually

    val length = if (stringB != null) stringB.length else -1

    //or with the Elvis operator

    val length = stringB?.length ?: -1
    Default Value & Elvis Operator ?:
    val length = stringB.length // Compilation error - stringB can be null !

    // use ? the safe call operator

    val length = stringB?.length //Value or null - length is type Int?
    val length = stringB!!.length // Value or explicit throw NPE - length is type Int
    Safe call with ?. or Explicit call with !!.

    View Slide

  24. #DevoxxFR 24
    Example
    Working with optional values

    View Slide

  25. #DevoxxFR 25
    Example
    Null check safety & Smart cast
    Securing with default values

    View Slide

  26. #DevoxxFR 26
    Class
    class User (

    val userName: String,

    val firstName: String,

    val lastName: String,

    var location: Point? = null

    )
    POJO +
    - Getter
    - Setter
    - Constructors
    public / closed by default
    optional Body

    View Slide

  27. #DevoxxFR 27
    Data Class
    data class User (

    val userName: String,

    val firstName: String,

    val lastName: String,

    var location: Point? = null

    )
    POJO +
    - Getter
    - Setter
    - Constructors
    - toString
    - hashcode
    - equals
    - copy

    View Slide

  28. #DevoxxFR 28
    Pojo ?
    Kotlin
    Java

    View Slide

  29. #DevoxxFR 29
    Properties
    // readonly property

    val isEmpty: Boolean

    get() = this.size == 0
    Properties can be declared in constructor or in class
    You can also handle getter & setter
    // customer getter & setter

    var stringRepresentation: String
    set(value) {


    }
    class ApiKey(var weatherKey: String, var geocodeKey: String){

    var debug : Boolean = false

    }
    * Lateinit, Lazy, Delegates …

    View Slide

  30. #DevoxxFR 30
    Object Component
    // singleton

    object Resource {

    val name = "Name"

    }
    class StringCalculator{
    // class helper

    companion object{

    val operators = arrayOf("+","-","x","/")

    }

    }
    Singleton Class
    Companion Object

    View Slide

  31. #DevoxxFR 31
    Example
    data class User(val name: String = "", val age: Int = 0)
    val jack = User(name = "Jack", age = 1) //no new keyword

    val anotherJack = jack.copy(age = 2)
    Data Class usage
    A simple GSon Class

    View Slide

  32. #DevoxxFR 32
    When
    when (s) {

    1 -> print("x == 1")

    2 -> print("x == 2")

    else -> { // Note the block

    print("x is neither 1 nor 2")

    }

    }
    Flow Control (replace your old if blocks)
    when (x) {

    in 1..10 -> print("x is in the range")

    in validNumbers -> print("x is valid")

    !in 10..20 -> print("x is outside the range")

    else -> print("none of the above")

    }
    Pattern Matching
    * can match with operators : in, as, is, range …

    View Slide

  33. #DevoxxFR 33
    Example

    View Slide

  34. #DevoxxFR 34
    Collections
    // immutable map

    val map = mapOf("a" to 1, "b" to 2, "c" to 3)

    for ((k, v) in map) {

    println("$k -> $v")

    }

    map2["a"] = "my value" // direct map access with array style
    // range

    for (i in 1..100) { //... }
    // immutable list

    val list = listOf("a", "b", "c","aa")

    list.filter { it.startsWith("a") }
    * collections operators : map, filter, take, flatMap, forEach, firstOrNull, last …

    View Slide

  35. #DevoxxFR 35
    Example
    Kotlin Collection
    Java 7 Collection

    View Slide

  36. #DevoxxFR 36
    InterOp Kotlin/Java
    Java
    Kotlin

    View Slide

  37. #DevoxxFR 37
    InterOp Java/Kotlin
    object UserSingleton{

    fun stringify(user : User) : String{

    // …

    }

    }
    fun WhatIsyourAge(user : User){

    println("Your age is ${user.age}")

    }
    data class User (val name: String, val age : Int){

    companion object{

    fun sayHello(user : User){
    //…

    }

    }

    }
    User u = new User("toto",42);

    User.Companion.sayHello(u);
    UserUtilsKt.WhatIsyourAge(u);
    UserSingleton.INSTANCE.stringify(u)

    View Slide

  38. #DevoxxFR 38
    Project presentation & setup

    View Slide

  39. #DevoxxFR 39
    My Weather app

    View Slide

  40. #DevoxxFR
    40
    MainApplication MainActivity
    DailyForecastModel
    DialogHelper
    WeatherSDKUtil
    WeatherSDK

    View Slide

  41. #DevoxxFR 41
    Get the project
    https://github.com/Ekito/2017-handson-kotlinAndroid

    View Slide

  42. #DevoxxFR 42
    Create your branch
    https://github.com/Ekito/2017-handson-kotlinAndroid/blob/master/README.pdf

    View Slide

  43. #DevoxxFR 43
    Adapt the config
    In app & weathersdk modules:
    -Android.BuildToolsVersion ~ 24.0.3
    -com.android.support ~ 24.1.1
    In root build.gradle:
    -kotlin_version : 1.0.6+ (1.1.1)
    -retrofit_version : 2.1.0+
    -okhttp_version : 3.3.1+
    In weathersdk module:
    -rxjava : 1.2.0+ (2.0.5)
    -rxandroid : 1.1.5 (2.0.1)
    In root build.gradle:
    -com.android.tools.build:gradle: 2.2.3
    Do not upgrade to Gradle 2.3+ !

    View Slide

  44. #DevoxxFR 44
    Hands-on
    part 1

    View Slide

  45. #DevoxxFR 45
    9:30 - Introduction

    9:35 - Intro to Kotlin part 1

    9:55 - Project presentation & setup

    10:05 - Hands-on part 1 (55 minutes)
    11:00 - Coffee break

    11:15 - Intro to Kotlin part 2

    11:30 - Hands-on part 2 (55 minutes)

    12:25 - Final Part
    Hands-on !

    View Slide

  46. #DevoxxFR 46
    9:30 - Introduction

    9:35 - Intro to Kotlin part 1

    9:55 - Project presentation & setup

    10:05 - Hands-on part 1 (55 minutes)

    11:00 - Coffee break
    11:15 - Intro to Kotlin part 2

    11:30 - Hands-on part 2 (55 minutes)

    12:25 - Final Part
    Coffee Break !

    View Slide

  47. #DevoxxFR 47
    Intro to
    part 2

    View Slide

  48. #DevoxxFR 48
    Sweet stuffs for

    View Slide

  49. #DevoxxFR 49
    Kotlin’s Android
    Extensions
    apply plugin: 'com.android.application'
    apply plugin: ‘kotlin-android’
    apply plugin: ‘kotlin-android-extensions’ // if use extensions

    View Slide

  50. #DevoxxFR
    50
    Kotlin
    Java
    * In Fragment : onViewCreated()

    View Slide

  51. #DevoxxFR 51
    Functions
    fun reformat(str: String,

    normalizeCase: Boolean = true,

    upperCaseFirstLetter: Boolean = true,

    wordSeparator: Char = ' '): String {


    }
    Named Parameters & default values
    reformat(str, true, true, '_') // old way to call

    reformat(str, wordSeparator = '_') // using default values & named params

    View Slide

  52. #DevoxxFR 52
    Functions Extensions
    Declaration
    Usage

    View Slide

  53. #DevoxxFR 53
    Lambdas
    val fab = findViewById(R.id.fab) as FloatingActionButton

    fab.setOnClickListener { view -> popLocationDialog(view) }
    A lambda expression or an anonymous function is a “function literal”, i.e. a function that is
    not declared, but passed immediately as an expression
    - A lambda expression is always surrounded by curly braces
    - Its parameters (if any) are declared before -> (parameter types may be omitted),
    - The body goes after -> (when present).
    myNumber.split("\n").flatMap { it.split(separator) }

    .map(Integer::parseInt)

    .map(::checkPositiveNumber)

    .filter { it <= 1000 }

    .sum()
    * Method references are not surrounded by curly braces

    View Slide

  54. #DevoxxFR 54
    Reactive
    Programming

    View Slide

  55. #DevoxxFR 55
    Lamba expressions
    Functional Programming
    Reactive Streams
    http://reactivex.io/documentation/operators.html
    http://www.reactive-streams.org/
    https://spring.io/blog/2016/06/07/notes-on-reactive-
    programming-part-i-the-reactive-landscape

    View Slide

  56. #DevoxxFR 56
    Example - Rx/Retrofit
    Kotlin
    Java

    View Slide

  57. #DevoxxFR 57
    Hands-on
    part 2

    View Slide

  58. #DevoxxFR 58
    9:30 - Introduction

    9:35 - Intro to Kotlin part 1

    9:55 - Project presentation & setup

    10:05 - Hands-on part 1 (55 minutes)

    11:00 - Coffee break

    11:15 - Intro to Kotlin part 2

    11:30 - Hands-on part 2 (55 minutes)
    12:25 - Final Part
    Hands-on !

    View Slide

  59. #DevoxxFR 59
    Final Part

    View Slide

  60. #DevoxxFR 60
    Time is over !

    View Slide

  61. #DevoxxFR 61
    Smarter development for the JVM
    Kotlin

    View Slide

  62. #DevoxxFR 62
    Mind your vote

    View Slide

  63. #DevoxxFR
    Thank you for
    coming
    63

    View Slide