Kotlin·Kotlin from syntax to a coroutine app

Build a small client

Fetch JSON, model it with data classes and render it with states.

Beginner 22 min read Lesson 10 of 300 Challenge included
What you will be able to do
  • Use a data class for the response.
  • Keep the API client separate.
  • Model loading, error and content.
  • Test the parser and the view model.

In plain English

The capstone fetches a list of lessons, parses it into data classes and renders loading, error and content states. Keep the network layer separate from the UI so both are testable.

Lessons.ktKotlin
data class Lesson(val title: String, val minutes: Int)

sealed interface UiState {
    data object Loading : UiState
    data class Content(val lessons: List<Lesson>) : UiState
    data class Error(val message: String) : UiState
}
Your turn

Add a loading state

Easy

Add a Loading object to the UiState interface.

KOTLIN · starter
sealed interface UiState {
Check your understanding before moving on.
Knowledge checkQuestion 1

Why model UI state as a sealed interface?

Select an answer to continue
Study tools

Make it yours.

Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.

Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign in
Finished reading?

Lock in what you just learned.

Mark this lesson complete to save your place and move one step closer to the certificate.

Lesson 10 of 300 · 0% of course