An Android Devstart toKotlin MPP
View Slide
PremisesI want to reuse my knowledgeI am an Android DevI don’t want to be x-plat
What about languages?
Or actually any other exampleEmpire State BuildingLights CalendarApp
ArchitectureActivity ViewModelRepository
LibrariesRepositoryRetrofitGson / Moshi
LibrariesRepositoryRetrofitGson / MoshiKtorkotlinx.serializer
ModularizationActivity ViewModelRepositoryNative moduleShared module
ModularizationRepositoryShared module
There is an app a plugin for that.PROJECT SETUP1
Install&Profit
Createthe project
WRITE SOME CODE2
expectactualIDE only helps after the directories havebeen created.
@Serializabledata class DayLight(val image: String,val color: String,val reason: String,val date: String)@Serializabledata class Lights(val todayColor: String,val picture: String,val calendar: List):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
:androidclass LightsViewModel : ViewModel() {private val repository by lazy {LightRepository()}private val _state : MutableLiveData> = MutableLiveData()val state : LiveData>get() = _statefun 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>get() = _statefun 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 happenWEIRD ERRORS3
Delete all .iml files in the projectClose project and IDEDelete .idea directoryRe-import the project
upgrade Gradle to 6.7 (KT-43039)
bit.ly/KMM-Codelab
USE COROUTINES4
REMEMBERYou cannot use extensionfunctions
extension ContentView {enum LoadableLights {case loadingcase result(Lights)case error(String)}class ViewModel: ObservableObject {let repo : LightRepository@Published var lights = LoadableLights.loadinginit(repo: LightRepository){self.repo = repoself.loadLights()}func loadLights() {self.lights = .loadingrepo.getLights(completionHandler: { lights, error inif let lights = lights {self.lights = .result(lights)} else {self.lights = .error(error?.localizedDescription ?? "error")}})}}}
self.repo = repoself.loadLights()}func loadLights() {self.lights = .loadingrepo.getLights(completionHandler: { lights, error inif 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))}}
Hello!I Am RobFollow me at @_tiwiz
Q&A