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

Kotlin User Group Belgium Meetup #2

Kotlin User Group Belgium Meetup #2

Slides for our second meetup of the Belgian Kotlin User Group

Guy Heylens

April 23, 2017
Tweet

More Decks by Guy Heylens

Other Decks in Technology

Transcript

  1. Kotlin Night in Brussels Kotlin User Group Belgium Friday 22th

    or Friday 29th of September we will host a Kotlin Night in Brussels. Speakers wanted. Jetbrains support for international speakers.
  2. Kotlin in Android Kotlin User Group Belgium Convert the new

    Activity to Kotlin code Code -> Convert Java File to Kotlin File.
  3. Kotlin in Android Kotlin User Group Belgium Convert the project

    to a Kotlin project Tools -> Kotlin -> Configure Kotlin in Project
  4. Kotlin in Android Kotlin User Group Belgium What did change

    ? root build.gradle What did change ? module build.gradle What did change ? added the Kotlin Library as a dependency
  5. Kotlin in Android Kotlin User Group Belgium Variables Mutable “var”

    Immutable “val” var x = 7 var y: String = "my String” var z = View(this) val x: Int = 20 val y: Double = 21.5 val z: Unit = Unit Type inference You need to do an explicit casting: val x: Int = 20 val y: Long = x.toLong() Equivalent to: Final in Java Readonly in C#
  6. Kotlin in Android Kotlin User Group Belgium Properties • Equivalent

    to “field” in Java • Will do the work of a field + getter + setter • Default getter and setter • You can explicitly add a getter and a setter in a class class Person(){ var name: String = "this is a text" get() = field.toUpperCase() set(value) { field = "Name: $value" } }
  7. Kotlin in Android Kotlin User Group Belgium Classes • Use

    the class keyword to create a class: class Person() • Closed by default • Classes have a default constructor • class Person(name: String, lastname:String) • Can have an explicit constructor class Customer(var id: Int, var name: String) { } class Customer(var id: Int, var name: String) { init { name.toUpperCase() } } class Customer(var id: Int, var name: String) { init { name.toUpperCase() } //secondairy constructor constructor(email:String): this(0, ""){ } }
  8. Kotlin in Android Kotlin User Group Belgium Everything in Kotlin

    is Closed by default So it can’t be extended, and children (in case a class can be extended) can’t override its functions, unless it’s indicated with the reserved word open.
  9. Kotlin in Android Kotlin User Group Belgium Extensions (findViewById) Integrating

    Kotlin Android Extensions in our code through module build.gradle
  10. Kotlin in Android Kotlin User Group Belgium Recovering views in

    an Activity or Fragment In your MainActivity set welcomeMessage text:
  11. Kotlin in Android Kotlin User Group Belgium Recovering views from

    another view In compilation time, you’ll be able to reference any view from any other view. This means you could be referencing a view that is not a direct child of a parent. This will fail in execution time, when it tries to recover a view that doesn’t exist. In this case, the views are not cached as it did for Activities and Fragments. But if you use this carefully, it can be a really powerful tool.
  12. Kotlin in Android Kotlin User Group Belgium Extension functions fun

    String.hello(){ println("It's me!") } fun main(args: Array<String>) { println("Hello ".hello()) } Adds new behaviour to any existing class. Even if we don’t have the source code for that class.
  13. Kotlin in Android Kotlin User Group Belgium Lambdas in Kotlin

    val sum = { x: Int, y: Int -> x + y } 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. The full syntactic form of lambda expressions, i.e. literals of function types, is as follows: val sum: (Int, Int) -> Int = { x, y -> x + y }
  14. Kotlin in Android Kotlin User Group Belgium Lambdas in Kotlin

    ints.filter { it > 0 } // this literal is of type '(it: Int) -> Boolean' Lambda expression with 1 parameter => not uncommon. If Kotlin can figure out signature it allows us not to declare the only parameter, and will implicitly declare it for us under the name it:
  15. What MVP? Kotlin User Group Belgium “In MVP the presenter

    assumes the functionality of the "middle-man". In MVP, all presentation logic is pushed to the presenter.”
  16. What MVP? Kotlin User Group Belgium Pattern description: • user

    interface architectural pattern engineered to facilitate automated unit testing • improve the separation of concerns in presentation logic
  17. What MVP? Kotlin User Group Belgium The model is an

    interface defining the data to be displayed. M
  18. What MVP? Kotlin User Group Belgium The view is a

    passive interface that displays data (the model) and routes user commands (events) to the presenter to act upon that data. V
  19. What MVP? Kotlin User Group Belgium The presenter acts upon

    the model and the view. It retrieves data from repositories (the model), and formats it for display in the view. P
  20. Why MVP? Kotlin User Group Belgium Android activities are closely

    coupled to both interface and data access mechanisms. View DAL Adapters Cursors CursorAdapter
  21. Why MVP? Kotlin User Group Belgium • makes views independent

    from our data source • divide the application into at least three different layers which let us test them independently • take most of logic out from the activities so that we can test it without using instrumentation tests.
  22. Why MVP? Kotlin User Group Belgium Separation of concerns: Separate

    content from presentation and the data-processing from the content.
  23. Implement MVP in Android Kotlin User Group Belgium • Many

    variations of MVP • Adapt the pattern to your needs and projectsize • How many responabilities you want to delegate to the presenter • No standard way to implement it
  24. Implement MVP in Android Kotlin User Group Belgium Presenter •

    The middle man between View and Model • Retrieves data from the model and returns it (formatted) to the View • Also decides what happens when you interact with the View (Unlike MVC)
  25. Implement MVP in Android Kotlin User Group Belgium View •

    Implemented by an Activity (fragment, view, …) • Holds a reference to the Presenter • Creates the Presenter object (through Dagger) • Keep the View as dumb as possible • Only calls a method from the Presenter when there is an interaction (Button Click)
  26. Implement MVP in Android Kotlin User Group Belgium Model •

    The gateway to the domain or business layer. • Provider of the data we want in our View