Kotlin Coroutines + Android Framework

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers. →

Kotlin Coroutines + Android Framework

Kotlin is changing the way we develop Android Apps.

Coroutines are another twist. We need to start understanding how to work with them in combination with the Android Framework.

In this lightning talk I explain an experiment where I tried Anko coroutines in Android listeners, which are available in 0.10-beta

I show how it can simplify Android development, how they fit on a typical MVP architecture, and if they make sense in that case.

Presented in Madrid Android Developer Group.

More about Kotlin + Android at http://antonioleiva.com/kotlin

Video (in Spanish): https://www.youtube.com/watch?v=uKLyNQMYLdM&t=35m50s

Antonio Leiva

May 11, 2017

Annual Pro sale · Save $30

Have presentations of your own?

Share with private links, password protection, and more control over your work.

Transcript

14 slides
  1. Coroutines for Android - Some libraries • kotlinx-coroutines-android (by Kotlin

    team) • async/await (by Metalab) • AsyncAwait-Android (by Niek Haarman) • Anko (by Kotlin team)
  2. Anko - coroutines embedded into the framework • Most async

    functions start from a UI event • What happens if we embed coroutines into listeners?
  3. Anko - coroutines embedded into the framework textView.onClick { val

    res = bg { findBackgroundColor() } textView.setBackgroundColor(res.await()) }
  4. Some examples - SearchView searchView.onQueryTextListener { onQueryTextChange { text ->

    progress.visible() val res = bg { doSearch(text) } updateContent(res.await()) progress.invisible() false } }
  5. Some examples - OnMenuItemClick toolbar.onMenuItemClick { when (it?.itemId) { R.id.action_edit

    -> startActivity<EditActivity>() R.id.action_save -> { bg { save() }.await() toast("Saved") } R.id.action_delete -> { bg { delete() }.await() toast("Deleted") } } }
  6. Does it fit into an architecture? searchView.onQueryTextListener { onQueryTextChange {

    presenter.onSearchTextChanged(it); false } } --- suspend fun onSearchTextChanged(search: String?) { view.showProgress() val res = bg { searchTextInteractor(search) } updateContent(res.await()) view.hideProgress() }
  7. Does it fit into an architecture? toolbar.onMenuItemClick { item ->

    item?.itemId?.let { presenter.onActionClicked(when (it) { R.id.action_edit -> Action.Edit R.id.action_save -> Action.Save R.id.action_delete -> Action.Delete else -> throw IllegalStateException("Unknown Action") }) } } suspend fun onActionClicked(action: Action) { when (action) { Action.Edit -> view.navigateToEdit() Action.Save -> { bg { save() }.await() view.showMessage("Saved") } Action.Delete -> { bg { delete() }.await() view.showMessage("Deleted") } } }
  8. Does it make sense? fun onSearchTextChanged(search: String?) { async(UI) {

    view.showProgress() val res = bg { searchTextInteractor(search) } updateContent(res.await()) view.hideProgress() } }

More Decks by Antonio Leiva

Other Decks in Programming