Modeling UI State with Sealed Classes
Apply sealed classes to represent Loading, Success, and Error UI states.
The UI State Problem
UI screens have multiple states: loading, showing data, showing an error, or empty. Managing these with booleans (isLoading, hasError) is fragile. Sealed classes fix this.
The Classic Boolean Hell
Using separate booleans leads to impossible states.
// Fragile: isLoading=true AND hasError=true is invalid
var isLoading = false
var hasError = false
var data: List<Item>? = null
// 8 combinations, most of which are invalid!All lessons in this course
- sealed class vs sealed interface: When to Use Each
- Exhaustive when with Sealed Hierarchies
- Modeling UI State with Sealed Classes
- Nesting and Combining Sealed Hierarchies