Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Experiments with Kotlin Multiplatform

Experiments with Kotlin Multiplatform

Lukas Lechner

April 04, 2019
Tweet

More Decks by Lukas Lechner

Other Decks in Technology

Transcript

  1. plugins { id 'kotlin-multiplatform' version '1.3.20' } kotlin { android()

    iosX64() … } Multiplatform Project Native App commonMain androidMain iosMain
  2. 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}") }
  3. 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
  4. UI Activities, Fragments, Views UI ViewController Presenter Presenter UseCases UseCases

    Repository Repository API Persistance API Persistance Utils Utils
  5. 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 )
  6. 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
  7. 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"))) }) } } }
  8. common interface JobsListView: BaseView { fun render(uiState: UiState<List<JobPosition>>) } class

    MainActivity : AppCompatActivity(), JobsListView { … override fun render(uiState: UiState<List<JobPosition>>) { when (uiState) { is UiState.Success -> { displayJobList(uiState.data) } is UiState.Loading -> { displayProgress() } is UiState.Error -> { displayError(uiState.throwable) } } … }
  9. common interface JobsListView: BaseView { fun render(uiState: UiState<List<JobPosition>>) } 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) } } … }
  10. • 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
  11. 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