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