Android & Kotlin
The code awakens #02
Omar Miatello
Member of GDG Milano (Italy)
Android Developer @ Satispay
Personal profile
google.com/+OmarMiatello
Google+ Community: Kotlin for Android
goo.gl/mUKF1w
Google Presentation
#01 goo.gl/0jHLmE
#02 goo.gl/h3uG8M
#03 goo.gl/hnwvqu
Google Photo
#01 goo.gl/photos/cKP9L6zqZcxDRGzQ8
#02 goo.gl/photos/sXdpkbihCi5xAAnx7
#03 goo.gl/photos/P6kGhLE8yrWYnhAW6
Previously on Android & Kotlin #01: goo.gl/0jHLmE
Properties
val a: Int = 1 // val = READ ONLY (getter)
var b: Int = 1 // var = READ/WRITE (getter/setter)
String templates
"My name is $name $surname"
Lambdas
view.setOnClickListener { Log.d("TAG", "Item clicked!") }
Delegated properties (example: lazy)
val item by lazy { MyItem() }
dummy/HeroAdapter.java
public class HeroAdapter extends RecyclerView.Adapter<HeroAdapter.ViewHolder> {
private final List<HeroItem> mValues;
private final HeroOnClickListener mListener;
public HeroAdapter(List<HeroItem> items, HeroOnClickListener listener) {
mValues = items;
mListener = listener;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_hero, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
HeroItem item = mValues.get(position);
“Convert Java File to Kotlin File”
CTRL + ALT + SHIFT + K
(or CMD + ALT + SHIFT + K)
Kotlin vs Java - Part 2
Null-safety
Elvis Operator
Smart-cast
Collections
class MyKotlinClass {
val a: String = "ciao"
val b: String = null // Error at compile time
}
#5 Kotlin - Null Safety
http://kotlinlang.org/docs/reference/null-safety.html
vs
class MyKotlinClass {
val a: String = "ciao"
val b: String = null // Error at compile time
val c: String? = null
val d: String? = "ok"
}
#5 Kotlin - Null Safety
http://kotlinlang.org/docs/reference/null-safety.html
vs
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?
}
}
#5 Kotlin - Null Safety
http://kotlinlang.org/docs/reference/null-safety.html
vs
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?
}
}
#5 Kotlin - Null Safety
http://kotlinlang.org/docs/reference/null-safety.html
vs
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();
}
}
}
#5 Kotlin - Null Safety
http://kotlinlang.org/docs/reference/null-safety.html
vs
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);
}
}
#6 Kotlin - Elvis Operator
http://kotlinlang.org/docs/reference/idioms.html#if-not-null-and-else-shorthand
vs
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";
}
}
#6 Kotlin - Elvis Operator
http://kotlinlang.org/docs/reference/idioms.html#if-not-null-and-else-shorthand
vs
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";
}
}
#6 Kotlin - Elvis Operator
http://kotlinlang.org/docs/reference/idioms.html#if-not-null-and-else-shorthand
vs
class MyUtils {
static void example(View myView) {
if (myView instanceof ImageView) {
}
}
}
#7 Kotlin - Smart Cast
http://kotlinlang.org/docs/reference/typecasts.html#smart-casts
vs
class MyUtils {
static void example(View myView) {
if (myView instanceof ImageView) {
ImageView imageView =
(ImageView) myView;
}
}
}
#7 Kotlin - Smart Cast
http://kotlinlang.org/docs/reference/typecasts.html#smart-casts
vs
class MyUtils {
static void example(View myView) {
if (myView instanceof ImageView) {
ImageView imageView =
(ImageView) myView;
imageView.setImageAlpha(10);
}
}
}
#7 Kotlin - Smart Cast
http://kotlinlang.org/docs/reference/typecasts.html#smart-casts
vs
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");
}
}
}
#7 Kotlin - Smart Cast
http://kotlinlang.org/docs/reference/typecasts.html#smart-casts
vs
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");
}
}
}
#7 Kotlin - Smart Cast
http://kotlinlang.org/docs/reference/typecasts.html#smart-casts
vs
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");
}
}
}
#7 Kotlin - Smart Cast
http://kotlinlang.org/docs/reference/typecasts.html#smart-casts
vs
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");
}
}
#8 Kotlin - Using collections
http://kotlinlang.org/docs/reference/basic-syntax.html#using-collections
vs
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);
}
}
}
#8 Kotlin - Using collections
http://kotlinlang.org/docs/reference/basic-syntax.html#using-collections
vs
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();
}
});
}
}
#8 Kotlin - Using collections
http://kotlinlang.org/docs/reference/basic-syntax.html#using-collections
vs
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();
}
}
}
#8 Kotlin - Using collections
http://kotlinlang.org/docs/reference/basic-syntax.html#using-collections
vs
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);
}
} }
#8 Kotlin - Using collections
http://kotlinlang.org/docs/reference/basic-syntax.html#using-collections
vs
Android Example
From Java to Kotlin!
https://github.com/jacklt/KotlinExample/tree/ex1
dummy/Items.kt
data class HeroItem(val name: String, val gender: String, val power: Int)
dummy/HeroDummyContent.kt
object HeroDummyContent {
private val MALE = "Male"
private val FEMALE = "Female"
val ITEMS = listOf(
HeroItem("Hulk", MALE, 9),
HeroItem("Iron Man", MALE, 8),
HeroItem("Thor", MALE, 8),
HeroItem("Jessica Jones", FEMALE, 2),
HeroItem("Spiderman", MALE, 4)
)
}
dummy/HeroDummyContent.kt > object vs class
object HeroDummyContent {
private val MALE = "Male"
private val FEMALE = "Female"
val ITEMS = listOf(
HeroItem("Hulk", MALE, 9),
HeroItem("Iron Man", MALE, 8),
HeroItem("Thor", MALE, 8),
HeroItem("Jessica Jones", FEMALE, 2),
HeroItem("Spiderman", MALE, 4)
)
}
dummy/HeroDummyContent.kt > listOf(...)
object HeroDummyContent {
private val MALE = "Male"
private val FEMALE = "Female"
val ITEMS = listOf(
HeroItem("Hulk", MALE, 9),
HeroItem("Iron Man", MALE, 8),
HeroItem("Thor", MALE, 8),
HeroItem("Jessica Jones", FEMALE, 2),
HeroItem("Spiderman", MALE, 4)
)
}
dummy/HeroAdapter.kt
class HeroAdapter(val mValues: List<HeroItem>, val mListener: Function1<HeroItem, Unit>?) :
RecyclerView.Adapter<HeroAdapter.ViewHolder>() {
// ...
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val hero = mValues[position]
holder.item = hero
holder.nameView.text = "${hero.name} (Power: ${hero.power})"
holder.genderView.text = hero.gender
holder.itemView.setOnClickListener {
mListener?.invoke(hero)
}
}
// ...
}
dummy/HeroAdapter.kt > String template
class HeroAdapter(val mValues: List<HeroItem>, val mListener: Function1<HeroItem, Unit>?) :
RecyclerView.Adapter<HeroAdapter.ViewHolder>() {
// ...
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val hero = mValues[position]
holder.item = hero
holder.nameView.text = "${hero.name} (Power: ${hero.power})"
holder.genderView.text = hero.gender
holder.itemView.setOnClickListener {
mListener?.invoke(hero)
}
}
// ...
}
MainActivity.kt
class MainActivity : AppCompatActivity() {
private var currentHero: HeroItem? = null
private val adapter by lazy {
HeroAdapter(HeroDummyContent.ITEMS) {
// onItemClick ...
}
}
// ...
}
MainActivity.kt > adapter (lazy)
HeroAdapter(HeroDummyContent.ITEMS) { // onClick
val previousHero = currentHero
if (previousHero != null) { // fight
val conclusion = if (previousHero.power == it.power) {
"It's a draw"
} else if (previousHero.power > it.power) {
"${previousHero.name} wins!"
} else {
"${it.name} wins!"
}
Snackbar.make(fab, "${previousHero.name} vs ${it.name}!n$conclusion", Snackbar.LENGTH_LONG).show()
currentHero = null
} else { // assign current hero
Snackbar.make(fab, "Choose: ${it.name}", Snackbar.LENGTH_SHORT).show()
currentHero = it
}
}
MainActivity.kt > adapter (lazy) > Smart cast
HeroAdapter(HeroDummyContent.ITEMS) { // onClick
val previousHero = currentHero
if (previousHero != null) { // fight
val conclusion = if (previousHero.power == it.power) {
"It's a draw"
} else if (previousHero.power > it.power) {
"${previousHero.name} wins!"
} else {
"${it.name} wins!"
}
Snackbar.make(fab, "${previousHero.name} vs ${it.name}!n$conclusion", Snackbar.LENGTH_LONG).show()
currentHero = null
} else { // assign current hero
Snackbar.make(fab, "Choose: ${it.name}", Snackbar.LENGTH_SHORT).show()
currentHero = it
}
}
MainActivity.kt > adapter (lazy) > return if expression
HeroAdapter(HeroDummyContent.ITEMS) { // onClick
val previousHero = currentHero
if (previousHero != null) { // fight
val conclusion = if (previousHero.power == it.power) {
"It's a draw"
} else if (previousHero.power > it.power) {
"${previousHero.name} wins!"
} else {
"${it.name} wins!"
}
Snackbar.make(fab, "${previousHero.name} vs ${it.name}!n$conclusion", Snackbar.LENGTH_LONG).show()
currentHero = null
} else { // assign current hero
Snackbar.make(fab, "Choose: ${it.name}", Snackbar.LENGTH_SHORT).show()
currentHero = it
}
}
Questions?
Developers playground - #EX2
- Aggiungere le proprietà a HeroItem: hair (String?), eyes (String?)
- Mostrarle (se presenti) nella lista
- Se vengono scelti 2 eroi di sesso opposto, viene creato un nuovo eroe
- Si possono usare le proprietà dei genitori, oppure dei valori di default se
mancano
Start with: https://github.com/jacklt/KotlinExample/tree/ex1
Solution: https://github.com/jacklt/KotlinExample/tree/ex2
THANKS!
Omar Miatello, Member of GDG Milano (Italy)
Android Developer @ Satispay

Android & Kotlin - The code awakens #02

  • 1.
    Android & Kotlin Thecode awakens #02
  • 2.
    Omar Miatello Member ofGDG Milano (Italy) Android Developer @ Satispay Personal profile google.com/+OmarMiatello Google+ Community: Kotlin for Android goo.gl/mUKF1w Google Presentation #01 goo.gl/0jHLmE #02 goo.gl/h3uG8M #03 goo.gl/hnwvqu Google Photo #01 goo.gl/photos/cKP9L6zqZcxDRGzQ8 #02 goo.gl/photos/sXdpkbihCi5xAAnx7 #03 goo.gl/photos/P6kGhLE8yrWYnhAW6
  • 3.
    Previously on Android& Kotlin #01: goo.gl/0jHLmE Properties val a: Int = 1 // val = READ ONLY (getter) var b: Int = 1 // var = READ/WRITE (getter/setter) String templates "My name is $name $surname" Lambdas view.setOnClickListener { Log.d("TAG", "Item clicked!") } Delegated properties (example: lazy) val item by lazy { MyItem() }
  • 4.
    dummy/HeroAdapter.java public class HeroAdapterextends RecyclerView.Adapter<HeroAdapter.ViewHolder> { private final List<HeroItem> mValues; private final HeroOnClickListener mListener; public HeroAdapter(List<HeroItem> items, HeroOnClickListener listener) { mValues = items; mListener = listener; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_hero, parent, false); return new ViewHolder(view); } @Override public void onBindViewHolder(final ViewHolder holder, int position) { HeroItem item = mValues.get(position); “Convert Java File to Kotlin File” CTRL + ALT + SHIFT + K (or CMD + ALT + SHIFT + K)
  • 5.
    Kotlin vs Java- Part 2 Null-safety Elvis Operator Smart-cast Collections
  • 6.
    class MyKotlinClass { vala: String = "ciao" val b: String = null // Error at compile time } #5 Kotlin - Null Safety http://kotlinlang.org/docs/reference/null-safety.html vs
  • 7.
    class MyKotlinClass { vala: String = "ciao" val b: String = null // Error at compile time val c: String? = null val d: String? = "ok" } #5 Kotlin - Null Safety http://kotlinlang.org/docs/reference/null-safety.html vs
  • 8.
    class MyKotlinClass { vala: 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? } } #5 Kotlin - Null Safety http://kotlinlang.org/docs/reference/null-safety.html vs
  • 9.
    class MyKotlinClass { vala: 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? } } #5 Kotlin - Null Safety http://kotlinlang.org/docs/reference/null-safety.html vs
  • 10.
    class MyKotlinClass { vala: 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(); } } } #5 Kotlin - Null Safety http://kotlinlang.org/docs/reference/null-safety.html vs
  • 11.
    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); } } #6 Kotlin - Elvis Operator http://kotlinlang.org/docs/reference/idioms.html#if-not-null-and-else-shorthand vs
  • 12.
    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"; } } #6 Kotlin - Elvis Operator http://kotlinlang.org/docs/reference/idioms.html#if-not-null-and-else-shorthand vs
  • 13.
    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"; } } #6 Kotlin - Elvis Operator http://kotlinlang.org/docs/reference/idioms.html#if-not-null-and-else-shorthand vs
  • 14.
    class MyUtils { staticvoid example(View myView) { if (myView instanceof ImageView) { } } } #7 Kotlin - Smart Cast http://kotlinlang.org/docs/reference/typecasts.html#smart-casts vs
  • 15.
    class MyUtils { staticvoid example(View myView) { if (myView instanceof ImageView) { ImageView imageView = (ImageView) myView; } } } #7 Kotlin - Smart Cast http://kotlinlang.org/docs/reference/typecasts.html#smart-casts vs
  • 16.
    class MyUtils { staticvoid example(View myView) { if (myView instanceof ImageView) { ImageView imageView = (ImageView) myView; imageView.setImageAlpha(10); } } } #7 Kotlin - Smart Cast http://kotlinlang.org/docs/reference/typecasts.html#smart-casts vs
  • 17.
    class MyUtils { staticvoid 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"); } } } #7 Kotlin - Smart Cast http://kotlinlang.org/docs/reference/typecasts.html#smart-casts vs
  • 18.
    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"); } } } #7 Kotlin - Smart Cast http://kotlinlang.org/docs/reference/typecasts.html#smart-casts vs
  • 19.
    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"); } } } #7 Kotlin - Smart Cast http://kotlinlang.org/docs/reference/typecasts.html#smart-casts vs
  • 20.
    fun example() { valos = listOf("Android", "iOS", null, "Windows Phone") } class MyUtils { static void example() { List<String> os = Arrays.asList("Android", "iOS", null, "Windows Phone"); } } #8 Kotlin - Using collections http://kotlinlang.org/docs/reference/basic-syntax.html#using-collections vs
  • 21.
    fun example() { valos = 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); } } } #8 Kotlin - Using collections http://kotlinlang.org/docs/reference/basic-syntax.html#using-collections vs
  • 22.
    fun example() { valos = 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(); } }); } } #8 Kotlin - Using collections http://kotlinlang.org/docs/reference/basic-syntax.html#using-collections vs
  • 23.
    fun example() { valos = 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(); } } } #8 Kotlin - Using collections http://kotlinlang.org/docs/reference/basic-syntax.html#using-collections vs
  • 24.
    fun example() { valos = 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); } } } #8 Kotlin - Using collections http://kotlinlang.org/docs/reference/basic-syntax.html#using-collections vs
  • 25.
    Android Example From Javato Kotlin! https://github.com/jacklt/KotlinExample/tree/ex1
  • 26.
    dummy/Items.kt data class HeroItem(valname: String, val gender: String, val power: Int)
  • 27.
    dummy/HeroDummyContent.kt object HeroDummyContent { privateval MALE = "Male" private val FEMALE = "Female" val ITEMS = listOf( HeroItem("Hulk", MALE, 9), HeroItem("Iron Man", MALE, 8), HeroItem("Thor", MALE, 8), HeroItem("Jessica Jones", FEMALE, 2), HeroItem("Spiderman", MALE, 4) ) }
  • 28.
    dummy/HeroDummyContent.kt > objectvs class object HeroDummyContent { private val MALE = "Male" private val FEMALE = "Female" val ITEMS = listOf( HeroItem("Hulk", MALE, 9), HeroItem("Iron Man", MALE, 8), HeroItem("Thor", MALE, 8), HeroItem("Jessica Jones", FEMALE, 2), HeroItem("Spiderman", MALE, 4) ) }
  • 29.
    dummy/HeroDummyContent.kt > listOf(...) objectHeroDummyContent { private val MALE = "Male" private val FEMALE = "Female" val ITEMS = listOf( HeroItem("Hulk", MALE, 9), HeroItem("Iron Man", MALE, 8), HeroItem("Thor", MALE, 8), HeroItem("Jessica Jones", FEMALE, 2), HeroItem("Spiderman", MALE, 4) ) }
  • 30.
    dummy/HeroAdapter.kt class HeroAdapter(val mValues:List<HeroItem>, val mListener: Function1<HeroItem, Unit>?) : RecyclerView.Adapter<HeroAdapter.ViewHolder>() { // ... override fun onBindViewHolder(holder: ViewHolder, position: Int) { val hero = mValues[position] holder.item = hero holder.nameView.text = "${hero.name} (Power: ${hero.power})" holder.genderView.text = hero.gender holder.itemView.setOnClickListener { mListener?.invoke(hero) } } // ... }
  • 31.
    dummy/HeroAdapter.kt > Stringtemplate class HeroAdapter(val mValues: List<HeroItem>, val mListener: Function1<HeroItem, Unit>?) : RecyclerView.Adapter<HeroAdapter.ViewHolder>() { // ... override fun onBindViewHolder(holder: ViewHolder, position: Int) { val hero = mValues[position] holder.item = hero holder.nameView.text = "${hero.name} (Power: ${hero.power})" holder.genderView.text = hero.gender holder.itemView.setOnClickListener { mListener?.invoke(hero) } } // ... }
  • 32.
    MainActivity.kt class MainActivity :AppCompatActivity() { private var currentHero: HeroItem? = null private val adapter by lazy { HeroAdapter(HeroDummyContent.ITEMS) { // onItemClick ... } } // ... }
  • 33.
    MainActivity.kt > adapter(lazy) HeroAdapter(HeroDummyContent.ITEMS) { // onClick val previousHero = currentHero if (previousHero != null) { // fight val conclusion = if (previousHero.power == it.power) { "It's a draw" } else if (previousHero.power > it.power) { "${previousHero.name} wins!" } else { "${it.name} wins!" } Snackbar.make(fab, "${previousHero.name} vs ${it.name}!n$conclusion", Snackbar.LENGTH_LONG).show() currentHero = null } else { // assign current hero Snackbar.make(fab, "Choose: ${it.name}", Snackbar.LENGTH_SHORT).show() currentHero = it } }
  • 34.
    MainActivity.kt > adapter(lazy) > Smart cast HeroAdapter(HeroDummyContent.ITEMS) { // onClick val previousHero = currentHero if (previousHero != null) { // fight val conclusion = if (previousHero.power == it.power) { "It's a draw" } else if (previousHero.power > it.power) { "${previousHero.name} wins!" } else { "${it.name} wins!" } Snackbar.make(fab, "${previousHero.name} vs ${it.name}!n$conclusion", Snackbar.LENGTH_LONG).show() currentHero = null } else { // assign current hero Snackbar.make(fab, "Choose: ${it.name}", Snackbar.LENGTH_SHORT).show() currentHero = it } }
  • 35.
    MainActivity.kt > adapter(lazy) > return if expression HeroAdapter(HeroDummyContent.ITEMS) { // onClick val previousHero = currentHero if (previousHero != null) { // fight val conclusion = if (previousHero.power == it.power) { "It's a draw" } else if (previousHero.power > it.power) { "${previousHero.name} wins!" } else { "${it.name} wins!" } Snackbar.make(fab, "${previousHero.name} vs ${it.name}!n$conclusion", Snackbar.LENGTH_LONG).show() currentHero = null } else { // assign current hero Snackbar.make(fab, "Choose: ${it.name}", Snackbar.LENGTH_SHORT).show() currentHero = it } }
  • 36.
    Questions? Developers playground -#EX2 - Aggiungere le proprietà a HeroItem: hair (String?), eyes (String?) - Mostrarle (se presenti) nella lista - Se vengono scelti 2 eroi di sesso opposto, viene creato un nuovo eroe - Si possono usare le proprietà dei genitori, oppure dei valori di default se mancano Start with: https://github.com/jacklt/KotlinExample/tree/ex1 Solution: https://github.com/jacklt/KotlinExample/tree/ex2
  • 37.
    THANKS! Omar Miatello, Memberof GDG Milano (Italy) Android Developer @ Satispay