Slide 1

Slide 1 text

An Android Dev start to Kotlin MPP

Slide 2

Slide 2 text

Premises I want to reuse my knowledge I am an Android Dev I don’t want to be x-plat

Slide 3

Slide 3 text

What about languages?

Slide 4

Slide 4 text

Or actually any other example Empire State Building Lights Calendar App

Slide 5

Slide 5 text

Architecture Activity ViewModel Repository

Slide 6

Slide 6 text

Libraries Repository Retrofit Gson / Moshi

Slide 7

Slide 7 text

Libraries Repository Retrofit Gson / Moshi Ktor kotlinx.serializer

Slide 8

Slide 8 text

Modularization Activity ViewModel Repository Native module Shared module

Slide 9

Slide 9 text

Modularization Repository Shared module

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

There is an app a plugin for that. PROJECT SETUP 1

Slide 12

Slide 12 text

Install & Profit

Slide 13

Slide 13 text

Create the project

Slide 14

Slide 14 text

Create the project

Slide 15

Slide 15 text

WRITE SOME CODE 2

Slide 16

Slide 16 text

expect actual IDE only helps after the directories have been created.

Slide 17

Slide 17 text

@Serializable data class DayLight( val image: String, val color: String, val reason: String, val date: String ) @Serializable data class Lights( val todayColor: String, val picture: String, val calendar: List ) :shared

Slide 18

Slide 18 text

class LightRepository { private val httpClient = HttpClient { install(JsonFeature) { val json = Json { ignoreUnknownKeys = true } serializer = KotlinxSerializer(json) } } suspend fun getLights() : Lights = httpClient.get(LIGHTS_ENDPOINT) } :shared

Slide 19

Slide 19 text

:android class LightsViewModel : ViewModel() { private val repository by lazy { LightRepository() } private val _state : MutableLiveData> = MutableLiveData() val state : LiveData> get() = _state fun loadLights() { _state.postValue(Lce.Loading) viewModelScope.launch { try { val lights = repository.getLights() _state.postValue(Lce.Success(lights)) } catch(e: Exception) { _state.postValue(Lce.Error(e)) } } } }

Slide 20

Slide 20 text

val state : LiveData> get() = _state fun loadLights() { _state.postValue(Lce.Loading) viewModelScope.launch { try { val lights = repository.getLights() _state.postValue(Lce.Success(lights)) } catch(e: Exception) { _state.postValue(Lce.Error(e)) } }

Slide 21

Slide 21 text

They will happen WEIRD ERRORS 3

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

Delete all .iml files in the project Close project and IDE Delete .idea directory Re-import the project

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

upgrade Gradle to 6.7 (KT-43039)

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

bit.ly/KMM-Codelab

Slide 28

Slide 28 text

USE COROUTINES 4

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

REMEMBER You cannot use extension functions

Slide 31

Slide 31 text

extension ContentView { enum LoadableLights { case loading case result(Lights) case error(String) } class ViewModel: ObservableObject { let repo : LightRepository @Published var lights = LoadableLights.loading init(repo: LightRepository){ self.repo = repo self.loadLights() } func loadLights() { self.lights = .loading repo.getLights(completionHandler: { lights, error in if let lights = lights { self.lights = .result(lights) } else { self.lights = .error(error?.localizedDescription ?? "error") } }) } } }

Slide 32

Slide 32 text

self.repo = repo self.loadLights() } func loadLights() { self.lights = .loading repo.getLights(completionHandler: { lights, error in if let lights = lights { self.lights = .result(lights) } else { self.lights = .error(error?.localizedDescription ?? "e } }) }

Slide 33

Slide 33 text

private func listView() -> AnyView { switch viewModel.lights { case .loading: return AnyView(Text("Loading...").multilineTextAlignment(.center)) case .result(let lodableLights): return AnyView(LightsView(lights: lodableLights)) case .error(let description): return AnyView(Text(description).multilineTextAlignment(.center)) } }

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

Hello! I Am Rob Follow me at @_tiwiz

Slide 37

Slide 37 text

Q&A