Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Unverified Commit 180b6af2 authored by Wolf-Martell Montwé's avatar Wolf-Martell Montwé
Browse files

Add ContentLoadingErrorView to design system

parent 5489a2c8
Loading
Loading
Loading
Loading
+31 −0
Original line number Diff line number Diff line
package app.k9mail.ui.catalog.ui.common.list

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.grid.GridItemSpan
import androidx.compose.foundation.lazy.grid.LazyGridScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import app.k9mail.core.ui.compose.designsystem.atom.text.TextCaption
import app.k9mail.core.ui.compose.theme.MainTheme

fun LazyGridScope.sectionInfoItem(
    text: String,
) {
    item(span = { GridItemSpan(maxLineSpan) }) {
        Column(
            modifier = Modifier
                .fillMaxWidth()
                .padding(
                    start = MainTheme.spacings.double,
                    end = MainTheme.spacings.double,
                ),
            horizontalAlignment = Alignment.CenterHorizontally,
        ) {
            TextCaption(
                text = text,
            )
        }
    }
}
+61 −0
Original line number Diff line number Diff line
package app.k9mail.ui.catalog.ui.molecule.items

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.lazy.grid.LazyGridScope
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import app.k9mail.core.ui.compose.designsystem.atom.text.TextSubtitle1
import app.k9mail.core.ui.compose.designsystem.molecule.ContentLoadingErrorState
import app.k9mail.core.ui.compose.designsystem.molecule.ContentLoadingErrorView
import app.k9mail.core.ui.compose.designsystem.molecule.ErrorView
import app.k9mail.core.ui.compose.designsystem.molecule.LoadingView
import app.k9mail.ui.catalog.ui.common.list.ItemOutlined
import app.k9mail.ui.catalog.ui.common.list.sectionHeaderItem
import app.k9mail.ui.catalog.ui.common.list.sectionInfoItem
import app.k9mail.ui.catalog.ui.common.list.sectionSubtitleItem

fun LazyGridScope.stateItems() {
@@ -33,4 +46,52 @@ fun LazyGridScope.stateItems() {
            )
        }
    }

    sectionHeaderItem(text = "ContentLoadingErrorView")
    sectionInfoItem(text = "Click below to change state")
    item {
        Column {
            ItemOutlined {
                StatefulContentLoadingErrorState()
            }
        }
    }
}

@Composable
private fun StatefulContentLoadingErrorState() {
    val state = remember {
        mutableStateOf<ContentLoadingErrorState>(ContentLoadingErrorState.Loading)
    }

    ContentLoadingErrorView(
        state = state.value,
        modifier = Modifier
            .clickable {
                when (state.value) {
                    ContentLoadingErrorState.Loading -> {
                        state.value = ContentLoadingErrorState.Content
                    }

                    ContentLoadingErrorState.Content -> {
                        state.value = ContentLoadingErrorState.Error
                    }

                    ContentLoadingErrorState.Error -> {
                        state.value = ContentLoadingErrorState.Loading
                    }
                }
            }
            .height(200.dp)
            .fillMaxSize(),
        error = {
            TextSubtitle1(text = "Error")
        },
        loading = {
            TextSubtitle1(text = "Loading...")
        },
        content = {
            TextSubtitle1(text = "Content")
        },
    )
}
+88 −0
Original line number Diff line number Diff line
package app.k9mail.core.ui.compose.designsystem.molecule

import androidx.compose.animation.AnimatedContent
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import app.k9mail.core.ui.compose.designsystem.atom.text.TextSubtitle1
import app.k9mail.core.ui.compose.theme.PreviewWithThemes

@OptIn(ExperimentalAnimationApi::class)
@Composable
fun ContentLoadingErrorView(
    state: ContentLoadingErrorState,
    loading: @Composable () -> Unit,
    error: @Composable () -> Unit,
    modifier: Modifier = Modifier,
    contentAlignment: Alignment = Alignment.Center,
    content: @Composable () -> Unit,
) {
    Box(
        modifier = modifier,
        contentAlignment = contentAlignment,
    ) {
        AnimatedContent(
            targetState = state,
            label = "CleView",
        ) { targetState ->
            when (targetState) {
                ContentLoadingErrorState.Loading -> loading()
                ContentLoadingErrorState.Content -> content()
                ContentLoadingErrorState.Error -> error()
            }
        }
    }
}

sealed interface ContentLoadingErrorState {
    object Loading : ContentLoadingErrorState
    object Content : ContentLoadingErrorState
    object Error : ContentLoadingErrorState
}

@Composable
@Preview(showBackground = true)
internal fun ContentLoadingErrorViewPreview() {
    PreviewWithThemes {
        val state = remember {
            mutableStateOf<ContentLoadingErrorState>(ContentLoadingErrorState.Loading)
        }

        ContentLoadingErrorView(
            state = state.value,
            modifier = Modifier
                .clickable {
                    when (state.value) {
                        ContentLoadingErrorState.Loading -> {
                            state.value = ContentLoadingErrorState.Content
                        }

                        ContentLoadingErrorState.Content -> {
                            state.value = ContentLoadingErrorState.Error
                        }

                        ContentLoadingErrorState.Error -> {
                            state.value = ContentLoadingErrorState.Loading
                        }
                    }
                }
                .fillMaxSize(),
            error = {
                TextSubtitle1(text = "Error")
            },
            loading = {
                TextSubtitle1(text = "Loading...")
            },
            content = {
                TextSubtitle1(text = "Content")
            },
        )
    }
}