From a10f51aa0863a741be20880f422d7e780e5720f6 Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Tue, 26 Apr 2022 22:16:07 +0100 Subject: [PATCH] adding an empty state to messages list --- .../app/dapk/st/design/components/Empty.kt | 18 ++++++++++++++++++ .../st/directory/DirectoryListingScreen.kt | 19 ++++++------------- .../app/dapk/st/directory/DirectoryState.kt | 1 + .../dapk/st/directory/DirectoryViewModel.kt | 8 ++++---- 4 files changed, 29 insertions(+), 17 deletions(-) create mode 100644 design-library/src/main/kotlin/app/dapk/st/design/components/Empty.kt diff --git a/design-library/src/main/kotlin/app/dapk/st/design/components/Empty.kt b/design-library/src/main/kotlin/app/dapk/st/design/components/Empty.kt new file mode 100644 index 0000000..3b13536 --- /dev/null +++ b/design-library/src/main/kotlin/app/dapk/st/design/components/Empty.kt @@ -0,0 +1,18 @@ +package app.dapk.st.design.components + +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.material.Text +import androidx.compose.runtime.Composable +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier + +@Composable +fun GenericEmpty() { + Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) { + Column(horizontalAlignment = Alignment.CenterHorizontally) { + Text("Nothing to see here...") + } + } +} \ No newline at end of file diff --git a/features/directory/src/main/kotlin/app/dapk/st/directory/DirectoryListingScreen.kt b/features/directory/src/main/kotlin/app/dapk/st/directory/DirectoryListingScreen.kt index a840dba..46e69dd 100644 --- a/features/directory/src/main/kotlin/app/dapk/st/directory/DirectoryListingScreen.kt +++ b/features/directory/src/main/kotlin/app/dapk/st/directory/DirectoryListingScreen.kt @@ -10,7 +10,6 @@ import androidx.compose.foundation.lazy.LazyListState import androidx.compose.foundation.lazy.items import androidx.compose.foundation.lazy.rememberLazyListState import androidx.compose.foundation.shape.CircleShape -import androidx.compose.material.Button import androidx.compose.material.MaterialTheme import androidx.compose.material.Text import androidx.compose.runtime.* @@ -33,6 +32,8 @@ import app.dapk.st.core.LifecycleEffect import app.dapk.st.core.StartObserving import app.dapk.st.core.components.CenteredLoading import app.dapk.st.design.components.CircleishAvatar +import app.dapk.st.design.components.GenericEmpty +import app.dapk.st.design.components.GenericError import app.dapk.st.design.components.Toolbar import app.dapk.st.directory.DirectoryEvent.OpenDownloadUrl import app.dapk.st.directory.DirectoryScreenState.Content @@ -85,20 +86,12 @@ fun DirectoryScreen(directoryViewModel: DirectoryViewModel) { .nestedScroll(nestedScrollConnection) ) { when (state) { - is Content -> { - Content(listState, state) - } EmptyLoading -> CenteredLoading() - is Error -> { - Box(contentAlignment = Alignment.Center) { - Column(horizontalAlignment = Alignment.CenterHorizontally) { - Text("Something went wrong...") - Button(onClick = {}) { - Text("Retry") - } - } - } + DirectoryScreenState.Empty -> GenericEmpty() + is Error -> GenericError { + // TODO } + is Content -> Content(listState, state) } Toolbar(title = "Messages", offset = { IntOffset(x = 0, y = toolbarOffsetHeightPx.value.roundToInt()) }) } diff --git a/features/directory/src/main/kotlin/app/dapk/st/directory/DirectoryState.kt b/features/directory/src/main/kotlin/app/dapk/st/directory/DirectoryState.kt index 6b1879e..2cf51fa 100644 --- a/features/directory/src/main/kotlin/app/dapk/st/directory/DirectoryState.kt +++ b/features/directory/src/main/kotlin/app/dapk/st/directory/DirectoryState.kt @@ -3,6 +3,7 @@ package app.dapk.st.directory sealed interface DirectoryScreenState { object EmptyLoading : DirectoryScreenState + object Empty : DirectoryScreenState data class Content( val overviewState: DirectoryState, ) : DirectoryScreenState diff --git a/features/directory/src/main/kotlin/app/dapk/st/directory/DirectoryViewModel.kt b/features/directory/src/main/kotlin/app/dapk/st/directory/DirectoryViewModel.kt index 0bd06d4..d8c4fdc 100644 --- a/features/directory/src/main/kotlin/app/dapk/st/directory/DirectoryViewModel.kt +++ b/features/directory/src/main/kotlin/app/dapk/st/directory/DirectoryViewModel.kt @@ -1,8 +1,7 @@ package app.dapk.st.directory import androidx.lifecycle.viewModelScope -import app.dapk.st.directory.DirectoryScreenState.Content -import app.dapk.st.directory.DirectoryScreenState.EmptyLoading +import app.dapk.st.directory.DirectoryScreenState.* import app.dapk.st.viewmodel.DapkViewModel import app.dapk.st.viewmodel.MutableStateFactory import app.dapk.st.viewmodel.defaultStateFactory @@ -26,8 +25,9 @@ class DirectoryViewModel( syncJob = viewModelScope.launch { directoryUseCase.state().onEach { shortcutHandler.onDirectoryUpdate(it.map { it.overview }) - if (it.isNotEmpty()) { - state = Content(it) + state = when (it.isEmpty()) { + true -> Empty + false -> Content(it) } }.collect() }