Slide 1

Slide 1 text

Yo Dawg! I heard you like Kotlin Antonio Leiva

Slide 2

Slide 2 text

Menu (hope you’re hungry!) 1. Kotlin instead of Java 2. Kotlin for Gradle files 3. Kotlin for views 4. KOTLIN EVERYWHEEAAAR!

Slide 3

Slide 3 text

Kotlin instead of Java

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

override fun showAlbum(albumDetail: AlbumDetail?) { if (albumDetail != null) { updateAlbumDetail(albumDetail) } else { postponeTransitions() } }

Slide 6

Slide 6 text

override fun showAlbum(albumDetail: AlbumDetail?) { albumDetail?.let {album -> updateAlbumDetail(album)} ?: postponeTransitions() }

Slide 7

Slide 7 text

override fun showAlbum(albumDetail: AlbumDetail?) { albumDetail?.let {album -> updateAlbumDetail(album)} ?: postponeTransitions() }

Slide 8

Slide 8 text

override fun showAlbum(albumDetail: AlbumDetail?) { albumDetail?.let { updateAlbumDetail(albumDetail) } ?: postponeTransitions() }

Slide 9

Slide 9 text

override fun showAlbum(albumDetail: AlbumDetail?) { albumDetail?.let { updateAlbumDetail(it) } ?: postponeTransitions() }

Slide 10

Slide 10 text

override fun showAlbum(albumDetail: AlbumDetail?) { albumDetail?.let(this::updateAlbumDetail) ?: postponeTransitions() }

Slide 11

Slide 11 text

private fun makeStatusBarTransparent() { supportsLollipop { window.setFlags(FLAG_TRANSLUCENT_STATUS, FLAG_TRANSLUCENT_STATUS) } }

Slide 12

Slide 12 text

inline fun supportsLollipop(code: () -> Unit) { if (Build.VERSION.SDK_INT >= 21) { code.invoke() } }

Slide 13

Slide 13 text

ui.listCard.animate().alpha(transparent) .setListener(object : AnimatorListenerAdapter() { override fun onAnimationEnd(animation: Animator?) { supportFinishAfterTransition() } })

Slide 14

Slide 14 text

inline fun ViewPropertyAnimator.onAnimationEnd( crossinline continuation: (Animator) -> Unit) { setListener(object : AnimatorListenerAdapter() { override fun onAnimationEnd(animation: Animator) { continuation(animation) } }) }

Slide 15

Slide 15 text

ui.listCard.animate().alpha(transparent) .onAnimationEnd { supportFinishAfterTransition() }

Slide 16

Slide 16 text

Kotlin instead of Java - Verdict

Slide 17

Slide 17 text

https://antonioleiva.com

Slide 18

Slide 18 text

Kotlin for Gradle files

Slide 19

Slide 19 text

Kotlin DSL - Pros

Slide 20

Slide 20 text

Kotlin DSL - Cons ● Not easy to configure ● Knowledge about plugin structure ● Not much documentation or examples

Slide 21

Slide 21 text

Kotlin DSL - Steps 1. Rename Gradle files 2. Write the config file 3. Convert and compile little by little

Slide 22

Slide 22 text

1. Rename Gradle Files .kts

Slide 23

Slide 23 text

2. Write the config file plugins { `kotlin-dsl` }

Slide 24

Slide 24 text

2. Write the config file class ProjectConfiguration { val buildPlugins = BuildPlugins() val android = Android() val libs = Libs() val testLibs = TestLibs() }

Slide 25

Slide 25 text

2. Write the config file class Android { val buildToolsVersion = "26.0.2" val minSdkVersion = 19 val targetSdkVersion = 26 val compileSdkVersion = 26 val applicationId = "com.antonioleiva.bandhookkotlin" val versionCode = 1 val versionName = "0.1" }

Slide 26

Slide 26 text

3. Convert and compile little by little val config = ProjectConfiguration() ... dependencies { classpath(config.buildPlugins.androidGradle) classpath(config.buildPlugins.kotlinGradlePlugin) }

Slide 27

Slide 27 text

3. Convert and compile little by little defaultConfig { 1. applicationId = config.android.applicationId 2. minSdkVersion(config.android.minSdkVersion) 3. targetSdkVersion(config.android.targetSdkVersion) 4. versionCode = config.android.versionCode 5. versionName = config.android.versionName }

Slide 28

Slide 28 text

Kotlin DSL - Verdict

Slide 29

Slide 29 text

Kotlin DSL

Slide 30

Slide 30 text

Kotlin for views

Slide 31

Slide 31 text

Anko Layouts verticalLayout { val user = editText { hint = "Username" } val pass = editText { hint = "Password" } button("Login") { setOnClickListener { longToast("${user.text}, ${pass.text}") } } }

Slide 32

Slide 32 text

Anko Layouts

Slide 33

Slide 33 text

Anko Layouts - Pros ● 4x faster ● Better composability ● Easier data binding, complex calculations

Slide 34

Slide 34 text

Anko Layouts - Cons ● Lack of designer ● Learn again from scratch ● Some things are more complex (like styles)

Slide 35

Slide 35 text

Anko Layouts recycler = autoFitRecycler() .apply(AutofitRecyclerView::style) .lparams(matchParent, matchParent) { behavior = AppBarLayout.ScrollingViewBehavior() }

Slide 36

Slide 36 text

fun ViewManager.autoFitRecycler(theme: Int = 0) = autoFitRecycler(theme) {} inline fun ViewManager.autoFitRecycler(theme: Int = 0, init: AutofitRecyclerView.() -> Unit) = ankoView(::AutofitRecyclerView, theme, init) Anko Layouts - Custom views

Slide 37

Slide 37 text

Anko Layouts recycler = autoFitRecycler() .apply(AutofitRecyclerView::style) .lparams(matchParent, matchParent) { behavior = AppBarLayout.ScrollingViewBehavior() }

Slide 38

Slide 38 text

Anko Layouts - styling fun AutofitRecyclerView.style() { clipToPadding = false columnWidth = dimen(R.dimen.column_width) scrollBarStyle = View.SCROLLBARS_OUTSIDE_OVERLAY horizontalPadding = dimen(R.dimen.recycler_spacing) verticalPadding = dip(2) addItemDecoration(PaddingItemDecoration(dip(2))) }

Slide 39

Slide 39 text

Anko Layouts - styling .applyRecursively { view -> when (view) { is EditText -> view.textSize = 18f } }

Slide 40

Slide 40 text

Anko Layouts recycler = autoFitRecycler() .apply(AutofitRecyclerView::style) .lparams(matchParent, matchParent) { behavior = AppBarLayout.ScrollingViewBehavior() }

Slide 41

Slide 41 text

Anko Layouts - Verdict

Slide 42

Slide 42 text

Anko Layouts

Slide 43

Slide 43 text

KOTLIN EVERYWHEEAAAR!

Slide 44

Slide 44 text

Multi-platform projects (Kotlin 1.2) Common Kotlin

Slide 45

Slide 45 text

Multi-platform projects (Kotlin 1.2) Common Android Kotlin

Slide 46

Slide 46 text

Multi-platform projects (Kotlin 1.2) Common Android Web Kotlin JS

Slide 47

Slide 47 text

Multi-platform projects (Kotlin 1.2) Common Android Server Kotlin Web

Slide 48

Slide 48 text

Multi-platform projects (Kotlin 1.2) Common Android Server Web iOS Kotlin/Native

Slide 49

Slide 49 text

Multi-platform projects (Kotlin 1.2) expect class MyClass { fun foo(): String fun bar(): Int } actual class MyClass { fun foo(): String = “” fun bar(): Int = 20 }

Slide 50

Slide 50 text

Multi-platform projects (Kotlin 1.2)

Slide 51

Slide 51 text

No content