@Immutable data object Loading : LoadingState<Nothing> @Immutable data class Success<out T>( val value: T ) : LoadingState<T> @Immutable data class Error( val cause: Throwable ) : LoadingState<Nothing> }
value != nullのときはリロード @Immutable sealed interface LoadingState<out T> { @Immutable data class Loading<out T>( val value: T? ) : LoadingState<T> @Immutable data class Success<out T>( val value: T ) : LoadingState<T> @Immutable data class Error<out T>( val value: T?, val cause: Throwable ) : LoadingState<T> }
value != nullのときはリロード @Immutable sealed interface LoadingState<out T> { val value: T? @Immutable data class Loading<out T>( override val value: T? ) : LoadingState<T> @Immutable data class Success<out T>( override val value: T ) : LoadingState<T> @Immutable data class Error<out T>( override val value: T?, val cause: Throwable ) : LoadingState<T> }
transitionSpec = { fadeIn() togetherWith fadeOut() using SizeTransform() }, contentKey = { it::class } ) { val value = it.value when { value != null -> { success(value) } it is LoadingState.Loading -> { loading() } it is LoadingState.Error -> { error(it.cause) } } }
data object class Loading<out T>( override val value: T? ) : LoadingState<T> } var coinItemsLoadingState: LoadingState<ImmutableList<CoinItems>> by mutableStateOf(LoadingState.Loading)
data object class Loading<out T>( override val value: T? ) : LoadingState<T> companion object { val Loading = Loading(null) } } var coinItemsLoadingState: LoadingState<ImmutableList<CoinItems>> by mutableStateOf(LoadingState.Loading)
data class Loading<out T>( override val value: T? ) : LoadingState<T> companion object { val Loading = Loading(null) } } var coinItemsLoadingState: LoadingState<ImmutableList<CoinItems>> by mutableStateOf(LoadingState.Loading) var coinItemsLoadingState: LoadingState<ImmutableList<CoinItems>> by mutableStateOf(LoadingState.Loading(null))
T? @Immutable data class Loading<out T>( override val value: T? ) : LoadingState<T> @Immutable data class Success<out T>( override val value: T ) : LoadingState<T> @Immutable data class Error<out T>( override val value: T?, val cause: Throwable ) : LoadingState<T> }
T? @Immutable data class Loading<out T>( override val value: T? ) : LoadingState<T> @Immutable data class Success<out T>( override val value: T ) : LoadingState<T> @Immutable data class Error<out T>( override val value: T?, val cause: Throwable, val isRetrying: Boolean ) : LoadingState<T> }
T? @Immutable data class Loading<out T>( override val value: T? ) : LoadingState<T> @Immutable data class Success<out T>( override val value: T ) : LoadingState<T> @Immutable data class Error<out T>( override val value: T?, val cause: Throwable ) : LoadingState<T> }
T? @Immutable data class Loading<out T>( override val value: T?, val error: Throwable? ) : LoadingState<T> @Immutable data class Success<out T>( override val value: T ) : LoadingState<T> @Immutable data class Error<out T>( override val value: T?, val cause: Throwable ) : LoadingState<T> }
() -> Unit, error: @Composable (Throwable, isRetrying: Boolean) -> Unit, success: @Composable (T) -> Unit ) { AnimatedContent(...) { val value = it.value when { value != null -> { success(value) } it is LoadingState.Retrying -> { error(it.cause, true) } it is LoadingState.Loading -> { loading() } it is LoadingState.Error -> { error(it.cause, false) } }
togetherWith fadeOut() using SizeTransform() }, contentKey = { it::class } ) { val value = it.value when { value != null -> { success(value) } it is LoadingState.Retrying -> { error(it.cause, true) } it is LoadingState.Loading -> { loading() } it is LoadingState.Error -> { error(it.cause, false) } } } ⚠
togetherWith fadeOut() using SizeTransform() }, contentKey = { when { it.value != null -> "success" it is LoadingState.Retrying -> "error" it is LoadingState.Loading -> "loading" it is LoadingState.Error -> "error" else -> "" } } ) { val value = it.value when { value != null -> { success(value) } it is LoadingState.Retrying -> { error(it.cause, true) }
sealed interface Loading<out T> : LoadingState<T> data object Initializing : Loading<Nothing> { override val value = null } data class Loading Reloading<out T>( override val value: T ) : Loading<T> }
interface Loading<out T> : LoadingState<T> data object Initializing : Loading<Nothing> { override val value = null } data class Loading Reloading<out T>( override val value: T ) : Loading<T> } var coinItemsLoadingState: LoadingState<ImmutableList<CoinItems>> by mutableStateOf(LoadingState.Loading(null))
val value = value return when { this is LoadingState.Loading -> this this is LoadingState.Failure -> LoadingState.Retrying(value, cause) value != null -> LoadingState.Reloading(value) else -> LoadingState.Initializing } }
val value = value return when { this is LoadingState.Loading -> this this is LoadingState.Failure -> LoadingState.Retrying(value, cause) value != null -> LoadingState.Reloading(value) else -> LoadingState.Initializing } } fun loadCoinItems() { viewModelScope.launch { coinItemsLoadingState = coinItemsLoadingState.toLoading() try { val coinItems = repository.fetchCoinItems() coinItemsLoadingState = LoadingState.Success(coinItems) } catch (e: Exception) { coinItemsLoadingState = coinItemsLoadingState.toError(e) } } }