infinum android talks #20 - benefits of using kotlin

37
We're an independent design & development agency.

Upload: infinum

Post on 08-Apr-2017

58 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Infinum Android Talks #20 - Benefits of using Kotlin

We're an independent design & development agency.

Page 2: Infinum Android Talks #20 - Benefits of using Kotlin

infinum.co/android-sweetsFresh news from Droid zone, curated by Željko Plesac

Page 3: Infinum Android Talks #20 - Benefits of using Kotlin

Benefits of using Kotlin

DINO KOVAČ

Page 4: Infinum Android Talks #20 - Benefits of using Kotlin

BASIC INFO

• statically typed JVM language

• fully interoperable with Java

• developed by JetBrains

Page 5: Infinum Android Talks #20 - Benefits of using Kotlin

HISTORY

• in development since 2010

• stable version released at 15.2.2016.

• used in 10 projects internally at JetBrains

Page 6: Infinum Android Talks #20 - Benefits of using Kotlin

GOALS

• concise

• expressive

• toolable

• interoperable

• pragmatic

Page 7: Infinum Android Talks #20 - Benefits of using Kotlin

REAL EXAMPLES

Page 8: Infinum Android Talks #20 - Benefits of using Kotlin

MODELS

Page 9: Infinum Android Talks #20 - Benefits of using Kotlin

public static class LoginResponse { @Json(name = "userName") private String username; @Json(name = "token_type") private String bearer; @Json(name = "expires_in") private int expiresIn; @Json(name = "access_token") private String accessToken; @Json(name = "refresh_token") private String refreshToken; public LoginResponse(String username, String bearer, int expiresIn, String accessToken, String refreshToken) { this.username = username; this.bearer = bearer; this.expiresIn = expiresIn; this.accessToken = accessToken; this.refreshToken = refreshToken; } public String getUsername() { return username; } public String getBearer() { return bearer; } public int getExpiresIn() { return expiresIn; } public String getAccessToken() { return accessToken; } public String getRefreshToken() { return refreshToken; }}

Page 10: Infinum Android Talks #20 - Benefits of using Kotlin

class LoginResponse( @Json(name = "userName") val username: String, @Json(name = "token_type") val bearer: String, @Json(name = "expires_in") val expiresIn: Int, @Json(name = "access_token") val accessToken: String, @Json(name = "refresh_token") val refreshToken: String)

Page 11: Infinum Android Talks #20 - Benefits of using Kotlin

KOTLIN ANDROID EXTENSIONS

Page 12: Infinum Android Talks #20 - Benefits of using Kotlin
Page 13: Infinum Android Talks #20 - Benefits of using Kotlin

OPERATIONS WITH COLLECTIONS

Page 14: Infinum Android Talks #20 - Benefits of using Kotlin

private List<FilterTagListView> tagGroupViews() { List<FilterTagListView> filterTagListViews = new ArrayList<>(); for (int i = 0; i < filterContainer.getChildCount(); i++) { View view = filterContainer.getChildAt(i); if (view instanceof FilterTagListView) { filterTagListViews.add((FilterTagListView) view); } } return filterTagListViews;}

Page 15: Infinum Android Talks #20 - Benefits of using Kotlin

private fun tagGroupViews(): List<FilterTagListView> { return (0..filterContainer.childCount - 1) .map { i -> filterContainer.getChildAt(i) } .filter { childView -> childView is FilterTagListView } .map { it as FilterTagListView } }

Page 16: Infinum Android Talks #20 - Benefits of using Kotlin

/** * finds view group which contains given tag and marks tag selected within that group */public void addSelectedTag(CoupeTag tag) { FilterTagListView view = null; List<FilterTagListView> tagGroupViews = tagGroupViews(); for (FilterTagListView tagGroupView : tagGroupViews) { if (tagGroupView.getFilter().getTitle().equals(tag.getFilter().getTitle())) { view = tagGroupView; break; } } if (view != null) { view.setTagSelection(tag.getTitle()); if (filtersSelectedListener != null) { filtersSelectedListener.onSelected(getSelectedTags()); } }}

Page 17: Infinum Android Talks #20 - Benefits of using Kotlin

/** * finds view group which contains given tag and marks tag selected within that group */fun addSelectedTag(tag: CoupeTag) { val view = tagGroupViews().firstOrNull { view -> view.filter.title == tag.filter.title } view?.let { view.setTagSelection(tag.title) filterSelectedListener?.onSelected(getSelectedTags()?.toList()) } }

Page 18: Infinum Android Talks #20 - Benefits of using Kotlin

LISTENER INTERFACES

Page 19: Infinum Android Talks #20 - Benefits of using Kotlin

// adapterpublic interface OnCheckedChangeListener { void onCheckedChange(Genre genre, boolean isChecked);} public void setOnCheckedChangeListener(@Nullable OnCheckedChangeListener listener) { this.listener = listener;}

if (listener != null) { listener.onCheckedChange(genre, isChecked);}

// activity this.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChange(Genre genre, boolean isChecked) { // TODO magic } });

Page 20: Infinum Android Talks #20 - Benefits of using Kotlin

// adaptervar onCheckedChangeListener: ((Genre, Boolean) -> Unit)? = null

onCheckedChangeListener?.invoke(genre, isChecked)

// activity genresAdapter.onCheckedChangeListener = { // TODO magic}

Page 21: Infinum Android Talks #20 - Benefits of using Kotlin

CONVENIENCES

Page 22: Infinum Android Talks #20 - Benefits of using Kotlin

List<AirDate> dummyAirdates = new ArrayList<>();dummyAirdates.add(new AirDate(ZonedDateTime.now(), 1));dummyAirdates.add(new AirDate(ZonedDateTime.now().plusDays(1), 2));dummyAirdates.add(new AirDate(ZonedDateTime.now().plusWeeks(1), 3));

Page 23: Infinum Android Talks #20 - Benefits of using Kotlin

val dummyAirDates = listOf( AirDate(ZonedDateTime.now(), 1), AirDate(ZonedDateTime.now().plusDays(1), 2), AirDate(ZonedDateTime.now().plusWeeks(1), 3) )

Page 24: Infinum Android Talks #20 - Benefits of using Kotlin

OTHERS

•setOf(), mapOf(), arrayOf() •emptyList(), emptySet(), emptyMap(), emptyArray()

• … plenty more

Page 25: Infinum Android Talks #20 - Benefits of using Kotlin

RISKS

Page 26: Infinum Android Talks #20 - Benefits of using Kotlin

WHAT IF JET BRAINS GOES OUT OF BUSINESS?

Page 27: Infinum Android Talks #20 - Benefits of using Kotlin

JETBRAINS

• 500+ employees

• fully bootstrapped

• steady revenue stream (subscription model)

• heavily invested in Kotlin

Page 28: Infinum Android Talks #20 - Benefits of using Kotlin

LANGUAGE STABILITY

Page 29: Infinum Android Talks #20 - Benefits of using Kotlin

SWIFT

OPEN SOURCE

Page 30: Infinum Android Talks #20 - Benefits of using Kotlin
Page 31: Infinum Android Talks #20 - Benefits of using Kotlin

I think it is fair to say that "open design" is slower and less predictable than "closed design”. However, the end result is significantly better, and therefore the tradeoff is worth it.

- CHRIS LATTNER, APPLE

Page 32: Infinum Android Talks #20 - Benefits of using Kotlin

WHAT IF GOOGLE BREAKS COMPATIBILITY?

Page 33: Infinum Android Talks #20 - Benefits of using Kotlin

Kotlin is interesting: works well with Java, more concise. JetBrains has done a nice job supporting this on android. But no plans to officially support anything new.

- ANWAR, ANDROIDENGTEAM

Page 34: Infinum Android Talks #20 - Benefits of using Kotlin

I don’t think we have any plans to break what already works there, but we don’t have plans to have a second, idiomatic-Kotlin version of the whole framework API surface area either.

- ADAM, ANDROIDENGTEAM

Page 35: Infinum Android Talks #20 - Benefits of using Kotlin

We don’t have any plans to move to a new language. Java has a lot of advantages to it and the versions 8, 9, and 10 have some pretty interesting stuff for developers.

- ANWAR, ANDROIDENGTEAM

Page 36: Infinum Android Talks #20 - Benefits of using Kotlin

CONCLUSION

• Kotlin can help you be more productive

• Kotlin is here to stay

• Be aware of the risks!

Page 37: Infinum Android Talks #20 - Benefits of using Kotlin

Thank you! [email protected] @DINO_BLACKSMITH

Visit infinum.co or find us on social networks:

infinum.co infinumco infinumco infinum