Slide 1

Slide 1 text

Magic and tricks: with Kotlin Multiplatform Dinorah Tovar Unidos compartiendo y aprendiendo #SGVirtual

Slide 2

Slide 2 text

DISCLAIMER!

Slide 3

Slide 3 text

KOTLIN
 IS COOL

Slide 4

Slide 4 text

KOTLIN MULTIPLATFORM 
 IS COOLER

Slide 5

Slide 5 text

KOTLIN IS A JVM LANGUAGE, NOT AN ANDROID LANGUAGE

Slide 6

Slide 6 text

WHAT THE HECK IS KMP?

Slide 7

Slide 7 text

TRY IT BEFORE YOU BUY IT FULL OPTIONAL

Slide 8

Slide 8 text

SHARING CODE

Slide 9

Slide 9 text

SO, IS CROSS-PLATFORM? OR A FRAMEWORK KINDA CROSS

Slide 10

Slide 10 text

ONLY LOGIC, NO UI

Slide 11

Slide 11 text

ONLY LOGIC, “NO UI”

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

SO, WHAT THE HECK IS? IS A FRAMEWORK? A SDK? A PLUGIN?

Slide 14

Slide 14 text

“Kotlin's philosophy is that you don't have to migrate the entire application. Start from a easy place”
 
 -Jetbrains IS A SDK!

Slide 15

Slide 15 text

SHARED BUSINESS LOGIC

Slide 16

Slide 16 text

SHARED BUSINESS LOGIC • Not restrictions •Appearance and behavior is the same •Performance is fully Native!

Slide 17

Slide 17 text

ANDROID AND IOS ARE KINDA THE SAME, RIGHT? •They are “Unix like” •They have multithreading operations •They have file/primitive saving of data and pretty much, they use SQL

Slide 18

Slide 18 text

HOW DOES IT WORK?

Slide 19

Slide 19 text

KOTLIN MULTIPLATFORM Kotlin 
 Compiler Kotlin
 Multiplatform Native JS JVM JAR/AAR (For Android) JS (For React or Node) Binaries (For iOS)

Slide 20

Slide 20 text

KOTLIN MULTIPLATFORM

Slide 21

Slide 21 text

KOTLIN MULTIPLATFORM sourceSets { commonMain { dependencies { api project(":common-all") implementation 'org.jetbrains.kotlin:kotlin-stdlib-common' --- } } commonTest { dependencies { implementation "org.jetbrains.kotlin:kotlin-test-common" --- } } androidMain { dependsOn commonMain dependencies {

Slide 22

Slide 22 text

commonTest { dependencies { implementation "org.jetbrains.kotlin:kotlin-test-common" --- } } androidMain { dependsOn commonMain dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" --- } } iOSMain { dependsOn commonMain dependencies { implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-native: $coroutines_version" ---- } } } KOTLIN MULTIPLATFORM

Slide 23

Slide 23 text

I DON’T GET IT, SHARING CODE? HOW?

Slide 24

Slide 24 text

KOTLIN MULTIPLATFORM •Use a Plugin in your grade to configure your “blocks” •We configure a Kotlin block, with our targets! One for Android and one for iOS

Slide 25

Slide 25 text

KOTLIN MULTIPLATFORM

Slide 26

Slide 26 text

EXAMPLE •The most primitive way to save a primitive variable in Android and iOS is: SharedPreferences and NSUserDefaults

Slide 27

Slide 27 text

EXAMPLE •The most primitive way to save a primitive variable in Android and iOS is: SharedPreferences and NSUserDefaults •Kotlin only knows how to compile Kotlin code into different targets but you can make Kotlin aware of this by using the expect/actual mechanism.

Slide 28

Slide 28 text

SAVING VALUES //Common Code expect fun saveValueLocally(value: String) //Android Code actual fun saveValueLocally(value: String) { //Create shared preferences sharedPreferences.edit { putString("Value", value) } } //iOS Code actual fun saveValueLocally(value: String) { NSUserDefaults.standardUserDefaults.setValue( value, forKey = "Value" ) }

Slide 29

Slide 29 text

MULTITHREADING

Slide 30

Slide 30 text

SAY HI TO KTOR! Ktor Kotlin Serialization Coroutines Retrofit Kotlin Serialization Coroutines /RxJava Alamofire RxSwift/ Composite Decode/ Encode/ etc…

Slide 31

Slide 31 text

HTTP CLIENT // commonMain expect val engine: HttpClientEngine // androidMain actual val engine by lazy { Android.create() } // iosMain actual val engine by lazy { Ios.create() }

Slide 32

Slide 32 text

HTTP CLIENT class GetDogsRepository { private val client = HttpClient(engine) { install(JsonFeature) { serializer = KotlinxSerializer().apply { register(Dogs.serializer().list) } } } suspend fun getDogs(): List = client.get("https://www.dogs.com.mx/isleofdogs") }

Slide 33

Slide 33 text

COROUTINES SUPPORT! expect open class MyViewModel() { val clientScope: CoroutineScope protected open fun onCleared() } actual open class MyViewModel actual constructor(): ViewModel()

Slide 34

Slide 34 text

COROUTINES SUPPORT FOR IOS… KINDA Coroutines Hot Streams Cold Streams

Slide 35

Slide 35 text

COROUTINES SUPPORT FOR IOS… KINDA actual open class MyViewModel actual constructor() { private val viewModelJob = SupervisorJob() val viewModelScope: CoroutineScope = CoroutineScope(IosMainDispatcher + viewModelJob) actual val clientScope: CoroutineScope = viewModelScope protected actual open fun onCleared() { viewModelJob.cancelChildren() } object IosMainDispatcher : CoroutineDispatcher() { override fun dispatch(context: CoroutineContext, block: Runnable) { dispatch_async(dispatch_get_main_queue()) { block.run() } } } }

Slide 36

Slide 36 text

OKIO AND OKHTTP

Slide 37

Slide 37 text

OKIO AND OKHTTP

Slide 38

Slide 38 text

DATABASES

Slide 39

Slide 39 text

SAY HI TO SQLITEDELIGHT! •Databases are hard! •SQLDelight generates typesafe APIs from your SQL statements •Android and iOS use SQL

Slide 40

Slide 40 text

SAY HI TO SQLITEDELIGHT! CREATE TABLE dogs ( dogName TEXT NOT NULL, dogRace TEXT NOT NULL ); 
 insert: INSERT INTO dogs (dogName, dogRace) VALUES ('Spots', 'Best friend');

Slide 41

Slide 41 text

FULL SUPPORT FOR IOS AND ANDROID class DogsDao(database: Dogs) { private val db = database.dogsModelQueries internal fun insert(item: Dogs) { db.insertItem( dogName = item.dogName, dogRace = item.dogRace ) } internal fun select():List = db.selectAll().executeAsList() }

Slide 42

Slide 42 text

FIREBASE

Slide 43

Slide 43 text

FIREBASE PLUGIN

Slide 44

Slide 44 text

GRAPHQL

Slide 45

Slide 45 text

APOLLO FOR MULTIPLATFORM •Apollo queries can be really complicated •One wrong detail in the query and there will be nothing in the tree to search

Slide 46

Slide 46 text

APOLLO FOR MULTIPLATFORM

Slide 47

Slide 47 text

APOLLO FOR MULTIPLATFORM •Apollo Runtime [Android] •Apollo API [iOS] •You can now add your schema.json and other graphql files under src/commonMain/graphql

Slide 48

Slide 48 text

THE REAL QUESTION?, IS READY FOR PROD?

Slide 49

Slide 49 text

WAIT FOR THE FUTURE

Slide 50

Slide 50 text

Magic and tricks: with Kotlin Multiplatform Unidos compartiendo y aprendiendo #SGVirtual Dinorah Tovar Lead Mobile Engineer @ddinorahtovar @ddinorahtovar @dinorahto @dinorahto