Slide 1

Slide 1 text

Meetup #2 Kotlin User Group Belgium Monday April 24th @Computerfutures Brussels

Slide 2

Slide 2 text

Thanks to our sponsors

Slide 3

Slide 3 text

Next Meetup Kotlin User Group Belgium June 19 or 26. Speaker(s) wanted!

Slide 4

Slide 4 text

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.

Slide 5

Slide 5 text

Kotlin in Android Kotlin User Group Belgium

Slide 6

Slide 6 text

Kotlin in Android Kotlin User Group Belgium Install Kotlin plugin

Slide 7

Slide 7 text

Kotlin in Android Kotlin User Group Belgium Create a new Android project

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

Kotlin in Android Kotlin User Group Belgium Convert the project to a Kotlin project Tools -> Kotlin -> Configure Kotlin in Project

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

Kotlin in Android Kotlin User Group Belgium Kotlin basics

Slide 12

Slide 12 text

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#

Slide 13

Slide 13 text

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" } }

Slide 14

Slide 14 text

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, ""){ } }

Slide 15

Slide 15 text

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.

Slide 16

Slide 16 text

Kotlin in Android Kotlin User Group Belgium Data class

Slide 17

Slide 17 text

Kotlin in Android Kotlin User Group Belgium Extensions (findViewById) Integrating Kotlin Android Extensions in our code through module build.gradle

Slide 18

Slide 18 text

Kotlin in Android Kotlin User Group Belgium Recovering views in an Activity or Fragment

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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.

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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 }

Slide 23

Slide 23 text

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:

Slide 24

Slide 24 text

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.”

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

What MVP? Kotlin User Group Belgium The model is an interface defining the data to be displayed. M

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

What MVP? Kotlin User Group Belgium

Slide 30

Slide 30 text

Why MVP? Kotlin User Group Belgium

Slide 31

Slide 31 text

Why MVP? Kotlin User Group Belgium Android activities are closely coupled to both interface and data access mechanisms. View DAL Adapters Cursors CursorAdapter

Slide 32

Slide 32 text

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.

Slide 33

Slide 33 text

Why MVP? Kotlin User Group Belgium Separation of concerns:

Slide 34

Slide 34 text

Why MVP? Kotlin User Group Belgium Separation of concerns: Separate content from presentation and the data-processing from the content.

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

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)

Slide 37

Slide 37 text

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)

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

Implement MVP in Android Kotlin User Group Belgium Demo

Slide 40

Slide 40 text

Our Github account Kotlin User Group Belgium https://github.com/BelKug/AndroidMVPBasicBoiler https://github.com/BelKug/MyFirstKotlin/