kotlin - lo swift di android

82
Kotlin lo Swift di Android Good News Everyone! 20 oct: Slide v1.1 3 oct: Slide v1.0

Upload: omar-miatello

Post on 13-Apr-2017

179 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Kotlin - lo Swift di Android

Kotlinlo Swift di Android

Good News Everyone!20 oct: Slide v1.1

3 oct: Slide v1.0

Page 2: Kotlin - lo Swift di Android

Omar MiatelloMember of GDG Milano (Italy)Android Developer @ Satispay

Personal profilegoogle.com/+OmarMiatelloGoogle+ Community: Kotlin for Androidgoo.gl/mUKF1wSlidesgoo.gl/7Vhy0s

Page 3: Kotlin - lo Swift di Android

What is Kotlin?

Statically typed programming language for the JVM, Android and the browser.(http://kotlinlang.org/)

Why Kotlin?

Concise: drastically reduce the amount of boilerplate code you need to write.

Safe: avoid entire classes of errors such as null pointer exceptions.

Interoperable: leverage existing frameworks and libraries of the JVM with 100% Java Interoperability.

and more... http://kotlinlang.org/docs/reference/comparison-to-java.html

Page 4: Kotlin - lo Swift di Android

vspublic class MyKotlinClass { val a: Int = 1}

public class MyJavaClass { private final int a = 1;

public int getA() { return a; }}

#1 Kotlin - Properties: val, varhttp://kotlinlang.org/docs/reference/properties.html

Page 5: Kotlin - lo Swift di Android

public class MyKotlinClass { val a: Int = 1 var b: Int = 1}

public class MyJavaClass { private final int a = 1; private int b = 1;

public int getA() { return a; }

public int getB() { return b; }

public void setB(int b) { this.b = b; }}

#1 Kotlin - Properties: val, varhttp://kotlinlang.org/docs/reference/properties.html

vs

Page 6: Kotlin - lo Swift di Android

public class MyKotlinClass { val a: Int = 1 var b: Int = 1 val c = 1 var d = 1}

public class MyJavaClass { private final int a = 1; private int b = 1; private final int c = 1; private int d = 1;

public int getA() { return a; } public int getB() { return b; } public void setB(int b) { this.b = b; } public int getC() { return c; } public int getD() { return d; } public void setD(int d) { this.d = d; }}

#1 Kotlin - Properties: val, varhttp://kotlinlang.org/docs/reference/properties.html

vs

Page 7: Kotlin - lo Swift di Android

class MyKotlinClass { val name = "Omar" val surname = "Miatello"}

class MyJavaClass { final String getName() { return "Omar"; }

final String getSurname() { return "Miatello"; }}

#2 Kotlin - String templateshttp://kotlinlang.org/docs/reference/basic-syntax.html#using-string-templates

vs

Page 8: Kotlin - lo Swift di Android

class MyKotlinClass { val name = "Omar" val surname = "Miatello" val example = "My name is $name $surname"}

class MyJavaClass { final String getName() { return "Omar"; }

final String getSurname() { return "Miatello"; }

final String getExample() { return String.format("My name is %s %s", getName(), getSurname()); }}

#2 Kotlin - String templateshttp://kotlinlang.org/docs/reference/basic-syntax.html#using-string-templates

vs

Page 9: Kotlin - lo Swift di Android

class MyKotlinClass { val name = "Omar" val surname = "Miatello" val example = "My name is $name $surname"}

class MyJavaClass { final String getName() { return "Omar"; }

final String getSurname() { return "Miatello"; }

final String getExample() { return String.format("My name is %s %s", getName(), getSurname()); }}

#2 Kotlin - String templateshttp://kotlinlang.org/docs/reference/basic-syntax.html#using-string-templates

vs

Page 10: Kotlin - lo Swift di Android

class MyJavaClass { class MyItem { }

MyItem item;}

#3 Kotlin - Delegated Properties: lazy (as example)http://kotlinlang.org/docs/reference/delegated-properties.html

vs

Page 11: Kotlin - lo Swift di Android

class MyJavaClass { class MyItem { }

MyItem item;

final MyItem getItem() { if (item == null) { item = new MyItem(); } return item; }}

#3 Kotlin - Delegated Properties: lazy (as example)http://kotlinlang.org/docs/reference/delegated-properties.html

vs

Page 12: Kotlin - lo Swift di Android

class MyItem

class MyKotlinClass { val item by lazy { MyItem() }}

// Simplified: in Kotlin is synchronized

class MyJavaClass { class MyItem { }

MyItem item;

final MyItem getItem() { if (item == null) { item = new MyItem(); } return item; }}

#3 Kotlin - Delegated Properties: lazy (as example)http://kotlinlang.org/docs/reference/delegated-properties.html

vs

Page 13: Kotlin - lo Swift di Android

class MyUtils {

static void example(View myView) { if (myView instanceof ImageView) {

} }

}

#4 Kotlin - Smart Casthttp://kotlinlang.org/docs/reference/typecasts.html#smart-casts

vs

Page 14: Kotlin - lo Swift di Android

class MyUtils {

static void example(View myView) { if (myView instanceof ImageView) { ImageView imageView = (ImageView) myView; } }

}

#4 Kotlin - Smart Casthttp://kotlinlang.org/docs/reference/typecasts.html#smart-casts

vs

Page 15: Kotlin - lo Swift di Android

class MyUtils {

static void example(View myView) { if (myView instanceof ImageView) { ImageView imageView = (ImageView) myView; imageView.setImageAlpha(10); } }

}

#4 Kotlin - Smart Casthttp://kotlinlang.org/docs/reference/typecasts.html#smart-casts

vs

Page 16: Kotlin - lo Swift di Android

class MyUtils {

static void example(View myView) { if (myView instanceof ImageView) { ImageView imageView = (ImageView) myView; imageView.setImageAlpha(10); } else if (myView instanceof TextView) { TextView textView = (TextView) myView; textView.setText("Ciao"); } }

}

#4 Kotlin - Smart Casthttp://kotlinlang.org/docs/reference/typecasts.html#smart-casts

vs

Page 17: Kotlin - lo Swift di Android

fun example1(myView: View) { if (myView is ImageView) { myView.setImageAlpha(10) } else if (myView is TextView) { myView.setText("Ciao") }}

class MyUtils {

static void example(View myView) { if (myView instanceof ImageView) { ImageView imageView = (ImageView) myView; imageView.setImageAlpha(10); } else if (myView instanceof TextView) { TextView textView = (TextView) myView; textView.setText("Ciao"); } }

}

#4 Kotlin - Smart Casthttp://kotlinlang.org/docs/reference/typecasts.html#smart-casts

vs

Page 18: Kotlin - lo Swift di Android

fun example1(myView: View) { if (myView is ImageView) { myView.setImageAlpha(10) } else if (myView is TextView) { myView.setText("Ciao") }}

fun example2(myView: View) { when (myView) { is ImageView -> myView.imageAlpha = 10 is TextView -> myView.text = "Ciao" }}

class MyUtils {

static void example(View myView) { if (myView instanceof ImageView) { ImageView imageView = (ImageView) myView; imageView.setImageAlpha(10); } else if (myView instanceof TextView) { TextView textView = (TextView) myView; textView.setText("Ciao"); } }

}

#4 Kotlin - Smart Casthttp://kotlinlang.org/docs/reference/typecasts.html#smart-casts

vs

NEW EXAMPL

E

Page 19: Kotlin - lo Swift di Android

class MyActivity : Activity() {

fun example() { val view = findViewById(R.id.button) view.setOnClickListener {

} }

}

class MyActivity extends Activity {

void example() { View view = findViewById(R.id.button); view.setOnClickListener(

); }

}

#5 Kotlin - Lambdashttp://kotlinlang.org/docs/reference/coding-conventions.html#lambdas

vs

Page 20: Kotlin - lo Swift di Android

class MyActivity : Activity() {

fun example() { val view = findViewById(R.id.button) view.setOnClickListener { Log.d("TAG", "Item clicked!") } }

}

class MyActivity extends Activity {

void example() { View view = findViewById(R.id.button); view.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { Log.d("TAG", "Item clicked!"); } } ); }

}

#5 Kotlin - Lambdashttp://kotlinlang.org/docs/reference/coding-conventions.html#lambdas

vs

Page 21: Kotlin - lo Swift di Android

class MyActivity : Activity() {

fun example() { val view = findViewById(R.id.button) view.setOnClickListener { Log.d("TAG", "Item clicked!") } }

}

class MyActivity extends Activity {

void example() { View view = findViewById(R.id.button); view.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { Log.d("TAG", "Item clicked!"); } } ); }

}

#5 Kotlin - Lambdashttp://kotlinlang.org/docs/reference/coding-conventions.html#lambdas

vs

Page 22: Kotlin - lo Swift di Android

fun example() { val os = listOf("Android", "iOS", null, "Windows Phone")

}

class MyUtils { static void example() { List<String> os = Arrays.asList("Android", "iOS", null, "Windows Phone"); }}

#6 Kotlin - Using collectionshttp://kotlinlang.org/docs/reference/basic-syntax.html#using-collections

vs

NEW EXAMPL

E

Page 23: Kotlin - lo Swift di Android

fun example() { val os = listOf("Android", "iOS", null, "Windows Phone")

os.filterNotNull()

}

class MyUtils { static void example() { List<String> os = Arrays.asList("Android", "iOS", null, "Windows Phone"); List<String> osNotNull = new ArrayList<>(); for (String name : os) { if (name != null) osNotNull.add(name); } }}

#6 Kotlin - Using collectionshttp://kotlinlang.org/docs/reference/basic-syntax.html#using-collections

vs

NEW EXAMPL

E

Page 24: Kotlin - lo Swift di Android

fun example() { val os = listOf("Android", "iOS", null, "Windows Phone")

os.filterNotNull().sortedBy { it.length() }

}

class MyUtils { static void example() { List<String> os = Arrays.asList("Android", "iOS", null, "Windows Phone"); List<String> osNotNull = new ArrayList<>(); for (String name : os) { if (name != null) osNotNull.add(name); } Collections.sort(osNotNull, new Comparator<String>() { @Override public int compare(String l, String r) { return r.length() - l.length(); } }); }}

#6 Kotlin - Using collectionshttp://kotlinlang.org/docs/reference/basic-syntax.html#using-collections

vs

NEW EXAMPL

E

Page 25: Kotlin - lo Swift di Android

fun example() { val os = listOf("Android", "iOS", null, "Windows Phone")

os.filterNotNull().sortedBy { it.length() } .map { it.toUpperCase() }

}

class MyUtils { static void example() { List<String> os = Arrays.asList("Android", "iOS", null, "Windows Phone"); List<String> osNotNull = new ArrayList<>(); for (String name : os) { if (name != null) osNotNull.add(name); } Collections.sort(osNotNull, new Comparator<String>() { @Override public int compare(String l, String r) { return l.length() - r.length(); } }); for (String name : osNotNull) { String value = name.toUpperCase(); } }}

#6 Kotlin - Using collectionshttp://kotlinlang.org/docs/reference/basic-syntax.html#using-collections

vs

NEW EXAMPL

E

Page 26: Kotlin - lo Swift di Android

fun example() { val os = listOf("Android", "iOS", null, "Windows Phone")

os.filterNotNull().sortedBy { it.length() } .map { it.toUpperCase() } .forEach { print(it) }

}

class MyUtils { static void example() { List<String> os = Arrays.asList("Android", "iOS", null, "Windows Phone"); List<String> osNotNull = new ArrayList<>(); for (String name : os) { if (name != null) osNotNull.add(name); } Collections.sort(osNotNull, new Comparator<String>() { @Override public int compare(String l, String r) { return l.length() - r.length(); } }); for (String name : osNotNull) { String value = name.toUpperCase(); print(value); }} }

#6 Kotlin - Using collectionshttp://kotlinlang.org/docs/reference/basic-syntax.html#using-collections

vs

NEW EXAMPL

E

Page 27: Kotlin - lo Swift di Android

fun String.isBig(): Boolean { return length() > 10}

#7 Kotlin - Extensionshttp://kotlinlang.org/docs/reference/extensions.html

vs

Page 28: Kotlin - lo Swift di Android

fun String.isBig(): Boolean { return length() > 10}

fun example() { "why?!".isBig() // false! "harder, better, ...".isBig() // true!}

#7 Kotlin - Extensionshttp://kotlinlang.org/docs/reference/extensions.html

vs

Page 29: Kotlin - lo Swift di Android

fun String.isBig(): Boolean { return length() > 10}

fun example() { "why?!".isBig() // false! "harder, better, ...".isBig() // true!}

// file MyUtils.java

class MyUtils { static boolean isBig(String str) { return str.length() > 10; }}

// file MyJavaClass.java

class MyJavaClass { void example() { MyUtils.isBig("why?!"); MyUtils.isBig("harder, better, ..."); }}

#7 Kotlin - Extensionshttp://kotlinlang.org/docs/reference/extensions.html

vs

Page 30: Kotlin - lo Swift di Android

fun String.isBig(): Boolean { return length() > 10}

fun example() { "why?!".isBig() "harder, better, ...".isBig()}

// file MyUtils.java

class MyUtils { static boolean isBig(String str) { return str.length() > 10; }}

// file MyJavaClass.java

class MyJavaClass { void example() { MyUtils.isBig("why?!"); MyUtils.isBig("harder, better, ..."); }}

#7 Kotlin - Extensionshttp://kotlinlang.org/docs/reference/extensions.html

vs

Page 31: Kotlin - lo Swift di Android

class Hero(val power: Int) class Hero { private final int power;

public Hero(int power) { this.power = power; }}

#8 Kotlin - Infix Notationhttp://kotlinlang.org/docs/reference/functions.html#infix-notation

vs

Page 32: Kotlin - lo Swift di Android

class Hero(val power: Int) { fun vs(opponent: Hero): Hero { return if (power > opponent.power) { this } else { opponent } }}

#8 Kotlin - Infix Notationhttp://kotlinlang.org/docs/reference/functions.html#infix-notation

vs

Page 33: Kotlin - lo Swift di Android

class Hero(val power: Int) { fun vs(opponent: Hero): Hero { return if (power > opponent.power) { this } else { opponent } }}

fun example() { val thor = Hero(7) val ironman = Hero(8) val spiderman = Hero(4)}

// Skip Java version of Hero class,// we use Kotlin class

class MyJavaClass { void example() { Hero thor = new Hero(7); Hero ironman = new Hero(8); Hero spiderman = new Hero(4); }}

#8 Kotlin - Infix Notationhttp://kotlinlang.org/docs/reference/functions.html#infix-notation

vs

Page 34: Kotlin - lo Swift di Android

class Hero(val power: Int) { fun vs(opponent: Hero): Hero { return if (power > opponent.power) { this } else { opponent } }}

fun example() { val thor = Hero(7) val ironman = Hero(8) val spiderman = Hero(4)

val theBest = thor vs ironman vs spiderman}

// Skip Java version of Hero class,// we use Kotlin class

class MyJavaClass { void example() { Hero thor = new Hero(7); Hero ironman = new Hero(8); Hero spiderman = new Hero(4);

Hero theBest = thor.vs(ironman).vs(spiderman); }}

#8 Kotlin - Infix Notationhttp://kotlinlang.org/docs/reference/functions.html#infix-notation

vs

Page 35: Kotlin - lo Swift di Android

class Hero(val power: Int) { fun vs(opponent: Hero): Hero { return if (power > opponent.power) { this } else { opponent } }}

fun example() { val thor = Hero(7) val ironman = Hero(8) val spiderman = Hero(4)

val theBest = thor vs ironman vs spiderman}

// Skip Java version of Hero class,// we use Kotlin class

class MyJavaClass { void example() { Hero thor = new Hero(7); Hero ironman = new Hero(8); Hero spiderman = new Hero(4);

Hero theBest = thor.vs(ironman).vs(spiderman); }}

#8 Kotlin - Infix Notationhttp://kotlinlang.org/docs/reference/functions.html#infix-notation

vs

Page 36: Kotlin - lo Swift di Android

class Male(val eyes: String)

class Female(val hair: String)

class Male { private String eyes;

public Male(String eyes) { this.eyes = eyes; }}

class Female { private String hair;

public Female(String hair) { this.hair = hair; }}

#9 Kotlin - Operator Overloadinghttp://kotlinlang.org/docs/reference/operator-overloading.html

vs

Page 37: Kotlin - lo Swift di Android

class Male(val eyes: String)

class Female(val hair: String)

class Baby(val eyes: String, val hair: String)

// Skip Java version of Male, Female and Baby// class, there is not enough space!

#9 Kotlin - Operator Overloadinghttp://kotlinlang.org/docs/reference/operator-overloading.html

vs

Page 38: Kotlin - lo Swift di Android

class Male(val eyes: String) { fun plus(her: Female): Baby { return Baby(this.eyes, her.hair) }}

class Female(val hair: String)

class Baby(val eyes: String, val hair: String)

// Skip Java version of Male, Female and Baby// class, there is not enough space!

#9 Kotlin - Operator Overloadinghttp://kotlinlang.org/docs/reference/operator-overloading.html

vs

Page 39: Kotlin - lo Swift di Android

class Male(val eyes: String) { fun plus(her: Female): Baby { return Baby(this.eyes, her.hair) }}

class Female(val hair: String)

class Baby(val eyes: String, val hair: String)

fun example() { val myBaby = Male("green") + Female("blond")}

// Skip Java version of Male, Female and Baby// class, there is not enough space!

class MyJavaClass { void example() { Baby myBaby = new Male("green").plus( new Female("blond")); }}

#9 Kotlin - Operator Overloadinghttp://kotlinlang.org/docs/reference/operator-overloading.html

vs

Page 40: Kotlin - lo Swift di Android

class Male(val eyes: String) { fun plus(her: Female): Baby { return Baby(this.eyes, her.hair) }}

class Female(val hair: String)

class Baby(val eyes: String, val hair: String)

fun example() { val myBaby = Male("green") + Female("blond")}

// Skip Java version of Male, Female and Baby// class, there is not enough space!

class MyJavaClass { void example() { Baby myBaby = new Male("green").plus(new Female("blond")); }}

#9 Kotlin - Operator Overloadinghttp://kotlinlang.org/docs/reference/operator-overloading.html

vs

Page 41: Kotlin - lo Swift di Android

class MyKotlinClass { val a: String = "ciao" val b: String = null // Error at compile time}

#10 Kotlin - Null Safetyhttp://kotlinlang.org/docs/reference/null-safety.html

vs

Page 42: Kotlin - lo Swift di Android

class MyKotlinClass { val a: String = "ciao" val b: String = null // Error at compile time val c: String? = null val d: String? = "ok"}

#10 Kotlin - Null Safetyhttp://kotlinlang.org/docs/reference/null-safety.html

vs

Page 43: Kotlin - lo Swift di Android

class MyKotlinClass { val a: String = "ciao" val b: String = null // Error at compile time val c: String? = null val d: String? = "ok"

fun example(e: String, f: String?) { e.length() f.length() // Error at compile time }}

static class MyUtils { void example(String e, String f) { e.length(); // throw NullPointerException? f.length(); // throw NullPointerException? }}

#10 Kotlin - Null Safetyhttp://kotlinlang.org/docs/reference/null-safety.html

vs

Page 44: Kotlin - lo Swift di Android

class MyKotlinClass { val a: String = "ciao" val b: String = null // Error at compile time val c: String? = null val d: String? = "ok"

fun example(e: String, f: String?) { e.length() f.length() // Error at compile time f?.length() }}

static class MyUtils { void example(String e, String f) { e.length(); // throw NullPointerException? f.length(); // throw NullPointerException? }}

#10 Kotlin - Null Safetyhttp://kotlinlang.org/docs/reference/null-safety.html

vs

Page 45: Kotlin - lo Swift di Android

class MyKotlinClass { val a: String = "ciao" val b: String = null // Error at compile time val c: String? = null val d: String? = "ok"

fun example(e: String, f: String?) { e.length() f.length() // Error at compile time f?.length()

if (f != null) { f.length() } }}

static class MyUtils { void example(String e, String f) { e.length(); // throw NullPointerException? f.length(); // throw NullPointerException?

if (e != null) { e.length(); }

if (f != null) { e.length(); } }}

#10 Kotlin - Null Safetyhttp://kotlinlang.org/docs/reference/null-safety.html

vs

Page 46: Kotlin - lo Swift di Android

val map = mapOf( "dog" to "woof", "cat" to "meow", "bird" to "tweet", "mouse" to "squeek")

fun sound(animal: String): String? { return map.get(animal)}

class MyUtils { static Map<String, String> map = // ...

static String sound(String animal) { return map.get(animal); }}

#11 Kotlin - Elvis Operatorhttp://kotlinlang.org/docs/reference/idioms.html#if-not-null-and-else-shorthand

vs

Page 47: Kotlin - lo Swift di Android

val map = mapOf( "dog" to "woof", "cat" to "meow", "bird" to "tweet", "mouse" to "squeek")

fun sound(animal: String): String? { return map.get(animal)}

fun example() { val foxSay = sound("fox") ?: "No one knows"}

class MyUtils { static Map<String, String> map = // ...

static String sound(String animal) { return map.get(animal); }

static void example() { String s = sound("fox"); String foxSay = s != null ? s : "No one knows";

}}

#11 Kotlin - Elvis Operatorhttp://kotlinlang.org/docs/reference/idioms.html#if-not-null-and-else-shorthand

vs

Page 48: Kotlin - lo Swift di Android

val map = mapOf( "dog" to "woof", "cat" to "meow", "bird" to "tweet", "mouse" to "squeek")

fun sound(animal: String): String? { return map.get(animal)}

fun example() { val foxSay = sound("fox") ?: "No one knows"}

class MyUtils { static Map<String, String> map = // ...

static String sound(String animal) { return map.get(animal); }

static void example() { String s = sound("fox"); String foxSay = s != null ? s : "No one knows";

}}

#11 Kotlin - Elvis Operatorhttp://kotlinlang.org/docs/reference/idioms.html#if-not-null-and-else-shorthand

vs

Page 49: Kotlin - lo Swift di Android

1. Open “Preferences”

Install Kotlin in Android Studio

Page 50: Kotlin - lo Swift di Android

1. Open “Preferences”

2. Choose “Plugins” and “Install JetBrains plugin…”

Install Kotlin in Android Studio

Page 51: Kotlin - lo Swift di Android

1. Open “Preferences”

2. Choose “Plugins” and “Install JetBrains plugin…”

3. Install “Kotlin” and “Kotlin Extensions For Android”

Install Kotlin in Android Studio

Page 52: Kotlin - lo Swift di Android

1. Open “Preferences”

2. Choose “Plugins” and “Install JetBrains plugin…”

3. Install “Kotlin” and “Kotlin Extensions For Android”

4. Restart Android Studio, and open (or create) a project

Install Kotlin in Android Studio

Page 53: Kotlin - lo Swift di Android

1. Open “Preferences”

2. Choose “Plugins” and “Install JetBrains plugin…”

3. Install “Kotlin” and “Kotlin Extensions For Android”

4. Restart Android Studio, and open (or create) a project

5. Create a new “Kotlin class”

Install Kotlin in Android Studio

Page 54: Kotlin - lo Swift di Android

1. Open “Preferences”

2. Choose “Plugins” and “Install JetBrains plugin…”

3. Install “Kotlin” and “Kotlin Extensions For Android”

4. Restart Android Studio, and open (or create) a project

5. Create a new “Kotlin class”

6. Choose from menu “Tools” > “Kotlin” > “Configure Kotlin in Project”

Install Kotlin in Android Studio

Page 55: Kotlin - lo Swift di Android

1. Open “Preferences”

2. Choose “Plugins” and “Install JetBrains plugin…”

3. Install “Kotlin” and “Kotlin Extensions For Android”

4. Restart Android Studio, and open (or create) a project

5. Create a new “Kotlin class”

6. Choose from menu “Tools” > “Kotlin” > “Configure Kotlin in Project”

7. Ready for Kotlin! :)

dependencies { // other dependencies ... compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"}buildscript { ext.kotlin_version = '0.13.1514' repositories { mavenCentral() } dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" }}repositories { mavenCentral()}

Install Kotlin in Android Studio

Page 56: Kotlin - lo Swift di Android

Preference Utils (Step 1)Write a small utility for Android!

Page 57: Kotlin - lo Swift di Android

Preference Utils (Step 1) - PreferenceUtils.ktclass AppPreferences(private val context: Context) { val preferences: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)}

Page 58: Kotlin - lo Swift di Android

Preference Utils (Step 1) - PreferenceUtils.ktclass AppPreferences(private val context: Context) { val preferences: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)}

object PreferenceDelegates { public fun string(defaultValue: String? = null): ReadWriteProperty<AppPreferences, String?> { return PrefString(defaultValue) }}

Page 59: Kotlin - lo Swift di Android

Preference Utils (Step 1) - PreferenceUtils.ktclass AppPreferences(private val context: Context) { val preferences: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)}

object PreferenceDelegates { public fun string(defaultValue: String? = null): ReadWriteProperty<AppPreferences, String?> { return PrefString(defaultValue) }}

private class PrefString(private val defaultValue: String?) : ReadWriteProperty<AppPreferences, String?> { override fun get(thisRef: AppPreferences, property: PropertyMetadata): String? { return thisRef.preferences.getString(property.name, defaultValue) } override fun set(thisRef: AppPreferences, property: PropertyMetadata, value: String?) { thisRef.preferences.edit().putString(property.name, value).apply() }}

Page 60: Kotlin - lo Swift di Android

Preference Utils (Step 1) - PreferenceUtils.ktclass AppPreferences(private val context: Context) { val preferences: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)}

object PreferenceDelegates { public fun string(defaultValue: String? = null): ReadWriteProperty<AppPreferences, String?> { return PrefString(defaultValue) }}

private class PrefString(private val defaultValue: String?) : ReadWriteProperty<AppPreferences, String?> { override fun get(thisRef: AppPreferences, property: PropertyMetadata): String? { return thisRef.preferences.getString(property.name, defaultValue) } override fun set(thisRef: AppPreferences, property: PropertyMetadata, value: String?) { thisRef.preferences.edit().putString(property.name, value).apply() }}

Page 61: Kotlin - lo Swift di Android

Preference Utils (Step 1) - PreferenceUtils.ktclass AppPreferences(private val context: Context) { val preferences: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)}

object PreferenceDelegates { public fun string(defaultValue: String? = null): ReadWriteProperty<AppPreferences, String?> { return PrefString(defaultValue) }}

private class PrefString(private val defaultValue: String?) : ReadWriteProperty<AppPreferences, String?> { override fun get(thisRef: AppPreferences, property: PropertyMetadata): String? { return thisRef.preferences.getString(property.name, defaultValue) } override fun set(thisRef: AppPreferences, property: PropertyMetadata, value: String?) { thisRef.preferences.edit().putString(property.name, value).apply() }}

Page 62: Kotlin - lo Swift di Android

Preference Utils (Step 1) - PreferenceUtils.ktclass AppPreferences(private val context: Context) { val preferences: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)

var userName by PreferenceDelegates.string() var userFavoriteAnimal by PreferenceDelegates.string()}

// ...

Page 63: Kotlin - lo Swift di Android

Preference Utils (Step 1) - MyActivity.javapublic class MyActivity extends AppCompatActivity {

private AppPreferences pref;

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); pref = new AppPreferences(this); }}

Page 64: Kotlin - lo Swift di Android

Preference Utils (Step 1) - MyActivity.javapublic class MyActivity extends AppCompatActivity {

private AppPreferences pref;

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); pref = new AppPreferences(this); String userName = pref.getUserName(); }}

Page 65: Kotlin - lo Swift di Android

Preference Utils (Step 1) - MyActivity.javapublic class MyActivity extends AppCompatActivity {

private AppPreferences pref;

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); pref = new AppPreferences(this); String userName = pref.getUserName();

String userFavoriteAnimal = pref.getUserFavoriteAnimal(); if (userFavoriteAnimal == null) { pref.setUserFavoriteAnimal("fox"); } }}

Page 66: Kotlin - lo Swift di Android

Preference Utils (Step 2)Write a small utility for your Android app!

Page 67: Kotlin - lo Swift di Android

Preference Utils (Step 2) - App.ktclass App : Application() { val pref by lazy { AppPreferences(this) }

override fun onCreate() { super.onCreate() // other initialization here }}

Page 68: Kotlin - lo Swift di Android

Preference Utils (Step 2) - App.ktclass App : Application() { val pref by lazy { AppPreferences(this) }

override fun onCreate() { super.onCreate() // other initialization here }}

Page 69: Kotlin - lo Swift di Android

Preference Utils (Step 2) - AndroidManifest.xml<manifest xmlns:android="http://schemas.android.com/apk/res/android"> <application android:name=".application.App" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" > <activity android:name=".MyActivity" /> </application></manifest>

Page 70: Kotlin - lo Swift di Android

Preference Utils (Step 2) - AndroidManifest.xml<manifest xmlns:android="http://schemas.android.com/apk/res/android"> <application android:name=".application.App" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" > <activity android:name=".MyActivity" /> </application></manifest>

Page 71: Kotlin - lo Swift di Android

Preference Utils (Step 2) - BaseActivity.ktopen class BaseActivity : AppCompatActivity() { val app by lazy { application as App } val pref by lazy { app.pref }}

Page 72: Kotlin - lo Swift di Android

Preference Utils (Step 2) - MyActivity.javapublic class MyActivity extends BaseActivity {

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

String userName = getPref().getUserName();

String userFavoriteAnimal = getPref().getUserFavoriteAnimal(); if (userFavoriteAnimal == null) { getPref().setUserFavoriteAnimal("fox"); } }}

Page 73: Kotlin - lo Swift di Android

Preference Utils (Step 3)Boost your Android app!

Page 74: Kotlin - lo Swift di Android

Preference Utils (Step 3) - MyActivity.ktpublic class MyActivity : BaseActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState)

val userName = pref.userName

val userFavoriteAnimal = pref.userFavoriteAnimal if (userFavoriteAnimal == null) { pref.userFavoriteAnimal = "fox" } }}

Page 75: Kotlin - lo Swift di Android

MyActivity.kt vs MyActivity.javapublic class MyActivity : BaseActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState)

val userName = pref.userName

val userFavoriteAnimal = pref.userFavoriteAnimal if (userFavoriteAnimal == null) { pref.userFavoriteAnimal = "fox" } }}

public class MyActivity extends BaseActivity {

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

String userName = getPref().getUserName();

String userFavoriteAnimal = getPref().getUserFavoril(); if (userFavoriteAnimal == null) { getPref().setUserFavoriteAnimal("fox"); } }}

Page 77: Kotlin - lo Swift di Android

Play with animations! - AnimationUtils.javafun View.animateAppear() { alpha = 0f scaleX = 0f scaleY = 0f val animator = ViewCompat.animate(this).alpha(1f).scaleX(1f).scaleY(1f) animator.start()}

Page 78: Kotlin - lo Swift di Android

Play with animations! - AnimationUtils.javafun View.animateAppear() { alpha = 0f scaleX = 0f scaleY = 0f val animator = ViewCompat.animate(this).alpha(1f).scaleX(1f).scaleY(1f) animator.start()}

fun calcDistance(x1: Int, y1: Int, x2: Int, y2: Int) = Math.sqrt(((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2)).toDouble())

Page 79: Kotlin - lo Swift di Android

Play with animations! - AnimationUtils.javafun View.animateAppear() { alpha = 0f scaleX = 0f scaleY = 0f val animator = ViewCompat.animate(this).alpha(1f).scaleX(1f).scaleY(1f) animator.start()}

fun calcDistance(x1: Int, y1: Int, x2: Int, y2: Int) = Math.sqrt(((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2)).toDouble())

fun List<View>.animateRelativeHierarchicalTiming(centerX: Int = 0, centerY: Int = 0) { forEach { val x = it.left val y = it.top val distance = calcDistance(centerX, centerY, x, y) it.postDelayed({ it.animateAppear() }, distance.toLong()) }}

Page 80: Kotlin - lo Swift di Android

Play with animations! - AnimationUtils.javafun View.animateAppear() { alpha = 0f scaleX = 0f scaleY = 0f val animator = ViewCompat.animate(this).alpha(1f).scaleX(1f).scaleY(1f) animator.start()}

fun calcDistance(x1: Int, y1: Int, x2: Int, y2: Int) = Math.sqrt(((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2)).toDouble())

fun List<View>.animateRelativeHierarchicalTiming(centerX: Int = 0, centerY: Int = 0) { forEach { val x = it.left val y = it.top val distance = calcDistance(centerX, centerY, x, y) it.postDelayed({ it.animateAppear() }, distance.toLong()) }}

Page 81: Kotlin - lo Swift di Android

Upcoming Events

JavaLand 2016 | Kotlin in Action (Justin Lee)Codemotion 2015 | Kotlin for Android Developers (Antonio Leiva Gordillo)Oredev | Advancing Android Development with the Kotlin Language (Jake Wharton)dotnetconf 2015 | Kotlin in Production. Why? How? (Kirill Bubochkin)DroidCon London 2015 | Advancing Development with the Kotlin Language (Jake Wharton)JavaOne 2015 | Kotlin in Anger (Justin Lee)JavaOne 2015 | Type Inference in Kotlin (Svetlana Isakova)JOKER | The Experience of Using Kotlin in JetBrains (Dmitry Jemerov)Mobilization 2015 | Kotlin for Android (Paweł Gajda)DevFest 2015 | Kotlin: lo Swift di Android (Omar Miatello)

http://kotlinlang.org/docs/events.html

Questions?

Page 82: Kotlin - lo Swift di Android

THANKS!Omar Miatello, Member of GDG Milano (Italy)

Android Developer @ Satispay