Slide 1

Slide 1 text

Experiments with Kotlin Multiplatform AndroidHeads Vienna 2019/04/04

Slide 2

Slide 2 text

What is Kotlin Multiplatform?

Slide 3

Slide 3 text

JVM Native JavaScript Kotlin everywhere

Slide 4

Slide 4 text

Multiplatform Project Compilation Targets

Slide 5

Slide 5 text

plugins { id 'kotlin-multiplatform' version '1.3.20' } kotlin { android() iosX64() … } Multiplatform Project Native App commonMain androidMain iosMain

Slide 6

Slide 6 text

expect - actual common expect fun logDebug(tag: String, message: String) import android.util.Log actual fun logDebug(tag: String, message: String) { Log.d(tag, message) } actual fun logDebug(tag: String, message: String) { println("${tag} : ${message}") }

Slide 7

Slide 7 text

Goal: Develop App with Kotlin Multiplatform • Load data from Web-API (Github Jobs API) • Display job information in a list • Persist job data for offline availability

Slide 8

Slide 8 text

UI Activities, Fragments, Views UI ViewController Presenter Presenter UseCases UseCases Repository Repository API Persistance API Persistance Utils Utils

Slide 9

Slide 9 text

UI Activities, Fragments, Views UI ViewController Presenter UseCases Repository API Persistance Utils Shared Library

Slide 10

Slide 10 text

Load Data from API

Slide 11

Slide 11 text

Load Data from API Engine ThreadPool with HttpURLConnections Engine Asynchronous NSURLSession internally

Slide 12

Slide 12 text

Load Data from API @Serializable data class JobPositionDto( @SerialName(“id") val id: String, @SerialName("company") val company: String, @SerialName("location") val location: String, @SerialName("title") val title: String, @SerialName("type") val type: String )

Slide 13

Slide 13 text

Persist Data

Slide 14

Slide 14 text

Repository

Slide 15

Slide 15 text

UI Activities, Fragments, Views UI ViewController Presenter UseCases Repository API Persistance Utils Shared Library Create instance of presenter Call getJobList() in Lifecycle method Implement render() of JobListView

Slide 16

Slide 16 text

Presenter - common class JobsListPresenter(private val view: JobsListView) : BasePresenter(view) { private val repository = JobPositionRepositoryImpl(GithubJobsApi()) fun getJobsList() { view.render(UiState.Loading()) launch(IoDispatcher) { repository.getJobsList().fold({ view.render(UiState.Success(it)) }, { view.render(UiState.Error(Throwable("Loading Jobs failed"))) }) } } }

Slide 17

Slide 17 text

common interface JobsListView: BaseView { fun render(uiState: UiState>) } class MainActivity : AppCompatActivity(), JobsListView { … override fun render(uiState: UiState>) { when (uiState) { is UiState.Success -> { displayJobList(uiState.data) } is UiState.Loading -> { displayProgress() } is UiState.Error -> { displayError(uiState.throwable) } } … }

Slide 18

Slide 18 text

common interface JobsListView: BaseView { fun render(uiState: UiState>) } class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, JobsListView { … func render(uiState: UiState) { if(uiState is UiState.Success){ let state = uiState as! UiState.Success displayJobList(jobs: state.data as! [JobPosition]) } if(uiState is UiState.Loading){ displayProgress() } if(uiState is UiState.Error){ let state = uiState as! UiState.Error displayError(error: state.throwable as! KotlinThrowable) } } … }

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

Ready for Production? Sharing pure logic Sharing architecture

Slide 21

Slide 21 text

• Experimental: Expect lots of changes • Very little documentation, blogposts, samples, … • Only Single-Threaded Coroutines in the common code • Not perfect Swift - Kotlin Interoperability • iOS debugging of the shared library not possible • Important libraries for the common module still missing (e.g. Date) Sharing architecture

Slide 22

Slide 22 text

Summary • Kotlin can run everywhere • Multiplatform Projects: More that one compilation target • Mobile Code Sharing: Everything except the UI and some platform specific functionality can be shared • Sample Application “Kotlin MPP Jobs” • Production-ready only for simple use cases

Slide 23

Slide 23 text

Resources • https://github.com/Lukle/Kotlin-mpp-jobs • @lukleDev on Twitter