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")
}
})
}
}
}