Skip to content

Add support for folding into composables #36

@warting

Description

@warting

Whould it make sence for this library to be able to fold types to composables? Maybe an extension that supports composable?

I have done something like this to convert the types into something that can be representable to the user:

@Composable
inline fun <C> UCT<C>.Compose(
    crossinline onLoading: @Composable () -> Unit,
    crossinline onError: @Composable (Throwable) -> Unit,
    crossinline onContent: @Composable (C) -> Unit
) {
    return ComposeTypes(
        onLoading = { onLoading() },
        onContent = { onContent(it.value) },
        onError = { onError(it.value) }
    )
}

@Composable
inline fun <C> UCT<C>.ComposeTypes(
    crossinline onLoading: @Composable (Type.Loading.UnitType) -> Unit,
    crossinline onContent: @Composable (Type.Content<C>) -> Unit,
    crossinline onError: @Composable (Type.Error.ThrowableType) -> Unit
) {
    return when (val type = asLceType()) {
        is Type.Loading.UnitType -> onLoading(type)
        is Type.Content -> onContent(type)
        is Type.Error.ThrowableType -> onError(type)
        else -> throw IllegalStateException("this should not happen: $type")
    }
}

and then the usages could be something like this:

    val uiState by remember {
        mutableStateOf(UCT.content("hello earth!"))
    }
    uiState.Compose(
        onError = {
            Text(text = it.message ?: "")
            ErrorView(stringResource(id = R.string.error))
        },
        onLoading = {
            LoadingView(stringResource(id = R.string.loading))
        },
        onContent = {
            Text(text = it)
        }
    )

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions