Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
An Android Dev start to Kotlin MPP
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Roberto Orgiu
November 16, 2020
Programming
210
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
An Android Dev start to Kotlin MPP
Roberto Orgiu
November 16, 2020
More Decks by Roberto Orgiu
See All by Roberto Orgiu
Wellness & Droid
tiwiz
0
140
Behind the curtains
tiwiz
0
89
The Importance of Being Tested
tiwiz
0
450
Fantastic API and where to find them
tiwiz
0
110
Flipping the Koin @ GDG Dev Party
tiwiz
1
79
Flipping the Koin
tiwiz
2
180
Trip into the async world @ NYC Kotlin Meetup
tiwiz
0
130
Trip into the async world
tiwiz
1
150
GraphQL IRL (Android Makers)
tiwiz
0
160
Other Decks in Programming
See All in Programming
進化を続けるGo toolsの現在地 / The Current State of Ever-Evolving Go Tools
hond0413
0
250
freeeにおけるEvalsの実践例の紹介
freee
PRO
0
130
Webエンジニアなのにブラウザの仕組みがわからないので、Pythonで自作してみた
tatsuki12
4
840
メールのエイリアス機能を履き違えない
isshinfunada
0
250
仕様駆動開発の消費期限
watany
20
8.8k
Hono + Inertia + React で LP を構築した話
oukayuka
2
110
自動化したのに回らない テスト運用の壁―AI時代の品質責任と生産性
mfunaki
0
360
AI Readyの正体はデータマネジメントだ メダリオン2.0の最前線
freee
PRO
0
340
Go を使い始めて 2 ヶ月の学び / My first two months with Go
contour_gara
0
320
運用ダッシュボードの設計を誰も教えてくれないのだけどみなさんどうしてるんですか? - チームに監視するという文化を根付かせるための第一歩を踏みたい -
satoshi256kbyte
1
140
AIが無かった頃の素敵な出会いの話
codmoninc
1
480
Discordを用いたラボオートメーション関連情報収集の自動化
noguhiro2002
0
170
Featured
See All Featured
16th Malabo Montpellier Forum Presentation
akademiya2063
PRO
0
350
AI Search: Where Are We & What Can We Do About It?
aleyda
0
7.8k
Art, The Web, and Tiny UX
lynnandtonic
304
22k
Leveraging Curiosity to Care for An Aging Population
cassininazir
1
470
Measuring Dark Social's Impact On Conversion and Attribution
stephenakadiri
2
260
DevOps and Value Stream Thinking: Enabling flow, efficiency and business value
helenjbeal
1
360
Leo the Paperboy
mayatellez
8
2.2k
Git: the NoSQL Database
bkeepers
PRO
432
67k
Optimising Largest Contentful Paint
csswizardry
37
3.9k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
162
16k
Bioeconomy Workshop: Dr. Julius Ecuru, Opportunities for a Bioeconomy in West Africa
akademiya2063
PRO
1
260
Building a Modern Day E-commerce SEO Strategy
aleyda
45
9.2k
Transcript
An Android Dev start to Kotlin MPP
Premises I want to reuse my knowledge I am an
Android Dev I don’t want to be x-plat
What about languages?
Or actually any other example Empire State Building Lights Calendar
App
Architecture Activity ViewModel Repository
Libraries Repository Retrofit Gson / Moshi
Libraries Repository Retrofit Gson / Moshi Ktor kotlinx.serializer
Modularization Activity ViewModel Repository Native module Shared module
Modularization Repository Shared module
None
There is an app a plugin for that. PROJECT SETUP
1
Install & Profit
Create the project
Create the project
WRITE SOME CODE 2
expect actual IDE only helps after the directories have been
created.
@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<DayLight> ) :shared
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
:android class LightsViewModel : ViewModel() { private val repository by
lazy { LightRepository() } private val _state : MutableLiveData<Lce<Lights>> = MutableLiveData() val state : LiveData<Lce<Lights>> 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)) } } } }
val state : LiveData<Lce<Lights>> 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)) } }
They will happen WEIRD ERRORS 3
None
Delete all .iml files in the project Close project and
IDE Delete .idea directory Re-import the project
None
upgrade Gradle to 6.7 (KT-43039)
None
bit.ly/KMM-Codelab
USE COROUTINES 4
None
REMEMBER You cannot use extension functions
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") } }) } } }
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 } }) }
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)) } }
None
None
Hello! I Am Rob Follow me at @_tiwiz
Q&A