package com.example.aiapp /** * What a screen knows about something it had to fetch: still finding out, got it, or couldn't. * * Three states rather than a value alongside a nullable error, because "we couldn't find out" must * not share a representation with "there is nothing" -- a failed fetch would otherwise render as an * empty list, which is the one wrong answer that looks like a right one. * * [Loading] and [Error] carry no payload, so they are `LoadState` and this is covariant in * [T]: one `LoadState.Loading` serves every screen. */ sealed class LoadState { data object Loading : LoadState() data class Loaded(val value: T) : LoadState() data class Error(val message: String) : LoadState() companion object { /** * The failure a fetch produces. Api.kt writes its messages to be read on this screen, so * this passes one through rather than replacing it; the fallback covers only a throwable * with no message at all, which [ApiException] never is. */ fun failed(e: ApiException): Error = Error(e.message ?: "Unknown error") } }