Share
Embed
Transcript
23 slides
-
plugins { id 'kotlin-multiplatform' version '1.3.20' } kotlin { android()
iosX64() … } Multiplatform Project Native App commonMain androidMain iosMain -
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}") } -
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 -
UI Activities, Fragments, Views UI ViewController Presenter Presenter UseCases UseCases
Repository Repository API Persistance API Persistance Utils Utils -
UI Activities, Fragments, Views UI ViewController Presenter UseCases Repository API
Persistance Utils Shared Library -
Load Data from API Engine ThreadPool with HttpURLConnections Engine Asynchronous
NSURLSession internally -
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 ) -
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 -
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"))) }) } } } -
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) } } … } -
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) } } … } -
• 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 -
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